python贪吃蛇最简单代码
时间: 2023-06-19 08:03:20 浏览: 43
以下是一个简单的 Python 贪吃蛇游戏的示例代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口大小
window_width = 400
window_height = 400
window = pygame.display.set_mode((window_width, window_height))
# 设置游戏标题
pygame.display.set_caption('贪吃蛇')
# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 设置蛇的属性
snake_block_size = 10
snake_speed = 15
# 定义蛇的类
class Snake:
def __init__(self):
self.length = 1
self.positions = [(window_width/2, window_height/2)]
self.direction = random.choice([pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN])
def get_head_position(self):
return self.positions[0]
def turn(self, point):
if self.length > 1 and (point[0]*-1, point[1]*-1) == self.direction:
return
else:
self.direction = point
def move(self):
cur = self.get_head_position()
x, y = self.direction
new = ((cur[0]+(x*snake_block_size)), (cur[1]+(y*snake_block_size)))
if new in self.positions[2:]:
self.reset()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
def reset(self):
self.length = 1
self.positions = [(window_width/2, window_height/2)]
self.direction = random.choice([pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN])
def draw(self, surface):
for p in self.positions:
r = pygame.Rect((p[0], p[1]), (snake_block_size, snake_block_size))
pygame.draw.rect(surface, white, r)
pygame.draw.rect(surface, black, r, 1)
# 定义食物的类
class Food:
def __init__(self):
x = random.randrange(0, window_width, snake_block_size)
y = random.randrange(0, window_height, snake_block_size)
self.position = (x, y)
def draw(self, surface):
r = pygame.Rect((self.position[0], self.position[1]), (snake_block_size, snake_block_size))
pygame.draw.rect(surface, red, r)
# 创建蛇和食物对象
snake = Snake()
food = Food()
# 游戏循环
game_over = False
while 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_ESCAPE:
game_over = True
elif event.key == pygame.K_LEFT:
snake.turn((-1, 0))
elif event.key == pygame.K_RIGHT:
snake.turn((1, 0))
elif event.key == pygame.K_UP:
snake.turn((0, -1))
elif event.key == pygame.K_DOWN:
snake.turn((0, 1))
# 移动蛇
snake.move()
# 检测是否吃到食物
if snake.get_head_position() == food.position:
snake.length += 1
food = Food()
# 绘制游戏场景
window.fill(black)
snake.draw(window)
food.draw(window)
pygame.display.update()
# 控制游戏速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 退出 Pygame
pygame.quit()
```
这是一个相对简单的示例代码,你可以根据自己的需要进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)