最新国产好看的视频,伊人天堂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.涉及知識(shí)點(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混音器支持以便配音,請(qǐng)先在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í)鐘對(duì)象 
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各種游戲庫(kù)可以做任何小游戲

到此這篇關(guān)于基于Python實(shí)現(xiàn)射擊小游戲的制作的文章就介紹到這了,更多相關(guān)Python射擊游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

定南县| 鄂尔多斯市| 台州市| 闵行区| 田林县| 嘉禾县| 石柱| 巴林左旗| 开远市| 曲靖市| 田阳县| 华安县| 高青县| 临沭县| 榆社县| 三都| 合江县| 遂川县| 博野县| 阿巴嘎旗| 崇明县| 庆阳市| 宜兰市| 乌恰县| 汕尾市| 通化县| 乡城县| 台安县| 达尔| 盐津县| 亳州市| 措美县| 鸡东县| 富顺县| 澎湖县| 平湖市| 玉田县| 大余县| 安化县| 彭州市| 岐山县|