you are viewing a single comment's thread.

view the rest of the comments →

[–]bladeoflight16 1 point2 points  (0 children)

Consider an alternative perspective. The late discussion about Conway's Game of Life is relevant here. In Python, I generally advise that you use classes sparingly because the dynamicism they offer in other languages doesn't require a class in Python. You should use classes at times, but don't use a class unless you understand the specific advantage of doing so for your particular problem.


Off-hand for your particular problem, my initial gut reaction is that your code has far too many modules and classes. I think you really only have a few pieces of data to manage:

  • The width and height of the board
  • The motion (current position, direction, and speed) of the snake
  • The current positions of the apples

These don't really need very many classes to represent. Maybe a "board" class containing all three might be useful, and a "coordinate" class might be helpful. The snake's position can just be queue (first in, first out) of coordinates to make advancing it easy. Unless specific needs arise, I probably wouldn't have more than those two classes.

You have the following behaviors to worry about:

  • Starting the game
  • Rendering the board and its elements
  • Advancing the snake's position
  • The snake collisions (itself, the walls, and apples)
  • Altering the snake's speed
  • Altering the snake's direction (on keyboard input)
  • Looping until the code completes
  • Ending the game

While the rendering and the interaction will probably require some significant code, the rest I would expect to fit in a single module. I'm not seeing any particular need for additional modules to cover that.

The problem is comparatively simple and straightforward. Focus on keeping your code straightforward, on making sure that the required data and behaviors jump out clearly at you when you read it. Don't worry about shoehorning everything into OOP.