猫史档案馆


【Python作品分享】飞机大战【作业帖】

用户:顾青山KdWo顾青山KdWo查看:0 回复:0 评论:0 创建时间:2023-04-26T10:52:59


【作品展示】

center_image

 

【作品介绍】

飞机大战

 

【作品源代码】

import pgzrun
import random
bs = []
WIDTH = 480
HEIGHT=852
bg1=Actor('background')
bg1.x=480/2
bg1.y=852/2
bg2 = Actor('background')
bg2.x = 480/2
bg2.y=-852/2
he=Actor('hero')
he.x=WIDTH/2
he.y=HEIGHT*2/3
bu=Actor('bullet')
bu.x=WIDTH/2
bu.y=-10
bs.append(bu)
en=Actor('enemy')
en.x=WIDTH/2
en.y=0
score=0
isLoose=False
sounds.game_music.play(-1)


def draw():
    bg1.draw()
    bg2.draw()
    he.draw()
    bu.draw()
    en.draw()
    screen.draw.text(str(score),(240,HEIGHT-50),fontsize=30,fontname='s',color='black')
    if isLoose:
        screen.draw.text("游戏失败!",(50,HEIGHT/2),fontsize=90,fontname='s',color='red')

def update():
    global score,isLoose
    if isLoose:
        return
    if bg1.y>852/2+852:
        bg1.y=-852/2
    if bg2.y > 852/2+852:
        bg2.y = -852/2
    bg1.y += 1
    bg2.y+=1
    for bullet in bs:
        if bu.y>-HEIGHT:
            bu.y=bu.y-1
            
    en.y+=3
    if en.y>HEIGHT:
        en.y=0
        en.x=random.randint(80,400)
    if bu.colliderect(en):
        sounds.喵.play()
        en.y=0
        en.x=random.randint(0,WIDTH)
        score=score+1
        bu.y=-HEIGHT

    if he.colliderect(en):
        isLoose=True
        sounds.explode.play()
        he.image='hero_blowup'


def on_mouse_move(pos,rel,buttons):
    if isLoose:
        return
    he.x=pos[0]
    he.y=pos[1]
def on_mouse_down():
    if isLoose:
        return
    bu.x=he.x
    bu.y=he.y-70
    bs.append(bu)
    sounds.喵.play()


pgzrun.go()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页