用户:
小智23查看:0 回复:1 评论:0 创建时间:2023-11-12T18:36:52
【作品展示】

【作品介绍】
小恐龙
【作品源代码】
import pygame
import sys
# 初始化 Pygame
pygame.init()
# 设置窗口大小
win_width, win_height = 600, 150
win = pygame.display.set_mode((win_width, win_height))
# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
# 设置恐龙和障碍物的属性
dino_height = 20
dino_width = 20
dino_x = 50
dino_y = win_height - dino_height
obstacle_width = 20
obstacle_height = 20
obstacle_x = win_width
obstacle_y = win_height - obstacle_height
obstacle_vel = 2
jump = False
jump_count = 10
# 游戏主循环
while True:
pygame.time.delay(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
jump = True
if jump:
if jump_count >= -10:
dino_y -= (jump_count * abs(jump_count)) * 0.3
jump_count -= 1
else:
jump = False
jump_count = 10
win.fill(white)
pygame.draw.rect(win, black, (dino_x, dino_y, dino_width, dino_height))
pygame.draw.rect(win, black, (obstacle_x, obstacle_y,
obstacle_width, obstacle_height))
obstacle_x -= obstacle_vel
if obstacle_x < -obstacle_width:
obstacle_x = win_width
if dino_y + dino_height > obstacle_y and dino_x + dino_width > obstacle_x:
if dino_x < obstacle_x + obstacle_width:
pygame.quit()
sys.exit()
pygame.display.update()
pygame.quit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn