python实现简单flappy bird
作者:wwxy261 时间:2022-08-03 12:06:33
本文实例为大家分享了python实现flappy bird的简单代码,供大家参考,具体内容如下
import pygame
from pygame.locals import *
from sys import exit
import random
# 屏幕宽度
SCREENWIDTH = 288
# 屏幕高度
SCREENHEIGHT = 512
IMAGES = {}
# 背景图片地址
BACKGROUND_PATH = 'back_ground.png'
PIPE_PATH = 'pipe.png'
BASE_PATH = 'base.png'
PLAYER_PATH = (
'bird2_0.png',
'bird2_1.png',
'bird2_2.png',
)
# 初始化
pygame.init()
# 创建窗口
SCREEN = pygame.display.set_mode((SCREENHEIGHT, SCREENHEIGHT))
# 设置窗口标题
pygame.display.set_caption("Flappy Bird")
# 加载图片,透明用convert_alpha,不透明用convert
IMAGES['background'] = pygame.image.load(BACKGROUND_PATH).convert()
IMAGES['base'] = pygame.image.load(BASE_PATH).convert_alpha()
IMAGES['bird'] = (
pygame.image.load(PLAYER_PATH[0]).convert_alpha(),
pygame.image.load(PLAYER_PATH[1]).convert_alpha(),
pygame.image.load(PLAYER_PATH[2]).convert_alpha(),
)
IMAGES['pipe'] = (
pygame.transform.rotate(pygame.image.load(PIPE_PATH).convert_alpha(), 180),
pygame.image.load(PIPE_PATH).convert_alpha()
)
BASEY = SCREENHEIGHT * 0.82
# 设置帧率
FPS = 30
FPSCLOCK = pygame.time.Clock()
PIPE_WIDTH = IMAGES['pipe'][0].get_width()
PIPE_HEIGHT = IMAGES['pipe'][0].get_height()
PLAYER_WIDTH = IMAGES['bird'][0].get_width()
PLAYER_HEIGHT = IMAGES['bird'][0].get_height()
PIPEGAPSIZE = 100 # 两个水管之间的距离
x = SCREENWIDTH//2
y = SCREENHEIGHT//2
move_x = 0
move_y = 0
flap = 0 # 小鸟初始状态
pipeVelX = -4 # 管道x方向的速度
playerVelY = 0 # 小鸟y方向的初速度
playerMaxVelY = 10 # 小鸟y方向的最大速度
playerMinVelY = -8 # 小鸟y方向的最小速度
playerAccY = 2 # 小鸟y方向的下降加速度
playerFlapAcc = -3 # 小鸟y方向的上升加速度
playerFLapped = False # 当小鸟飞的时候为真
playery = int((SCREENHEIGHT - PLAYER_HEIGHT)/2)
# 随机移动柱子
def getRandomPipe():
# 两个水管之间的距离有如下变量
gapYs = [20, 30, 40, 50, 60, 70, 80, 90]
index = random.randint(0, len(gapYs) - 1)
gapY = gapYs[index]
gapY += int(BASEY * 0.2)
# 水管x坐标
pipeX = SCREENWIDTH + 10
return [
{'x': pipeX, 'y': gapY - PIPE_HEIGHT}, # 上面水管的左上角位置
{'x': pipeX, 'y': gapY + PIPEGAPSIZE}, # 下面水管的左上角位置
]
newPipel = getRandomPipe()
upperPipes = [
{'x': SCREENWIDTH, 'y':newPipel[0]['y']}
]
lowerPipes = [
{'x': SCREENWIDTH, 'y':newPipel[1]['y']}
]
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN:
if event.key == K_LEFT:
move_x = -3
elif event.key == K_RIGHT:
move_x = 3
elif event.key == K_UP:
move_y = -3
elif event.key == K_DOWN:
move_y = 3
elif event.type == KEYUP:
move_x = 0
move_y = 0
x = x + move_x
y = y + move_y
# 防止冲出边界
if x > SCREENWIDTH:
x = 0
elif x < 0:
x = SCREENWIDTH
if y > SCREENHEIGHT:
y = 0
elif y < 0:
y = SCREENHEIGHT
# 贴图在左上角
SCREEN.blit(IMAGES['background'], (0, 0)) # 背景
# 显示水管
for uPipe, lPipe in zip(upperPipes, lowerPipes):
SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))
SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))
# 放小鸟
SCREEN.blit(IMAGES['bird'][flap], (x, y))
flap = flap + 1
if flap % 3 == 0:
flap = 0
for uPipe, lPipe in zip(upperPipes, lowerPipes):
uPipe['x'] += pipeVelX
lPipe['x'] += pipeVelX
# 当水管移动到某一位置的时候,生成新的水管
if 0 < upperPipes[0]['x'] < 5:
newPipe = getRandomPipe()
upperPipes.append(newPipe[0])
lowerPipes.append(newPipe[1])
# 如果水管从右往左移动到边缘,则摧毁水管
if upperPipes[0]['x'] < -PIPE_WIDTH:
# 队列头出队
upperPipes.pop(0)
lowerPipes.pop(0)
# 刷新画面
pygame.display.update()
FPSCLOCK.tick(FPS)
来源:https://blog.csdn.net/wwxy1995/article/details/81436788
标签:python,flappy,bird
0
投稿
猜你喜欢
Python双端队列deque的实现
2022-07-07 02:37:29
开启MySQL的binlog日志的方法步骤
2024-01-29 07:31:25
SqlServer将数据库中的表复制到另一个数据库
2024-01-22 11:31:33
简单介绍Python虚拟环境及使用方法
2021-03-19 20:59:53
详解Python Socket网络编程
2022-09-05 14:04:16
采用memcache在web集群中实现session的同步会话
2023-10-01 22:26:37
MySQL自动为查询数据结果加序号
2024-01-20 03:17:51
golang使用map支持高并发的方法(1000万次操作14ms)
2024-05-13 10:41:33
比较详细PHP生成静态页面教程
2023-10-14 18:54:31
Tensorflow 2.1完成对MPG回归预测详解
2023-06-30 07:31:36
SQL Server中的执行引擎入门 图解
2012-06-06 20:08:26
浅谈vue单一组件下动态修改数据时的全部重渲染
2024-04-27 15:51:55
Python反爬虫伪装浏览器进行爬虫
2023-07-05 22:33:38
vue中watch监听器用法之deep、immediate、flush
2024-04-27 16:13:35
Flask-SQLALchemy基本使用方法
2023-07-01 23:19:16
Javascript 中截取小数位并实现四舍五入的方法
2008-08-05 18:11:00
Oracle 查看表空间的大小及使用情况sql语句
2023-07-14 20:06:31
Thinkphp5.0框架视图view的循环标签用法示例
2024-06-05 09:22:48
Linux下mysql的root密码修改方法
2024-01-13 17:39:44
python初学者,用python实现基本的学生管理系统(python3)代码实例
2023-07-01 11:40:01