Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyGame] pygame, help with making a function to detect collision between player and enemy.
Hi guys.

I am making a game in pygame, where you play as a little spaceship and your mission is to shoot aliens. I am struggling on making a function, when the spaceship hits one of the enemies, i want the game to be game over. I have made a collision function for the missile, when it hits the enemy, the enemy respawn and you get + 1 in score and it works fine, but i am struggling with this.

Hope you can understand my code, but ask if there is any questions. I have used a long time on the game so i hope someone can help me!
Best regards.


Here is my code:
Why aren't you using the pygame sprite class? Pygame has collision detection built in. It would be simple, like this for example.

hit = pygame.sprite.spritecollide(player, enemies, True)

if hit:
So, the answer is to use the Sprite class and sprite groups.
metulburr likes this post
I would also suggest a restructuring for a player class and an enemy class to help make things more organized. My tutorial series 1-9 actually uses a space shooter type game with similar properties. Regarding your issue, specifically this one.
michael1789 likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Problem with collision detection... michael1789 4 389 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Arcade Collision Problem randor 0 285 Oct-28-2019, 11:17 PM
Last Post: randor
  Multiple wall collision in pacman rustyjoe 4 855 Aug-11-2019, 08:08 AM
Last Post: rustyjoe
  use the print function in pygame rwahdan 5 439 Jul-07-2019, 04:52 PM
Last Post: metulburr
  [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 733 Jun-11-2019, 08:32 PM
Last Post: metulburr
  [PyGame] Making Player Sprite Ricochet of walls michael1789 4 618 Jun-03-2019, 10:45 PM
Last Post: Windspar
  drawing, moving, and collision problems (pygame) SheeppOSU 26 3,461 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU
  How to begin coding for game making [Install pygame on mac] mattkrebs 2 2,332 Apr-02-2017, 10:57 AM
Last Post: machrider

Forum Jump:

Users browsing this thread: 1 Guest(s)