aboutsummaryrefslogtreecommitdiff
path: root/turtetris_master/game.py
diff options
context:
space:
mode:
authorKarel Kočí <karel.koci@nic.cz>2017-07-20 10:06:34 +0200
committerKarel Kočí <karel.koci@nic.cz>2017-07-20 10:59:25 +0200
commit1784c9bc91ea5cc63a85887d813fe37906a8234d (patch)
treed3ec44c2ca873643724b1c61cc8fa7a8ef7cf60f /turtetris_master/game.py
parentebe88b130fa830b572f83a7fb7ac190a37f70c7c (diff)
downloadturris-tetris-1784c9bc91ea5cc63a85887d813fe37906a8234d.tar.gz
turris-tetris-1784c9bc91ea5cc63a85887d813fe37906a8234d.tar.bz2
turris-tetris-1784c9bc91ea5cc63a85887d813fe37906a8234d.zip
Add movement
Diffstat (limited to 'turtetris_master/game.py')
-rw-r--r--turtetris_master/game.py56
1 files changed, 49 insertions, 7 deletions
diff --git a/turtetris_master/game.py b/turtetris_master/game.py
index 2d646b7..1da176e 100644
--- a/turtetris_master/game.py
+++ b/turtetris_master/game.py
@@ -43,10 +43,10 @@ class Game:
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.mx[i] = [0]*matrix.height
self.stone_next = SHAPES[randrange(len(SHAPES))][:]
# Don't have to check result as it should always be successful
- self.new_stone()
+ assert self.new_stone()
self.step = 0
self.step_edge = 60
matrix.display()
@@ -77,6 +77,7 @@ class Game:
COLORS[self.stone_next[x][y]])
else:
self.matrix.pixel(11 - x, 9 - y, 'black')
+ return True
def __render_stone__(self):
"Render stone"
@@ -98,29 +99,70 @@ class Game:
def __check_collision__(self, x, y, stone):
"Check if stone collides. Returns True of so."
+ for a in range(len(stone)):
+ for b in range(len(stone[a])):
+ sx = 11 - a - x
+ sy = 9 - b - y
+ if stone[a][b] != 0 and (
+ sx < 0 or sy < 0 or sx > 11 or sy > 9 or
+ self.mx[sx][sy] != 0):
+ return True
return False
+ def __place__(self):
+ "Stone can't move so place it, check lines and generate new one"
+ for x in range(len(self.stone)):
+ for y in range(len(self.stone[x])):
+ if self.stone[x][y] != 0:
+ self.mx[11 - x - self.stone_x][9 - y - self.stone_y] =\
+ self.stone[x][y]
+ # TODO Line completion and removal and step division
+ return self.new_stone()
+
def __down__(self):
"Move stone down"
- pass
+ new_y = self.stone_y + 1
+ if self.__check_collision__(self.stone_x, new_y, self.stone):
+ return self.__place__()
+ else:
+ self.__clear_stone__()
+ self.stone_y = new_y
+ self.__render_stone__()
def __rotate__(self):
"Rotate stone"
- pass
+ rotated = [
+ [self.stone[y][x] for y in range(len(self.stone))]
+ for x in range(len(self.stone[0]) - 1, -1, -1)]
+ if not self.__check_collision__(self.stone_x, self.stone_y, rotated):
+ self.stone = rotated
+ self.__clear_stone__()
+ self.__render_stone__()
def __move__(self, left):
"Move stone left or right"
- pass
+ new_x = self.stone_x
+ if left:
+ new_x += 1
+ else:
+ new_x -= 1
+ if not self.__check_collision__(new_x, self.stone_y, self.stone):
+ self.__clear_stone__()
+ self.stone_x = new_x
+ self.__render_stone__()
def tick(self, input):
"Do game tick"
+ gameover = False
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__()
+ gameover = self.__down__()
self.step = 0
+ else:
+ self.step += 1
self.matrix.display()
- return not input['select']
+ return not gameover