利用Python寫個簡易版星空大戰(zhàn)游戲
前言
通過辣條最近觀察,大家好像對劃水摸魚是情有獨(dú)鐘啊。于是乎我重操舊業(yè)又寫上了這么一個簡單版的星空大戰(zhàn)小游戲。
當(dāng)然了辣條的初衷絕對不是讓你們劃水摸魚啊,是真想你們能從中學(xué)習(xí)那么一點(diǎn)點(diǎn)東西的,總而言之希望大家在Python這條路上少踩坑吧,因?yàn)樽蛱爝€有人在跟學(xué)長說,以后增刪改查一樣拿高薪,我不信現(xiàn)在都墮落成這樣子了
就像問一下懂事理的兄弟姐妹們,你們覺得有可能嘛?
一.游戲畫面
二.游戲結(jié)束畫面
三.游戲素材
四.游戲代碼
星空飛碟大戰(zhàn).py
由于配音需要混音器,這里用到了pygame的混音器,
五、核心代碼
1.導(dǎo)入模塊
from sprites import * import pygame.mixer
2.動態(tài)星空背景函數(shù)
def star_move(): """動態(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)
3.不定時產(chǎn)生敵機(jī)函數(shù)
def spawn_enemy(): """不定時產(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()
4.飛碟的移動
def enemymove(): """飛碟的移動""" 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()
5.子彈的移動
def bulletmove(): """子彈的移動""" for b in list(bullets): b.move(0,10) if b.collide_edge():b.remove()
6.玩家射擊函數(shù)
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() # 播放射擊聲
7.播放背景音樂與生成聲效對象
# 播放背景音樂與生成聲效對象 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')
8.新建屏幕
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]
9.移動圖章實(shí)現(xiàn)星星
# 星星,用來做向下滾動背景,星星的移動也可以通過移動圖章實(shí)現(xiàn) # 這樣可以有更多的星星。如果用克隆的話有數(shù)量限制,根據(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)
10.哭臉
cry = Sprite(shape='cry.png',visible=False,pos=(0,100))
11.玩家
player = Sprite(shape='res/fighter.png',pos=(0,-200)) player.scale(0.65) player.alive = True ? ? ? ? ? ? # 表示player是活的 m1 = Mouse() ? ? ? ? ? ? ? ? ? ?# 鼠標(biāo)左鍵檢測實(shí)例 clock = Clock() ? ? ? ? ? ? ? ? # 實(shí)鐘對象,用來控制fps framecounter = 0 counter = 0 ? ? ? ? ? ? ? ? ? ? # 統(tǒng)計擊中的ufo數(shù)量 bullets = Group('bullet') ? ? ? # 子彈組 enemys = Group('enemy') ? ? ? ? # ufo敵人組
12.飛碟移動與子彈移動
while counter<100: framecounter += 1 # 幀計數(shù)器 spawn_enemy() # 不定時產(chǎn)生敵機(jī)UFO player_shoot() # 單擊鼠標(biāo)左鍵,射擊子彈 enemymove() # 飛碟們的移動 bulletmove() # 子彈的移動 if player.alive: player.goto(mousepos()) else: cry.show() # 顯示哭臉,表示失敗 star_move() # 星空滾動背景
13.敵機(jī)的碰撞檢測
for e in list(enemys): ? ? ? ?# 對每架敵機(jī)進(jìn)行碰撞檢測 ? 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)計 ? ? ? ?? ? ? ? break ? ? ? ? ? ? screen.title('星空飛碟大戰(zhàn),當(dāng)前擊斃:' + ?str(counter) + " 架UFO") ? clock.tick(60)
14.闖關(guān)成功把子彈刪除
[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() ? ? ? ? ? ? ? ? ? # 星空滾動背景 ? ? clock.tick(60)
六、總結(jié)
看到這里的都是妥妥的鐵粉無疑了,既然是鐵粉那就給你們一次親密接觸機(jī)會,底下是我的個人ming片找到我的可是有大把源碼,學(xué)習(xí)路線啥的,多的我就不透露,看大家自己的積極性了啊~
其次呢再次重申辣條我真的不是想讓你們摸魚的,雖然說是個很簡單的東西,但是對于初學(xué)者,或者基礎(chǔ)不扎實(shí)的弟弟妹妹們而言估計也很難實(shí)現(xiàn)出來的,所以最后大家加油吧~
以上就是利用Python寫個簡易版星空大戰(zhàn)游戲的詳細(xì)內(nèi)容,更多關(guān)于Python星空大戰(zhàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Scrapy-redis爬蟲分布式爬取的分析和實(shí)現(xiàn)
所謂的scrapy-Redis實(shí)際上就是scrapy+redis,其中對redis的操作采用redis-py客戶端。下面這篇文章詳細(xì)介紹了Scrapy-redis爬蟲分布式爬取的分析和實(shí)現(xiàn),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02利用python對mysql表做全局模糊搜索并分頁實(shí)例
這篇文章主要介紹了利用python對mysql表做全局模糊搜索并分頁實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07python selenium 無界面瀏覽器的實(shí)現(xiàn)
有時我們不想讓瀏覽器窗口跳出來,而是想在后臺進(jìn)行操作,這就需要用到無界面瀏覽器,本文主要介紹了python selenium 無界面瀏覽器的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-10-10一篇教程教你學(xué)會Python進(jìn)制轉(zhuǎn)換(十進(jìn)制轉(zhuǎn)二進(jìn)制、八進(jìn)制、十六進(jìn)制)
計算機(jī)中只有1和0,所以就導(dǎo)致很多時候需要進(jìn)制轉(zhuǎn)換,本文介紹了Python進(jìn)制轉(zhuǎn)換,十進(jìn)制轉(zhuǎn)二進(jìn)制,十進(jìn)制轉(zhuǎn)八進(jìn)制,十進(jìn)制轉(zhuǎn)十六進(jìn)制,有興趣的可以了解一下2021-05-05python機(jī)器學(xué)習(xí)GCN圖卷積神經(jīng)網(wǎng)絡(luò)原理解析
這篇文章主要為大家介紹了GCN圖卷積神經(jīng)網(wǎng)絡(luò)原理及代碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05PyTorch?device與cuda.device用法介紹
這篇文章主要介紹了PyTorch?device與cuda.device用法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04