Flappy Bird游戏创建小鸟类

本文介绍了一个简单的Flappy Bird游戏实现方法,使用Python和Pygame库。通过不断按键使小鸟保持飞行状态,涉及游戏对象的定义及更新逻辑,如小鸟的位置调整、重力加速度等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

# -*- coding:utf-8 -*-
import pygame
import sys
import random

class Bird(object):
    def __init__(self):
        self.birdRect=pygame.Rect(65,50,50,50)
        self.birdStatus =[pygame.image.load("assets/1.png"),
                          pygame.image.load("assets/2.png"),
                          pygame.image.load("assets/dead.png")]

        # self.birdStatus = [pygame.image.load("assets/1.png"),
        #                    pygame.image.load("assets/2.png"),
        #                    pygame.image.load("assets/dead.png")]
        self.status=0
        self.birdX=120
        self.birdY=350
        self.jump=False
        self.jumpSpeed=10
        self.gravity=5
        self.dead=False



    def birdUpdate(self):
        if self.jump:
            self.jumpSpeed-=1
            self.birdY-=self.jumpSpeed
        else:
            self.gravity+=0.2
            self.birdY+=self.gravity
        self.birdRect[1]=self.birdY

class Pipeline(object):
    def __init__(self):
        pass


    def updatePipeline(self):
        pass



def createMap():
    """定义创建地图的方法"""
    screen.fill((255, 255, 255))  # 填充颜色(screen还没定义不要着急)
    screen.blit(background, (0, 0))  # 填入到背景
    if Bird.dead:
        Bird.status=2
    elif Bird.jump:
        Bird.status=1

    screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY))
    Bird.birdUpdate()
    pygame.display.update()




    pygame.display.update()  # 更新显示
#
if __name__=='__main__':
    pygame.init()

    size =width,height=400,620
    screen=pygame.display.set_mode(size)
    clock = pygame.time.Clock()

    Pipeline=Pipeline()
    Bird=Bird()

    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
            if(event.type==pygame.KEYDOWN or event.type==pygame.MOUSEBUTTONDOWN)and not Bird.dead:
                Bird.jump=True
                Bird.gravity=5
                Bird.jumpSpeed=10





        background = pygame.image.load("assets/background.png")
        createMap()
    pygame.quit()

运行结果如图:

用鼠标或者键盘任意键连续不停的按点击或者按下,鸟才能飞起来 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值