Python贪吃蛇游戏代码:简单易懂,快速上手!想学习如何用Python编写游戏吗?这是一个经典的贪吃蛇游戏代码示例,使用Pygame库实现,简单易懂,非常适合初学者!### 代码pythonimport pygameimport random# 定义颜色BLACK = (0, 0, 0)WHITE = (255, 255, 255)GREEN = (0, 255, 0)RED = (255, 0, 0)# 设置屏幕宽高和格子大小SCREEN_WIDTH = 800SCREEN_HEIGHT = 600GRID_SIZE = 20# 初始化游戏pygame.init()screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))pygame.display.set_caption('贪吃蛇')# 定义贪吃蛇类class Snake: def init(self): self.body = [(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)] # 蛇身体坐标列表 self.direction = random.choice(['up', 'down', 'left', 'right']) # 蛇移动方向 def move(self): head_x, head_y = self.body[0] if self.direction == 'up': new_head = (head_x, head_y - GRID_SIZE) elif self.direction == 'down': new_head = (head_x, head_y + GRID_SIZE) elif self.direction == 'left': new_head = (head_x - GRID_SIZE, head_y) elif self.direction == 'right': new_head = (head_x + GRID_SIZE, head_y) self.body.insert(0, new_head) self.body.pop() def change_direction(self, new_direction): if new_direction == 'up' and self.direction != 'down': self.direction = new_direction elif new_direction == 'down' and self.direction != 'up': self.direction = new_direction elif new_direction == 'left' and self.direction != 'right': self.direction = new_direction elif new_direction == 'right' and self.direction != 'left': self.direction = new_direction def check_collision(self): head = self.body[0] if head[0] < 0 or head[0] >= SCREEN_WIDTH or head[1] < 0 or head[1] >= SCREEN_HEIGHT: return True for segment in self.body[1:]: if segment == head: return True return False def eat_food(self, food): if self.body[0] == food.position: self.body.append(food.position) food.generate_position()# 定义食物类class Food: def init(self): self.position = (0, 0) self.generate_position() def generate_position(self): x = random.randint(0, (SCREEN_WIDTH - GRID_SIZE) // GRID_SIZE) * GRID_SIZE y = random.randint(0, (SCREEN_HEIGHT - GRID_SIZE) // GRID_SIZE) * GRID_SIZE self.position = (x, y)# 初始化贪吃蛇和食物snake = Snake()food = Food()clock = pygame.time.Clock()game_over = Falsewhile not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: snake.change_direction('up') elif event.key == pygame.K_DOWN: snake.change_direction('down') elif event.key == pygame.K_LEFT: snake.change_direction('left') elif event.key == pygame.K_RIGHT: snake.change_direction('right') snake.move() snake.check_collision() snake.eat_food(food) screen.fill(BLACK) for segment in snake.body: pygame.draw.rect(screen, GREEN, (segment[0], segment[1], GRID_SIZE, GRID_SIZE)) pygame.draw.rect(screen, RED, (food.position[0], food.position[1], GRID_SIZE, GRID_SIZE)) pygame.display.flip() clock.tick(10)pygame.quit()### 代码说明:- 这段代码使用了Pygame库来创建窗口、处理用户输入和绘制图形。- 贪吃蛇通过键盘控制移动方向,吃掉食物会增长身体长度,当蛇头碰到边界或自己的身体时游戏结束。- 游戏窗口大小为800x600像素,每个格子大小为20x20像素。- 贪吃蛇的移动速度为每秒10帧。### 如何运行代码:1. 确保你的电脑上安装了Python和Pygame库。2. 将代码保存为.py文件,例如snake/_game.py。3. 在终端或命令行中,进入代码所在的目录,然后运行命令 python snake_game.py。希望你喜欢这个简单的Python贪吃蛇游戏!

Python贪吃蛇游戏代码:简单易懂,快速上手!

原文地址: https://www.cveoy.top/t/topic/lwK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录