V0.3 Release
New musics Piece lock optimize
@ -1,21 +1,21 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import sys
|
import sys
|
||||||
import locale
|
|
||||||
import time
|
|
||||||
import os
|
|
||||||
|
|
||||||
import configparser
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import arcade
|
import arcade
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
str(e)
|
str(e) + """
|
||||||
+ """
|
|
||||||
This game require arcade library.
|
This game require arcade library.
|
||||||
You can install it with:
|
You can install it with:
|
||||||
python -m pip install --user arcade"""
|
python -m pip install --user arcade"""
|
||||||
)
|
)
|
||||||
|
import pyglet
|
||||||
|
|
||||||
|
import locale
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import itertools
|
||||||
|
import configparser
|
||||||
|
|
||||||
from tetrislogic import TetrisLogic, Color, State, Coord
|
from tetrislogic import TetrisLogic, Color, State, Coord
|
||||||
|
|
||||||
@ -61,18 +61,19 @@ MINO_SPRITE_SIZE = 21
|
|||||||
|
|
||||||
if getattr(sys, 'frozen', False):
|
if getattr(sys, 'frozen', False):
|
||||||
# The application is frozen
|
# The application is frozen
|
||||||
DATA_DIR = os.path.dirname(sys.executable)
|
PROGRAM_DIR = os.path.dirname(sys.executable)
|
||||||
else:
|
else:
|
||||||
# The application is not frozen
|
# The application is not frozen
|
||||||
DATA_DIR = os.path.dirname(__file__)
|
PROGRAM_DIR = os.path.dirname(__file__)
|
||||||
DATA_DIR = os.path.join(DATA_DIR, "res")
|
RESOURCES_DIR = os.path.join(PROGRAM_DIR, "resources")
|
||||||
|
|
||||||
# Sprites
|
# Sprites
|
||||||
WINDOW_BG_PATH = os.path.join(DATA_DIR, "bg.jpg")
|
IMAGES_DIR = os.path.join(RESOURCES_DIR, "images")
|
||||||
MATRIX_BG_PATH = os.path.join(DATA_DIR, "matrix.png")
|
WINDOW_BG_PATH = os.path.join(IMAGES_DIR, "bg.jpg")
|
||||||
HELD_BG_PATH = os.path.join(DATA_DIR, "held.png")
|
MATRIX_BG_PATH = os.path.join(IMAGES_DIR, "matrix.png")
|
||||||
NEXT_BG_PATH = os.path.join(DATA_DIR, "next.png")
|
HELD_BG_PATH = os.path.join(IMAGES_DIR, "held.png")
|
||||||
MINOES_SPRITES_PATH = os.path.join(DATA_DIR, "minoes.png")
|
NEXT_BG_PATH = os.path.join(IMAGES_DIR, "next.png")
|
||||||
|
MINOES_SPRITES_PATH = os.path.join(IMAGES_DIR, "minoes.png")
|
||||||
Color.PRELOCKED = 7
|
Color.PRELOCKED = 7
|
||||||
MINOES_COLOR_ID = {
|
MINOES_COLOR_ID = {
|
||||||
Color.BLUE: 0,
|
Color.BLUE: 0,
|
||||||
@ -90,11 +91,12 @@ TEXTURES = arcade.load_textures(
|
|||||||
TEXTURES = {color: TEXTURES[i] for color, i in MINOES_COLOR_ID.items()}
|
TEXTURES = {color: TEXTURES[i] for color, i in MINOES_COLOR_ID.items()}
|
||||||
|
|
||||||
# Music
|
# Music
|
||||||
MUSIC_PATH = os.path.join(DATA_DIR, "Tetris - Song A.mp3")
|
MUSIC_DIR = os.path.join(RESOURCES_DIR, "musics")
|
||||||
|
MUSICS_PATHS = (entry.path for entry in os.scandir(MUSIC_DIR))
|
||||||
|
|
||||||
# Text
|
# Text
|
||||||
TEXT_COLOR = arcade.color.BUBBLES
|
TEXT_COLOR = arcade.color.BUBBLES
|
||||||
FONT_NAME = os.path.join(DATA_DIR, "joystix monospace.ttf")
|
FONT_NAME = os.path.join(RESOURCES_DIR, "fonts/joystix monospace.ttf")
|
||||||
STATS_TEXT_MARGIN = 40
|
STATS_TEXT_MARGIN = 40
|
||||||
STATS_TEXT_SIZE = 14
|
STATS_TEXT_SIZE = 14
|
||||||
STATS_TEXT_WIDTH = 150
|
STATS_TEXT_WIDTH = 150
|
||||||
@ -165,7 +167,11 @@ class MatrixSprites(MinoesSprites):
|
|||||||
for x, mino in enumerate(line):
|
for x, mino in enumerate(line):
|
||||||
if mino:
|
if mino:
|
||||||
mino.sprite.refresh(x, y)
|
mino.sprite.refresh(x, y)
|
||||||
self.append(mino.sprite)
|
|
||||||
|
def remove_line(self, y):
|
||||||
|
for mino in self.matrix[y]:
|
||||||
|
if mino:
|
||||||
|
self.remove(mino.sprite)
|
||||||
|
|
||||||
|
|
||||||
class TetrArcade(TetrisLogic, arcade.Window):
|
class TetrArcade(TetrisLogic, arcade.Window):
|
||||||
@ -219,9 +225,14 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
self.next.bg.alpha = BAR_ALPHA
|
self.next.bg.alpha = BAR_ALPHA
|
||||||
self.matrix.sprites = MatrixSprites(self.matrix)
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
self.on_resize(self.init_width, self.init_height)
|
self.on_resize(self.init_width, self.init_height)
|
||||||
|
|
||||||
if self.play_music:
|
if self.play_music:
|
||||||
self.music = arcade.Sound(MUSIC_PATH)
|
self.music = pyglet.media.Player()
|
||||||
self.music_player = None
|
playlist = itertools.cycle(
|
||||||
|
pyglet.media.load(path)
|
||||||
|
for path in MUSICS_PATHS
|
||||||
|
)
|
||||||
|
self.music.queue(playlist)
|
||||||
|
|
||||||
def new_conf(self):
|
def new_conf(self):
|
||||||
self.conf["WINDOW"] = {"width": WINDOW_WIDTH, "height": WINDOW_HEIGHT, "fullscreen": False}
|
self.conf["WINDOW"] = {"width": WINDOW_WIDTH, "height": WINDOW_HEIGHT, "fullscreen": False}
|
||||||
@ -308,13 +319,10 @@ AGAIN""".format(
|
|||||||
def new_game(self):
|
def new_game(self):
|
||||||
self.highlight_texts = []
|
self.highlight_texts = []
|
||||||
super().new_game()
|
super().new_game()
|
||||||
|
self.matrix.sprites = MatrixSprites(self.matrix)
|
||||||
if self.play_music:
|
if self.play_music:
|
||||||
if self.music_player:
|
self.music.seek(0)
|
||||||
self.music_player.seek(0)
|
self.music.play()
|
||||||
self.music_player.play()
|
|
||||||
else:
|
|
||||||
self.music_player = self.music.player.play()
|
|
||||||
self.music_player.loop = True
|
|
||||||
|
|
||||||
def new_tetromino(self):
|
def new_tetromino(self):
|
||||||
tetromino = super().new_tetromino()
|
tetromino = super().new_tetromino()
|
||||||
@ -322,7 +330,6 @@ AGAIN""".format(
|
|||||||
return tetromino
|
return tetromino
|
||||||
|
|
||||||
def new_matrix_piece(self):
|
def new_matrix_piece(self):
|
||||||
self.matrix.sprites = MatrixSprites(self.matrix)
|
|
||||||
super().new_matrix_piece()
|
super().new_matrix_piece()
|
||||||
self.matrix.ghost.sprites = TetrominoSprites(self.matrix.ghost, self, GHOST_ALPHA)
|
self.matrix.ghost.sprites = TetrominoSprites(self.matrix.ghost, self, GHOST_ALPHA)
|
||||||
for tetromino in [self.matrix.piece, self.matrix.ghost] + self.next.pieces:
|
for tetromino in [self.matrix.piece, self.matrix.ghost] + self.next.pieces:
|
||||||
@ -353,21 +360,31 @@ AGAIN""".format(
|
|||||||
self.matrix.piece.prelocked = False
|
self.matrix.piece.prelocked = False
|
||||||
self.matrix.piece.sprites.refresh()
|
self.matrix.piece.sprites.refresh()
|
||||||
super().lock()
|
super().lock()
|
||||||
|
self.matrix.sprites.refresh()
|
||||||
|
|
||||||
|
def enter_the_matrix(self):
|
||||||
|
super().enter_the_matrix()
|
||||||
|
for mino in self.matrix.piece:
|
||||||
|
self.matrix.sprites.append(mino.sprite)
|
||||||
|
|
||||||
|
def remove_line(self, y):
|
||||||
|
self.matrix.sprites.remove_line(y)
|
||||||
|
super().remove_line(y)
|
||||||
|
|
||||||
def pause(self):
|
def pause(self):
|
||||||
super().pause()
|
super().pause()
|
||||||
if self.play_music:
|
if self.play_music:
|
||||||
self.music_player.pause()
|
self.music.pause()
|
||||||
|
|
||||||
def resume(self):
|
def resume(self):
|
||||||
super().resume()
|
super().resume()
|
||||||
if self.play_music:
|
if self.play_music:
|
||||||
self.music_player.play()
|
self.music.play()
|
||||||
|
|
||||||
def game_over(self):
|
def game_over(self):
|
||||||
super().game_over()
|
super().game_over()
|
||||||
if self.play_music:
|
if self.play_music:
|
||||||
self.music_player.pause()
|
self.music.pause()
|
||||||
|
|
||||||
def on_key_press(self, key, modifiers):
|
def on_key_press(self, key, modifiers):
|
||||||
for key_or_modifier in (key, modifiers):
|
for key_or_modifier in (key, modifiers):
|
||||||
@ -552,7 +569,7 @@ High score could not be saved:
|
|||||||
def on_close(self):
|
def on_close(self):
|
||||||
self.save_high_score()
|
self.save_high_score()
|
||||||
if self.play_music:
|
if self.play_music:
|
||||||
self.music_player.pause()
|
self.music.pause()
|
||||||
super().on_close()
|
super().on_close()
|
||||||
|
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 153 KiB After Width: | Height: | Size: 153 KiB |
Before Width: | Height: | Size: 499 B After Width: | Height: | Size: 499 B |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 389 B After Width: | Height: | Size: 389 B |
Before Width: | Height: | Size: 475 B After Width: | Height: | Size: 475 B |
BIN
resources/musics/2-!!!.mp3
Normal file
BIN
resources/musics/3-Boogie!.mp3
Normal file
BIN
resources/musics/4-Riff Blues.mp3
Normal file
2
setup.py
@ -35,7 +35,7 @@ options = {
|
|||||||
}
|
}
|
||||||
setup(
|
setup(
|
||||||
name = "TetrArcade",
|
name = "TetrArcade",
|
||||||
version = "0.2",
|
version = "0.3",
|
||||||
description = "Tetris clone",
|
description = "Tetris clone",
|
||||||
author = "AdrienMalin",
|
author = "AdrienMalin",
|
||||||
executables = [executable],
|
executables = [executable],
|
||||||
|
1
test.py
@ -11,6 +11,7 @@ game.rotate_clockwise()
|
|||||||
game.rotate_counter()
|
game.rotate_counter()
|
||||||
for i in range(12):
|
for i in range(12):
|
||||||
game.soft_drop()
|
game.soft_drop()
|
||||||
|
game.matrix.sprites.refresh()
|
||||||
game.on_draw()
|
game.on_draw()
|
||||||
while game.state != State.OVER:
|
while game.state != State.OVER:
|
||||||
game.hard_drop()
|
game.hard_drop()
|
||||||
|
@ -255,18 +255,14 @@ class TetrisLogic:
|
|||||||
else:
|
else:
|
||||||
t_spin = T_Spin.NONE
|
t_spin = T_Spin.NONE
|
||||||
|
|
||||||
for mino in self.matrix.piece:
|
self.enter_the_matrix()
|
||||||
coord = mino.coord + self.matrix.piece.coord
|
|
||||||
del mino.coord
|
|
||||||
if coord.y <= self.NB_LINES + 3:
|
|
||||||
self.matrix[coord.y][coord.x] = mino
|
|
||||||
|
|
||||||
# Clear complete lines
|
# Clear complete lines
|
||||||
nb_lines_cleared = 0
|
nb_lines_cleared = 0
|
||||||
for y, line in reversed(list(enumerate(self.matrix))):
|
for y, line in reversed(list(enumerate(self.matrix))):
|
||||||
if all(mino for mino in line):
|
if all(mino for mino in line):
|
||||||
nb_lines_cleared += 1
|
nb_lines_cleared += 1
|
||||||
self.matrix.pop(y)
|
self.remove_line(y)
|
||||||
self.append_new_line_to_matrix()
|
self.append_new_line_to_matrix()
|
||||||
if nb_lines_cleared:
|
if nb_lines_cleared:
|
||||||
self.nb_lines_cleared += nb_lines_cleared
|
self.nb_lines_cleared += nb_lines_cleared
|
||||||
@ -303,6 +299,15 @@ class TetrisLogic:
|
|||||||
else:
|
else:
|
||||||
self.new_matrix_piece()
|
self.new_matrix_piece()
|
||||||
|
|
||||||
|
def enter_the_matrix(self):
|
||||||
|
for mino in self.matrix.piece:
|
||||||
|
coord = mino.coord + self.matrix.piece.coord
|
||||||
|
if coord.y <= self.NB_LINES + 3:
|
||||||
|
self.matrix[coord.y][coord.x] = mino
|
||||||
|
|
||||||
|
def remove_line(self, y):
|
||||||
|
self.matrix.pop(y)
|
||||||
|
|
||||||
def can_move(self, potential_coord, minoes_coords):
|
def can_move(self, potential_coord, minoes_coords):
|
||||||
return all(self.matrix.cell_is_free(potential_coord + mino_coord) for mino_coord in minoes_coords)
|
return all(self.matrix.cell_is_free(potential_coord + mino_coord) for mino_coord in minoes_coords)
|
||||||
|
|
||||||
|