Python贪吃蛇游戏代码示例 - 从零实现经典游戏
Python贪吃蛇游戏代码示例 - 从零实现经典游戏
想学习如何使用Python编写游戏吗?这篇文章提供了一个简单的贪吃蛇游戏代码示例,带你体验用代码实现经典游戏的乐趣。
代码实现
本示例使用 pygame 库来处理图形界面和用户输入。如果你还没有安装 pygame,可以使用 pip install pygame 命令进行安装。pythonimport pygameimport random
游戏窗口的宽度和高度WINDOW_WIDTH = 400WINDOW_HEIGHT = 400
蛇身方块的大小和初始长度BLOCK_SIZE = 20INITIAL_LENGTH = 4
颜色定义BLACK = (0, 0, 0)WHITE = (255, 255, 255)GREEN = (0, 255, 0)RED = (255, 0, 0)
初始化pygamepygame.init()
创建游戏窗口window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))pygame.display.set_caption('贪吃蛇游戏')
clock = pygame.time.Clock()
class Snake: def init(self): self.length = INITIAL_LENGTH self.positions = [] self.direction = 'right'
# 初始化蛇身位置 for i in range(self.length, 0, -1): self.positions.append((i * BLOCK_SIZE, 0))
def move(self): head_x, head_y = self.positions[0] if self.direction == 'up': new_head = (head_x, head_y - BLOCK_SIZE) elif self.direction == 'down': new_head = (head_x, head_y + BLOCK_SIZE) elif self.direction == 'left': new_head = (head_x - BLOCK_SIZE, head_y) else: new_head = (head_x + BLOCK_SIZE, head_y)
self.positions.insert(0, new_head) if len(self.positions) > self.length: self.positions.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 draw(self): for position in self.positions: pygame.draw.rect(window, GREEN, (position[0], position[1], BLOCK_SIZE, BLOCK_SIZE))
class Food: def init(self): self.position = (0, 0) self.spawn_food()
def spawn_food(self): x = random.randint(0, WINDOW_WIDTH // BLOCK_SIZE - 1) * BLOCK_SIZE y = random.randint(0, WINDOW_HEIGHT // BLOCK_SIZE - 1) * BLOCK_SIZE self.position = (x, y)
def draw(self): pygame.draw.rect(window, RED, (self.position[0], self.position[1], BLOCK_SIZE, BLOCK_SIZE))
snake = Snake()food = Food()
running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False 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()
if snake.positions[0] == food.position: snake.length += 1 food.spawn_food()
if (snake.positions[0][0] < 0 or snake.positions[0][0] >= WINDOW_WIDTH or snake.positions[0][1] < 0 or snake.positions[0][1] >= WINDOW_HEIGHT or snake.positions[0] in snake.positions[1:]): running = False
window.fill(BLACK) snake.draw() food.draw() pygame.display.update()
clock.tick(10)
pygame.quit()
代码说明
- 初始化: 设置游戏窗口大小、颜色、蛇的初始状态以及食物的生成。2. 游戏循环: 不断监听用户输入,控制蛇的移动,判断是否吃到食物,以及判断游戏是否结束。3. 蛇的移动: 根据用户输入的方向更新蛇头的位置,并判断是否撞到边界或自身。4. 食物: 随机生成食物的位置,并在蛇吃到食物后重新生成。5. 游戏结束: 当蛇撞到边界或自身时游戏结束。
总结
这只是一个简单的贪吃蛇游戏示例,你可以尝试添加更多功能,例如计分、难度等级、游戏音效等,让游戏更加有趣。
原文地址: https://www.cveoy.top/t/topic/bt0c 著作权归作者所有。请勿转载和采集!