From ebe88b130fa830b572f83a7fb7ac190a37f70c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Wed, 19 Jul 2017 19:56:09 +0200 Subject: Implement game --- do.sh | 6 ++ turtetris_master/game.py | 118 +++++++++++++++++++++++++++++++++++++- turtetris_master/led_output.py | 6 ++ turtetris_master/state_machine.py | 2 + 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100755 do.sh diff --git a/do.sh b/do.sh new file mode 100755 index 0000000..8956f19 --- /dev/null +++ b/do.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +git add . +git ci --amend -C HEAD +git rev-parse HEAD | clip +git push -f diff --git a/turtetris_master/game.py b/turtetris_master/game.py index 9b0c7cf..2d646b7 100644 --- a/turtetris_master/game.py +++ b/turtetris_master/game.py @@ -1,10 +1,126 @@ +from random import randrange + + +SHAPES = [ + [[1, 1, 1], + [0, 1, 0]], + + [[0, 2, 2], + [2, 2, 0]], + + [[3, 3, 0], + [0, 3, 3]], + + [[4, 0, 0], + [4, 4, 4]], + + [[0, 0, 5], + [5, 5, 5]], + + [[6, 6, 6, 6]], + + [[7, 7], + [7, 7]] +] + +COLORS = [ + 'black', + 'FF5500', + '64C873', + '786CF5', + 'FF8C32', + '327834', + '92CA49', + '96A1DA' +] + + class Game: "game it self" def __init__(self, matrix): self.matrix = matrix + matrix.fill('black') # Clear game area + self.mx = [None]*(matrix.width - 3) + for i in range(len(self.mx)): + self.mx[i] = [None]*matrix.height + self.stone_next = SHAPES[randrange(len(SHAPES))][:] + # Don't have to check result as it should always be successful + self.new_stone() + self.step = 0 + self.step_edge = 60 + matrix.display() + + def new_stone(self): + "Create new stone to next one and move next one to stone" + self.stone = self.stone_next + self.stone_next = SHAPES[randrange(len(SHAPES))][:] # Note: we do copy + # Render stone on top + self.stone_x = 3 + self.stone_y = 0 + if self.__check_collision__(self.stone_x, self.stone_y, self.stone): + # locate different place + self.stone_x = 0 + while self.stone_x < (self.matrix.width - 3) and \ + self.__check_collision__(self.stone_x, self.stone_y, + self.stone): + self.stone_x += 1 + if self.stone_x >= (self.matrix.width - 3): + # Than game over + return False + self.__render_stone__() + # Render next stone + for x in range(2): + for y in range(4): + if x < len(self.stone_next) and y < len(self.stone_next[x]): + self.matrix.pixel(11 - x, 9 - y, + COLORS[self.stone_next[x][y]]) + else: + self.matrix.pixel(11 - x, 9 - y, 'black') + + def __render_stone__(self): + "Render stone" + for x in range(len(self.stone)): + for y in range(len(self.stone[x])): + if self.stone[x][y] != 0: + self.matrix.pixel(11 - x - 3 - self.stone_x, + 9 - y - self.stone_y, + COLORS[self.stone[x][y]]) + + def __clear_stone__(self): + "Clear rendered stone" + for x in range(len(self.stone)): + for y in range(len(self.stone[x])): + if self.stone[x][y] != 0: + self.matrix.pixel(11 - x - 3 - self.stone_x, + 9 - y - self.stone_y, + 'black') + + def __check_collision__(self, x, y, stone): + "Check if stone collides. Returns True of so." + return False + + def __down__(self): + "Move stone down" + pass + + def __rotate__(self): + "Rotate stone" + pass + + def __move__(self, left): + "Move stone left or right" pass def tick(self, input): "Do game tick" - return True + if input['up']: + self.__rotate__() + if input['left'] != input['right']: + self.__move__(input['left']) + if self.step >= self.step_edge or \ + (input['down'] and self.step >= self.step_edge/2): + self.__down__() + self.step = 0 + self.matrix.display() + return not input['select'] diff --git a/turtetris_master/led_output.py b/turtetris_master/led_output.py index 443c44f..1b3c435 100644 --- a/turtetris_master/led_output.py +++ b/turtetris_master/led_output.py @@ -29,3 +29,9 @@ class Matrix: if x < 0 or x > 11 or y < 0 or y > 9: raise Exception('Pixel out of matrix') self.__mat__[y][x] = color + + def fill(self, color): + "Fill whole matrix with given color" + for x in range(self.width): + for y in range(self.height): + self.pixel(x, y, color) diff --git a/turtetris_master/state_machine.py b/turtetris_master/state_machine.py index cef03e4..182ce7b 100644 --- a/turtetris_master/state_machine.py +++ b/turtetris_master/state_machine.py @@ -30,6 +30,8 @@ class StateMachine: elif state == "game-over": if self.state != "game": __exception__() + self.matrix.fill('red') + self.matrix.display() else: __exception__() self.state = state -- cgit v1.2.3