Pygame - Does an enemy fly out of the screen in a collision with the ground?

first time here.

I try to keep it as simple as possible. I am trying to make a game in pygame, but it seems that my collision detection is valid. I have a player class that determines whether the player is facing the ground or any other objects in the environment list, and I have an enemy class that collides with something on the same list. Enemies are working out in which direction they need to travel in order to try to hit the player. There is seriousness that is likely to play an important role in the problem.

The problem is that when the "flies" are placed on the ground and fall to the ground, they immediately jump out of the screen, although their X and Y values ​​appear (according to the item logs on the screen) so as not to move at all?

As an explanation, two “flies” are placed on the secondary list to provide collision detection. In addition, as a side note, a “glitch” does not occur if there is no detection of left and right collisions ... Thanks to everyone who can help :)

def collisions():

#Detection Working as intended
    for fly in Fly.List:
        fly_proj = pygame.sprite.spritecollide(fly, Projectile.List, True)
        if len(fly_proj) > 0:
            for hit in fly_proj:
                fly.health -= 100

        X = pygame.sprite.spritecollide(fly, current_level.object_list, False)
        if len(X) == 0: #Gravity here:
            if (fly.vspeed == 0):
                fly.vspeed = 1
                print("Gravity")
            else:
                fly.vspeed += 0.35
        if len(X) > 1: 
            for hit in X:

                if fly.vspeed > 0:            
                    fly.rect.bottom = hit.rect.top +1
                    fly.vspeed = 0
                elif fly.vspeed < 0:    
                    fly.rect.top = hit.rect.bottom -1
                elif fly.hspeed > 0:
                    fly.rect.right = hit.rect.left
                    fly.hspeed = 0
                elif fly.hspeed < 0:            
                    fly.rect.left = hit.rect.right
                    fly.hspeed = 0

        print(len(X),framecounter, fly.vspeed, fly.hspeed)
#Kill fly if health reaches 0            
        if fly.health <= 0:
            fly.destroy(Fly)

 #Detect where the player is
        if window_rect.contains(fly.rect):
            fly.playersnapshot = player.rect.x
        if fly.rect.x - player.rect.x >= -10:
            #print("LEFTING")
            fly.hspeed = -2
        if fly.rect.x - player.rect.x <= 10:
            fly.hspeed = 2
        fly.truefalse = True
        event = None

        fly.rect.y += fly.vspeed
        fly.rect.x += fly.hspeed
+4
source share
2 answers

I think yours is if/elsewrong.

, , vspeed , if/else hspeed, left/right fly left/right.

.

  • if/else vspeed
  • if/else hspeed

-

:

 # move horizontally only

 fly.rect.x += fly.hspeed

 X = pygame.sprite.spritecollide( ... )

 for hit in X:
     if fly.hspeed > 0:
         fly.rect.right = hit.rect.left
     else:
         fly.rect.left = hit.rect.right

 # move vertically only

 fly.rect.y += fly.vspeed

 X = pygame.sprite.spritecollide( ... )

 for hit in X:
     if fly.vspeed > 0:
         fly.rect.bottom = hit.rect.top
     else:
         fly.rect.top = hit.rect.bottom

     # on_ground = True

"Platform Jumper" ProgramArcadeGames.com

. platform_jumper.py

+1

, -, , . , , :

                if fly.vspeed > 0:            
                    fly.rect.bottom = hit.rect.top +1
                    fly.vspeed = 0
                elif fly.vspeed < 0:    
                    fly.rect.top = hit.rect.bottom -1
                elif fly.hspeed > 0:
                    fly.rect.right = hit.rect.left
                    fly.hspeed = 0
                elif fly.hspeed < 0:            
                    fly.rect.left = hit.rect.right
                    fly.hspeed = 0

:

                fly.rect.y += fly.hspeed
                if fly.vspeed > 0:
                    fly.rect.bottom = hit.rect.top
                else:
                    fly.rect.top = hit.rect.bottom

                if fly.rect.bottom == hit.rect.top:
                    fly.vspeed = 0
                if fly.vspeed == 0:
                    fly.rect.x += fly.vspeed
                    if fly.hspeed > 0:
                        fly.rect.right = hit.rect.left
                    else:
                        fly.rect.left = hit.rect.right

, fly (vspeed ), :

if self.vspeed == 0: 
    self.vspeed = 1 
else: 
    self.vspeed += 0.35 #Rate of gravity

- , , , :)

UPDATE

Furas, : D , - :)

, , : (on_ground) collllisions().

        fly.rect.y += fly.vspeed

        X = pygame.sprite.spritecollide(fly, current_level.object_list, False)

        for hit in X:
            if fly.vspeed > 0:
                fly.rect.bottom = hit.rect.top
                on_ground = True
            else:
                fly.rect.top = hit.rect.bottom

        # move horizontally only

        fly.rect.x += fly.hspeed

        X = pygame.sprite.spritecollide(fly, current_level.object_list, False)

        for hit in X:
            if fly.hspeed > 0:
                fly.rect.right = hit.rect.left
            else:
                fly.rect.left = hit.rect.right

        if on_ground == True:
            fly.vspeed = 0
            #print("WAT2")
        else:
            fly.vspeed += 0.35
0

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


All Articles