1

Alright so the enemy I have only moves when I move my character, but it also sometimes spawns completely out of the game's window. Also the background, player and enemy just kinda... Dont pop up until I press a movement button, im so sorry for this lmfao Also sorry about the text and stuff I was told to put them there for reminders on wtf to

import pygame import random

pygame.init()

screen = pygame.display.set_mode((800, 600))

background = pygame.image.load("BackgroundImg.jpg")

pygame.display.set_caption("Real Hero") icon = pygame.image.load('Icon.png') pygame.display.set_icon(icon)

playerImg = pygame.image.load('Player.png') playerX = 370 playerY = 480 playerX_change = 0

enemyImg = pygame.image.load('Enemy.jpg') enemyX = random.randint(0, 746) enemyY = random.randint(50, 746) enemyX_change = 0.2 enemyY_change = 40

def player(x, y): screen.blit(playerImg, (x, y))

def enemy(x, y): screen.blit(enemyImg, (x, y))

running = True while running:

screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        playerX_change = -0.3
    if event.key == pygame.K_RIGHT:
        playerX_change = 0.3
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0

    playerX += playerX_change

    if playerX <= 0:
        playerX = 0
    elif playerX >= 736:
        playerX = 736
    
    enemyX += enemyX_change

    if enemyX <= 0:
        enemyX_change = 0.2
        enemyY += enemyY_change
    elif enemyX >= 736:
        enemyX_change = -0.2
        enemyY += enemyY_change

    enemy(enemyX, enemyY)
    player(playerX, playerY)
    pygame.display.update()
  • 1
    Welcome to Stack Overflow. Please read [ask] and the [formatting help](https://stackoverflow.com/help/formatting) and make sure you understand how to post code with proper formatting, and https://stackoverflow.com/help/minimal-reproducible-example for hints on showing us the *relevant* code, and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ to *understand* the problem. Please also make sure you understand that this is *not a discussion forum*; we do not want conversational language here - we want a clear, *focused question*. – Karl Knechtel Apr 05 '22 at 02:27
  • Does this answer your question? [Character only moving when the mouse moves?](https://stackoverflow.com/questions/71744633/character-only-moving-when-the-mouse-moves) – Esraa Abdelmaksoud Apr 05 '22 at 02:37

1 Answers1

1

The reason as to why the enemy only moves when you press a movement key is because you have the enemy movement code inside of the if event.type == pygame.KEYDOWN: if statement. You are also only updating the screen when you press a movement key because the pygame.display.update() is also inside that ifstatement. You need to move any code that shouldn't only be run when a key is pressed out of the if statement.

KingsDev
  • 654
  • 5
  • 21