1

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()
Navegante
  • 25
  • 1
  • 4

1 Answers1

1

When you do tron_path_1.append(p1.player_rect) then you don't create a copy of p1.player_rect. p1.player_rect is a variable that reference a pygame.Rect object.
After tron_path_1.append(p1.player_rect), the variable p1.player_rect and the list element tron_path_1[0] refer to the same object.
Hence, when you change p1.player_rect, then the elements of the list seems to change, too, because there is just 1 pygame.Rect object.

Use .copy() to create a copy of the rectangle:

class Player():
    # [...]

    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) <--- CHANGE TO
        self.tron_path.append(self.player_rect.copy())
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Hi, thank you for your response! I actually edited my code recently. The previous version had the player rectangles appended to their respective lists ONLY when I pressed one of the keydown events. I changed it so that that the player rects were constantly being appended wherever they went. However, I'm still running into the same problem again. – Navegante Apr 13 '20 at 19:43
  • ooh I apologize. I realized my mistake.. I put the .copy() in line 25 as self.tron_path.append(self.player_rect.copy()) and it is working now. Thank you, this is exactly what I was looking for!! – Navegante Apr 13 '20 at 19:53