我使用python和codeskulptor
/ simplegui
制作游戏,我收到了"undefined: RangeError: Maximum call stack size exceeded"
这个错误
我在这里定义一个班级。
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()
我的想法是你使用箭头键移动,并避免使用球,但你的位置会引起地震而且一个人会向那个方向倾斜,如果这个人跌倒或者你击中了球,你输了。
答案 0 :(得分:0)
您似乎以递归方式为您的类创建对象,示例 -
Game() -> Player() -> Game() -> .... This goes on.
这是在行 -
中完成的对于游戏类 -
类游戏(): 全球球 def init (个体经营): self.person =人() self.ball = Ball() self.player = Player()#Notice你在这里创建了Player对象。
对于Player类 -
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.
这很可能是造成这个问题的原因。如果您希望Player()
引用game
从Game()
s'__init__()
方法作为参数传递它,则不应递归创建对象(如此) 。示例 -
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
你也很可能在Ball()
中不必要地创建Person()
对象,很可能你也想以类似的方式修复它。