0

Basically I'm very new to Python and game making, but I've coded a lot before. I'm making a simple game just to have a play around basically where a ball runs into some other balls and they delete as it hits them.

At the moment it works okay, but sometimes it doesn't register the collision on some of the balls. Here's my code for the collision and the redraw

 pygame.draw.rect(screen,(0,0,0), (50,50,edge_x,edge_y))
    screen.blit(ball,(x,y))
    for food in foods:
            if food.x  < x+35 and food.x > x and food.y  < y +35 and food.y > y or food.x  < x-35 and food.x > x and food.y  < y -35 and food.y > y:
                    foods.remove(food)
                    Score = Score + 1
                    break
    for food in foods:
            screen.blit(foodImage, food)
            screen.blit(label, (100, 10))

I thought the reason it might not be detecting it, is because of my collision isn't in sync with where the for loop is in the list of sprites, but I could be wrong.

Any ideas?

Rhys Drury
  • 305
  • 2
  • 19
  • It's not entirely clear from your code: what are the dimensions of your balls? Are they 35 pixels in each direction? – Blckknght Mar 01 '14 at 21:10
  • 1
    Pygame has it's own collision detection using the Rect class that you'd probably be better off using. Also for an intro in making games with Pygame I'd recommend [this book](http://inventwithpython.com/chapters/) (a game with Pygame later in the book), followed by [this book](http://inventwithpython.com/chapters/) (details how to make several games using Pygame), both of which are free to read online. The first book only makes one graphics based game, but rather close to what you are trying to do. – Nuclearman Mar 01 '14 at 21:20
  • I actually managed to fix it using if food.x < x+35 and food.x > x+10 what I'd done wrong was i didn't allow the collision to work from every direction, therefore some weren't triggering simply because i'd not told it to work that way. Really silly. But thanks for that book, I'll have a gander. – Rhys Drury Mar 02 '14 at 00:35

0 Answers0