Python rectangle collision handling with pygame

I have done extensive research on this topic over the past few days, and I cannot find an answer for my exact problem.

So, I have a simple game in which I have a player at 0, 0 with a width of 10x10

player= pygame.Rect(0, 0, 10, 10) 

and in addition, the player has a speed of x: 0, y: 10, which will make him fall (y is positive, because the beginning of the screen is in the upper left corner.)

and I have a tile at 0, 100, as shown:

  dirt= pygame.Rect(0, 100, 10, 10) 

therefore, how can I handle the collision, I already know that I can detect it with Rect.colliderect (Rect).

I tried several ways, but ran into some problems:

I can’t cut the player’s speed to 0 when he stumbled on something, and then move it back until it just touches the object, because it still causes a walking problem, when it walks, I apply +10 speeds in x, but, unfortunately, the game is still handling the fact that it crashes and collides and moves sideways, so it just moves it back to where it started.

I'm a newbie, so a simple answer would be very useful for me, and I would not want to use other third-party modules that would be different if I did not have to.

Update:

Here is an example of the rough test code I tried:

  def sim(obj, time, world): time= time / 1000 obj.physProp['vel']= (obj.physProp['vel'][0] + (accel[0] * time), obj.physProp['vel'][1] + (accel[1] * time)) if obj.physProp['vel'][1] > terminalY: obj.physProp['vel']= (obj.physProp['vel'][0], terminalY) obj.pos= (obj.pos[0] + (obj.physProp['vel'][0] * time) + ((accel[0] / 2) * (time ** 2)), obj.pos[1] + (obj.physProp['vel'][1] * time) + ((accel[1] / 2) * (time ** 2))) for ID in world: if obj.getRect().colliderect(world[ID].getRect()) == True: pass return (obj.pos, obj.physProp['vel']) 
+4
source share
2 answers

The Pygame API invites you to write all your plot objects in an object-oriented way, so that your falling character has all the “methods” and “attributes” in order to react correctly to things in the script - for example, to strike.

So, if your character is defined for something simple:

 class Char(object): # these start as class attributes, # but whenever they are assigned to with a "self.var = bla" in # a method, an instance attribute starts existing x, y = 0,0 vx, vy = 0,0 def update(self): self.x += self.vx self.y += self.vy 

And your external code, having detected a collision, could do just that:

 def mainloop(): while True: ... obj.update() if obj.getRect().colliderect(world[ID].getRect()): # don't do "== True" in `if - it is just silly # take character back to the precious position obj.x -= obj.vx obj.y -= obj.vy # zero out velocities to make it stop: obj.vx = obj.vy = 0 

And so on - you will soon realize that you think of your “things” as “objects”, since they are used in programming so that the code flow is completely natural - as soon as you get it, look at the Pygame help module - which allows you to automate many checks and updates without the need to explicitly write for loops for each check

+1
source

Divide the motion x / y.

Move x, check if collides, if yes, go back and set xspeed to 0.

Move y, check if collides, if so, go back and set yspeed to 0.

This means that two collisions are checked at every step, but it is super smooth. :)

+4
source

Source: https://habr.com/ru/post/1441950/


All Articles