用代码写个推箱子游戏

not image

```python import pygame import sys import random # 初始化pygame pygame.init() # 设置屏幕大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置标题 pygame.display.set_caption("推箱子游戏") # 加载图片 bg = pygame.image.load("background.png") box = pygame.image.load("box.png") # 设置颜色 WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 设置速度 clock = pygame.time.Clock() speed = 10 # 设置方向 direction = "right" # 设置初始位置 x = screen_width // 2 - box.get_width() // 2 y = screen_height - box.get_height() // 2 # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and direction != "down": direction = "up" elif event.key == pygame.K_DOWN and direction != "up": direction = "down" elif event.key == pygame.K_LEFT and direction != "right": direction = "left" elif event.key == pygame.K_RIGHT and direction != "left": direction = "right" # 更新位置 x += speed * cos(direction) y += speed * sin(direction) # 检查碰撞 if x < 0 or x > screen_width - box.get_width() or y < 0 or y > screen_height - box.get_height(): return False # 绘制背景和箱子 screen.blit(bg, (0, 0)) screen.blit(box, (x, y)) # 更新屏幕 pygame.display.update() # 控制速度 clock.tick(60) ```

评论区:

评论:

请输入内容: