我制作的代码有效,但我想知道为什么。我不小心在代码中犯了一个错误,但由于某种原因它可以工作,为什么?当我传递lastkeys
给时move.mov
,键不应该是一个新变量吗?与 , 相同ent.playerlocation
,不应该player
是move.mov
一个新变量吗?当我退出函数时,原始变量将更改为新变量的值。我试图重新创建它,但无法做到。
主要.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)
移动.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