0

This is the enemy movement:

    if playerX > enemyX:
        enemyX += 0.5
    elif playerX < enemyX:
        enemyX -= 0.5
    elif playerY > enemyY:
        enemyY += 0.5
    elif playerY < enemyY:
        enemyY -= 0.5

Both the position of the player and the enemy are predetermined. The enemy starts at (370,50) and the player at (370, 480). The player can be moved using the arrow keys. If the enemy collides with the player the game ends. That part works just fine. The problem is that once I change enemyY or enemyX to below 0.5 the enemyY won't change anymore. But 0.5 is too fast, the player barely has any time to evade the enemy. I was wondering if I can write this differently so that the issue won't occur anymore? I don't know how to fix this and I have been searching for a solution for a while. It would be great if somebody could kindly help me out here.

morloq
  • 123
  • 6

1 Answers1

2

if you are using pygame.rect to solve enemies movement problem, xy axis were using int, +=0.5 or -=0.5 might lead to original x or y, try to make them float tho, and see this can fix the problem. Moreover, you can also use vector to solve, just make them a velocity instead of direct movement

JohntheTyro
  • 136
  • 4
  • 1
    This might help you more: https://stackoverflow.com/questions/50859819/pygame-making-things-move-slower-than-1 – JohntheTyro Mar 23 '21 at 19:27
  • making the original values of x and y floats as well does not solve the issue, thanks though. What do you mean by using vectors and velocity? – morloq Mar 23 '21 at 20:58
  • actually, I have just tried it again and suddenly it works just fine with 0.1 for enemy movement – morloq Mar 23 '21 at 21:07
  • yea, good to hear that, see the website I linked you will find the velocity and vectors. – JohntheTyro Mar 23 '21 at 21:55