我非常困难,我正在使用Python和Pygame制作一个游戏项目。 (屏幕是770 x 485) 它涉及通过5个垂直通道向左或向右移动一个圆圈,随机生成的矩形下拉。我有一个Wall类用于随机生成它们,但我不知道如何检测矩形的EACH实例和我的圆之间的碰撞。
在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)
答案 0 :(得分:1)
你可以看一下pygame文档,但基本上你想要使用这个函数,它会返回一个bool,看看一个sprite是否与另一个sprite碰撞:
pygame.sprite.spritecollide(<sprite>,<group>,False) #in almost all circumstances the third argument should be False. This will check for collision between a sprite and group.
有关更多信息,请查看pygame文档:
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide