Compare commits
2 Commits
f5225b3a55
...
4edad3c1cf
Author | SHA1 | Date | |
---|---|---|---|
4edad3c1cf | |||
d70173f87b |
112
tetrarcade.py
112
tetrarcade.py
@ -21,6 +21,7 @@ from tetrislogic import TetrisLogic, State
|
|||||||
WINDOW_WIDTH = 800
|
WINDOW_WIDTH = 800
|
||||||
WINDOW_HEIGHT = 600
|
WINDOW_HEIGHT = 600
|
||||||
WINDOW_TITLE = "TETRARCADE"
|
WINDOW_TITLE = "TETRARCADE"
|
||||||
|
BG_COLOR = (7, 11, 21)
|
||||||
|
|
||||||
# Delays (seconds)
|
# Delays (seconds)
|
||||||
HIGHLIGHT_TEXT_DISPLAY_DELAY = 0.7
|
HIGHLIGHT_TEXT_DISPLAY_DELAY = 0.7
|
||||||
@ -57,7 +58,7 @@ FONT_NAME = "joystix monospace.ttf"
|
|||||||
TEXT_MARGIN = 40
|
TEXT_MARGIN = 40
|
||||||
FONT_SIZE = 16
|
FONT_SIZE = 16
|
||||||
TEXT_HEIGHT = 20.8
|
TEXT_HEIGHT = 20.8
|
||||||
HIGHLIGHT_TEXT_FONT_SIZE = 20
|
HIGHLIGHT_TEXT_SIZE = 20
|
||||||
|
|
||||||
CONTROL_TEXT = """
|
CONTROL_TEXT = """
|
||||||
|
|
||||||
@ -71,7 +72,9 @@ HARD DROP SPACE
|
|||||||
ROTATE CLOCKWISE ↑
|
ROTATE CLOCKWISE ↑
|
||||||
ROTATE COUNTER Z
|
ROTATE COUNTER Z
|
||||||
HOLD C
|
HOLD C
|
||||||
|
FULLSCREEN F11
|
||||||
PAUSE ESC
|
PAUSE ESC
|
||||||
|
QUIT ALT+F4
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -98,6 +101,18 @@ TO PLAY
|
|||||||
AGAIN"""
|
AGAIN"""
|
||||||
|
|
||||||
|
|
||||||
|
class ResizableSprite(arcade.Sprite):
|
||||||
|
|
||||||
|
def __init__(self, path):
|
||||||
|
super().__init__(path)
|
||||||
|
self.init_width = self.width
|
||||||
|
self.init_height = self.height
|
||||||
|
|
||||||
|
def resize(self, ratio):
|
||||||
|
self.width = ratio * self.init_width
|
||||||
|
self.height = ratio * self.init_height
|
||||||
|
|
||||||
|
|
||||||
class MinoSprites(arcade.SpriteList):
|
class MinoSprites(arcade.SpriteList):
|
||||||
|
|
||||||
def __init__(self, matrix):
|
def __init__(self, matrix):
|
||||||
@ -109,12 +124,18 @@ class MinoSprites(arcade.SpriteList):
|
|||||||
mino.sprite.bottom = self.matrix.sprite.bottom + y*(mino.sprite.height-1)
|
mino.sprite.bottom = self.matrix.sprite.bottom + y*(mino.sprite.height-1)
|
||||||
mino.sprite.alpha = alpha
|
mino.sprite.alpha = alpha
|
||||||
|
|
||||||
|
def resize(self, ratio):
|
||||||
|
for sprite in self:
|
||||||
|
sprite.resize(ratio)
|
||||||
|
self.update(sprite)
|
||||||
|
|
||||||
|
|
||||||
class MatrixSprites(MinoSprites):
|
class MatrixSprites(MinoSprites):
|
||||||
|
|
||||||
def __init__(self, matrix):
|
def update(self):
|
||||||
super().__init__(matrix)
|
for sprite in self.matrix.sprites:
|
||||||
for y, line in enumerate(matrix):
|
self.matrix.sprite.remove(sprite)
|
||||||
|
for y, line in enumerate(self.matrix):
|
||||||
for x, mino in enumerate(line):
|
for x, mino in enumerate(line):
|
||||||
if mino:
|
if mino:
|
||||||
self.update_mino(mino, x, y, NORMAL_ALPHA)
|
self.update_mino(mino, x, y, NORMAL_ALPHA)
|
||||||
@ -129,8 +150,7 @@ class TetrominoSprites(MinoSprites):
|
|||||||
path = MINOES_SPRITES_PATHS[tetromino.MINOES_COLOR]
|
path = MINOES_SPRITES_PATHS[tetromino.MINOES_COLOR]
|
||||||
self.alpha = alpha
|
self.alpha = alpha
|
||||||
for mino in tetromino:
|
for mino in tetromino:
|
||||||
mino.sprite = arcade.Sprite(path)
|
mino.sprite = ResizableSprite(path)
|
||||||
mino.sprite.alpha = alpha
|
|
||||||
self.append(mino.sprite)
|
self.append(mino.sprite)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
@ -153,7 +173,8 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
self.KEY_MAP = {
|
self.KEY_MAP = {
|
||||||
State.STARTING: {
|
State.STARTING: {
|
||||||
arcade.key.ENTER: self.new_game
|
arcade.key.ENTER: self.new_game,
|
||||||
|
arcade.key.F11: self.toogle_fullscreen
|
||||||
},
|
},
|
||||||
State.PLAYING: {
|
State.PLAYING: {
|
||||||
arcade.key.LEFT: self.move_left,
|
arcade.key.LEFT: self.move_left,
|
||||||
@ -177,29 +198,35 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
arcade.key.NUM_0: self.swap,
|
arcade.key.NUM_0: self.swap,
|
||||||
arcade.key.ESCAPE: self.pause,
|
arcade.key.ESCAPE: self.pause,
|
||||||
arcade.key.F1: self.pause,
|
arcade.key.F1: self.pause,
|
||||||
|
arcade.key.F11: self.toogle_fullscreen
|
||||||
},
|
},
|
||||||
State.PAUSED: {
|
State.PAUSED: {
|
||||||
arcade.key.ESCAPE: self.resume,
|
arcade.key.ESCAPE: self.resume,
|
||||||
arcade.key.F1: self.resume
|
arcade.key.F1: self.resume,
|
||||||
|
arcade.key.F11: self.toogle_fullscreen
|
||||||
},
|
},
|
||||||
State.OVER: {
|
State.OVER: {
|
||||||
arcade.key.ENTER: self.new_game
|
arcade.key.ENTER: self.new_game,
|
||||||
|
arcade.key.F11: self.toogle_fullscreen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
center_x = WINDOW_WIDTH / 2
|
arcade.Window.__init__(
|
||||||
center_y = WINDOW_HEIGHT / 2
|
self,
|
||||||
self.bg_sprite = arcade.Sprite(WINDOW_BG_PATH)
|
width = WINDOW_WIDTH,
|
||||||
self.bg_sprite.center_x = center_x
|
height = WINDOW_HEIGHT,
|
||||||
self.bg_sprite.center_y = center_y
|
title = WINDOW_TITLE,
|
||||||
self.matrix.sprite = arcade.Sprite(MATRIX_SPRITE_PATH)
|
resizable = True,
|
||||||
|
antialiasing = False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.init_width = self.width
|
||||||
|
self.init_height = self.height
|
||||||
|
self.sprite = ResizableSprite(WINDOW_BG_PATH)
|
||||||
|
self.matrix.sprite = ResizableSprite(MATRIX_SPRITE_PATH)
|
||||||
self.matrix.sprite.alpha = MATRIX_SPRITE_ALPHA
|
self.matrix.sprite.alpha = MATRIX_SPRITE_ALPHA
|
||||||
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.matrix.sprites = MatrixSprites(self.matrix)
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
self.stats_text = arcade.create_text(
|
self.stats_text = arcade.create_text(
|
||||||
text = STATS_TEXT,
|
text = STATS_TEXT,
|
||||||
@ -208,15 +235,42 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
font_name = FONT_NAME,
|
font_name = FONT_NAME,
|
||||||
anchor_x = 'right'
|
anchor_x = 'right'
|
||||||
)
|
)
|
||||||
|
self.highlight_text_size = HIGHLIGHT_TEXT_SIZE
|
||||||
|
arcade.set_background_color(BG_COLOR)
|
||||||
|
|
||||||
arcade.Window.__init__(
|
def on_resize(self, width, height):
|
||||||
self,
|
super().on_resize(width, height)
|
||||||
width = WINDOW_WIDTH,
|
center_x = width / 2
|
||||||
height = WINDOW_HEIGHT,
|
center_y = height / 2
|
||||||
title = WINDOW_TITLE,
|
self.sprite.center_x = center_x
|
||||||
resizable = False,
|
self.sprite.center_y = center_y
|
||||||
antialiasing = False
|
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)
|
||||||
|
ratio = min(
|
||||||
|
width / self.init_width,
|
||||||
|
height / self.init_height
|
||||||
)
|
)
|
||||||
|
for sprite in [
|
||||||
|
self.sprite,
|
||||||
|
self.matrix.sprite
|
||||||
|
]:
|
||||||
|
sprite.resize(ratio)
|
||||||
|
for minoes in [self.matrix, self.held, self.current, self.ghost] + self.next:
|
||||||
|
minoes.sprites.resize(ratio)
|
||||||
|
self.font_size = FONT_SIZE * ratio
|
||||||
|
self.stats_text = arcade.create_text(
|
||||||
|
text = STATS_TEXT,
|
||||||
|
color = TEXT_COLOR,
|
||||||
|
font_size = FONT_SIZE,
|
||||||
|
font_name = FONT_NAME,
|
||||||
|
anchor_x = 'right'
|
||||||
|
)
|
||||||
|
self.highlight_text_size = HIGHLIGHT_TEXT_SIZE * ratio
|
||||||
|
|
||||||
|
def toogle_fullscreen(self):
|
||||||
|
self.fullscreen = not self.fullscreen
|
||||||
|
|
||||||
def new_game(self):
|
def new_game(self):
|
||||||
self.highlight_texts = []
|
self.highlight_texts = []
|
||||||
@ -257,7 +311,7 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
def lock(self):
|
def lock(self):
|
||||||
self.current.sprites.update()
|
self.current.sprites.update()
|
||||||
super().lock()
|
super().lock()
|
||||||
self.matrix.sprites = MatrixSprites(self.matrix)
|
self.matrix.sprites.update()
|
||||||
|
|
||||||
def game_over(self):
|
def game_over(self):
|
||||||
super().game_over()
|
super().game_over()
|
||||||
@ -292,7 +346,7 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
def on_draw(self):
|
def on_draw(self):
|
||||||
arcade.start_render()
|
arcade.start_render()
|
||||||
self.bg_sprite.draw()
|
self.sprite.draw()
|
||||||
|
|
||||||
if self.state in (State.PLAYING, State.OVER):
|
if self.state in (State.PLAYING, State.OVER):
|
||||||
self.matrix.sprite.draw()
|
self.matrix.sprite.draw()
|
||||||
@ -343,7 +397,7 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
start_x = self.matrix.sprite.center_x,
|
start_x = self.matrix.sprite.center_x,
|
||||||
start_y = self.matrix.sprite.center_y,
|
start_y = self.matrix.sprite.center_y,
|
||||||
color = HIGHLIGHT_TEXT_COLOR,
|
color = HIGHLIGHT_TEXT_COLOR,
|
||||||
font_size = HIGHLIGHT_TEXT_FONT_SIZE,
|
font_size = self.highlight_text_size,
|
||||||
align = 'center',
|
align = 'center',
|
||||||
font_name = FONT_NAME,
|
font_name = FONT_NAME,
|
||||||
anchor_x = 'center',
|
anchor_x = 'center',
|
||||||
|
@ -207,7 +207,11 @@ class TetrisLogic():
|
|||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
# Piece unlocked
|
# Piece unlocked
|
||||||
if self.move(Movement.DOWN):
|
if self.can_move(
|
||||||
|
self.current.coord + Movement.DOWN,
|
||||||
|
(mino.coord for mino in self.current)
|
||||||
|
):
|
||||||
|
self.restart(self.lock, self.lock_delay)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Start lock
|
# Start lock
|
||||||
|
Loading…
x
Reference in New Issue
Block a user