gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:48:38 24 4
gpt4 key购买 nike

我正在努力做到这一点,所以如果你在我的 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 个矩形。
一个额外的 time.sleep(0.02)pygame.display.update() 之后添加稍微放慢游戏速度,以​​免在程序启动时 tron 角色全速奔跑。所以必须导入一个附加模块。因此添加 import time在代码的开头。

关于python - 实现一种让我的 tron 角色无法通过自己的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67082117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com