0

I am incredibly stuck, I'm using Python and Pygame to make a game project. (screen is 770 x 485) It involves moving a circle left or right through 5 vertical lanes as randomly generated rectangles drop down. I have a Wall class for generating them randomly but I do not know how to detect collision between EACH instance of the rectangle and my circle.

Right in the bit with z and the c list is where I ATTEMPT to try and make them collide but it only collides with the first rectangle and it does not detect any other falling rectangles (which are generated after a period of 100 ticks between each other) Can anyone point me in the right direction? I'm really not sure about the z variable, c list and v variable :\

Thanks in advance!

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)
4
  • 1
    Please format your code according to Pythonic conventions before we can help you. Commented Apr 29, 2013 at 7:20
  • 1
    we cannot help you, if you do not indent correctly, from a first glance though, you are onlu checking the collision on one object - z[-1]. Commented Apr 29, 2013 at 8:42
  • 1
    Pygame has built in collision detection (for rects). Have a look at this question for an idea of how to use it: stackoverflow.com/questions/16227616/… Commented Apr 29, 2013 at 10:59
  • 1
    Pygame also has built in sprite Group classes. They make it easy to detect collision with any sprite in the group. Commented Apr 29, 2013 at 18:21

1 Answer 1

1

You could just look at pygame documentation, but basically you want to use this function, which will return a bool of whether or not one sprite is colliding with another:

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.

For more information, check the pygame docs:

http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.