写个跑酷游戏代码并运行
时间: 2023-05-26 09:05:52 浏览: 140
很抱歉,我是一个AI语言模型,无法直接运行代码。以下是一个简单的跑酷游戏代码供你参考:
```python
import pygame
import random
pygame.init()
# 设置游戏画面大小
screen = pygame.display.set_mode((800, 600))
# 加载游戏素材
player_img = pygame.image.load('player.png')
obstacle_img = pygame.image.load('obstacle.png')
score_font = pygame.font.SysFont(None, 30)
# 设置游戏参数
player_x = 50
player_y = 500
obstacle_x = 800
obstacle_y = 500
score = 0
clock = pygame.time.Clock()
# 游戏主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 更新游戏参数
player_x += 5
obstacle_x -= 8
if obstacle_x < -40:
obstacle_x = 800
obstacle_y = random.choice([450, 500, 550])
score += 1
# 碰撞检测
player_rect = pygame.Rect(player_x, player_y, 40, 40)
obstacle_rect = pygame.Rect(obstacle_x, obstacle_y, 40, 40)
if player_rect.colliderect(obstacle_rect):
score = 0
player_x = 50
obstacle_x = 800
# 绘制画面
screen.fill((255, 255, 255))
screen.blit(player_img, (player_x, player_y))
screen.blit(obstacle_img, (obstacle_x, obstacle_y))
score_text = score_font.render('Score: {}'.format(score), True, (0, 0, 0))
screen.blit(score_text, (10, 10))
pygame.display.update()
# 控制帧率
clock.tick(60)
```
这段代码通过Pygame库实现了一个简单的跑酷游戏,具体预期表现可以运行试试看。
阅读全文