Pygame Collision Detection - Pygame RPG Tutorial #5

preview_player
Показать описание
In this video, I show you how to do Pygame Collision Detection! This pygame collision detection tutorial teaches you how to do pygame collisions, including a pygame collision example. Pygame sprite collisions use special pygame collision functions, making it a lot easier to do pygame hitbox collision.

This is the 5th video in my Pygame RPG tutorial series, and if you found it useful, be sure to leave a like!

Thanks for watching!
Рекомендации по теме
Комментарии
Автор

I also want to say - The quality of your code far exceeds what I'm seeing from other youtubers who are making tutorials. If you keep this up (and it's been half a year since you posted this, so far you have), you'll exceed them.

wizard-warbler
Автор

it is nice to have something like this on Mondays for me at work as i learn python.

alicemystery
Автор

Edit: I figured it out! Expand comment!

I'm at the very last thing in the video, and when I try to go right or down, I fling off the screen in the opposite direction. I am not even sure which part of the code is causing this bug, but I saw a couple other people have this issue as well.

Edit: A-ha! I found it. This actually led me on a journey to understand the program a little better.

So if you are lazy like me, you realized the Block and Ground classes are virtually similar and did a copy and paste job. My issue was that in the Ground class, I had the following:

| class Ground(pygame.sprite.Sprite):
| ...
| self.groups = self.game.all_sprites, self.game.blocks

You have to remove self.game.blocks else you will be colliding with the ground.

TheLumbersnack
Автор

Nice collision detection method. Keep up the good work.😀

rcrist
Автор

Shaw u are inspiration to me . Sometimes u demotivate me when I see the age difference 😂 . keep it up ❤️🙏

muhammadsaadkhan
Автор

Greetings from Russia. A very good tutorial on making games in python. I'm waiting for the next episode.

coldmouseart
Автор

Thank you Shaw, this was the point I gave up a month ago, as I couldn't figure this My game is how working, thanks 😀

kyoujinko
Автор

man i am so ready to know this and write my own code in my way smh
, you great little buddy, thank you so much for the tutorials

antwarior
Автор

thank you very much my friend. This is exactly what I needed. Well done!

apexlegends
Автор

hi there! thanks for the tutorial! I tried to implement a simplified version of this and for some reason the game always reads "hits" as being true. so in this case the console keeps printing "bottom bump" forever as the player falls down due to gravity.

def collision_test(self, ground_group):
if abs(self.vel.x) > 0:
hits = pygame.sprite.spritecollide(self, ground_group, False)
if hits:
if self.vel.x > 0:
print("right bump")
pass
if self.vel.x < 0:
print("left bump")
pass
if abs(self.vel.y) > 0:
hits = pygame.sprite.spritecollide(self, ground_group, False)
if hits:
if self.vel.y > 0:
print("bottom bump")
pass
if self.vel.y < 0:
print("top bump")
pass

update: seems like it's maybe an issue with the rect for my ground sprite. if I position it where it's supposed to be ("topleft"), then there are never any collisions, whereas if I leave it at the default (0, 0) it constantly reads collisions:

class Ground(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image =
self.rect = self.image.get_rect(topleft=(0, 325))

update 2: it appears I had not included a line to update player.rect.position so it just kept displaying whatever was happening at the first frame when the player was at the top left of the screen. hopefully someone can learn from my mistakes!

el_mallem
Автор

This is exactly what I needed :p Thanks for the help man

Btw, how do you make the enemy follow you in PyGame? I’m trying to make a simple Maze RPG Game

dantom
Автор

Problem: "Hits" is always True and player acts crazy moving. It is because the rect inside "Hits[0]" are not from Block. They come from Ground and more than one.

I spent few days to solve this issue.

Issue is that the group of block sprites contains about 374 sprites including the sprites of ground and player. Blocks group should have 74 sprites only. More precisely, self.game.blocks built is the same as self.game.all_sprites in my coded program. It was not created as expected and I don't know why even.... compared my coding with the youtube many many times and found no difference. It could be environment problems: PC, software version ....etc.

Root causes: Rect in Hits[0] are from ground and more than one.

Direction to solve: Build the self.game.blocks in different way. (Please note: I used different variable names and def names in my coding. It is not exactly the same as yours.)

1. Locate 'def createTilemap(self)' inside class Game. Add this line below it:



2. Locate this few lines below 'def createTilemap(self)'.

Block(self, ix, iy)

(j and i in your case.)

3. Replace it with

self.blocksgroup.add(Block(self, ix, iy)

4. Add this line right before return of 'def create Tilemap(self)':

self.blocks=self.blocksgroup

It works now!

I need to thanks ShawCode very much to introduce this. I learnt a lot from him.
David

davidlam
Автор

You gained me as a Subscriber today! Your awesome man and simple to understand. Are you still active>?

GeikoTreyarch
Автор

If i want to be slowed by PLAYER_SPEED/2 instead of being blocked to move when i hit a block, what should I do?

naturallyaspiratedv
Автор

Hi shawcode! Your collision works well for me, apart from when I went to use a different rect to self.rect. I want to use a seperate rect called self.hitbox to collide with theplayer so I can make it as small or as big as I want. But when I do this and make it so it collides with hitbox instead it bugs out, even if the hitbox rect is an identical copy of the self.rect. Th eplayer flips to the other side of the hitbox instead of moving away when trying to walk away from it like it gets stuck. Any idea How to fix this I have been truggling for about a week now

WilliamReddingGuitar
Автор

Heya shaw.. can i ask did you learn all this from a BootCamp? and how many years have you been programming? I'm really amaze by what you do.

francisvincejaca
Автор

@ShawCode is there anyway you can include the A* Pathfinding algorithm in these tutorials to make the enemy follow the player through walls? I’m struggling with it Bc the pathfinding tutorials I find on YouTube (aka Tech with Tim) is too advanced for me. I’m only a beginner lol

dantom
Автор

Hi,
I have doubt example, if we have a coin and when a sprite collides with it then it should disappear and there should be a record of how many coins you have collected lifetime.
How to do this?
( Any help is ok)
( Nice Vid BTW Subbed :D )
Thanks

shouryacb
Автор

Bro tell me which python version to download for windows 10 - 64 bit...

Tell your pc specs also ?
Please...

michellefereira
Автор

My collision isn't working I have exact code that you have I'm confused !!???

Nonpor
join shbcf.ru