请写一段火柴人跑酷代码
时间: 2024-01-21 07:19:19 浏览: 117
以下是一个基于Python的简单火柴人跑酷游戏代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption("Stickman Parkour")
# 设置字体
font = pygame.font.SysFont(None, 30)
# 设置火柴人
stickman_image = pygame.image.load("stickman.png")
stickman_width = 50
stickman_height = 50
stickman_x = 100
stickman_y = 450
stickman_y_velocity = 0
# 设置障碍物
obstacle_width = 50
obstacle_height = 50
obstacle_x = screen_width
obstacle_y = 450
# 设置得分
score = 0
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
stickman_y_velocity = -10
# 移动火柴人
stickman_y += stickman_y_velocity
stickman_y_velocity += 1
if stickman_y > 450:
stickman_y = 450
stickman_y_velocity = 0
# 移动障碍物
obstacle_x -= 5
if obstacle_x < -obstacle_width:
obstacle_x = screen_width
obstacle_y = random.randint(350, 450)
score += 1
# 绘制屏幕
screen.fill((255, 255, 255))
screen.blit(stickman_image, (stickman_x, stickman_y))
pygame.draw.rect(screen, (255, 0, 0), (obstacle_x, obstacle_y, obstacle_width, obstacle_height))
score_text = font.render("Score: " + str(score), True, (0, 0, 0))
screen.blit(score_text, (10, 10))
pygame.display.update()
```
运行该代码,即可在 Pygame 窗口中看到一个简单的火柴人跑酷游戏,玩家需要通过按下空格键使火柴人跳过障碍物。
阅读全文