I am new to Python and programming in general, so I've decided to make a basic clone of Tron with 2 players. To clarify, Tron is a game where players are riding a bike that creates a path/trail behind them. If a player crashes into the path/trail of another player, they lose. I am trying to detect this type of collision in my code by utilizing rectangles in pygame.
I have a general idea of how I want to implement this idea: Every time a player moves, they make continuous rectangles behind them and those rectangles get appended to a list called tron_path_1 or tron_path_2. So if player 1's rect collides with a rect in the list of the opposing player(tron_path_2), I want to detect that collision. However in my code, I can only detect a collision if the two heads of each player collide with each other. So my question is: How can I detect the collision of a player's rect with the previous rects made by the opposing player?
import pygame
import sys
class Player():
def __init__(self, screen, x, y, w, h, dx, dy, tron_path, color):
"""Create player's tron bike."""
self.screen = screen
self.screen_rect = screen.get_rect()
self.x = x
self.y = y
self.w = w #width
self.h = h #height
self.dx = dx
self.dy = dy
self.tron_path = tron_path
self.color = color
self.player_rect = pygame.Rect(self.x, self.y, self.w, self.h)
def update_position(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)
def draw_player(self):
pygame.draw.rect(self.screen, self.color, self.player_rect)
# Initialize pygame settings.
pygame.init()
screen = pygame.display.set_mode((1200,700))
pygame.display.set_caption("Tron")
# Player settings
p1_color = ((219,62,177)) #pink
p2_color = ((255,255,255)) #white
players = []
tron_path_1 = [] # List to keep track of rectangles for each player
tron_path_2 = []
p1 = Player(screen, x=200, y=500, w=5, h=5, dx=5, dy=0, tron_path=tron_path_1, color=p1_color) #player 1
p2 = Player(screen, x=1000, y=500, w=5, h=5, dx=-5, dy=0, tron_path=tron_path_2, color=p2_color) #player 2
players.append(p1)
players.append(p2)
# Initialize
while True:
clockobject = pygame.time.Clock()
clockobject.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
# Player 1 controls
if event.key == pygame.K_w:
p1.dx = 0
p1.dy = -5
elif event.key == pygame.K_s:
p1.dx = 0
p1.dy = 5
elif event.key == pygame.K_d:
p1.dx = 5
p1.dy = 0
elif event.key == pygame.K_a:
p1.dx = -5
p1.dy = 0
# Player 2 controls
if event.key == pygame.K_UP:
p2.dx = 0
p2.dy = -5
elif event.key == pygame.K_DOWN:
p2.dx = 0
p2.dy = 5
elif event.key == pygame.K_RIGHT:
p2.dx = 5
p2.dy = 0
elif event.key == pygame.K_LEFT:
p2.dx = -5
p2.dy = 0
p1.update_position()
p2.update_position()
p1.draw_player()
p2.draw_player()
# 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("collision")
for rect in tron_path_2:
if p1.player_rect.colliderect(rect):
print("collision")
pygame.display.flip()