2

Basically whenever I press left, right, a, or d, it moves both characters instead of just one. I've tried including print statements before and after updates, and I've asked around and been told it's an aliasing issue, but I still can't find the solution. Do I need to make two separate classes or am I doing something wrong within some of my code?

import simplegui

WIDTH = 800
HEIGHT = 600
#class for character
class Character:
    def __init__(self, pos, radius, vel = [0,0]):
        self.pos = pos
        self.vel = vel
        self.radius = radius
#updates position
    def update(self, state):
        self.pos[0] += self.vel[0]
        self.pos[1] += self.vel[1]
#moving left and right
    def change_left_vel(self):
        self.vel[0] -= 2
    def change_right_vel(self):
        self.vel[0] += 2
#draws character
    def draw(self, canvas, state):
        canvas.draw_circle(self.pos, self.radius, 5, "White", "White")    
#initializes characters and frame
class GameState:
    def __init__(self):
        self.frame = simplegui.create_frame("Super Smash Squad", WIDTH, HEIGHT)
        self.frame.set_keydown_handler(self.keydown)
        self.frame.set_keyup_handler(self.keyup)
        self.frame.set_draw_handler(self.draw)        
        self.char1 = Character([200, 200], 15)
        self.char2 = Character([400, 200], 25)
        self.frame.start()
#movement for both characters
    def keydown(self, key):
        if key == simplegui.KEY_MAP["a"]:
            self.char1.change_left_vel()
        elif key == simplegui.KEY_MAP["d"]:
            self.char1.change_right_vel()
        elif key == simplegui.KEY_MAP["left"]:
            self.char2.change_left_vel()
        elif key == simplegui.KEY_MAP["right"]:
            self.char2.change_right_vel()    
    def keyup(self, key):
        if key == simplegui.KEY_MAP["a"]:
            self.char1.change_right_vel()
        elif key == simplegui.KEY_MAP["d"]:
            self.char1.change_left_vel()
        elif key == simplegui.KEY_MAP["left"]:
            self.char2.change_right_vel()
        elif key == simplegui.KEY_MAP["right"]:
            self.char2.change_left_vel()    
#draws everything
    def draw(self, canvas):
        self.char1.update(self)
        self.char2.update(self)
        self.char1.draw(canvas, self)
        self.char2.draw(canvas, self)
# start frame
GameState()
0TTT0
  • 1,288
  • 1
  • 13
  • 23
Daniel
  • 21
  • 1
  • Possible duplicate of ["Least Astonishment" and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – jasonharper Oct 26 '17 at 04:21
  • 2
    Every `Character` you create, that doesn't give an explicit value for the `vel` parameter, shares the SAME default value for `self.vel` - and everything you do with that value later just changes it in place, so everybody continues to share the same velocity. – jasonharper Oct 26 '17 at 04:24
  • Value types to the rescue! – EvilTak Oct 26 '17 at 05:39

0 Answers0