Python Forum
Good class design - with a Snake game as an example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Good class design - with a Snake game as an example
#1
Hi guys,

in another topic, I shared my first Python project for feedback: My first Python project - Snake game

In case you don't want to check out that other thread, here's the direct link to the repo: https://github.com/bearek/snake

I got quite a few years of programming experience, but class design is still something I don't feel fully comfortable at. I never know how much dependency or flexibility is too much.

I think that Snake is a perfect case for class design practice. Currently, my game doesn't have a Snake class at all. I'd like to create one to separate the snake's logic from the rest. I'm still not sure what should my snake do itself and what it shouldn't do. Let's take a relationship between Snake and Grid (level) as an example. My Grid is just a 2D grid of X×Y size. Here are the three ideas how to implement the Snake class:

1. No information about the grid whatsoever. My Snake would just be a List of directions, which it actually is.
2. Giving the width and height to the Snake class to store the snake's points. That would make my snake know when it wraps around the edge of the grid. It'd also know when it dies, because without it, the snake doesn't know when it wraps long enough to bite its tail.
3. Just using Grid class as a dependency.

I don't have that sense of "the cleanest" solution when it comes to classes. Looks like idea 1. makes the snake the most reusable, but using it would be a bit troublesome. Idea 3 ties Snake and Grid together, so my Snake is no longer useful in any context without the Grid.

Is there some set of questions that I could ask myself to feel what's the cleanest solution? Are all of the above 3 solutions equally good? I'd love to have that skill of being sure I design great classes.

Thanks in advance for your feedback!
Reply
#2
Hi rice purity

It's great to see you seeking feedback on your Python project. When it comes to class design, it's important to strike a balance between dependency and flexibility. It's good that you're considering different approaches for implementing the Snake class.

In order to determine the cleanest solution, you might want to consider questions such as: How reusable is each approach? How tightly coupled are the classes? How easy is it to maintain and extend the code in the long run?

It's also worth thinking about how each approach aligns with the overall design goals of your project. Ultimately, the "cleanest" solution will depend on various factors specific to your project.

Best of luck with your class design, and I hope you find a solution that works well for your Snake game!

Cheers!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 472 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Mouvement Snake (The game) Catif 0 1,311 Nov-27-2020, 08:28 PM
Last Post: Catif
  using class functions in roguelike game trousers1 3 2,561 Dec-02-2019, 08:22 PM
Last Post: ichabod801
  Whats a good design/approach? hshivaraj 1 1,768 Sep-16-2019, 01:44 AM
Last Post: snippsat
  Shortest paths to win snake and ladder sandaab 5 4,218 Jun-30-2019, 03:20 PM
Last Post: sandaab
  snake game quit() ^ SyntaxError: invalid syntax arti 1 4,023 Jan-17-2018, 06:26 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020