easyx飞翔的小鸟
时间: 2023-11-28 20:04:37 浏览: 39
以下是使用easyx框架开发的飞翔的小鸟游戏:
```python
from easygraphics import *
import random
# 定义常量
WIDTH = 288
HEIGHT = 512
GROUNDY = HEIGHT * 0.8
PIPE_GAP_SIZE = 100
PIPE_SPEED = 2
BIRD_SPEED = 5
GRAVITY = 0.3
JUMP_SPEED = -6
BIRD_SIZE = 36
PIPE_WIDTH = 52
PIPE_HEIGHT = 320
PIPE_COUNT = 3
SCORE_FONT_SIZE = 36
SCORE_X = WIDTH // 2
SCORE_Y = HEIGHT // 5
# 定义变量
bird_y = 0
bird_speed = 0
score = 0
pipe_x_list = []
pipe_y_list = []
pipe_passed_list = []
game_over = False
# 初始化窗口
init_graph(WIDTH, HEIGHT, "Flappy Bird")
# 加载图片
bg_img = Image.open("assets/bg.png")
ground_img = Image.open("assets/ground.png")
bird_imgs = [Image.open("assets/bird0.png"),
Image.open("assets/bird1.png"),
Image.open("assets/bird2.png")]
pipe_img = Image.open("assets/pipe.png")
score_font = Font("Arial", SCORE_FONT_SIZE)
# 初始化管道位置
for i in range(PIPE_COUNT):
pipe_x = WIDTH + i * (WIDTH + PIPE_WIDTH) // PIPE_COUNT
pipe_y = random.randint(-PIPE_HEIGHT // 2, PIPE_HEIGHT // 2)
pipe_x_list.append(pipe_x)
pipe_y_list.append(pipe_y)
pipe_passed_list.append(False)
# 游戏循环
while is_run():
# 处理事件
if has_kb_hit():
key = get_kb_hit()
if key == ' ':
bird_speed = JUMP_SPEED
elif key == 'r':
# 重新开始游戏
bird_y = 0
bird_speed = 0
score = 0
pipe_x_list = []
pipe_y_list = []
pipe_passed_list = []
game_over = False
for i in range(PIPE_COUNT):
pipe_x = WIDTH + i * (WIDTH + PIPE_WIDTH) // PIPE_COUNT
pipe_y = random.randint(-PIPE_HEIGHT // 2, PIPE_HEIGHT // 2)
pipe_x_list.append(pipe_x)
pipe_y_list.append(pipe_y)
pipe_passed_list.append(False)
# 更新游戏状态
if not game_over:
# 更新小鸟位置
bird_speed += GRAVITY
bird_y += bird_speed
if bird_y > GROUNDY - BIRD_SIZE // 2:
bird_y = GROUNDY - BIRD_SIZE // 2
bird_speed = 0
game_over = True
# 更新管道位置
for i in range(PIPE_COUNT):
pipe_x_list[i] -= PIPE_SPEED
if pipe_x_list[i] < -PIPE_WIDTH // 2:
pipe_x_list[i] = WIDTH + PIPE_WIDTH // 2
pipe_y_list[i] = random.randint(-PIPE_HEIGHT // 2, PIPE_HEIGHT // 2)
pipe_passed_list[i] = False
if not pipe_passed_list[i] and pipe_x_list[i] < BIRD_SIZE // 2:
pipe_passed_list[i] = True
score += 1
if not game_over and pipe_x_list[i] < BIRD_SIZE // 2 + PIPE_WIDTH // 2 \
and pipe_x_list[i] > -BIRD_SIZE // 2 - PIPE_WIDTH // 2 \
and (bird_y - bird_imgs[0].height // 2 < pipe_y_list[i] + PIPE_GAP_SIZE // 2
or bird_y + bird_imgs[0].height // 2 > pipe_y_list[i] + PIPE_GAP_SIZE // 2 + PIPE_GAP_SIZE):
game_over = True
# 绘制游戏界面
clear_device()
# 绘制背景
bg_img.draw(0, 0)
# 绘制管道
for i in range(PIPE_COUNT):
pipe_img.draw(pipe_x_list[i] - PIPE_WIDTH // 2, pipe_y_list[i] - PIPE_HEIGHT // 2)
pipe_img.draw(pipe_x_list[i] - PIPE_WIDTH // 2, pipe_y_list[i] + PIPE_GAP_SIZE + PIPE_HEIGHT // 2)
# 绘制地面
ground_img.draw(0, GROUNDY - ground_img.height)
# 绘制小鸟
bird_index = (get_clock() // 100) % len(bird_imgs)
bird_imgs[bird_index].draw(WIDTH // 2 - BIRD_SIZE // 2, bird_y - BIRD_SIZE // 2)
# 绘制分数
set_font(score_font)
set_color("white")
draw_text(SCORE_X, SCORE_Y, str(score))
# 显示游戏结束文字
if game_over:
set_font(score_font)
set_color("red")
draw_text(SCORE_X, SCORE_Y * 2, "Game Over")
# 更新窗口
delay_fps(60)
# 关闭窗口
close_graph()
```