writertriada.blogg.se

Thonny raspberry pi pygame
Thonny raspberry pi pygame





thonny raspberry pi pygame

def _init_(self, height, width): self.height = height self.width = width for i in range(height): new_line = for j in range(width): new_line.append(0) (new_line)

thonny raspberry pi pygame

We initialize the game with the following simple method: class Tetris. The field is the field of the game that contains zeros where it is empty, and the colors where there are figures (except the one that is still flying down). Where the state tells us if we are still playing a game or not.

thonny raspberry pi pygame

We first initialize the Game with some variables: class Tetris: level = 2 score = 0 state = "start" field = height = 0 width = 0 x = 100 y = 60 zoom = 20 figure = None def image(self): return self.figures def rotate(self): self.rotation = (self.rotation + 1) % len(self.figures) Where we randomly pick a type and a color.Īnd we need to quickly be able to rotate and get the current rotation of a figure, for this we have these two simple methods: class Figure. def _init_(self, x, y): self.x = x self.y = y self.type = random.randint(0, len(self.figures) - 1) lor = random.randint(1, len(colors) - 1) self.rotation = 0 The _init_ function would be as follows: class Figure. To better understand that, please refer to the picture above.Īs an exercise try to add some missing figures here, namely the “z” figures. For instance, the figure represents a line. The numbers in each figure represent the positions in a 4x4 matrix where the figure is solid. Where the main list contains figure types, and the inner lists contain their rotations. So, we simply have a list of lists of figures like that: class Figure: figures =, ],, ,, ],, ,, ],, ,, ], ], ]







Thonny raspberry pi pygame