绘制蛇身、处理蛇头移动、绘制随机位置的食物、吃到食物判定、碰撞检测代码
时间: 2023-10-05 15:11:43 浏览: 72
各种小游戏代码,贪吃蛇等等
好的,我理解您的问题。您在问一个有关贪吃蛇游戏的问题,它需要绘制蛇身,处理蛇头移动,绘制随机位置的食物,吃到食物的判定和碰撞检测代码。以下是一个可能的实现:
```python
import pygame
import random
# 设置游戏窗口和游戏区域的大小
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
BLOCK_SIZE = 10
GAME_AREA_WIDTH = WINDOW_WIDTH // BLOCK_SIZE
GAME_AREA_HEIGHT = WINDOW_HEIGHT // BLOCK_SIZE
# 定义游戏所需的颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
class Snake():
def __init__(self):
self.body = [(5, 10), (4, 10), (3, 10), (2, 10)]
self.direction = "right"
def move(self):
head = self.body[0]
if self.direction == "right":
new_head = (head[0] + 1, head[1])
elif self.direction == "left":
new_head = (head[0] - 1, head[1])
elif self.direction == "up":
new_head = (head[0], head[1] - 1)
elif self.direction == "down":
new_head = (head[0], head[1] + 1)
self.body = [new_head] + self.body[:-1]
def grow(self):
tail = self.body[-1]
if self.direction == "right":
new_tail = (tail[0] + 1, tail[1])
elif self.direction == "left":
new_tail = (tail[0] - 1, tail[1])
elif self.direction == "up":
new_tail = (tail[0], tail[1] - 1)
elif self.direction == "down":
new_tail = (tail[0], tail[1] + 1)
self.body.append(new_tail)
def change_direction(self, new_direction):
if new_direction == "right" and self.direction != "left":
self.direction = "right"
elif new_direction == "left" and self.direction != "right":
self.direction = "left"
elif new_direction == "up" and self.direction != "down":
self.direction = "up"
elif new_direction == "down" and self.direction != "up":
self.direction = "down"
def collide_with_wall(self):
head = self.body[0]
if head[0] < 0 or head[0] >= GAME_AREA_WIDTH or head[1] < 0 or head[1] >= GAME_AREA_HEIGHT:
return True
return False
def collide_with_body(self):
head = self.body[0]
for block in self.body[1:]:
if head == block:
return True
return False
def draw(self, screen):
for block in self.body:
x, y = block
pygame.draw.rect(screen, GREEN, (x*BLOCK_SIZE, y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
class Food():
def __init__(self):
self.position = self.place_food()
def place_food(self):
while True:
x = random.randint(0, GAME_AREA_WIDTH-1)
y = random.randint(0, GAME_AREA_HEIGHT-1)
if (x, y) not in snake.body:
return (x, y)
def draw(self, screen):
x, y = self.position
pygame.draw.rect(screen, RED, (x*BLOCK_SIZE, y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 初始化pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('贪吃蛇')
# 创建一个时钟对象
clock = pygame.time.Clock()
# 创建一个蛇对象和一个食物对象
snake = Snake()
food = Food()
# 游戏循环
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.body[0] == food.position:
snake.grow()
food.position = food.place_food()
# 如果蛇头碰到了墙或者自己的身体,游戏结束
if snake.collide_with_wall() or snake.collide_with_body():
pygame.quit()
quit()
# 绘制游戏区域
screen.fill(BLACK)
snake.draw(screen)
food.draw(screen)
pygame.display.update()
# 控制游戏的帧率
clock.tick(10)
```
这是一个简单的贪吃蛇游戏实现,其中包含了绘制蛇身、处理蛇头移动、绘制随机位置的食物、吃到食物判定和碰撞检测的代码。注意代码中使用了 Pygame 库来处理游戏窗口、游戏事件等。
阅读全文