猫史档案馆


【Python作品分享】7-2【作品秀】

用户:郭敏nlrJ郭敏nlrJ查看:0 回复:1 评论:0 创建时间:2023-07-22T21:13:50


【作品展示】

center_image

 

【作品介绍】

飞机大战!

 

【作品源代码】

import pgzrun  # 导入游戏库
import random
isLoose=False
WIDTH = 480    # 设置窗口的宽度
HEIGHT = 700   # 设置窗口的高度
score=0
background1 = Actor('background')  # 导入背景图片
background1.x = 480/2  # 背景1的x坐标
background1.y = 852/2  # 背景1的y坐标
background2 = Actor('background')  # 导入背景图片
background2.x = 480/2   # 背景2的x坐标
background2.y = -852/2  # 背景2的y坐标
bullet = Actor('bullet')
bullet.x=WIDTH/2
bullet.y=-HEIGHT/2
hero = Actor('hero')  # 导入玩家飞机图片
hero.x = WIDTH/2      # 设置玩家飞机的x坐标
hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标

enemy = Actor('enemy')
enemy.x = WIDTH/2
enemy.y = 0

bullet1 = Actor('bullet1')
bullet1.x = enemy.x
bullet1.y = enemy.y+50

def on_mouse_down():
    bullet.x=hero.x
    bullet.y = hero.y-70




def draw():  # 绘制模块,每帧重复执行
    background1.draw()  # 绘制游戏背景
    background2.draw()  # 绘制游戏背景
    hero.draw()  # 绘制玩家飞机
    enemy.draw()
    bullet.draw()
    bullet1.draw()
    screen.draw.text(str(score), (240, HEIGHT-50), fontsize=30, 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 hero.colliderect(enemy):
        isLoose=True
        hero.image='hero_blowup'
        
    global score  # 以下代码用于实现背景图片的循环滚动效果
    if background1.y > 852/2 + 852:
        background1.y = -852/2  # 背景1移动到背景2的正上方
    if background2.y > 852/2 + 852:
        background2.y = -852/2  # 背景2移动到背景1的正上方
    background1.y += 1  # 背景1向下滚动
    background2.y += 1  # 背景2向下滚动
    if bullet.y>-HEIGHT:
        bullet.y = bullet.y-10

    enemy.y+=3
    bullet1.y+=10
    if enemy.y>HEIGHT:
        enemy.y=0
        enemy.x=random.randint(50,HEIGHT-50)

    if bullet.colliderect(enemy):
        enemy.y=0
        enemy.x=random.randint(0,WIDTH)
        score=score+1
        bullet.y=-HEIGHT

def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行
    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标
    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标


pgzrun.go()  # 开始执行游戏

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
善良的星熊喵善良的星熊喵

让我改成游戏(程序,后缀为.exe)发给你

点赞0


评论