So I tried to make a piece of code which has an ‘enemy’ just move to you. It worked until I added some code with button presses moving the character itself. How can I solve this?
I’ve put the code which doesn’t work in bold.
Here is the entire code:
pygame.init()
font = pygame.font.SysFont('Arial', 15)
game = pygame.display.set_mode((800,600))
pygame.display.set_caption("Test lol")
enemy = pygame.image.load("SS Tree 1 Transparent.png")
player = pygame.image.load("SS RML.png")
player_x = 100
player_x2, player_y2 = player.get_size()
player_y = 400
enemy_x = 300
enemy_x2, enemy_y2 = enemy.get_size()
enemy_y = 400
enemy_velocity = 0
player_velocity = 0
while True:
game.fill((255,0,0))
game.blit(player, (player_x, player_y))
game.blit(enemy, (enemy_x, enemy_y))
enemy_velocity -= 0.1
player_velocity -= 0.1
if enemy_y >= 400-enemy_y2:
enemy_velocity = 0
enemy_y = 400-enemy_y2
if player_y >= 400-player_y2:
player_velocity = 0
player_y = 400-player_y2
if player_x > enemy_x:
enemy_x += 0.1
elif player_x < enemy_x:
enemy_x -= 0.1
if player_y < enemy_y and enemy_velocity == 0 and enemy_y == 400:
enemy_velocity += 5
keys = pygame.key.get_pressed()
**if keys[pygame.K_LEFT]:
print("r")
player_x -= 1
if keys[pygame.K_RIGHT]:
player_x += 1
if keys[pygame.K_UP] and player_y == 400:
player_velocity = 5**
enemy_y -= enemy_velocity
player_y -= player_velocity
pygame.display.flip()
I checked another piece of code with the same code in another program, but I didn’t see any differences. That program is working.
BliepMonster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.