1

I made a loop to keep the "enemy" character moving to left and right, but for some reason that I really can't tell why, he just goes to the right and when he hits the border limit that I defined, he "teleports" to the left border and keeps going to the right forever. I even tried to make the "enemyX_change" beeing negative, but he just goes to the left, hit the border and stops. I'm using Python 3.8.

import pygame
import random

# Initiate pygame
pygame.init()

# Display the game window
screen = pygame.display.set_mode((800, 600))

# Title and Icon
pygame.display.set_caption('Space Invaders')
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

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

# Enemy
enemySprite = pygame.image.load('enemy.png')
enemyX = random.randint(0, 736)
enemyY = random.randint(50, 150)
enemyX_change = 0.3
enemyY_change = 0


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


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


# Game Loop
running = True
while running:

    # Background color (RGB)
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # If a key is pressed, check if it's the right or left arrow key
        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

    # Move the spaceship to the left or right
    playerX += playerX_change

    # Prevents the player from going off the border
    if playerX <= 0:
        playerX = 0
    elif playerX >= 736:
        playerX = 736

    # Moves the enemy
    enemyX += enemyX_change

    # Prevents the enemy from going off the border
    if enemyX <= 0:
        enemyX = 0.3
    elif enemyX >= 736:
        enemyX = -0.3

    player(playerX, playerY)
    enemy(enemyX, enemyY)
    pygame.display.update()
1
  • Why should the enemy ever move to the left with your code? If enemyX is greater or equal 736 you decrease enemyX by -0.3 but in your loop you always add enemyX_change which is 0.3 too, so enemyX is unchanged once it reaches 736. Commented Apr 12, 2020 at 22:52

1 Answer 1

1

When the enemy goes out of bounds, then you have to change the movement direction (enemyX_change) rather than the position of the player (enemyX):

enemyX = -0.3

enemyX_change *= -1 

Invert enemyX_change when it is less than 0 or greater than the width of the window:

# Moves the enemy
enemyX += enemyX_change

# Prevents the enemy from going off the border
if enemyX <= 0 or enemyX >= 736:
    enemyX_change *= -1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.