def run(self):
while True:
self.now = pygame.time.get_ticks()
self._process_events()
self._update_gamedata()
self._update_display()
self.clock.tick(self.fps)
def _process_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
elif event.type == pygame.KEYDOWN:
action, kwargs = self.key_event_binds.get(event.key,
(None, None))
action(**kwargs) if kwargs else action() if action else None
def _update_gamedata(self):
for name, action in self.gamedata_update_actions.items():
if not action["next_time"]:
action["run"]()
elif self.now >= action["next_time"]:
action["next_time"] += action["interval"]
action["run"]()
def _update_display(self):
for action in self.display_update_actions:
action()
pygame.display.flip()
def draw(self):
for row in self.cell_array:
for cell in row:
if cell:
rect = pygame.Rect(cell.x * CELL_SIZE, cell.y * CELL_SIZE,
CELL_SIZE, CELL_SIZE)
self.surface.fill(cell.color1, rect)
self.surface.fill(cell.color2, rect.inflate(-4, -4))
def turn(self, **kwargs):
if (self.direction in [LEFT, RIGHT] and
args["direction"] in [UP, DOWN] or
self.direction in [UP, DOWN] and
args["direction"] in [LEFT, RIGHT]):
self.new_direction = args["direction"]
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/pysnake/pysnake7.py", line 221, in <module>
PySnake().run()
File "C:/Users/Administrator/Desktop/pysnake/pysnake7.py", line 204, in __init__
self.field = Field(self.screen, COLUMNS, ROWS)
File "C:/Users/Administrator/Desktop/pysnake/pysnake7.py", line 121, in __init__
self.cell_array = [[None] * self.columns for i in range(self.rows)]
TypeError: 'float' object cannot be interpreted as an integer
错误代码(一行代码对应一条错误信息):
PySnake().run()
self.field = Field(self.screen, COLUMNS, ROWS)
self.cell_array = [[None] * self.columns for i in range(self.rows)]
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/pysnake/pysnake7.py", line 222, in <module>
PySnake().run()
File "C:/Users/Administrator/Desktop/pysnake/pysnake7.py", line 210, in __init__
self.key_bind(KEY_UP, self.key_test, text="上")
AttributeError: 'PySnake' object has no attribute 'key_test'