How To Download Tetris On Ti-84 Plus
How to write Tetris in Python
Step past footstep guide to writing Tetris in Python with PyGame
In this tutorial, we will write a simple Tetris using the PyGame library in Python. The algorithms inside are pretty uncomplicated just can be a little challenging for the beginners. We volition not concentrate on PyGame mechanics too much, but rather focus on the game logic. If you are besides lazy to read all the stuff, you lot may just copy and paste the code in the stop.
Prerequisites
- Python3. This may be downloaded from the official website.
- PyGame. Go to your command prompt or last, depending on the Os y'all are using, and type
pip install pygame
orpip3 install pygame
. - Basic noesis of Python. Come across my other manufactures for that if needed.
You may experience issues with installing PyGame or Python itself, but this is out of scope here. Please refer to StackOverflow for that :)
I personally experienced a problem on Mac with having anything displayed on the screen, and installing some specific version of PyGame solved the problem: pip install pygame==2.0.0.dev4
.
The Effigy Class
We start with the Figures class. Our goal is to shop the effigy types together with the rotations. We could, of course, rotate them using matrix rotation, merely that can make it as well complex.
So, nosotros simply accept a listing of lists of figures like that:
class Effigy:
figures = [
[[ane, v, 9, thirteen], [four, 5, half-dozen, 7]],
[[1, 2, 5, nine], [0, 4, 5, 6], [1, five, ix, 8], [4, v, 6, 10]],
[[1, 2, half dozen, 10], [5, vi, 7, 9], [2, vi, 10, 11], [3, 5, six, vii]],
[[1, iv, 5, 6], [1, 4, 5, 9], [4, 5, 6, 9], [1, five, six, 9]],
[[i, 2, 5, 6]],
]
Where the main list contains figure types, and the inner lists contain their rotations. The numbers in each figure represent the positions in a 4x4 matrix where the figure is solid. For instance, the figure [1,5,9,13] represents a line. To improve understand that, please refer to the picture in a higher place.
As an exercise try to add some missing figures hither, namely the "z" figures.
The __init__
function would be as follows:
class Figure:
...
def __init__(cocky, ten, y):
self.ten = 10
self.y = y
self.type = random.randint(0, len(self.figures) - 1)
self.colour = random.randint(ane, len(colors) - 1)
self.rotation = 0
where we randomly choice a type and a color.
And we demand to quickly exist able to rotate and become the electric current rotation of a figure, for this we have these ii simple methods:
course Figure:
...
def image(self):
return cocky.figures[cocky.type][self.rotation] def rotate(cocky):
cocky.rotation = (cocky.rotation + ane) % len(self.figures[self.type])
The Tetris Form
We first initialize the Game with some variables:
course Tetris:
level = 2
score = 0
state = "start"
field = []
meridian = 0
width = 0
ten = 100
y = 60
zoom = 20
effigy = None
where the country tells us if nosotros are still playing a game or not. The field
is the field of the game that contains zeros where information technology is empty, and the colors where there are figures (except the one that is still flying down).
We initialize the game with the following elementary method:
class Tetris:
...
def __init__(self, pinnacle, width):
self.height = height
self.width = width
for i in range(height):
new_line = []
for j in range(width):
new_line.append(0)
self.field.append(new_line)
That creates a field with the size height x width
.
Creating a new figure and position it at coordinates (three,0) is simple:
course Tetris:
...
def new_figure(self):
self.figure = Figure(3, 0)
The more interesting function is to check if the currently flying figure intersecting with something fixed on the field. This may happen when the figure is moving left, right, down, or rotating.
class Tetris:
...
def intersects(cocky):
intersection = False
for i in range(4):
for j in range(4):
if i * four + j in cocky.effigy.image():
if i + self.figure.y > self.height - i or \
j + cocky.effigy.10 > cocky.width - 1 or \
j + self.figure.x < 0 or \
self.field[i + self.figure.y][j + cocky.figure.ten] > 0:
intersection = True
return intersection
It is pretty simple: we become and check each cell in the 4x4 matrix of the current Figure, whether it is out of game premises and whether it is touching some busy game field. We cheque if self.field[..][..] > 0
, because at that place may exist any colour. And if there is a null, that means that the field is empty, so in that location is no problem.
Having this function, we tin can now check if we are allowed to move or rotate the Effigy. If it moves down and intersects, then this means nosotros have reached the bottom, so nosotros need to "freeze" the figure on our field:
course Tetris:
...
def freeze(self):
for i in range(4):
for j in range(four):
if i * 4 + j in self.figure.image():
cocky.field[i + cocky.figure.y][j + self.figure.x] = cocky.figure.colour
cocky.break_lines()
self.new_figure()
if self.intersects():
game.land = "gameover"
After freezing, we have to check if at that place are some full horizontal lines that should be destroyed. Then we create a new Figure, and if it already intersects, so game over :)
Checking the full lines is relatively simple and straightforward, but pay attention to the fact that destroying a line goes from the bottom to the top:
grade Tetris:
...
def break_lines(self):
lines = 0
for i in range(ane, self.height):
zeros = 0
for j in range(self.width):
if self.field[i][j] == 0:
zeros += ane
if zeros == 0:
lines += 1
for i1 in range(i, 1, -1):
for j in range(self.width):
self.field[i1][j] = self.field[i1 - 1][j]
self.score += lines ** 2
At present, we are missing the moving methods:
class Tetris:
...
def go_space(self):
while non self.intersects():
cocky.effigy.y += ane
self.figure.y -= ane
self.freeze() def go_down(self):
self.figure.y += i
if cocky.intersects():
self.figure.y -= ane
self.freeze()
def go_side(self, dx):
old_x = cocky.effigy.x
self.figure.x += dx
if self.intersects():
self.figure.ten = old_x
def rotate(cocky):
old_rotation = self.effigy.rotation
self.figure.rotate()
if self.intersects():
self.effigy.rotation = old_rotation
Every bit you can see, thego_space
method practically duplicates the go_down
method, but information technology goes downwards until it reaches the bottom or some fixed figure.
And in every method, we retrieve the concluding position, change the coordinates, and cheque if there is an intersection. If there is, we return to the previous land.
PyGame and the Complete Code
We are about done!
There is some simple logic left, which is the game loop and the PyGame stuff. So, permit's run across at the consummate lawmaking now:
Try to re-create and paste information technology into a py
file. Run and enjoy the game! :)
And don't forget to share it with your friends!
Source: https://levelup.gitconnected.com/writing-tetris-in-python-2a16bddb5318
Posted by: hayeswhearding.blogspot.com
0 Response to "How To Download Tetris On Ti-84 Plus"
Post a Comment