0
\$\begingroup\$

Every time I press the left or right arrow keys it moves a little however when I wiggle the mouse around while pressing the keys the player flys across the screen for no reason. I'm following freeCodeCamps video on pygame and I did everything he did but its not working. Any help is apricated thanks!

    import pygame

# Starts pygame
pygame.init()

# Creates screen
# Needs two sets of () for some reason
screen = pygame.display.set_mode((800, 600))

# Sets top text of window
pygame.display.set_caption("LaBruh Invaders")

# Sets window Icon
icon = pygame.image.load('labruh.png')
pygame.display.set_icon(icon)

# player starting Pic and location
playerIMG = pygame.image.load('farmerCapy.png')
playerX = 400
playerX_change = 0

playerY = 300
playerY_change = 0

# Resizes img
playerIMG = pygame.transform.scale(playerIMG, (150, 150))


def player(x, y):
    # Blit means draw on surface
    screen.blit(playerIMG, (x, y))


# Game loop (Update every frame)
running = True

while running:
    # Changes color of screen
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        # Checks if key is pressed
        if event.type == pygame.KEYDOWN:
            # If left arrow is pressed moves left
            if event.key == pygame.K_LEFT:
                playerX_change = -5
            if event.key == pygame.K_RIGHT:
                playerX_change = 5

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

        playerX += playerX_change

        # Runs player funct which draws the player at X and Y cords
        player(playerX, playerY)

        # Makes screen quitable

        if playerX > 750:
            playerX = 0

        if playerX < -20:
            playerX = 740

        if event.type == pygame.QUIT:
            running = False

        # Updates the screen
        pygame.display.update()
\$\endgroup\$
3
  • \$\begingroup\$ Could you try to add something like playerX_change = 0 right after the for event in pygame.event.get():? \$\endgroup\$
    – Vaillancourt
    May 10, 2022 at 16:00
  • \$\begingroup\$ Maybe pygame.event.get() contains mouse events. you should discard all unexpected input. \$\endgroup\$
    – Mangata
    May 10, 2022 at 16:05
  • \$\begingroup\$ I see the motion code inside the for loop. So it would run for every event it got. It also means that if it didn't get any event it does not move at all. I believe that code should be outside the for loop. \$\endgroup\$
    – Theraot
    May 11, 2022 at 2:30

1 Answer 1

1
\$\begingroup\$

You should discard all unexpected input. like:

import pygame

# Starts pygame
pygame.init()

# Creates screen
# Needs two sets of () for some reason
screen = pygame.display.set_mode((800, 600))

# Sets top text of window
pygame.display.set_caption("LaBruh Invaders")

# Sets window Icon
icon = pygame.image.load('labruh.png')
pygame.display.set_icon(icon)

# player starting Pic and location
playerIMG = pygame.image.load('farmerCapy.png')
playerX = 400
playerX_change = 0

playerY = 300
playerY_change = 0

# Resizes img
playerIMG = pygame.transform.scale(playerIMG, (150, 150))


def player(x, y):
    # Blit means draw on surface
    screen.blit(playerIMG, (x, y))


# Game loop (Update every frame)
running = True

while running:
    # Changes color of screen
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        # Checks if key is pressed
        if event.type == pygame.KEYDOWN:
            # If left arrow is pressed moves left
            if event.key == pygame.K_LEFT:
                playerX_change = -5
            if event.key == pygame.K_RIGHT:
                playerX_change = 5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
        elif event.type == pygame.QUIT:
            running = False
            break
        else:
            continue

        playerX += playerX_change

        # Runs player funct which draws the player at X and Y cords
        player(playerX, playerY)

        # Makes screen quitable

        if playerX > 750:
            playerX = 0

        if playerX < -20:
            playerX = 740

        # Updates the screen
        pygame.display.update()
\$\endgroup\$
3
  • \$\begingroup\$ I pasted this code and it still didnt work youtube.com/watch?v=FfWpgLFMI7w&t=2718s Heres the video link if that helps \$\endgroup\$
    – CS-Jai
    May 10, 2022 at 20:32
  • \$\begingroup\$ @CS-Jai Due to the particularity of the python language, you may meet problems such as inconsistent indentation or line breaks when copying the code, I have added the complete code, which works for me. hope it helps. \$\endgroup\$
    – Mangata
    May 11, 2022 at 2:18
  • \$\begingroup\$ @CS-Jai Please keep in mind that our site is not the right place to learn coding; we expect that you understand the basics and can debug your code to some extent. \$\endgroup\$
    – Vaillancourt
    May 11, 2022 at 12:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .