您好,登錄后才能下訂單哦!
小編給大家分享一下如何利用Python實(shí)現(xiàn)星空大戰(zhàn)游戲,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
星空飛碟大戰(zhàn).py
由于配音需要混音器,這里用到了pygame的混音器,
from sprites import * import pygame.mixer
def star_move(): """動(dòng)態(tài)星空背景函數(shù)""" for star in stars: star.move(0,-20) if star.ycor() < -height//2: x = random.randint(-width//2,width//2) y = random.randint(10+height//2,height*2) star.reborn(x,y,0,-20)
def spawn_enemy(): """不定時(shí)產(chǎn)生敵機(jī)函數(shù)""" if random.randint(1,10)==1 and len(enemys)<10: x = random.randint(-200,200) y = random.randint(100,300) enemy = Sprite(shape='res/ufo.png',visible=False,pos=(x,y),tag='enemy') enemy._rotatemode = 1 enemy.scale(0.5) enemy.setheading(random.randint(1,360)) enemy.show()
def enemymove(): """飛碟的移動(dòng)""" for e in enemys: e.fd(3) # 設(shè)定一定的機(jī)率讓ufo朝向player if random.randint(10,100) == 10 and \ abs(e.xcor())<200 and abs(e.ycor()<250): e.heading(player) e.bounce_on_edge()
def bulletmove(): """子彈的移動(dòng)""" for b in list(bullets): b.move(0,10) if b.collide_edge():b.remove()
def player_shoot(): """玩家射擊函數(shù)""" if player.alive == False : return if m1.down() and framecounter % 5 == 0 : b = Sprite(shape='circle',visible=False,tag='bullet') b.scale(0.5) b.color('yellow') b.goto(player.pos()) # 移到player坐標(biāo) b.show() # 顯示子彈 shoot.play() # 播放射擊聲
# 播放背景音樂(lè)與生成聲效對(duì)象 pygame.mixer.init() pygame.mixer.music.load('audio/FrozenJam.ogg') pygame.mixer.music.play(-1,0) explosion = pygame.mixer.Sound('audio/expl3.wav') shoot = pygame.mixer.Sound('audio/pew.wav')
width,height = 480,640 screen = Screen() # 新建屏幕 screen.bgcolor('black') # 屏幕背景色為黑 screen.setup(width,height) screen.title("星空飛碟大戰(zhàn)by大海老師") screen.addshape('res/fighter.png') screen.addshape('res/ufo.png') frames = ['res/explosion0.png','res/explosion1.png'] [screen.addshape(frame) for frame in frames]
# 星星,用來(lái)做向下滾動(dòng)背景,星星的移動(dòng)也可以通過(guò)移動(dòng)圖章實(shí)現(xiàn) # 這樣可以有更多的星星。如果用克隆的話有數(shù)量限制,根據(jù)計(jì)算機(jī)配置不同而不同。 star = Sprite(shape='circle') star.color('white') star.scale(0.1) stars = [star] stars.extend([star.clone() for _ in range(20)]) for star in stars: x = random.randint(-width//2,width//2) y = random.randint(10+height//2,height*2) star.goto(x,y)
cry = Sprite(shape='cry.png',visible=False,pos=(0,100))
player = Sprite(shape='res/fighter.png',pos=(0,-200)) player.scale(0.65) player.alive = True # 表示player是活的 m1 = Mouse() # 鼠標(biāo)左鍵檢測(cè)實(shí)例 clock = Clock() # 實(shí)鐘對(duì)象,用來(lái)控制fps framecounter = 0 counter = 0 # 統(tǒng)計(jì)擊中的ufo數(shù)量 bullets = Group('bullet') # 子彈組 enemys = Group('enemy') # ufo敵人組
while counter<100: framecounter += 1 # 幀計(jì)數(shù)器 spawn_enemy() # 不定時(shí)產(chǎn)生敵機(jī)UFO player_shoot() # 單擊鼠標(biāo)左鍵,射擊子彈 enemymove() # 飛碟們的移動(dòng) bulletmove() # 子彈的移動(dòng) if player.alive: player.goto(mousepos()) else: cry.show() # 顯示哭臉,表示失敗 star_move() # 星空滾動(dòng)背景
for e in list(enemys): # 對(duì)每架敵機(jī)進(jìn)行碰撞檢測(cè) if e.collide(player,scale=0.6): explode(e.position(),frames) e.remove() explode(player.pos(),frames) player.remove() player.alive = False explosion.play() # 爆炸聲 break # 敵機(jī)是否碰到任意一顆子彈 for b in list(bullets): if b.collide(e,scale=0.6): # 如果子彈碰到UFO explode(e.position(),frames) e.remove() b.remove() explosion.play() # 爆炸聲 counter +=1 # 進(jìn)行統(tǒng)計(jì) break screen.title('星空飛碟大戰(zhàn),當(dāng)前擊斃:' + str(counter) + " 架UFO") clock.tick(60)
[b.remove() for b in list(bullets)] # 闖關(guān)成功把子彈刪除 for e in list(enemys): # 每架飛碟都爆炸 explode(e.position(),frames) e.remove() clock.tick(10) t = Turtle(visible=False) t.color('yellow') t.write('成功闖關(guān)!',align='center',font=('黑體',32,'normal')) while True: player.goto(mousepos()) star_move() # 星空滾動(dòng)背景 clock.tick(60)
以上是“如何利用Python實(shí)現(xiàn)星空大戰(zhàn)游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。