I am trying to make it so if you go into yourself in my tron game, you'll die, like in the classic arcade game. I have implemented it so if you touch the other player you die, but I cannot seem to figure out how to make it so if you were to hit yourself, you would also lose. How would I implement this feature to make the game more like classic tron?
import pygame
import sys
pygame.init()
black = 0,0,0 #Create black for background
blue = 0,0,225 #Create blue for player 1
red = 255,0,0 #Create red for player 2
class Player:
def __init__(self, screen, x, y, w, h, dx, dy, color, tron_path):
self.x = x #X coord
self.y = y #Y coord
self.w = w
self.h = h
self.dx = dx
self.dy = dy
self.color = color #color
self.screen = screen
self.tron_path = tron_path
self.player_rect = pygame.Rect(self.x, self.y, self.w, self.h)
def move(self):
self.player_rect[0] += self.dx #changes rect's x-coordinate
self.player_rect[1] += self.dy #changes rect's y-coordinate
#self.tron_path.append(self.player_rect)
self.tron_path.append(self.player_rect.copy())
def draw(self):
pygame.draw.rect(self.screen, self.color, self.player_rect)
screen = pygame.display.set_mode((1000, 750)) # creates window
pygame.display.set_caption("Tron") # sets window title
clock = pygame.time.Clock()
players = []
tron_path_1 = []
tron_path_2 = []
p1 = Player(screen, 100, 100, 5, 5, 1, 0, blue, tron_path_1) # creates p1
p2 = Player(screen, 500, 500, 5, 5, 1, 0, red, tron_path_2) #create p2
screen.fill(black)
players.append(p1)
players.append(p2)
done = False
while not done:
for event in pygame.event.get(): # gets all event in last tick
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
# === Player 1 === #
if event.key == pygame.K_w:
p1.dx = 0
p1.dy = -1
elif event.key == pygame.K_s:
p1.dx = 0
p1.dy = 1
elif event.key == pygame.K_a:
p1.dx = -1
p1.dy = 0
elif event.key == pygame.K_d:
p1.dx = 1
p1.dy = 0
# === Player 2 === #
if event.key == pygame.K_UP:
p2.dx = 0
p2.dy = -1
elif event.key == pygame.K_DOWN:
p2.dx = 0
p2.dy = 1
elif event.key == pygame.K_LEFT:
p2.dx = -1
p2.dy = 0
elif event.key == pygame.K_RIGHT:
p2.dx = 1
p2.dy = 0
p1.draw()
p1.move()
p2.draw()
p2.move()
# Trying to detect collision with one player and another player's path
for rect in tron_path_1:
if p2.player_rect.colliderect(rect):
print("Player 1 Wins!")
sys.exit()
for rect in tron_path_2:
if p1.player_rect.colliderect(rect):
print("Player 2 Wins!")
sys.exit()
pygame.display.update()
pygame.quit()