0

For some reason even though no error is appearing the player1 sprite is not moving even after I set the variables to global. I have checked several times and I can not see where the error is so i would be very great-full if anyone helped me out. I am using pygame, sys and os libraries. Sorry for the messy code.

Code:

from GGD import *

screen = pygame.display.set_mode((226, 318))
player_pos=[100,50]

pygame.display.set_icon(pygame.image.load('Sprites/Icon.png'))
pygame.display.set_caption("Knock Knight")
player1 = pygame.image.load('Sprites/Player.png')
player_rect = pygame.Rect(player_pos[0], player_pos[1], player1.get_width(), player1.get_height())

spriteGlobals()
clock = pygame.time.Clock()
while True:
    screen.fill((0, 0, 0))

    screen.blit(player1, player_pos)

    spriteMovement(player1, player_pos)

    pygame.display.update()
    clock.tick(60)

pygame.quit()

GGD:

from pygame.locals import *
import pygame
import sys
import os

print('Loading[*]')

pygame.init()

moving_right = False
moving_left = False
moving_up = False
moving_down = False

def spriteGlobals():
    global moving_up, moving_down, moving_left, moving_right

def spriteMovement(player1, player_pos=[0,0]):
    global moving_up, moving_down, moving_left, moving_right

    if moving_right == True:
        player_pos[0] += 4
    if moving_left == True:
        player_pos[0] -= 4
    if moving_up == True:
        player_pos[1] -=4
    if moving_down == True:
        player_pos[1] +=4

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
        if event.key == K_RIGHT:
            moving_right = True
        if event.key == K_LEFT:
            moving_left = True
        if event.key == K_UP:
            moving_up = True
        if event.key == K_DOWN:
            moving_down = True
    if event.type == KEYUP:
        if event.key == K_RIGHT:
            moving_right = False
        if event.key == K_LEFT:
            moving_left = False
        if event.key == K_UP:
            moving_up = False
        if event.key == K_DOWN:
            moving_down = False
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
Jigsaw
  • 294
  • 1
  • 3
  • 14
  • Sorry, but your code has syntax errors, cause by the ill formed [Indentation](https://docs.python.org/3/reference/lexical_analysis.html). However, if I fix the indentation, your problem will not be reproducible. – Rabbid76 Apr 04 '21 at 21:28
  • 1
    Instead of apologizing for the messy code, clean it up before posting it. – mkrieger1 Apr 04 '21 at 21:28
  • See how to create a [mcve]. Remove any code not relevant to the question, e.g. most of the key press code is irrelevant. Produce something absolutely minimal which still demonstrates the problem. – Peter Wood Apr 04 '21 at 21:46
  • first of I suggest using `keys = pygame.key.get_pressed()`, also I have heard that using global variables is not that great. and how do You suppose to get that `player_pos` variable? I have also heard that it is not the best practice how You use it there. right it could be possible that every time You call the function it resets player pos to `0, 0`, so it could help if You moved the first four if statements to the end (that may actually solve the issue, maybe). just take a look at a tutorial like [this one](https://www.youtube.com/watch?v=i6xMBig-pP4&list=PLzMcBGfZo4-lp3jAExUCewBfMx3UZFkh5) – Matiiss Apr 04 '21 at 23:50

0 Answers0