Python Pygame實現(xiàn)兔子獵人守護城堡游戲
效果圖
守衛(wèi)類游戲大家應(yīng)該玩過吧,什么植物大戰(zhàn)僵尸呀,保衛(wèi)蘿卜呀,今天我們自己用python來寫一個自己的守護類小游戲兔子獵人守護城堡,讓大家看看效果圖。



主要代碼
下面我來說一下是怎么得到的將代碼分享一下給大家
首先得將要用到的庫導(dǎo)入進來
import cfg import math import random import pygame from modules.Sprites import * from modules.interfaces import *
游戲初始化
?初始化pygame, 設(shè)置展示窗口
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption(' QQ群號: 932574150')
加載必要的游戲素材
game_images = {}
for key, value in cfg.IMAGE_PATHS.items():
game_images[key] = pygame.image.load(value)
game_sounds = {}
for key, value in cfg.SOUNDS_PATHS.items():
if key != 'moonlight':
game_sounds[key] = pygame.mixer.Sound(value)
return screen, game_images, game_sounds
主函數(shù)
初始化
screen, game_images, game_sounds = initGame()
播放背景音樂
pygame.mixer.music.load(cfg.SOUNDS_PATHS['moonlight']) pygame.mixer.music.play(-1, 0.0)
加載字體
font = pygame.font.Font(None, 24)
定義兔子
bunny = BunnySprite(image=game_images.get('rabbit'), position=(100, 100))
跟蹤玩家的精度變量, 記錄了射出的箭頭數(shù)和被擊中的獾的數(shù)量.
acc_record = [0., 0.]
生命值
healthvalue = 194
弓箭
arrow_sprites_group = pygame.sprite.Group()
獾
badguy_sprites_group = pygame.sprite.Group()
badguy = BadguySprite(game_images.get('badguy'), position=(640, 100))
badguy_sprites_group.add(badguy)
定義了一個定時器, 使得游戲里經(jīng)過一段時間后就新建一支獾
badtimer = 100 badtimer1 = 0
游戲主循環(huán), running變量會跟蹤游戲是否結(jié)束, exitcode變量會跟蹤玩家是否勝利.
running, exitcode = True, False clock = pygame.time.Clock() while running:
在給屏幕畫任何東西之前用黑色進行填充
screen.fill(0)
添加的風(fēng)景也需要畫在屏幕上
for x in range(cfg.SCREENSIZE[0]//game_images['grass'].get_width()+1): for y in range(cfg.SCREENSIZE[1]//game_images['grass'].get_height()+1): screen.blit(game_images['grass'], (x*100, y*100)) for i in range(4): screen.blit(game_images['castle'], (0, 30+105*i))
倒計時信息
countdown_text = font.render(str((90000-pygame.time.get_ticks())//60000)+":"+str((90000-pygame.time.get_ticks())//1000%60).zfill(2), True, (0, 0, 0)) countdown_rect = countdown_text.get_rect() countdown_rect.topright = [635, 5] screen.blit(countdown_text, countdown_rect)
按鍵檢測,退出與射擊
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
game_sounds['shoot'].play()
acc_record[1] += 1
mouse_pos = pygame.mouse.get_pos()
angle = math.atan2(mouse_pos[1]-(bunny.rotated_position[1]+32), mouse_pos[0]-(bunny.rotated_position[0]+26))
arrow = ArrowSprite(game_images.get('arrow'), (angle, bunny.rotated_position[0]+32, bunny.rotated_position[1]+26))
arrow_sprites_group.add(arrow)
移動兔子
key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_w]: bunny.move(cfg.SCREENSIZE, 'up') elif key_pressed[pygame.K_s]: bunny.move(cfg.SCREENSIZE, 'down') elif key_pressed[pygame.K_a]: bunny.move(cfg.SCREENSIZE, 'left') elif key_pressed[pygame.K_d]: bunny.move(cfg.SCREENSIZE, 'right')
更新弓箭
for arrow in arrow_sprites_group: if arrow.update(cfg.SCREENSIZE): arrow_sprites_group.remove(arrow)
更新獾
if badtimer == 0:
badguy = BadguySprite(game_images.get('badguy'), position=(640, random.randint(50, 430)))
badguy_sprites_group.add(badguy)
badtimer = 100 - (badtimer1 * 2)
badtimer1 = 20 if badtimer1>=20 else badtimer1+2
badtimer -= 1
for badguy in badguy_sprites_group:
if badguy.update():
game_sounds['hit'].play()
healthvalue -= random.randint(4, 8)
badguy_sprites_group.remove(badguy)
碰撞檢測
for arrow in arrow_sprites_group: for badguy in badguy_sprites_group: if pygame.sprite.collide_mask(arrow, badguy): game_sounds['enemy'].play() arrow_sprites_group.remove(arrow) badguy_sprites_group.remove(badguy) acc_record[0] += 1
畫出弓箭
arrow_sprites_group.draw(screen)
畫出獾
badguy_sprites_group.draw(screen)
畫出兔子
bunny.draw(screen, pygame.mouse.get_pos())
畫出城堡健康值, 首先畫了一個全紅色的生命值條, 然后根據(jù)城堡的生命值往生命條里面添加綠色.
screen.blit(game_images.get('healthbar'), (5, 5))
for i in range(healthvalue):
screen.blit(game_images.get('health'), (i+8, 8))
判斷游戲是否結(jié)束
if pygame.time.get_ticks() >= 90000: running, exitcode = False, True if healthvalue <= 0: running, exitcode = False, False
更新屏幕
pygame.display.flip() clock.tick(cfg.FPS)
計算一下游戲的準(zhǔn)確性
accuracy = acc_record[0] / acc_record[1] * 100 if acc_record[1] > 0 else 0 accuracy = '%.2f' % accuracy showEndGameInterface(screen, exitcode, accuracy, game_images)
運行?
if __name__ == '__main__': main()
上面我將代碼分散了,大家可以看一下整合一下就可以運行了?
到此這篇關(guān)于Python Pygame實現(xiàn)兔子獵人守護城堡游戲的文章就介紹到這了,更多相關(guān)Python Pygame游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解讀matplotlib和seaborn顏色圖(colormap)和調(diào)色板(color palette)
這篇文章主要介紹了matplotlib和seaborn顏色圖(colormap)和調(diào)色板(color palette),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Python使用collections模塊實現(xiàn)擴展數(shù)據(jù)類
Python?標(biāo)準(zhǔn)庫提供了一個?collections?模塊,里面提供了很多的數(shù)據(jù)類,在工作中使用這些類能夠簡化我們的開發(fā),本文就來看看collections是如何實現(xiàn)擴展數(shù)據(jù)類的吧2023-06-06
Python利用D3Blocks繪制可動態(tài)交互的圖表
今天小編給大家來介紹一款十分好用的可視化模塊,D3Blocks,不僅可以用來繪制可動態(tài)交互的圖表,并且導(dǎo)出的圖表可以是HTML格式,方便在瀏覽器上面呈現(xiàn),感興趣的可以了解一下2023-02-02
Python警察與小偷的實現(xiàn)之一客戶端與服務(wù)端通信實例
這篇文章主要介紹了Python警察與小偷的實現(xiàn)之一客戶端與服務(wù)端通信實例,并附有難點及易錯點的分析與說明,需要的朋友可以參考下2014-10-10
matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())
這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim()),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python3利用scapy局域網(wǎng)實現(xiàn)自動多線程arp掃描功能
這篇文章主要介紹了Python3利用scapy局域網(wǎng)實現(xiàn)自動多線程arp掃描功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Pandas時間序列重采樣(resample)方法中closed、label的作用詳解
這篇文章主要介紹了Pandas時間序列重采樣(resample)方法中closed、label的作用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

