Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enemy wont move on its own in pygame

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading