The code I made works, but I want to know why. I accidentally made a mistake in the code, but for some reason it works, why? When I pass lastkeys
to move.mov
, shouldn't keys be a new variable? The same with ent.playerlocation
, shouldn't be player
in move.mov
be a new variable? The original variable is changed to the value of the new one when I exit the function. I've tried to recreate this, but haven't been able to.
main.py:
import pygame
import move, updatescreen
class entities:
def __init__(self):
self.playerlocation = [64,64]
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((640,360),pygame.RESIZABLE,32)#pygame.FULLSCREEN
pygame.display.set_caption('Pygame Window')
ent = entities()
lastkeys = [0,0,0,0]
ispaused = 0
while True:
move.userevents(lastkeys)
move.mov(lastkeys, ent.playerlocation)
updatescreen.gameupdate(screen,ent)
move.py:
import pygame, sys
def userevents(keys):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
keys[0] = 1
elif event.key == pygame.K_RIGHT:
keys[1] = 1
elif event.key == pygame.K_UP:
keys[2] = 1
elif event.key == pygame.K_DOWN:
keys[3] = 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
keys[0] = 0
elif event.key == pygame.K_RIGHT:
keys[1] = 0
elif event.key == pygame.K_UP:
keys[2] = 0
elif event.key == pygame.K_DOWN:
keys[3] = 0
def mov(keys,player):
if keys[0]:
player[0] -=1
elif keys[1]:
player[0] +=1
if keys[2]:
player[1] -=1
elif keys[3]:
player[1] +=1