I am trying to recreate a game commonly known as Tron. The goal of the game is for the player to stay alive as long as possible. The player is constantly moving and can change direction only in 90-degree turns, if the player steps on a place which they have already stood on before, they die and the game resets. I have an issue with the lists that store the coordinates of the places the player has already stood on. When the player moves for the first time it instantly registers a loss and resets the game. Please take a look at my code below, and tell me the issue, especially with the final 'if block' at the end of the game loop.
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
player_y = 300
player_x = 300
player_x_list = [300]
player_y_list = [300]
player_rect = pygame.Rect(0, 0, 25, 25)
player_direction_y = 0
player_direction_x = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s and player_direction_y == 0:
player_direction_y = 25
player_direction_x = 0
if event.key == pygame.K_w and player_direction_y == 0:
player_direction_y = -25
player_direction_x = 0
if event.key == pygame.K_d and player_direction_x == 0:
player_direction_x = 25
player_direction_y = 0
if event.key == pygame.K_a and player_direction_x == 0:
player_direction_x = -25
player_direction_y = 0
player_y += player_direction_y
player_x += player_direction_x
# This is supposed to record every x coord the player was located in on a list,
# and every y coord a player was located in on a list
# I only record the coord once as seen below
if player_x_list.count(player_x) == 0:
player_x_list.append(player_x)
if player_y_list.count(player_y) == 0:
player_y_list.append(player_y)
player_rect.center = player_x, player_y
# This right here is supposed to reset the game when the player steps on
# coords that were already stepped on previosly.
# If you remove this, the player is able to move freely, so the issue is here
if player_x_list.count(player_x) > 0 and player_y_list.count(player_y) > 0:
player_x = 300
player_y = 300
player_direction_x = 0
player_direction_y = 0
player_x_list.clear()
player_y_list.clear()
screen.fill((pygame.Color('black')))
pygame.draw.rect(screen, (pygame.Color('white')), player_rect)
pygame.display.update()
clock.tick(10)
I appreciate your time sincerely.