4:42 AM
@Sciborg, I'm going to brain-dump about my python drawing code in here and if you come in I'd appreciate advice for restructuring.
Okay, so there are a few levels of objects to consider here. There's the Game screen (inherits from pygame.Screen), which has a Board, which has a list of Blocks, which have lists of Points
I'm planning to make Points inherit from pygame.Rect (or I might make a Square class to do that, because I use Point for other reasons which would not involve Rects, e.g. tracking where the center is of a tetris piece)
That's the objects. Now onto drawing the tetris pieces
Each frame, after all keypresses/mouseclicks and subsequent cascading events occur, the main loop calls cur_screen.draw()
(for tetris-drawing purposes cur_screen
can be assumed to be a Game) to draw the current screen's current state.
The .draw()
method in Game in turn will call self.board.draw(self.window)
with self.window
being something it gets from inheritance, basically it's what everything should be drawn onto
Now, to draw a thing you need:
- something to draw on
- location
- color
- size
I can pass the aforementioned window
down as many calls as I want, but I need to figure out where the rest of these come from/should be stored
Tracking color makes sense on a the level of Blocks, I think, because colors are on a by-block basis in Tetris: every part of a piece is the same color
Size is something that I think I'll be storing/calculating on the level of the Board because it knows how big the play area it is (which is not the size of the whole window
because other things are on the screen, e.g. a quit button)
Location is twofold: I have to know the Tetris-coordinate based location (a row/column within the board), and then I need to be able to translate that to a pixel-based location
Points are what know Tetris-coordinate location: they're currently serving as glorified tuples for Tetris-coordinate handling abstraction
Boards, however, are what know how to translate Tetris-coordinates to pixel-coordinates, since (again) they know how much space the board should be taking up
If the goal of this overhaul is to not create the Rects just to draw them, they have to be stored somewhere
On creation they'll be given size and color (?) and then stored in Points (?) and moved as the Points move (?)
I could make a class-level variable for Point that would store size, and then set its value from the Board during its instantiation
But that feels a little cheaty in a way? Or bad? Because I'd be assuming that all Points no matter what must abide by the Board's sizing standards, which is valid here because there is but one Board at a time and all Points are associated with it
Also I just realized that besides how big they are the Points (to choose location) would also need to know how much offset to use from the corner of the screen; that's something that the Board knows but not them. And the Board also knows how many columns/rows there are which may affect offset calculations
If the Points have the Rects then I'd expect the .draw
for the Board to call one on each Block (?) which would call on each Point to draw its Rect
Or wait, if I make the Point class inherit from Rect I could directly pass it to pygame.draw.rect
so it doesn't have to draw itself, the Block could handle drawing all of them
Or I could just leave the Board to do all the drawing by looping through each of it's Block's Points and drawing the corresponding Rects
My design questions boil down to: who knows what, when/how do they update/learn that, and who uses this to draw things, and how do they do that?
All of this is open-ended and probably not thought through well; I'll leave my brain-dump here to perlocate