我正在 pygame 中制作一个贪吃蛇游戏,但不知道如何在游戏边界上进行碰撞检查。我想对边界进行碰撞检查,这样当蛇撞到边界时我就可以结束游戏。我想知道做到这一点的最佳方法是什么。如果有帮助的话,这是我的代码。
import pygame
import sys
import random
pygame.init()
win_width, win_height = 900, 1000
game_width, game_height = 800, 800
GRID_SIZE = 40
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
win = pygame.display.set_mode((win_width, win_height), pygame.RESIZABLE)
class Snake:
def __init__(self):
self.length = 1
self.positions = [(game_width // 2 + 50, game_height // 2 + 50)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
self.color = (0, 255, 0)
def get_head_position(self):
return self.positions[0]
def update(self):
head = self.get_head_position()
x, y = self.direction
new = (((head[0] + (x * GRID_SIZE)) % game_width), (head[1] + (y * GRID_SIZE)) % game_height)
if len(self.positions) > 2 and 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 = [(game_width, game_height)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
game_over_sound = pygame.mixer.Sound("Assets/Sounds/game_over.ogg")
game_over_sound.play()
def render(self, surface):
for p in self.positions:
snake_rect = pygame.draw.rect(surface, self.color, (p[0], p[1], GRID_SIZE, GRID_SIZE))
snake_up = pygame.image.load("Assets/Images/snake_up.png")
snake_down = pygame.image.load("Assets/Images/snake_down.png")
snake_left = pygame.image.load("Assets/Images/snake_left.png")
snake_right = pygame.image.load("Assets/Images/snake_right.png")
if self.direction == UP:
surface.blit(snake_up, snake_rect)
if self.direction == DOWN:
surface.blit(snake_down, snake_rect)
if self.direction == LEFT:
surface.blit(snake_left, snake_rect)
if self.direction == RIGHT:
surface.blit(snake_right, snake_rect)
class Food:
pass
def main():
clock = pygame.time.Clock()
fps = 10
bg = pygame.image.load("Assets/Images/background.png")
snake = Snake()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and not snake.direction == DOWN:
snake.direction = UP
elif event.key == pygame.K_DOWN and not snake.direction == UP:
snake.direction = DOWN
elif event.key == pygame.K_LEFT and not snake.direction == RIGHT:
snake.direction = LEFT
elif event.key == pygame.K_RIGHT and not snake.direction == LEFT:
snake.direction = RIGHT
elif event.key == pygame.K_ESCAPE:
pygame.quit()
win.fill((255, 210, 190))
win.blit(bg, (50, 50))
pygame.draw.rect(win, (100, 100, 100), (30, 30, 840, 840), width=20)
snake.update()
snake.render(win)
pygame.display.update()
clock.tick(fps)
if __name__ == "__main__":
main()
我已经尝试过“pygame.Rect.colliderect(Rect)”但无法让它工作
以下是如何修改
Snake
类以实现与边框的碰撞检查:
class Snake:
# ... (previous code)
def update(self):
head = self.get_head_position()
x, y = self.direction
new = (((head[0] + (x * GRID_SIZE)) % game_width), (head[1] + (y * GRID_SIZE)) % game_height)
# Check if the snake's head position is outside the border
if new[0] < 30 or new[0] >= 870 or new[1] < 30 or new[1] >= 870:
self.reset()
else:
if len(self.positions) > 2 and new in self.positions[2:]:
self.reset()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
在修改后的代码中,我添加了条件来检查新的头部位置是否在边界之外,根据您为边界指定的尺寸定义为
30, 30, 870, 870
。如果头部超出这些坐标,则调用 reset()
方法来结束游戏。
现在,当蛇的头接触到边界时,它会按照你想要的方式重置游戏。