用Python写一个贪吃蛇游戏代码
时间: 2024-01-29 13:02:39 浏览: 76
好的,以下是用Python编写贪吃蛇游戏的代码:
```
import pygame
import random
# 初始化游戏
pygame.init()
# 定义常量
WIDTH = 640
HEIGHT = 480
BLOCK_SIZE = 20
# 设置窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.x = WIDTH//2
self.y = HEIGHT//2
self._body = [(self.x, self.y), (self.x-BLOCK_SIZE, self.y), (self.x-2*BLOCK_SIZE, self.y)]
self._direction = 'right'
def get_head(self):
return self._body[0]
def get_body(self):
return self._body
def move(self):
x, y = self.get_head()
if self._direction == 'right':
self._body.insert(0, (x+BLOCK_SIZE, y))
elif self._direction == 'left':
self._body.insert(0, (x-BLOCK_SIZE, y))
elif self._direction == 'up':
self._body.insert(0, (x, y-BLOCK_SIZE))
elif self._direction == 'down':
self._body.insert(0, (x, y+BLOCK_SIZE))
self._body.pop()
def change_direction(self, direction):
if direction == 'right' and self._direction != 'left':
self._direction = 'right'
elif direction == 'left' and self._direction != 'right':
self._direction = 'left'
elif direction == 'up' and self._direction != 'down':
self._direction = 'up'
elif direction == 'down' and self._direction != 'up':
self._direction = 'down'
def eat_food(self):
x, y = self.get_head()
if self._direction == 'right':
self._body.insert(0, (x+BLOCK_SIZE, y))
elif self._direction == 'left':
self._body.insert(0, (x-BLOCK_SIZE, y))
elif self._direction == 'up':
self._body.insert(0, (x, y-BLOCK_SIZE))
elif self._direction == 'down':
self._body.insert(0, (x, y+BLOCK_SIZE))
# 定义食物类
class Food:
def __init__(self):
self.x = random.randint(BLOCK_SIZE, WIDTH-BLOCK_SIZE)
self.y = random.randint(BLOCK_SIZE, HEIGHT-BLOCK_SIZE)
self.color = GREEN
def get_pos(self):
return (self.x, self.y)
def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, BLOCK_SIZE, BLOCK_SIZE))
# 创建贪吃蛇和食物
snake = Snake()
food = Food()
# 设置游戏时钟
clock = pygame.time.Clock()
# 运行游戏
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
snake.change_direction('right')
elif event.key == pygame.K_LEFT:
snake.change_direction('left')
elif event.key == pygame.K_UP:
snake.change_direction('up')
elif event.key == pygame.K_DOWN:
snake.change_direction('down')
# 移动贪吃蛇
snake.move()
# 判断是否吃到食物
if snake.get_head() == food.get_pos():
snake.eat_food()
food = Food()
# 检测碰撞
if snake.get_head()[0] < 0 or snake.get_head()[0] > WIDTH-BLOCK_SIZE or snake.get_head()[1] < 0 or snake.get_head()[1] > HEIGHT-BLOCK_SIZE:
pygame.quit()
quit()
for block in snake.get_body()[1:]:
if snake.get_head() == block:
pygame.quit()
quit()
# 绘制屏幕
screen.fill(BLACK)
snake_body = snake.get_body()
for block in snake_body:
pygame.draw.rect(screen, WHITE, (block[0], block[1], BLOCK_SIZE, BLOCK_SIZE))
food.draw()
pygame.display.update()
# 设置游戏帧率
clock.tick(10)
```
以上就是用Python编写贪吃蛇游戏的代码,希望能对你有所帮助。
阅读全文