I am making a snake game in pygame for my programming class, and I got most of it working. I have two problems however. The first problem is with the detection of the snake colliding with itself. I will provide more spesific info later. The other problem is the snake continously growing larger. When the snake moves forwards, it just keeps on generating out of the point of origin.
Full code:
import pygame, random, time
snake_speed = 5
pygame.init()
background_colour = (0, 0, 0)
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption('Snake')
screen.fill(background_colour)
fps = pygame.time.Clock()
fruit_pos = [random.randrange(1, (570//10)) * 10,
random.randrange(1, (570//10)) * 10]
snake_position = [120, 300]
snake_body = [[120, 300],
[90, 300],
[60, 300],
[30, 300]
]
direction = 'right'
change_to = direction
score = 0
full = 50
def game_win():
my_font = pygame.font.SysFont('times new roman', 50)
game_win_surface = my_font.render('You Won!', True, (0, 255, 0))
game_win_rect = game_win_surface.get_rect()
game_win_rect.midtop = (600/2, 600/2)
screen.blit(game_win_surface, game_win_rect)
def game_over():
my_font = pygame.font.SysFont('times new roman', 50)
game_over_surface = my_font.render('You lost!', True, (255, 0, 0))
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (600/2, 600/2)
screen.blit(game_over_surface, game_over_rect)
pygame.display.flip()
time.sleep(3)
pygame.quit()
quit()
fruit_spawn = True
while True:
if score == full:
game_win()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
if event.key == pygame.K_LEFT:
change_to = 'LEFT'
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
if direction == 'UP':
snake_position[1] -= 30
if direction == 'DOWN':
snake_position[1] += 30
if direction == 'LEFT':
snake_position[0] -= 30
if direction == 'RIGHT':
snake_position[0] += 30
snake_body.insert(0, list(snake_position))
if snake_position[0] == fruit_pos[0] and snake_position[1] == fruit_pos[1]:
score += 10
fruit_spawn = False
else:
snake_body.pop()
if not fruit_spawn:
fruit_pos = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10]
fruit_spawn = True
screen.fill((0, 0, 0))
for pos in snake_body:
pygame.draw.rect(screen, (0, 255, 0),
pygame.Rect(pos[0], pos[1], 30, 30))
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(fruit_pos[0], fruit_pos[1], 30, 30))
if snake_position[0] < 0 or snake_position[0] > 600:
game_over()
if snake_position[1] < 0 or snake_position[1] > 600:
game_over()
# for block in snake_body[1:]:
# if snake_position[0] == block[0] and snake_position[1] == block[1]:
# game_over()
pygame.display.update()
fps.tick(snake_speed)
The part of the code that i have turned into a comment is supposed to be detecting if the snake collides with itself, but when I try to start the game, it immideately thinks it is colliding with itself.
for block in snake_body[1:]:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over()