Simplegui: undefined: RangeError: Maximum call stack size exceeded

448 Views Asked by At

I'm using python and codeskulptor/simplegui to make a game, and I got this error that said "undefined: RangeError: Maximum call stack size exceeded" here when I'm defining a class.

import simplegui, random, time 
level = 1
balls = []
class Person():
    def __init__(self):
        self.deg = 0
        self.fall = False
        self.ball = Ball()
class Ball():
    global level
    def __init__(self):
        self.velo = 0
        self.pos = [random.randint(100, 500), 0] //error here
        self.xChange = random.randint(-5, 5)
    def move(self):
        self.velo = level
        self.pos[1] += level
        self.pos[0] += xChange
        if self.pos[0] <= 0 or self.pos[0] >= 600:
            self.xChange *= -1
        if self.pos[1] >= 500:
            self.pos[0] = random.randint(100, 500)
            self.pos[1] = 0 
class Player():
    def __init__(self):
        global balls
        self.posx = 300
        self.velo = 0
        self.gameover = False
        self.pearson = Person()
        self.ball = Ball()
        self.game = Game()
    def left(self):
        self.velo -= 0.5
        self.pearson.deg -= 0.5
    def right(self): 
        self.velo += 0.5 
        self.pearson.deg += 0.5
    def renewInfo(self):
        self.posx += self.velo
    def hit(self):
        if self.posx + 10 <= self.ball.pos[0] and self.posx - 10 >= self.ball.pos[0] and self.ball.pos[1] >= 470:
            self.game.gameover 
class Game():
    global balls
    def __init__(self):
        self.person = Person()
        self.ball = Ball()
        self.player = Player()
    def spBall(self):
        for i in range(10):
            balls.append(self.ball)
    def move(self):
        pass
    def gameover(self):
        pass
game = Game()
def draw(canvas):
    pass
def keydown(key):
    if key == 37:
        game.player.left
    if key == 39:
        game.player.right
def time_handler():
    global level
    level += 1 
timer = simplegui.create_timer(30000, time_handler)
timer.start()
frame = simplegui.create_frame('Quake Control', 800, 500)
frame.set_canvas_background('white')
frame.set_draw_handler(draw) 
frame.set_keydown_handler(keydown)
frame.start()  

My idea is that you use arrow keys to move, and avoid balls, but you position will cause an earthquake and a person will tilt in that direction, if the person falls or you hit the ball, you lose.

1

There are 1 best solutions below

0
On BEST ANSWER

You seem to be recursively creating objects for your class , Example -

Game() -> Player() -> Game() -> .... This goes on.

This is done in the lines -

For Game class -

class Game(): global balls def init(self): self.person = Person() self.ball = Ball() self.player = Player() #Notice you are creating Player object here.

For Player class -

class Player():
    def __init__(self):
        global balls
        self.posx = 300
        self.velo = 0
        self.gameover = False
        self.pearson = Person()
        self.ball = Ball()
        self.game = Game()      #Notice you are creating new Game() object here.

This is most probably what is causing the issue. You should not recursively create objects, like this, if you want the Player() to have a reference to the game pass it from the Game()s' __init__() method as an argument. Example -

class Player():
    def __init__(self, person, ball, game):
        global balls
        self.posx = 300
        self.velo = 0
        self.gameover = False
        self.pearson = person
        self.ball = ball
        self.game = game

class Game():
    global balls
    def __init__(self):
        self.person = Person()
        self.ball = Ball()
        self.player = Player(self.person, self.ball, self)
    def spBall(self):
        for i in range(10):
            balls.append(self.ball)
    def move(self):
        pass
    def gameover(self):
        pass

You are also, most probably unnecessarily creating Ball() object in Person() , most probably you want to fix that in a similar way there as well.