Python Forum
[PyGame] pygame, help with making a function to detect collision between player and enemy.
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.
#1
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.

Kris.

Here is my code:

https://pastebin.com/5V3d1hTc
Reply
#2
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:
   game_over()
So, the answer is to use the Sprite class and sprite groups.
Reply
#3
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.
Recommended Tutorials:
Reply
#4
Hey again. Sorry for the long answer. I have been a little busy the last couple of weeks.
Thanks for the answer, i would try and read about classes in my Python book and look into it after.
:D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 601 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  [PyGame] platformer enemy animation Yegor123 3 1,960 May-03-2023, 08:42 AM
Last Post: Windspar
  player just randomly teleporting to the edge of a platform in pygame BliepMonster 5 2,240 Jan-26-2023, 10:12 PM
Last Post: deanhystad
  can't get collision detection to work in platform game chairmanme0wme0w 10 3,663 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  How to make enemy chase me in pygame? Jan_97 3 4,271 Nov-23-2021, 05:08 PM
Last Post: Jan_97
  [PyGame] Collision in not happening onizuka 3 3,359 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,596 Aug-18-2020, 01:29 PM
Last Post: onizuka
  Problem with collision detection... michael1789 4 3,219 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Arcade Collision Problem randor 0 2,656 Oct-28-2019, 11:17 PM
Last Post: randor
  Multiple wall collision in pacman rustyjoe 4 4,029 Aug-11-2019, 08:08 AM
Last Post: rustyjoe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020