基于Python實(shí)現(xiàn)射擊小游戲的制作
1.游戲畫面
1.1開始
1.2射擊怪物
2.涉及知識點(diǎn)
1.sprites
2.pygame混音器
3.圖章
4.python基礎(chǔ)語法
3.代碼
3.1發(fā)射聲
from sprites import * try: import pygame pygame.mixer.init() fire_sound = pygame.mixer.Sound("audio/發(fā)射聲.wav") cricket_sound = pygame.mixer.Sound('audio/cricket.wav') except: import sys input("本程序需要pygame混音器支持以便配音,請先在cmd下用pip install pygame安裝此模塊。")
3.2背景
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()
3.3射擊效果
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) # 鼠標(biāo)左鍵 m3 = Mouse(3) # 鼠標(biāo)右鍵 clock = Clock() # 時(shí)鐘對象 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) # 蝙蝠碰到鼠標(biāo)指針并且按下了鼠標(biāo)左鍵 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()) # 發(fā)射子彈,用蓋圖章留下彈洞,為防連續(xù)發(fā)射用了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)
4.經(jīng)驗(yàn)總結(jié)
利用python各種游戲庫可以做任何小游戲
到此這篇關(guān)于基于Python實(shí)現(xiàn)射擊小游戲的制作的文章就介紹到這了,更多相關(guān)Python射擊游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)曲線的肘部點(diǎn)檢測詳解
肘部法則是經(jīng)常使用的法則。很多時(shí)候,可以憑人工經(jīng)驗(yàn)去找最優(yōu)拐點(diǎn),但有時(shí)需要自動尋找拐點(diǎn)。本文為大家介紹了Python實(shí)現(xiàn)曲線的肘部點(diǎn)檢測的方法,希望對大家有所幫助2023-02-02Python函數(shù)的作用域及內(nèi)置函數(shù)詳解
這篇文章主要介紹了python函數(shù)的作用域及內(nèi)置函數(shù)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09如何基于windows實(shí)現(xiàn)python定時(shí)爬蟲
這篇文章主要介紹了如何基于windows實(shí)現(xiàn)python定時(shí)爬蟲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05python基于pygame實(shí)現(xiàn)飛機(jī)大作戰(zhàn)小游戲
這篇文章主要為大家詳細(xì)介紹了python基于pygame實(shí)現(xiàn)飛機(jī)大作戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11pycharm安裝django框架詳細(xì)圖文教程(指定版本)
這篇文章主要給大家介紹了關(guān)于pycharm安裝django框架(指定版本)的相關(guān)資料,PyCharm是一種Python?IDE,帶有一整套可以幫助用戶在使用Python語言開發(fā)時(shí)提高其效率的工具,需要的朋友可以參考下2023-10-10