2
\$\begingroup\$

I am making a game using python and pygame and as my game started to grow, in some of my classes the number of instance variables started to get big. Like in the settings class is like this:

class Settings: """A class to store all settings for Alien Invasion.""" def __init__(self): """Initialize the game's static settings.""" # Screen Settings self.screen_size = (1260, 700) self.screen_width = 1260 self.screen_height = 700 # game background images self.bg_img = pygame.image.load('images/background/space.jpg') self.second_bg = pygame.image.load('images/background/space2.png') self.third_bg = pygame.image.load('images/background/space4.jpg') self.game_over = pygame.image.load('images/other/gameover.png') self.pause = pygame.image.load('images/other/pause.png') self.fire_sound = pygame.mixer.Sound('sounds/fire.wav') self.endless = False self.last_stand = False # Ships settings self.max_hp = 5 self.thunderbird_hp = 3 self.phoenix_hp = 3 # Thunderbolt settings self.thunderbird_bullet_count = 1 # Firebird settings self.phoenix_bullet_count = 50 # Alien settings self.alien_direction = 1 self.max_alien_speed = 4.0 self.max_aliens_num = 35 self.boss_hp = 50 self.boss_points = 2500 self.alien_points = 1 self.endless_num = 50 # PowerUps settings self.powerup_speed = 1.5 # How quickly the game speeds up self.speedup_scale = 0.3 self.score_scale = 4 self.initialize_dynamic_settings() # Player1 controls self.p1_controls = ("Player 1:\n" "Movement: Arrow Keys\n" "Shoot: Enter\n" "Ship skin: Numpad 1, 2, 3\n" "Pause: P") # Player2 controls self.p2_controls = ("Player 2:\n" "Movement: W, A, S, D\n" "Shoot: Space\n" "Ship skin: 1, 2, 3\n" "Pause: P") 

My question now is, i should create different classes for different groups of variables and call the classes inside the Settings, o I should just group the variables in different methods in the Settings class?

\$\endgroup\$
2
  • \$\begingroup\$I'm not a python expert so I can't provide a good answer. On thing you should consider is separating the game logic from the display logic. Most software is organized this way. Some Design patterns you should look into are Model View Viewmodel (MVVM) and Model View Controller (MVC).\$\endgroup\$
    – pacmaninbw
    CommentedMar 13, 2023 at 22:46
  • 1
    \$\begingroup\$The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.\$\endgroup\$CommentedMar 14, 2023 at 11:07

1 Answer 1

6
\$\begingroup\$

It looks like you're actually mixing a few unrelated things here. Consider: how many of these are actually settings? That is, how many of these does it make sense for the end user to modify in some way?

  • Fields like bg_img and game_over are clearly not settings, but rather constants which are built into the game.
  • Fields like powerup_speed and boss_points could conceivably be dynamic (boss_points could be a function which depends on some points-increasing powerup, for example), but the user probably shouldn't be able to change them.

Fields which are not user settings could be part of, for example, an admin settings class/instance (if that needs to exist), or just constants which can't be changed at runtime.

As for solutions for handling lots of values (constant and dynamic) across lots of entities, you probably want to look into entity component systems.

As a side note, screen_size duplicates the information in screen_width and screen_height, so it probably shouldn't exist at all.

\$\endgroup\$
1
  • \$\begingroup\$Thank you for your answer! Most of the settings are not changed by the user. They change based on the level/difficulty. In my ships module for example I have the ship class but for some reason as I implemented for example animations for the ship being hit, warping, a shield animation the class got big and has too many instance variables. Should I just move all the animations for example in another class? I also created some methods that are just for loading files, formatting text, should I move them in a different module?\$\endgroup\$
    – Alex
    CommentedMar 14, 2023 at 3:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.