I am designing a game and need to convert the script-variables from meters to pixels. My conversion factor is 30; 30 pixels to 1 meter. I have three levels of code: my game engine, the game level, and my scripts.
The Game Engine creates a new game level “curmap” from the base class. Every frame in the game, the engine calls curmap.animate, which returns a list of where every object in the game gets blit’ed. The level iterates over every object within the level, and calls animate on each object.
My Problem
The issue I have is that in the Level-animate method, the player-animate method is returning direct references to the player-class-variables. The two commented lines in the Level-animate were meant to only alter the returned value, but instead they are modifying the player-variables. I can’t reproduce this in a test class so I’m at a complete loss.
How do I uncomment those two inline multiplication statements without changing the player script?
The Game Engine:
# fps is an instance of pygame.time.clock()
# events is a modified list of pygame.event.get()
for item in curmap.animate(fps.tick()/1000, events):
screen.blit(curmap.assets['textures'][item[0]],item[1],item[2])
The Level:
def animate(self, timedelta, events):
returns = []
for item in self.objects:
ret = item.animate(timedelta, events)
#ret[1][0] *= 30
#ret[1][1] *= 30
returns.append(ret)
return returns
The Player Script:
def animate(self, timedelta, input):
# Process Inputs
for item in input['keydown']:
self.keydown.append(item)
for item in input['keyup']:
self.keydown.remove(item)
# Change Velocity and Position
if "left" in self.keydown:
self.velocity[0] = 5
elif 'right' in self.keydown:
self.velocity[0] = -5
else: self.velocity[0] = 0
print(self.position, self.velocity)
self.position[0] += self.velocity[0]*timedelta
self.position[1] += self.velocity[1]*timedelta
# Change Sprite
self.time += timedelta
if self.time > self.altertime:
self.current = (self.current+1)%2
self.time -= self.altertime
return [self.image, self.position, self.sprite[self.current] + self.dimensions]
You didn’t say what exactly
self.positionis in yourPlayer.animatemethod, but if it’s a simple list containing numeric values, just copy this list:You are returning the list
Player.position, and when you alter it (you change the values inside the list) inLevel.animate, you are actually changingPlayer.position, since you are working on the same list.