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():
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:
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)
if fly.health <= 0:
fly.destroy(Fly)
if window_rect.contains(fly.rect):
fly.playersnapshot = player.rect.x
if fly.rect.x - player.rect.x >= -10:
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
source
share