Skip to content

Commit c6e0ac6

Browse files
committed
python/ledris: Allow to be imported
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 054722d commit c6e0ac6

File tree

1 file changed

+132
-122
lines changed

1 file changed

+132
-122
lines changed

python/ledris.py

Lines changed: 132 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
from inputmodule.gui.ledmatrix import show_string
1010
from inputmodule.inputmodule import ledmatrix
1111

12-
# Initialize pygame
13-
pygame.init()
14-
1512
# Set the screen width and height for a 34 x 9 block Ledris game
1613
block_width = 20
1714
block_height = 20
@@ -25,12 +22,6 @@
2522
black = (0, 0, 0)
2623
white = (255, 255, 255)
2724

28-
# Create the screen
29-
screen = pygame.display.set_mode((width, height))
30-
31-
# Clock to control the speed of the game
32-
clock = pygame.time.Clock()
33-
3425
# Ledrimino shapes
3526
shapes = [
3627
[[1, 1, 1, 1]], # I shape
@@ -63,25 +54,6 @@ def draw_ledmatrix(board, devices):
6354
#vals = [0 for _ in range(39)]
6455
#send_command(dev, CommandVals.Draw, vals)
6556

66-
# Function to draw the game based on the board state
67-
def draw_board(board, devices):
68-
draw_ledmatrix(board, devices)
69-
screen.fill(white)
70-
for y in range(rows):
71-
for x in range(cols):
72-
if board[y][x]:
73-
rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height)
74-
pygame.draw.rect(screen, black, rect)
75-
draw_grid()
76-
pygame.display.update()
77-
78-
# Function to draw a grid
79-
def draw_grid():
80-
for y in range(rows):
81-
for x in range(cols):
82-
rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height)
83-
pygame.draw.rect(screen, black, rect, 1)
84-
8557
# Function to check if the position is valid
8658
def check_collision(board, shape, offset):
8759
off_x, off_y = offset
@@ -139,106 +111,144 @@ def display_score(board, score):
139111
[[1, 1, 1], [1, 0, 1], [1, 1, 1], [0, 0, 1], [1, 1, 1]], # 9
140112
]
141113

142-
# Main game function
143-
def gameLoop(devices):
144-
board = [[0 for _ in range(cols)] for _ in range(rows)]
145-
current_shape = random.choice(shapes)
146-
current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display
147-
game_over = False
148-
fall_time = 0
149-
fall_speed = 500 # Falling speed in milliseconds
150-
score = 0
151-
152-
while not game_over:
153-
# Adjust falling speed based on score
154-
fall_speed = max(100, 500 - (score * 10))
155-
156-
# Draw the current board state
114+
115+
class Ledris:
116+
# Function to draw a grid
117+
def draw_grid(self):
118+
for y in range(rows):
119+
for x in range(cols):
120+
rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height)
121+
pygame.draw.rect(self.screen, black, rect, 1)
122+
123+
# Function to draw the game based on the board state
124+
def draw_board(self, board, devices):
125+
draw_ledmatrix(board, devices)
126+
self.screen.fill(white)
127+
for y in range(rows):
128+
for x in range(cols):
129+
if board[y][x]:
130+
rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height)
131+
pygame.draw.rect(self.screen, black, rect)
132+
self.draw_grid()
133+
pygame.display.update()
134+
135+
# Main game function
136+
def gameLoop(self, devices):
137+
board = [[0 for _ in range(cols)] for _ in range(rows)]
138+
current_shape = random.choice(shapes)
139+
current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display
140+
game_over = False
141+
fall_time = 0
142+
fall_speed = 500 # Falling speed in milliseconds
143+
score = 0
144+
145+
while not game_over:
146+
# Adjust falling speed based on score
147+
fall_speed = max(100, 500 - (score * 10))
148+
149+
# Draw the current board state
150+
board_state = get_board_state(board, current_shape, current_pos)
151+
display_score(board_state, score)
152+
self.draw_board(board_state, devices)
153+
154+
# Event handling
155+
for event in pygame.event.get():
156+
if event.type == pygame.QUIT:
157+
game_over = True
158+
159+
if event.type == pygame.KEYDOWN:
160+
if event.key in [pygame.K_LEFT, pygame.K_h]:
161+
new_pos = [current_pos[0] - 1, current_pos[1]]
162+
if not check_collision(board, current_shape, new_pos):
163+
current_pos = new_pos
164+
elif event.key in [pygame.K_RIGHT, pygame.K_l]:
165+
new_pos = [current_pos[0] + 1, current_pos[1]]
166+
if not check_collision(board, current_shape, new_pos):
167+
current_pos = new_pos
168+
elif event.key in [pygame.K_DOWN, pygame.K_j]:
169+
new_pos = [current_pos[0], current_pos[1] + 1]
170+
if not check_collision(board, current_shape, new_pos):
171+
current_pos = new_pos
172+
elif event.key in [pygame.K_UP, pygame.K_k]:
173+
rotated_shape = list(zip(*current_shape[::-1]))
174+
if not check_collision(board, rotated_shape, current_pos):
175+
current_shape = rotated_shape
176+
elif event.key == pygame.K_SPACE: # Hard drop
177+
while not check_collision(board, current_shape, [current_pos[0], current_pos[1] + 1]):
178+
current_pos[1] += 1
179+
180+
# Automatic falling
181+
fall_time += self.clock.get_time()
182+
if fall_time >= fall_speed:
183+
fall_time = 0
184+
new_pos = [current_pos[0], current_pos[1] + 1]
185+
if not check_collision(board, current_shape, new_pos):
186+
current_pos = new_pos
187+
else:
188+
merge_shape(board, current_shape, current_pos)
189+
board, cleared_rows = clear_rows(board)
190+
score += cleared_rows # Increase score by one for each row cleared
191+
current_shape = random.choice(shapes)
192+
current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display
193+
if check_collision(board, current_shape, current_pos):
194+
game_over = True
195+
196+
self.clock.tick(30)
197+
198+
# Flash the screen twice before waiting for restart
199+
for _ in range(2):
200+
for dev in devices:
201+
ledmatrix.percentage(dev, 0)
202+
self.screen.fill(black)
203+
pygame.display.update()
204+
time.sleep(0.3)
205+
206+
for dev in devices:
207+
ledmatrix.percentage(dev, 100)
208+
self.screen.fill(white)
209+
pygame.display.update()
210+
time.sleep(0.3)
211+
212+
# Display final score and wait for restart without clearing the screen
157213
board_state = get_board_state(board, current_shape, current_pos)
158214
display_score(board_state, score)
159-
draw_board(board_state, devices)
160-
161-
# Event handling
162-
for event in pygame.event.get():
163-
if event.type == pygame.QUIT:
164-
game_over = True
165-
166-
if event.type == pygame.KEYDOWN:
167-
if event.key in [pygame.K_LEFT, pygame.K_h]:
168-
new_pos = [current_pos[0] - 1, current_pos[1]]
169-
if not check_collision(board, current_shape, new_pos):
170-
current_pos = new_pos
171-
elif event.key in [pygame.K_RIGHT, pygame.K_l]:
172-
new_pos = [current_pos[0] + 1, current_pos[1]]
173-
if not check_collision(board, current_shape, new_pos):
174-
current_pos = new_pos
175-
elif event.key in [pygame.K_DOWN, pygame.K_j]:
176-
new_pos = [current_pos[0], current_pos[1] + 1]
177-
if not check_collision(board, current_shape, new_pos):
178-
current_pos = new_pos
179-
elif event.key in [pygame.K_UP, pygame.K_k]:
180-
rotated_shape = list(zip(*current_shape[::-1]))
181-
if not check_collision(board, rotated_shape, current_pos):
182-
current_shape = rotated_shape
183-
elif event.key == pygame.K_SPACE: # Hard drop
184-
while not check_collision(board, current_shape, [current_pos[0], current_pos[1] + 1]):
185-
current_pos[1] += 1
186-
187-
# Automatic falling
188-
fall_time += clock.get_time()
189-
if fall_time >= fall_speed:
190-
fall_time = 0
191-
new_pos = [current_pos[0], current_pos[1] + 1]
192-
if not check_collision(board, current_shape, new_pos):
193-
current_pos = new_pos
194-
else:
195-
merge_shape(board, current_shape, current_pos)
196-
board, cleared_rows = clear_rows(board)
197-
score += cleared_rows # Increase score by one for each row cleared
198-
current_shape = random.choice(shapes)
199-
current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display
200-
if check_collision(board, current_shape, current_pos):
215+
self.draw_board(board_state, devices)
216+
217+
waiting = True
218+
while waiting:
219+
for event in pygame.event.get():
220+
if event.type == pygame.QUIT:
221+
waiting = False
201222
game_over = True
223+
if event.type == pygame.KEYDOWN:
224+
if event.key == pygame.K_q:
225+
waiting = False
226+
if event.key == pygame.K_r:
227+
board = [[0 for _ in range(cols)] for _ in range(rows)]
228+
gameLoop()
202229

203-
clock.tick(30)
230+
pygame.quit()
231+
quit()
204232

205-
# Flash the screen twice before waiting for restart
206-
for _ in range(2):
207-
for dev in devices:
208-
ledmatrix.percentage(dev, 0)
209-
screen.fill(black)
210-
pygame.display.update()
211-
time.sleep(0.3)
233+
def __init__(self):
234+
# Initialize pygame
235+
pygame.init()
212236

213-
for dev in devices:
214-
ledmatrix.percentage(dev, 100)
215-
screen.fill(white)
216-
pygame.display.update()
217-
time.sleep(0.3)
218-
219-
# Display final score and wait for restart without clearing the screen
220-
board_state = get_board_state(board, current_shape, current_pos)
221-
display_score(board_state, score)
222-
draw_board(board_state, devices)
223-
224-
waiting = True
225-
while waiting:
226-
for event in pygame.event.get():
227-
if event.type == pygame.QUIT:
228-
waiting = False
229-
game_over = True
230-
if event.type == pygame.KEYDOWN:
231-
if event.key == pygame.K_q:
232-
waiting = False
233-
if event.key == pygame.K_r:
234-
board = [[0 for _ in range(cols)] for _ in range(rows)]
235-
gameLoop()
237+
# Create the screen
238+
self.screen = pygame.display.set_mode((width, height))
236239

237-
pygame.quit()
238-
quit()
240+
# Clock to control the speed of the game
241+
self.clock = pygame.time.Clock()
239242

240-
if __name__ == "__main__":
243+
def main_devices(devices):
244+
ledris = Ledris()
245+
ledris.gameLoop(devices)
246+
247+
def main():
241248
devices = cli.find_devs()
242-
for dev in devices:
243-
show_string(dev, 'YAY')
244-
gameLoop(devices)
249+
250+
ledris = Ledris()
251+
ledris.gameLoop(devices)
252+
253+
if __name__ == "__main__":
254+
main()

0 commit comments

Comments
 (0)