refactorize using inheritance

This commit is contained in:
adrienmalin 2019-09-28 04:24:04 +02:00
parent dcac72251f
commit 30e5bf30c3
2 changed files with 145 additions and 151 deletions

View File

@ -13,7 +13,7 @@ python -m pip install --user arcade
"""
)
from gamelogic import GameLogic, Status, Movement, Rotation
from tetris import Tetris, Status
# Constants
@ -89,11 +89,12 @@ GHOST_ALPHA = 50
MATRIX_SRITE_ALPHA = 100
class TetrArcade(arcade.Window):
class TetrArcade(Tetris, arcade.Window):
def __init__(self):
super().__init__()
locale.setlocale(locale.LC_ALL, '')
self.game = GameLogic(self)
self.actions = {
Status.PLAYING: {
@ -129,7 +130,11 @@ class TetrArcade(arcade.Window):
}
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
super().__init__(
self.highlight_texts = []
self.pressed_actions = []
arcade.Window.__init__(
self,
width = WINDOW_WIDTH,
height = WINDOW_HEIGHT,
title = WINDOW_TITLE,
@ -153,112 +158,45 @@ class TetrArcade(arcade.Window):
anchor_x = 'right'
)
self.new_game()
def on_resize(self, width, height):
center_x = self.width / 2
center_y = self.height / 2
self.bg_sprite.center_x = center_x
self.bg_sprite.center_y = center_y
self.matrix_sprite.center_x = center_x
self.matrix_sprite.center_y = center_y
self.matrix_sprite.left = int(self.matrix_sprite.left)
self.matrix_sprite.top = int(self.matrix_sprite.top)
self.load_matrix()
def new_game(self):
self.pressed_actions = []
self.highlight_texts = []
self.pressed_actions = []
self.auto_repeat = False
arcade.schedule(self.clock, 1)
self.game.new_game()
self.new_current_piece()
super().new_game()
def new_current_piece(self):
self.load_next_pieces()
self.load_current_piece()
self.load_matrix()
super().new_current_piece()
self.reload_next_pieces()
self.reload_current_piece()
self.reload_matrix()
if self.pressed_actions:
self.stop_autorepeat()
arcade.schedule(self.repeat_action, AUTOREPEAT_DELAY)
def move_left(self, delta_time=0):
self.game.move(Movement.LEFT)
def lock(self, _=0):
super().lock()
self.reload_matrix()
self.reload_next_pieces()
self.reload_current_piece()
def move_right(self, delta_time=0):
self.game.move(Movement.RIGHT)
def swap(self, _=0):
super().swap()
self.reload_held_piece()
self.reload_current_piece()
def soft_drop(self, delta_time=0):
self.game.soft_drop()
def hard_drop(self, delta_time=0):
self.game.hard_drop()
self.lock()
def rotate_counterclockwise(self, delta_time=0):
self.game.rotate(Rotation.COUNTERCLOCKWISE)
def rotate_clockwise(self, delta_time=0):
self.game.rotate(Rotation.CLOCKWISE)
def fall(self, delta_time=0):
self.game.move(Movement.DOWN)
def start_fall(self):
arcade.schedule(self.fall, self.game.fall_delay)
def stop_fall(self):
arcade.unschedule(self.fall)
def prelock(self, restart=False):
if restart:
self.cancel_prelock()
arcade.schedule(self.lock, self.game.lock_delay)
def cancel_prelock(self):
arcade.unschedule(self.lock)
def lock(self, delta_time=0):
self.game.lock()
self.load_matrix()
self.load_next_pieces()
self.load_current_piece()
if self.game.status == Status.PLAYING:
self.new_current_piece()
elif self.game.status == Status.OVER:
self.game_over()
def swap(self, delta_time=0):
self.game.swap()
self.load_held_piece()
self.load_current_piece()
def pause(self, delta_time=0):
self.game.status = Status.PAUSED
self.stop_fall()
self.cancel_prelock()
arcade.unschedule(self.clock)
def pause(self, _=0):
super().pause()
self.pressed_actions = []
self.stop_autorepeat()
def resume(self, delta_time=0):
self.highlight_texts = []
self.game.status = Status.PLAYING
self.start_fall()
if self.game.current_piece.prelocked:
arcade.schedule(self.lock, self.game.lock_delay)
arcade.schedule(self.clock, 1)
def game_over(self):
arcade.unschedule(self.repeat_action)
self.cancel_prelock()
self.stop_fall()
arcade.unschedule(self.clock)
super().game_over()
self.unschedule(self.repeat_action)
def on_key_press(self, key, modifiers):
for key_or_modifier in (key, modifiers):
try:
action = self.actions[self.game.status][key_or_modifier]
action = self.actions[self.status][key_or_modifier]
except KeyError:
pass
else:
@ -270,7 +208,7 @@ class TetrArcade(arcade.Window):
def on_key_release(self, key, modifiers):
try:
action = self.actions[self.game.status][key]
action = self.actions[self.status][key]
except KeyError:
pass
else:
@ -297,21 +235,18 @@ class TetrArcade(arcade.Window):
def stop_autorepeat(self):
self.auto_repeat = False
arcade.unschedule(self.repeat_action)
self.unschedule(self.repeat_action)
def add_highlight_text(self, string):
self.highlight_texts.append(string)
arcade.schedule(self.del_highlight_text, HIGHLIGHT_TEXT_DISPLAY_DELAY)
def show_text(self, text):
self.highlight_texts.append(text)
self.schedule(self.del_highlight_text, HIGHLIGHT_TEXT_DISPLAY_DELAY)
def del_highlight_text(self, delta_time=0):
def del_highlight_text(self, _=0):
self.highlight_texts.pop(0)
if not self.highlight_texts:
arcade.unschedule(self.del_highlight_text)
self.unschedule(self.del_highlight_text)
def clock(self, delta_time=0):
self.game.time += delta_time
def load_piece(self, piece):
def reload_piece(self, piece):
piece_sprites = arcade.SpriteList()
for mino_position in piece.minoes_positions:
mino_sprite_path = MINOES_SPRITES_PATHS[piece.MINOES_COLOR]
@ -320,26 +255,26 @@ class TetrArcade(arcade.Window):
piece_sprites.append(mino_sprite)
return piece_sprites
def load_held_piece(self):
self.held_piece_sprites = self.load_piece(self.game.held_piece)
def reload_held_piece(self):
self.held_piece_sprites = self.reload_piece(self.held_piece)
def load_next_pieces(self):
def reload_next_pieces(self):
self.next_pieces_sprites = arcade.SpriteList()
for piece in self.game.next_pieces:
for piece in self.next_pieces:
for mino_position in piece.minoes_positions:
mino_sprite_path = MINOES_SPRITES_PATHS[piece.MINOES_COLOR]
mino_sprite = arcade.Sprite(mino_sprite_path)
mino_sprite.alpha = NORMAL_ALPHA
self.next_pieces_sprites.append(mino_sprite)
def load_current_piece(self):
self.current_piece_sprites = self.load_piece(self.game.current_piece)
self.ghost_piece_sprites = self.load_piece(self.game.ghost_piece)
def reload_current_piece(self):
self.current_piece_sprites = self.reload_piece(self.current_piece)
self.ghost_piece_sprites = self.reload_piece(self.ghost_piece)
def load_matrix(self):
if self.game.matrix:
def reload_matrix(self):
if self.matrix:
self.matrix_minoes_sprites = arcade.SpriteList()
for y, line in enumerate(self.game.matrix):
for y, line in enumerate(self.matrix):
for x, mino_color in enumerate(line):
if mino_color:
mino_sprite_path = MINOES_SPRITES_PATHS[mino_color]
@ -362,25 +297,25 @@ class TetrArcade(arcade.Window):
arcade.start_render()
self.bg_sprite.draw()
self.matrix_sprite.draw()
if not self.game.status == Status.PAUSED:
if not self.status == Status.PAUSED:
self.matrix_minoes_sprites.draw()
self.update_piece(self.game.held_piece, self.held_piece_sprites)
self.update_piece(self.held_piece, self.held_piece_sprites)
self.held_piece_sprites.draw()
self.update_piece(self.game.current_piece, self.current_piece_sprites)
if self.game.current_piece.prelocked:
alpha = PRELOCKED_ALPHA if self.game.current_piece.prelocked else NORMAL_ALPHA
self.update_piece(self.current_piece, self.current_piece_sprites)
if self.current_piece.prelocked:
alpha = PRELOCKED_ALPHA if self.current_piece.prelocked else NORMAL_ALPHA
for mino_sprite in self.current_piece_sprites:
mino_sprite.alpha = alpha
self.current_piece_sprites.draw()
self.update_piece(self.game.ghost_piece, self.ghost_piece_sprites)
self.update_piece(self.ghost_piece, self.ghost_piece_sprites)
for mino_sprite in self.ghost_piece_sprites:
mino_sprite.alpha = GHOST_ALPHA
self.ghost_piece_sprites.draw()
for n, piece in enumerate(self.game.next_pieces):
for n, piece in enumerate(self.next_pieces):
for mino_sprite, mino_position in zip(
self.next_pieces_sprites[4*n:4*(n+1)], piece.minoes_positions
):
@ -394,15 +329,15 @@ class TetrArcade(arcade.Window):
self.matrix_sprite.left - TEXT_MARGIN,
self.matrix_sprite.bottom
)
t = time.localtime(self.game.time)
t = time.localtime(self.time)
for y, text in enumerate(
(
"{:n}".format(self.game.nb_lines),
"{:n}".format(self.game.goal),
"{:n}".format(self.game.level),
"{:n}".format(self.nb_lines),
"{:n}".format(self.goal),
"{:n}".format(self.level),
"{:02d}:{:02d}:{:02d}".format(t.tm_hour-1, t.tm_min, t.tm_sec),
"{:n}".format(self.game.high_score),
"{:n}".format(self.game.score)
"{:n}".format(self.high_score),
"{:n}".format(self.score)
),
start=14
):
@ -420,7 +355,7 @@ class TetrArcade(arcade.Window):
Status.PLAYING: self.highlight_texts[0] if self.highlight_texts else "",
Status.PAUSED: PAUSE_TEXT,
Status.OVER: GAME_OVER_TEXT
}.get(self.game.status, "")
}.get(self.status, "")
if highlight_text:
arcade.draw_text(
text = highlight_text,
@ -434,9 +369,27 @@ class TetrArcade(arcade.Window):
anchor_y = 'center'
)
def on_resize(self, width, height):
center_x = self.width / 2
center_y = self.height / 2
self.bg_sprite.center_x = center_x
self.bg_sprite.center_y = center_y
self.matrix_sprite.center_x = center_x
self.matrix_sprite.center_y = center_y
self.matrix_sprite.left = int(self.matrix_sprite.left)
self.matrix_sprite.top = int(self.matrix_sprite.top)
self.reload_matrix()
def schedule(self, task, period):
arcade.schedule(task, period)
def unschedule(self, task):
arcade.unschedule(task)
def main():
TetrArcade()
tetrarcade = TetrArcade()
tetrarcade.new_game()
arcade.run()
if __name__ == "__main__":

View File

@ -167,7 +167,7 @@ class Tetromino:
return cls.random_bag.pop()()
class GameLogic():
class Tetris():
T_SLOT = (Coord(-1, 1), Coord(1, 1), Coord(1, -1), Coord(-1, -1))
SCORES = (
@ -178,8 +178,7 @@ class GameLogic():
{"name": "TETRIS", T_Spin.NO_T_SPIN: 8}
)
def __init__(self, ui):
self.ui = ui
def __init__(self):
self.high_score = 0
self.status = Status.STARTING
self.matrix = []
@ -206,8 +205,8 @@ class GameLogic():
self.current_piece = None
self.held_piece = None
self.status = Status.PLAYING
self.schedule(self.clock, 1)
self.new_level()
self.new_current_piece()
def new_level(self):
self.level += 1
@ -216,7 +215,9 @@ class GameLogic():
self.fall_delay = pow(0.8 - ((self.level-1)*0.007), self.level-1)
if self.level > 15:
self.lock_delay = 0.5 * pow(0.9, self.level-15)
self.ui.add_highlight_text("Level\n{:n}".format(self.level))
self.show_text("Level\n{:n}".format(self.level))
self.schedule(self.drop, self.fall_delay)
self.new_current_piece()
def new_current_piece(self):
self.current_piece = self.next_pieces.pop(0)
@ -226,12 +227,10 @@ class GameLogic():
self.next_pieces.append(Tetromino())
for piece, position in zip (self.next_pieces, NEXT_PIECES_POSITIONS):
piece.position = position
if self.can_move(
if not self.can_move(
self.current_piece.position,
self.current_piece.minoes_positions
):
self.ui.start_fall()
else:
self.game_over()
def cell_is_free(self, position):
@ -251,7 +250,8 @@ class GameLogic():
potential_position = self.current_piece.position + movement
if self.can_move(potential_position, self.current_piece.minoes_positions):
if self.current_piece.prelocked:
self.ui.prelock(restart=True)
self.unschedule(self.lock)
self.schedule(self.lock, self.lock_delay)
self.current_piece.position = potential_position
self.current_piece.last_rotation_point_used = None
self.move_ghost()
@ -262,9 +262,15 @@ class GameLogic():
and movement == Movement.DOWN
):
self.current_piece.prelocked = True
self.ui.prelock()
self.schedule(self.lock, self.lock_delay)
return False
def move_left(self, delta_time=0):
self.move(Movement.LEFT)
def move_right(self, delta_time=0):
self.move(Movement.RIGHT)
def rotate(self, direction):
rotated_minoes_positions = tuple(
Coord(-direction*mino_position.y, direction*mino_position.x)
@ -277,7 +283,8 @@ class GameLogic():
potential_position = self.current_piece.position + liberty_degree
if self.can_move(potential_position, rotated_minoes_positions):
if self.current_piece.prelocked:
self.ui.prelock(restart=True)
self.unschedule(self.lock)
self.schedule(self.lock, self.lock_delay)
self.current_piece.position = potential_position
self.current_piece.minoes_positions = rotated_minoes_positions
self.current_piece.orientation = (
@ -289,6 +296,12 @@ class GameLogic():
else:
return False
def rotate_counterclockwise(self, delta_time=0):
self.rotate(Rotation.COUNTERCLOCKWISE)
def rotate_clockwise(self, delta_time=0):
self.rotate(Rotation.CLOCKWISE)
def move_ghost(self):
self.ghost_piece.position = self.current_piece.position
self.ghost_piece.minoes_positions = self.current_piece.minoes_positions
@ -298,6 +311,9 @@ class GameLogic():
):
self.ghost_piece.position += Movement.DOWN
def drop(self, _=0):
self.move(Movement.DOWN)
def add_to_score(self, ds):
self.score += ds
if self.score > self.high_score:
@ -311,26 +327,22 @@ class GameLogic():
return False
def hard_drop(self):
drop_score = 0
while self.move(Movement.DOWN, prelock_on_stuck=False):
drop_score += 2
self.add_to_score(drop_score)
return drop_score
self.add_to_score(2)
self.lock()
def lock(self):
if self.move(Movement.DOWN):
self.ui.cancel_prelock()
return
if all(
(mino_position + self.current_piece.position).y >= NB_LINES
for mino_position in self.current_piece.minoes_positions
):
self.current_piece.prelocked = False
self.game_over()
return
self.ui.stop_fall()
self.unschedule(self.lock)
for mino_position in self.current_piece.minoes_positions:
position = mino_position + self.current_piece.position
@ -392,21 +404,21 @@ class GameLogic():
lock_strings.append(str(ds))
if lock_strings:
self.ui.add_highlight_text("\n".join(lock_strings))
self.show_text("\n".join(lock_strings))
self.add_to_score(lock_score)
if self.goal <= 0:
self.unschedule(self.drop)
self.new_level()
else:
self.new_current_piece()
def swap(self):
if self.current_piece.hold_enabled:
self.current_piece.hold_enabled = False
self.current_piece.prelocked = False
self.ui.cancel_prelock()
self.ui.stop_fall()
self.unschedule(self.lock)
self.current_piece, self.held_piece = self.held_piece, self.current_piece
if self.held_piece.__class__ == Tetromino.I:
self.held_piece.position = HELD_I_POSITION
@ -417,10 +429,39 @@ class GameLogic():
self.current_piece.position = MATRIX_PIECE_INIT_POSITION
self.ghost_piece = self.current_piece.ghost()
self.move_ghost()
self.ui.start_fall()
else:
self.new_current_piece()
def pause(self, _=0):
self.status = Status.PAUSED
self.unschedule(self.drop)
self.unschedule(self.lock)
self.unschedule(self.clock)
self.pressed_actions = []
self.stop_autorepeat()
def resume(self, delta_time=0):
self.status = Status.PLAYING
self.schedule(self.drop, self.fall_delay)
if self.current_piece.prelocked:
self.schedule(self.lock, self.lock_delay)
self.schedule(self.clock, 1)
def clock(self, delta_time=1):
self.time += delta_time
def game_over(self):
self.status = Status.OVER
self.unschedule(self.lock)
self.unschedule(self.drop)
self.unschedule(self.clock)
def schedule(task, period):
raise NotImplementedError
def unschedule(self, task):
raise NotImplementedError
def show_text(self, text):
print(text)