I don't really know what the issue is can anyone tell me my issue and point me in the right direction

Joined
Jul 25, 2024
Messages
18
Reaction score
1
Code:
import pygame, sys
from pygame.locals import *
pygame.init()

playerPos = {"characterX":750, "characterY":325, "characterWidth":50, "characterHeight":70}

def playerMovement(pressed):
    if (pressed[K_RIGHT]):
        playerPos.characterX = playerPos.characterX + 3
    if (pressed[K_LEFT]):
        playerPos.characterX = playerPos.characterX - 3
    if (pressed[K_DOWN]):
        playerPos.characterY = playerPos.characterY + 3
    if (pressed[K_UP]):
        playerPos.characterY = playerPos.characterY - 3

#Colours
BACKGROUND = (255, 255, 255)

CHARACTER = (255, 30, 70)

# Game Setup
FPS = 60
fpsClock = pygame.time.Clock()
WINDOW_WIDTH = 1500
WINDOW_HEIGHT = 750

WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("My Game!")

# The Main function that controls the game\
def main():
    looping = True

    # The main game loop
    while looping:
        # Get inputs
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        
            playerMovement(pressed = pygame.key.get_pressed())

        # Processing
        # This section will be built out later

        character = pygame.Rect(playerPos)

        # Render elements of the game
        WINDOW.fill(BACKGROUND)
        pygame.draw.rect(WINDOW, CHARACTER, character)
        pygame.display.update()
        fpsClock.tick(FPS)
        
main()
 
Joined
Dec 10, 2022
Messages
106
Reaction score
26
Chane the is
Python:
character = pygame.Rect(playerPos)
to
Python:
character = pygame.Rect([val for val in playerPos.values()])
 
Joined
Dec 10, 2022
Messages
106
Reaction score
26
I've corrected your code to work. Commented in triple quotes the changes

Python:
import pygame, sys
from pygame.locals import *
pygame.init()

playerPos = {"characterX":750, "characterY":325, "characterWidth":50, "characterHeight":70}

''' Corrected for movemment '''
def playerMovement(pressed):
    if (pressed[K_RIGHT]):
        playerPos['characterX'] += 8
    if (pressed[K_LEFT]):
        playerPos['characterX'] -= 8
    if (pressed[K_DOWN]):
        playerPos['characterY'] += 8
    if (pressed[K_UP]):
        playerPos['characterY'] -= 8

#Colours
BACKGROUND = (255, 255, 255)

CHARACTER = (255, 30, 70)

# Game Setup
FPS = 60
fpsClock = pygame.time.Clock()
WINDOW_WIDTH = 1500
WINDOW_HEIGHT = 750

WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("My Game!")

# The Main function that controls the game\
def main():
    looping = True

    # The main game loop
    while looping:
        # Get inputs
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
               
        ''' Moved this out of the event loop '''
        playerMovement(pressed = pygame.key.get_pressed())

        # Processing
        # This section will be built out later
        ''' This needs to be a rect. Did so by using list comprehension '''
        character = pygame.Rect([val for val in playerPos.values()])

        # Render elements of the game
        WINDOW.fill(BACKGROUND)
        pygame.draw.rect(WINDOW, CHARACTER, character)
        pygame.display.update()
        fpsClock.tick(FPS)
       
main()
 
Last edited:
Joined
Jul 25, 2024
Messages
18
Reaction score
1
thanks that fixed my problem but just so you know

Code:
def playerMovement(pressed):
    if (pressed[K_RIGHT]):
        playerPos["characterX"] = playerPos["characterX"] + 8
    if (pressed[K_LEFT]):
        playerPos["characterX"] = playerPos["characterX"] - 8
    if (pressed[K_DOWN]):
        playerPos["characterY"] = playerPos["characterY"] + 8
    if (pressed[K_UP]):
        playerPos["characterY"] = playerPos["characterY"] - 8
that is way the movement code is supposed to be written or the movements don't work right
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,075
Messages
2,570,562
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top