Python 实现一种方法,使我的tron角色无法通过自己

Python 实现一种方法,使我的tron角色无法通过自己,python,pygame,Python,Pygame,我试图让它,如果你进入自己在我的tron游戏,你会死,就像在经典的街机游戏。我已经实施了,所以如果你碰了另一个玩家你就死了,但我似乎不知道怎么做,如果你打自己,你也会输。我如何实现这个功能,使游戏更像经典的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 #Cre

我试图让它,如果你进入自己在我的tron游戏,你会死,就像在经典的街机游戏。我已经实施了,所以如果你碰了另一个玩家你就死了,但我似乎不知道怎么做,如果你打自己,你也会输。我如何实现这个功能,使游戏更像经典的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()

您可以在tron_path_1中添加p1的碰撞条件,但这会立即结束游戏,因为矩形会与自身碰撞。所以你可以把它和另外两个循环相加

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()
<>这是因为它检查了与原来循环相同的碰撞条件,但是没有考虑路径的最后10个矩形。
pygame.display.update()
之后添加一个额外的
time.sleep(0.02)
,以使游戏稍微慢一点,这样当程序启动时,tron角色就不会全速奔跑。因此,必须导入一个加法模块。因此,在代码的开头添加一个导入时间。

您是否考虑过将身体占据的位置存储在一个双端队列
self.components=collections.deque()
和一个集合
self.body=set()
中。这将允许您在O(1)时间内通过从三角帆的尾部弹出并附加到三角帆的头部来更新您的位置。然后,通过检查当前位置是否在身体内,可以检查当前位置是否在O(1)时间内与身体重叠。记住每次移动时都要更新身体。我不明白你的问题是什么-如果你知道如何检查碰撞
p1
p2
,那么你应该对
p1
做同样的检查,对tron\u路径中的rect做同样的
p1
-
:如果p1.player\u rect.collide rect(rect):
这样做会导致角色与自身发生碰撞,因为它在循环的一次迭代中移动了1个单位,但hitbox的长度是5个单位,在旋转过程中,它可能与更多的长方体发生碰撞,因此我们需要忽略其路径中的最后10个矩形