最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Python實(shí)現(xiàn)射擊小游戲的制作

 更新時(shí)間:2022年04月06日 10:04:01   作者:五包辣條!  
這篇文章主要介紹了如何利用Python制作一個(gè)自己專屬的第一人稱射擊小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試

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)文章

最新評論

海兴县| 泰安市| 囊谦县| 五河县| 垫江县| 娱乐| 正镶白旗| 涿鹿县| 农安县| 怀宁县| 思南县| 格尔木市| 大庆市| 齐齐哈尔市| 登封市| 开江县| 沽源县| 东乌| 大田县| 宜丰县| 黑河市| 无为县| 桦南县| 大兴区| 凉山| 鄂托克旗| 河间市| 崇信县| 南昌市| 延吉市| 图片| 哈巴河县| 大新县| 碌曲县| 雷州市| 尚志市| 临潭县| 雅江县| 宁国市| 涞水县| 手游|