How Do I Move My Player Object In Pygame?

Question:

I am currently learning how to use classes for cleaner code. In a game I started to work on in Python has a player object, and I don’t know how to move the player.

When I press any arrow key the player’s rect moves; however, the player’s image doesn’t change position. I printed the player rect’s x and y positions to ensure that the keys are working: the keys do work because the x and y values are being printed in a changed position. For whatever reason, my player doesn’t move, just the rect. Can someone help me understand how to allow for the player’s image to actually move with the rect’s position?

This is my current project code:

# Imports
import pygame
import sys
from pygame.locals import *


# Init Pygame
pygame.init()


# Screen Info
FPS = 120
fps_clock = pygame.time.Clock()
WINDOW_SIZE = 400, 400
screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
pygame.display.set_caption("Sanctified Being")

# Colors
BG_COLOR = 170, 170, 170
PLAYER_NORMAL_COLOR = 0, 255, 0
PLAYER_DAMAGED_COLOR = 255, 0, 0



# Quit Game Function
def quit_game():
    pygame.quit()
    sys.exit()


# Player Class
class Player:
    def __init__(self):
        self.pos = [200, 200]
        self.dimension = [50, 50]
        self.player_rect = pygame.Rect(self.pos[0], self.pos[1], self.dimension[0], self.dimension[1])
        self.current_state = "normal"
        self.color = PLAYER_NORMAL_COLOR
        self.speed = 1


    def draw(self):
        pygame.draw.rect(screen, self.color, self.player_rect)


    def movement(self):
        key_pressed = pygame.key.get_pressed()
        if key_pressed[K_LEFT]:
            self.pos[0] -= self.speed
        elif key_pressed[K_RIGHT]:
            self.pos[0] += self.speed
        elif key_pressed[K_UP]:
            self.pos[1] -= self.speed
        elif key_pressed[K_DOWN]:
            self.pos[1] += self.speed
        else:
            pass # IDK for now


# Player
player_one = Player()



# Game Loop
while True:

    # Background Color
    screen.fill(BG_COLOR)

    # Display Player
    player_one.draw()
    player_one.movement()


    # Check For Events
    for event in pygame.event.get():
        if event.type == QUIT:
            quit_game()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                quit_game()
            
    print("X:", player_one.pos[0])
    print("Y:", player_one.pos[1])


    # Update The Screen Every Frame
    pygame.display.update()
    fps_clock.tick(FPS)
Asked By: TAJ

||

Answers:

You are moving the player’s position, but not updating the position of the player’s rectangle. In order to do that, you can update the self.player_rect attribute inside the movement method by modifying it like this:

def movement(self):
    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_LEFT]:
        self.pos[0] -= self.speed
    elif key_pressed[K_RIGHT]:
        self.pos[0] += self.speed
    elif key_pressed[K_UP]:
        self.pos[1] -= self.speed
    elif key_pressed[K_DOWN]:
        self.pos[1] += self.speed
    else:
        pass # IDK for now
            
    self.player_rect.x = self.pos[0]
    self.player_rect.y = self.pos[1]
Answered By: Ake
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.