-
Notifications
You must be signed in to change notification settings - Fork 0
/
Display.py
174 lines (136 loc) · 5.11 KB
/
Display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys
sys.path.append('/home/pi/rpi_rgb_led_matrix/bindings/python/samples')
from samplebase import SampleBase
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
from Playground import Playground
from Player import Player
from enums.PlaygroundTile import PlaygroundTile
from enums.GameStatus import GameStatus
from typing import List
import datetime
import threading
import time
import numpy as np
class Display(SampleBase):
def __init__(self, *args, **kwargs):
"""
Constructur based on SampleBase.py.
"""
super(Display, self).__init__(self, *args, **kwargs)
self.playground = Playground(20, 20)
self.player = Player(self.playground)
self._running = True
def terminate(self) -> None:
"""
Function to stop the display loop
"""
self._running = False
def setPlayground(self, playground: Playground) -> None:
"""
Function to set the playground.
"""
self.playground = playground
def setPlayer(self, player: Player) -> None:
"""
Function to set the player.
"""
self.player = player
def getPlayground(self) -> List[List[PlaygroundTile]]:
"""
Function to get the playground matrix
:rtype: 2d list
:returns: playground matrix
"""
matrix = self.playground.getPlaygroundMatrix()
"""out = [None]*len(matrix)
N = len(matrix)
M = len(matrix[0])
for i in range(0, len(matrix)):
out[i] = matrix[i]
out = np.rot90(out, 2)"""
return matrix
def run(self) -> None:
"""
Function to display the game
"""
offset_canvas = self.matrix.CreateFrameCanvas()
font = graphics.Font()
font.LoadFont("/home/pi/rpi_rgb_led_matrix/fonts/5x8.bdf")
textColor = graphics.Color(255, 255, 0)
pos = offset_canvas.width
my_text = "tetris.informatik.fh-swf.de"
while self._running:
self.draw(offset_canvas)
length = graphics.DrawText(offset_canvas, font, pos, 60, textColor, my_text)
pos -= 1
if (pos + length < 0):
pos = offset_canvas.width
time.sleep(0.03)
offset_canvas = self.matrix.SwapOnVSync(offset_canvas)
time_now = datetime.datetime.now()
while time_now > datetime.datetime.now() - datetime.timedelta(seconds=3):
graphics.DrawText(offset_canvas, font, int(32 - (len("Game Over") * 5) / 2), 25, textColor, "Game Over!")
offset_canvas = self.matrix.SwapOnVSync(offset_canvas)
def draw(self, offset_canvas) -> None:
"""
Function to draw the playground
"""
matrix = self.getPlayground()
offsetX = 32 - (int(len(self.getPlayground())) * 2) / 2
offsetY = 20
offset_canvas.Clear()
font = graphics.Font()
font.LoadFont("/home/pi/rpi_rgb_led_matrix/fonts/4x6.bdf")
text_length = len("Score: " + str(self.player.getScore())) * 4
graphics.DrawText(
offset_canvas,
font,
32 - (int(text_length / 2)), 15,
graphics.Color(255, 255, 255),
"Score: " + str(self.player.getScore())
)
for row in range(0, len(matrix)):
for col in range(0, len(matrix[0])):
# width - height - endwidth - endheight
color: tuple = (0, 0, 0)
brightness = 1
brightness_background = 0.2
if matrix[row][col] == PlaygroundTile.VOID:
color = (
56 * brightness_background,
65 * brightness_background,
242 * brightness_background
)
elif matrix[row][col] == PlaygroundTile.SNAKE:
color = (
248 * brightness,
255 * brightness,
4 * brightness
)
elif matrix[row][col] == PlaygroundTile.FOOD:
color = (
255 * brightness,
4 * brightness,
21 * brightness
)
elif matrix[row][col] == PlaygroundTile.TEXT:
color = (
255 * brightness,
255 * brightness,
255 * brightness
)
elif matrix[row][col] == PlaygroundTile.WALL:
color = (
255 * brightness,
255 * brightness,
255 * brightness
)
for i in range(0, 2):
for k in range(0, 2):
offset_canvas.SetPixel(
col * 2 + k + offsetX,
row * 2 + i + offsetY,
color[0],
color[1],
color[2]
)