我被困住了,我正在使用 Python 和 Pygame 制作游戏项目。(屏幕为 770 x 485)它涉及在随机生成的矩形下拉时将一个圆圈向左或向右移动通过 5 个垂直车道。我有一个 Wall 类来随机生成它们,但我不知道如何检测矩形的每个实例和我的圆之间的碰撞。
就在带有 z 和 c 列表的位中,我尝试尝试使它们发生碰撞,但它只与第一个矩形发生碰撞,并且它没有检测到任何其他下降的矩形(它们是在彼此之间 100 个滴答声之后生成的) 谁能指出我正确的方向?我真的不确定 z 变量、c 列表和 v 变量:\
提前致谢!
class Wall:
def __init__(self,colour,x,y,life):
self.colour = colour
self.x = x
self.y = y
self.life = life
def drop(self):
pygame.draw.rect(screen, self.colour, (self.x, self.y, 154, 30))
self.y += 1.25
self.life -= 1
if self.life < 0:
wall.remove(self)
def playMode(): #assume everything is indented properly
global wall, points
check = True
left = False
right = False
circlex = 385
degrees = 0
health = 3
wall = []
x = 2
points = 0
n = 0
c = []
starttime = time.time()
while True:
runningtime = time.clock()
screen.fill((255,255,255))
x -= 1
if x == 1:
wall.append(Wall((random.randint(1,255),random.randint(1,255),random.randint(1,255)),
random.choice([0, 154, 308, 462, 616]), -30 , 450))
x = 100
for i in wall:
i.drop()
z = wall[-1]
c.append(z)
v = c[(n)]
if 445 >= v.y >= 340 and v.x == (circlex - 77):
health -= 1
points -= 5
v.y = 485 #moves this instance offscreen so it doesn't make the hp go to 0 immediately
n += 1
print v.y
circle = pygame.draw.circle(screen, colour,(circlex, 409), 50)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
showPauseScreen()
elif event.key == K_p:
showPauseScreen()
elif event.key == K_LEFT:
if circlex > 77:
circlex = circlex - 154
elif event.key == K_RIGHT:
if circlex < 616:
circlex = circlex + 154
elif event.type == QUIT:
terminate()
if health == 0:
check = False
return #goes to gameover screen basically
pygame.display.update()
clock.tick(100)