Implement a way for my tron characters to not be able to go through themselves

Viewed 39

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()
1 Answers

You can add the colliding condition of p1 in tron_path_1 but that would immediately end the game because the rectangle would collide with itself. So something you can do is add this with the other two loops

for rect in tron_path_1[:-10]:
    if p1.player_rect.colliderect(rect):
        print("Player 2 Wins!")
        sys.exit()

for rect in tron_path_2[:-10]:
    if p2.player_rect.colliderect(rect):
        print("Player 1 Wins!")
        sys.exit()

What this does is that it checks the same colliding condition as in your original loops but doesnt consider the last 10 rectangles of the path. An additional time.sleep(0.02) is added after pygame.display.update() to slow the game a bit, so that the tron characters do not go rushing with full speed as the program starts. So an addition module has to be imported. Hence add an import time at the start of your code.

Related