栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

飞扬的小鸟游戏_飞扬的小鸟代码?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

飞扬的小鸟游戏_飞扬的小鸟代码?

飞扬的小鸟(Python)

话不多说直接看效果!上代码!

话不多说直接看效果!

上代码!
import pgzrun
import random

WIDTH = 350 # 设置窗口宽度
HEIGHT = 600 # 设置窗口高度
score = 0 # 得分
speed = 1 # 速度
flag = 0 # 标志位,判断游戏是否结束

background = Actor('background') # 导入背景图片

Birds = [Actor('bird11'),Actor('bird22'),Actor('bird33')] # 导入小鸟的图片
numBirds = len(Birds) # 计算一共有多少张小鸟图片
birdsIndex = 0 # 当前下标
bird_x = 50 # 设置小鸟x轴位置
bird_y = HEIGHT / 2 # 设置小鸟y轴位置
birdsSpeed = 0 # 小鸟切换的速度
for i in range(numBirds): # 将位置赋值给每一张图片
    Birds[i].x = bird_x
    Birds[i].y = bird_y

bar_up = Actor('bar_up') # 上面的障碍物
bar_up.x = 300
bar_up.y = 0
bar_down = Actor('bar_down') # 下面的障碍物
bar_down.x = 300
bar_down.y = 600

def draw():
    background.draw() # 画背景
    bar_up.draw() # 画上面的障碍物
    bar_down.draw() # 画下面的障碍物
    screen.draw.text(str(score),(30,30),fontsize=50,color='red') # 画分数
    Birds[birdsIndex].draw() # 画小鸟
    if flag: # 如果游戏失败就显示下列文本
        screen.draw.text('游戏结束',(60,200),fontsize=60,fontname='fzshuangqtjw_cu',color=(237,169,82))
        screen.draw.text('重新开始', (100, 350), fontsize=40, fontname='fzshuangqtjw_cu', color=(147,233,148))


def update():
    global score,speed,birdsIndex,bird_y,bird_x,birdsSpeed,flag # 全局变量
    if not flag: # 如果游戏没有失败
        # 障碍物向左移动,造成小鸟在往前飞的假象
        bar_up.x -= speed
        bar_down.x -= speed
        # 如果障碍物移到最左侧,就让它们从最右侧重新开始
        if bar_up.x < -25 or bar_down.x < -25:
            bar_up.x = WIDTH
            bar_down.x = WIDTH
            bar_up.y = random.randint(-200,200) # 用随机来实现障碍物出现在不同的位置
            bar_down.y = 600 + bar_up.y
            score += 10 # 每当一个障碍物移到最左侧时,分数就增加10分
            if score % 50 == 0: # 当分数每增加50时,障碍物移动速度也相应增加,提高游戏难度
                speed += 0.5
        bird_y += (2 + speed * 0.2) # 小鸟的下落速度,
        for i in range(numBirds): # 更新小鸟位置,并判断小鸟是否与障碍物碰撞,如果发生碰撞,标志位就置1,表示游戏失败
            Birds[i].y = bird_y
            if Birds[i].colliderect(bar_up) or Birds[i].colliderect(bar_down) or Birds[i].y < 0 or Birds[i].y > HEIGHT:
                flag = 1
        # 下面这段就是减缓小鸟煽动翅膀的速度
        birdsSpeed += 1
        if birdsSpeed % 10 == 0:
            birdsIndex += 1
            if birdsIndex >= numBirds:
                birdsIndex = 0

def on_mouse_down(pos,button):
    global bird_y,flag,score,speed,bird_x,birdsSpeed
    if not flag: # 如果游戏没有失败
        bird_y -= (50 + speed * 5) # 小鸟上升,随着游戏速度的增加,上升的距离也随之增加
    if flag: # 如果游戏失败,判断玩家是否点击了重新开始,如果点击了,一切数据回归初始状态
        if button == mouse.LEFT and pos[0] > 100 and pos[0] < 262 and pos[1] > 350 and pos[1] < 398: # 如果鼠标左键按下退出游戏
            flag = 0
            score = 0
            speed = 1
            birdsSpeed = 0
            bird_x = 50
            bird_y = HEIGHT / 2
            bar_up.x = WIDTH
            bar_up.y = 0
            bar_down.x = WIDTH
            bar_down.y = 600

pgzrun.go() # 开始游戏
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/787021.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号