微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决实现一种让我的 tron 角色无法通过自己的方法

我正在努力做到这一点,如果你进入我的 tron 游戏,你会死,就像在经典的街机游戏中一样。我已经实现了它,所以如果你碰到其他玩家就会死,但我似乎无法弄清楚如何做到这一点,所以如果你击中自己,你也会输。我将如何实现此功能以使游戏更像经典的 tron?

import pygame
import sys
pygame.init()

black = 0,0 #Create black for background
blue = 0,225 #Create blue for player 1
red = 255,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,5,1,blue,tron_path_1)  # creates p1
p2 = Player(screen,500,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 个矩形。 在 time.sleep(0.02) 之后添加了额外的 pygame.display.update() 以稍微减慢游戏速度,以​​便 tron 角色在程序启动时不会全速冲刺。所以必须导入一个附加模块。因此,在代码开头添加 import time

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。