我正在制作一个类似tron的游戏,两个玩家使用玩家1的按键w,a,s,d控制他们的角色,而玩家2则使用上,下,左,右来控制他们的角色。在玩家移动时,用户进行追踪,并且如果对方玩家触摸玩家的踪迹,则表示他们“不在”。
游戏代码:
from steel2D import *
window = Window(600, 450, "Steel 2D Tutorial")
running = True
char1 = Rect(window, 0, 0, 5, 5, (255, 0, 0))
char2 = Rect(window, 595, 445, 5, 5, (0, 0, 255))
trail1 = []
trail2 = []
red = 0
blue = 0
while running:
sleep(2)
keys = OnKeyInput()
if keys[pygame.K_w]:
char1.y -= 2.2
trail1.append(char1)
if keys[pygame.K_a]:
char1.x -= 2.2
trail1.append(char1)
if keys[pygame.K_s]:
char1.y += 2.2
trail1.append(char1)
if keys[pygame.K_d]:
char1.x += 2.2
trail1.append(char1)
if keys[pygame.K_UP]:
char2.y -= 2.2
trail2.append(char2)
if keys[pygame.K_LEFT]:
char2.x -= 2.2
trail2.append(char2)
if keys[pygame.K_DOWN]:
char2.y += 2.2
trail2.append(char2)
if keys[pygame.K_RIGHT]:
char2.x += 2.2
trail2.append(char2)
if char1.x >= 595:
char1.x = 595
if char1.x <= 0:
char1.x = 0
if char1.y >= 445:
char1.y = 445
if char1.y <= 0:
char1.y = 0
if char2.x >= 595:
char2.x = 595
if char2.x <= 0:
char2.x = 0
if char2.y >= 445:
char2.y = 445
if char2.y <= 0:
char2.y = 0
char1.draw()
char2.draw()
Update()
for t1, t2 in zip(trail1, trail2):
if OnRectCollison(t1, t2):
exit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
在每次按键时,用户跟踪都会添加到列表中,然后循环遍历以检查是否存在冲突。现在看来,只有在玩家的第一个足迹碰到时,碰撞才会发生。
steel2D(我的游戏引擎)的代码:
import pygame
from threading import Thread
pygame.init()
def sleep(ms):
pygame.time.delay(ms)
def Window(w, h, title="steel2D"):
window = pygame.display.set_mode((w, h))
pygame.display.set_caption(title)
return window
def Update():
pygame.display.update()
def OnKeyInput():
return pygame.key.get_pressed()
class Rect:
def __init__(self, window, x, y, w, h, color=(0, 0, 0)):
self.window = window
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
def draw(self):
pygame.draw.rect(self.window, self.color, (self.x, self.y, self.w, self.h))
class Circle:
def __init__(self, window, x, y, r, color=(0, 0, 0)):
self.window = window
self.x = x
self.y = y
self.r = r
self.color = color
def draw(self):
pygame.draw.circle(self.window, self.color, (self.x, self.y), self.r, self.r)
def Line(window, color, sx, sy, ex, ey, width=2):
pygame.draw.line(window, color, (sx, sy), (ex, ey), width)
def OnRectCollison(rect1, rect2):
if rect1.x < rect2.x + rect2.w and rect1.x + rect1.w > rect2.x and rect1.y < rect2.y + rect2.h and rect1.y + rect1.h > rect2.y:
return True
else:
return False
任何帮助将不胜感激。
答案 0 :(得分:1)
有几个问题:
trail1
和trail2
中的每个矩形,则 zip
是错误的功能。您只会在相同的索引处获得成对的rect,并且还会删除较长列表中的元素。看一下这个例子:
>>> trail1 = [1, 2, 3]
>>> trail2 = ['a', 'b', 'c', 'd', 'e']
>>> list(zip(trail1, trail2))
[(1, 'a'), (2, 'b'), (3, 'c')]
只需遍历路径列表中的矩形并检查char1
或char2
是否与它们冲突。
# I skip the last few rects by slicing the list, otherwise they would collide immediately.
for trail in trail1[:-5]:
if OnRectCollison(char1, trail):
print('collision')
for trail in trail2:
if OnRectCollison(char1, trail):
print('collision')
在移动时,会将相同的Rect
对象(char1
,char2
)添加到跟踪列表,因此列表仅包含对头部的引用,因此仅步道的头部能够碰撞。
trail1.append(char1)
添加(浅)副本:
trail1.append(copy.copy(char1))
侧面说明:碰撞的拼写错误。