Compare commits
No commits in common. "4edad3c1cffcc1bf9f44d7627843b8fabc6ce462" and "f5225b3a553e8855378f2ad49536a97aee903044" have entirely different histories.
4edad3c1cf
...
f5225b3a55
118
tetrarcade.py
118
tetrarcade.py
@ -21,7 +21,6 @@ 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
|
||||||
@ -58,7 +57,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_SIZE = 20
|
HIGHLIGHT_TEXT_FONT_SIZE = 20
|
||||||
|
|
||||||
CONTROL_TEXT = """
|
CONTROL_TEXT = """
|
||||||
|
|
||||||
@ -72,9 +71,7 @@ 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
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -99,18 +96,6 @@ PRESS
|
|||||||
[ENTER]
|
[ENTER]
|
||||||
TO PLAY
|
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):
|
||||||
@ -123,19 +108,13 @@ class MinoSprites(arcade.SpriteList):
|
|||||||
mino.sprite.left = self.matrix.sprite.left + x*(mino.sprite.width-1)
|
mino.sprite.left = self.matrix.sprite.left + x*(mino.sprite.width-1)
|
||||||
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 update(self):
|
def __init__(self, matrix):
|
||||||
for sprite in self.matrix.sprites:
|
super().__init__(matrix)
|
||||||
self.matrix.sprite.remove(sprite)
|
for y, line in enumerate(matrix):
|
||||||
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)
|
||||||
@ -150,7 +129,8 @@ 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 = ResizableSprite(path)
|
mino.sprite = arcade.Sprite(path)
|
||||||
|
mino.sprite.alpha = alpha
|
||||||
self.append(mino.sprite)
|
self.append(mino.sprite)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
@ -173,8 +153,7 @@ 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,
|
||||||
@ -198,35 +177,29 @@ 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__()
|
||||||
|
|
||||||
arcade.Window.__init__(
|
center_x = WINDOW_WIDTH / 2
|
||||||
self,
|
center_y = WINDOW_HEIGHT / 2
|
||||||
width = WINDOW_WIDTH,
|
self.bg_sprite = arcade.Sprite(WINDOW_BG_PATH)
|
||||||
height = WINDOW_HEIGHT,
|
self.bg_sprite.center_x = center_x
|
||||||
title = WINDOW_TITLE,
|
self.bg_sprite.center_y = center_y
|
||||||
resizable = True,
|
self.matrix.sprite = arcade.Sprite(MATRIX_SPRITE_PATH)
|
||||||
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,
|
||||||
@ -235,43 +208,16 @@ 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__(
|
||||||
|
self,
|
||||||
def on_resize(self, width, height):
|
width = WINDOW_WIDTH,
|
||||||
super().on_resize(width, height)
|
height = WINDOW_HEIGHT,
|
||||||
center_x = width / 2
|
title = WINDOW_TITLE,
|
||||||
center_y = height / 2
|
resizable = False,
|
||||||
self.sprite.center_x = center_x
|
antialiasing = False
|
||||||
self.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)
|
|
||||||
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 = []
|
||||||
self.matrix.sprites = MatrixSprites(self.matrix)
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
@ -311,7 +257,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.update()
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
|
|
||||||
def game_over(self):
|
def game_over(self):
|
||||||
super().game_over()
|
super().game_over()
|
||||||
@ -346,7 +292,7 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
def on_draw(self):
|
def on_draw(self):
|
||||||
arcade.start_render()
|
arcade.start_render()
|
||||||
self.sprite.draw()
|
self.bg_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()
|
||||||
@ -397,7 +343,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 = self.highlight_text_size,
|
font_size = HIGHLIGHT_TEXT_FONT_SIZE,
|
||||||
align = 'center',
|
align = 'center',
|
||||||
font_name = FONT_NAME,
|
font_name = FONT_NAME,
|
||||||
anchor_x = 'center',
|
anchor_x = 'center',
|
||||||
|
@ -207,11 +207,7 @@ class TetrisLogic():
|
|||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
# Piece unlocked
|
# Piece unlocked
|
||||||
if self.can_move(
|
if self.move(Movement.DOWN):
|
||||||
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