您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“基于Python怎么實現射擊小游戲”,內容詳細,步驟清晰,細節處理妥當,希望這篇“基于Python怎么實現射擊小游戲”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
1.sprites
2.pygame混音器
3.圖章
4.python基礎語法
from sprites import * try: import pygame pygame.mixer.init() fire_sound = pygame.mixer.Sound("audio/發射聲.wav") cricket_sound = pygame.mixer.Sound('audio/cricket.wav') except: import sys input("本程序需要pygame混音器支持以便配音,請先在cmd下用pip install pygame安裝此模塊。")
width,height = 480,360 screen = Screen() screen.bgpic('res/ghosthouse.jpg') screen.setup(width,height) batimages = ['res/bat1.png','res/bat2.png'] batindex = 0 bat = Sprite(visible=False,pos=(-50-width//2,100)) bat.dx = 3 bat.dy = 0 bat.alive = True bat.show()
def bat_alt_costume(): global batindex batindex = 1 - batindex bat.shape(batimages[batindex]) screen.ontimer(bat_alt_costume,90) bat_alt_costume() hole = Sprite(shape='res/Bullet_Hole.png',visible=False) m1 = Mouse(1) # 鼠標左鍵 m3 = Mouse(3) # 鼠標右鍵 clock = Clock() # 時鐘對象 start_stamp = False while True: bat.move(bat.dx,bat.dy) # 掉到地面就蓋圖章,留下尸體 if bat.ycor() < random.randint(-200,-100): bat.dx = 0 bat.dy = 0 bat.setheading(random.randint(1,360)) bat.stamp() bat.reborn(-500-width//2,100,3,0,delay=2) bat.alive = True bat.setheading(0) # 蝙蝠碰到鼠標指針并且按下了鼠標左鍵 if bat.collide_mouse() and m1.down() and bat.alive: bat.dy = -10 # 開始往下掉 bat.alive = False try: cricket_sound.play() except:pass # 到了最右邊就到最左邊去重新開始 if bat.xcor() > width//2 : bat.reborn(-500-width//2,100,3,0,delay=2) bat.alive = True bat.setheading(0) hole.goto(mouse_position()) # 發射子彈,用蓋圖章留下彈洞,為防連續發射用了start_stamp變量 if m1.down() and not start_stamp: hole.stamp() start_stamp = True try: fire_sound.play() except: pass # 松開按鍵后 if not m1.down():start_stamp = False clock.tick(60)
讀到這里,這篇“基于Python怎么實現射擊小游戲”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。