Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
[fix] Refact shit..
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu1nel committed Feb 7, 2024
1 parent 0c332e7 commit 308ea27
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Game 2048 written in Python, pygame module. First pet-project. cringe..."""

import sys
from pathlib import Path

Expand Down
9 changes: 4 additions & 5 deletions src/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from random import randint, random, shuffle
from typing import Any

from src.interface import Interface
from src.logics import get_index_from_number, get_number_from_index, quick_copy


Expand Down Expand Up @@ -40,7 +39,7 @@ def __setitem__(self, key: int, value: Any) -> None:
def __iter__(self) -> Iterator:
return iter(self.__mas)

def move_left(self, game: Interface) -> None:
def move_left(self, game: Any) -> None:
"""Moves the board to the left."""
origin = quick_copy(self)
game.delta = 0
Expand All @@ -61,7 +60,7 @@ def move_left(self, game: Interface) -> None:
self[x].append(0)
self.is_board_move = origin != self.get_mas

def move_right(self, game: Interface) -> None:
def move_right(self, game: Any) -> None:
"""Moves the board to the right."""
origin = quick_copy(self)
game.delta = 0
Expand All @@ -82,7 +81,7 @@ def move_right(self, game: Interface) -> None:
self[x].insert(0, 0)
self.is_board_move = origin != self.get_mas

def move_up(self, game: Interface) -> None:
def move_up(self, game: Any) -> None:
"""Moves the board to the up."""
origin = quick_copy(self)
game.delta = 0
Expand All @@ -102,7 +101,7 @@ def move_up(self, game: Interface) -> None:
self[x][y] = column[x]
self.is_board_move = origin != self.get_mas

def move_down(self, game: Interface) -> None:
def move_down(self, game: Any) -> None:
"""Moves the board to the down."""
origin = quick_copy(self)
game.delta = 0
Expand Down
62 changes: 31 additions & 31 deletions src/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def draw_game_over(self) -> None:
self.screen.blit(blur, (0, 0))

self.screen.blit(
pg.font.Font(self.generalFont, 60).render("Game Over!", antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(self.generalFont, 60).render("Game Over!", True, config.COLORS["WHITE"]),
(100, 290),
)
pg.draw.rect(self.screen, "#8d8d8d", repeat_box, border_radius=8)
Expand Down Expand Up @@ -87,45 +87,45 @@ def draw_top_gamers(self) -> None:
self.screen.blit(pg.transform.scale(menu, (50, 50)), (236, 543))

self.screen.blit(
pg.font.Font(self.generalFont, 120).render("Rating", antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(self.generalFont, 120).render("Rating", True, config.COLORS["WHITE"]),
(86, -50),
)

for idx, player in all_players.items():
if player["name"] is None:
self.screen.blit(
pg.font.Font(self.generalFont, 45).render("Nothing", antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(self.generalFont, 45).render("Nothing", True, config.COLORS["WHITE"]),
(180, 115 + 100 * idx),
)
else:
name = pg.font.Font(self.generalFont, 40).render(
player["name"] + ":",
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
)
if name.get_width() > 154: # noqa: PLR2004
s = player["name"] + ":"
self.screen.blit(
pg.font.Font(self.generalFont, 28).render(s, antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(self.generalFont, 28).render(s, True, config.COLORS["WHITE"]),
(117, 135 + 100 * idx),
)
size_font = 35
score_txt = pg.font.Font(self.generalFont, size_font).render(
str(player["score"]),
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
)
while 289 - name.get_width() + 20 + score_txt.get_width() - 117 > 309: # noqa: PLR2004
score_txt = pg.font.Font(self.generalFont, size_font).render(
str(player["score"]),
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
)
size_font -= 2
y = 128 if size_font == 35 else 133 # noqa: PLR2004
x = (
pg.font.Font(self.generalFont, 28)
.render(player["name"] + ":", antialias=True, color=config.COLORS["WHITE"])
.render(player["name"] + ":", True, config.COLORS["WHITE"])
.get_width()
+ 127
)
Expand All @@ -135,14 +135,14 @@ def draw_top_gamers(self) -> None:
size_font = 35
score_txt = pg.font.Font(self.generalFont, size_font).render(
str(player["score"]),
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
)
while 289 - name.get_width() + 20 + score_txt.get_width() - 117 > 309: # noqa: PLR2004
score_txt = pg.font.Font(self.generalFont, size_font).render(
str(player["score"]),
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
)
size_font -= 2
y = 128 if size_font == 35 else 133 # noqa: PLR2004
Expand Down Expand Up @@ -179,11 +179,11 @@ def draw_menu(self) -> None:

font = pg.font.Font(self.generalFont, 45)
self.screen.blit(
pg.font.Font(self.generalFont, 120).render("2048", antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(self.generalFont, 120).render("2048", True, config.COLORS["WHITE"]),
(108, 60),
)
self.screen.blit(font.render("PLAY", antialias=True, color=config.COLORS["WHITE"]), (210, 270))
self.screen.blit(font.render("RATING", antialias=True, color=config.COLORS["WHITE"]), (186, 370))
self.screen.blit(font.render("PLAY", True, config.COLORS["WHITE"]), (210, 270))
self.screen.blit(font.render("RATING", True, config.COLORS["WHITE"]), (186, 370))

pg.display.update()

Expand Down Expand Up @@ -219,10 +219,10 @@ def draw_victory(self) -> None:
self.screen.blit(blur, (0, 0))

font_h1 = pg.font.Font(self.generalFont, 90)
text_h1 = font_h1.render("You Win!", antialias=True, color=config.COLORS["WHITE"])
text_h1 = font_h1.render("You Win!", True, config.COLORS["WHITE"])
self.screen.blit(text_h1, (self.width // 2 - text_h1.get_size()[0] // 2, 330))
font_h3 = pg.font.Font(self.generalFont, 35)
text_h3 = font_h3.render("Click any button to Continue", antialias=True, color=config.COLORS["WHITE"])
text_h3 = font_h3.render("Click any button to Continue", True, config.COLORS["WHITE"])
self.screen.blit(text_h3, (self.width // 2 - text_h3.get_size()[0] // 2, 455))

pg.display.update()
Expand Down Expand Up @@ -253,15 +253,15 @@ def draw_main(self) -> None:
self.screen.blit(pg.transform.scale(pg.image.load(ELEMENTS_PATH / Path("home.png")), (38, 38)), (314, 162))

self.screen.blit(
pg.font.Font(self.generalFont, 17).render("HIGH SCORE", antialias=True, color=config.COLORS["GRAY"]),
pg.font.Font(self.generalFont, 17).render("HIGH SCORE", True, config.COLORS["GRAY"]),
(402, 55),
)
self.screen.blit(
pg.font.Font(self.generalFont, 18).render("SCORE", antialias=True, color=config.COLORS["GRAY"]),
pg.font.Font(self.generalFont, 18).render("SCORE", True, config.COLORS["GRAY"]),
(300, 55),
)
self.screen.blit(
pg.font.Font(self.generalFont, 86).render("2048", antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(self.generalFont, 86).render("2048", True, config.COLORS["WHITE"]),
(30, 2),
)

Expand All @@ -273,8 +273,8 @@ def draw_main(self) -> None:
self.screen.blit(
pg.font.Font(self.generalFont, size_score).render(
f"{self.score}",
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
),
(325 - correct, 77),
)
Expand All @@ -283,8 +283,8 @@ def draw_main(self) -> None:
self.screen.blit(
pg.font.Font(self.generalFont, size_high_score).render(
f"{high_score}",
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
),
(447 - correct, 77),
)
Expand All @@ -294,8 +294,8 @@ def draw_main(self) -> None:
self.screen.blit(
pg.font.Font(self.generalFont, 34).render(
f"+{self.delta}",
antialias=True,
color=config.COLORS["WHITE"],
True,
config.COLORS["WHITE"],
),
(115 - correct, 160),
)
Expand All @@ -305,8 +305,8 @@ def draw_main(self) -> None:
value, font = get_const_4_cell(self.board[row][column], self.generalFont)
text = font.render(
f"{value}",
antialias=True,
color=("#545E66" if value in (2, 4) else "#ebeeff"),
True,
("#545E66" if value in (2, 4) else "#ebeeff"),
) # GRAY or WHITE
w = column * self.size_block + (column - 1) * self.margin + 30
h = row * self.size_block + (row - 1) * self.margin + 240
Expand Down
5 changes: 2 additions & 3 deletions src/logics.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Any

import pygame as pg

from src.board import GameBoard


@dataclass(slots=True)
class Point: # noqa: D101
Expand All @@ -29,7 +28,7 @@ def get_side(self) -> str: # noqa: D102
return "DOWN"


def quick_copy(data: GameBoard | None) -> list:
def quick_copy(data: Any) -> list:
"""Return a copy of game board."""
mas = [] if data is None else data.get_mas
return [list(row) for row in mas]
Expand Down
29 changes: 15 additions & 14 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ def _render(game: Interface) -> None:
name_bg = pg.image.load(BG_PATH / Path("input_username.jpg"))
menu = pg.image.load(ELEMENTS_PATH / Path("home.png"))
game.screen.blit(
pg.font.Font(game.generalFont, 120).render(CAPTION, antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(game.generalFont, 120).render(CAPTION, True, config.COLORS["WHITE"]),
(108, 60),
)
game.screen.blit(name_bg, (0, 0))
game.screen.blit(pg.transform.scale(menu, [50, 50]), (236, 494))
game.screen.blit(
pg.font.Font(game.generalFont, 45).render("OK", antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(game.generalFont, 45).render("OK", True, config.COLORS["WHITE"]),
(229, 371),
)

Expand Down Expand Up @@ -91,23 +91,22 @@ def _render(game: Interface) -> None:
input_name = True
elif event.key == pg.K_BACKSPACE:
name = name[:-1]
elif (
font_input.render(name, antialias=True, color=config.COLORS["WHITE"]).get_width()
< 261 # noqa: PLR2004
):
elif font_input.render(name, True, config.COLORS["WHITE"]).get_width() < 261: # noqa: PLR2004
name += event.unicode
_render(self)
if name == "" and color == inactive_colour:
self.screen.blit(font_input.render("Username", antialias=True, color=config.COLORS["GRAY"]), (155, 267))
txt = font_input.render(name, antialias=True, color=config.COLORS["WHITE"])
self.screen.blit(font_input.render("Username", True, config.COLORS["GRAY"]), (155, 267))
txt = font_input.render(name, True, config.COLORS["WHITE"])
pg.draw.rect(self.screen, color, input_box, 1, border_radius=15)
self.screen.blit(txt, (input_box.w - txt.get_width() // 2 - 26, 267))
pg.display.update()

def load_game(self) -> None:
"""Loads a last game from save."""
path = Path.cwd()

if "save.txt" in os.listdir(path):

with open("save.txt") as file:
data = json.load(file)
self.board = GameBoard(data["board"])
Expand All @@ -116,6 +115,7 @@ def load_game(self) -> None:
full_path = path / Path("save.txt")
Path(full_path).unlink()
else:

super().__init__()
self.board = GameBoard()
self.move_mouse = False
Expand Down Expand Up @@ -146,21 +146,21 @@ def around_arrow(self) -> None:
self.screen.blit(blur, (0, 0))

self.screen.blit(
pg.font.Font(self.generalFont, 53).render("Reset game?", antialias=True, color=config.COLORS["WHITE"]),
pg.font.Font(self.generalFont, 53).render("Reset game?", True, config.COLORS["WHITE"]),
(60, 200),
)
font_h3 = pg.font.Font(self.generalFont, 32)
self.screen.blit(
font_h3.render("Are you sure you wish to", antialias=True, color=config.COLORS["WHITE"]),
font_h3.render("Are you sure you wish to", True, config.COLORS["WHITE"]),
(60, 300),
)
self.screen.blit(font_h3.render("reset the game?", antialias=True, color=config.COLORS["WHITE"]), (60, 340))
self.screen.blit(font_h3.render("reset the game?", True, config.COLORS["WHITE"]), (60, 340))

pg.draw.rect(self.screen, (110, 110, 110), cancel_box, border_radius=12)
self.screen.blit(font_h3.render("Cancel", antialias=True, color=config.COLORS["WHITE"]), (174, 413))
self.screen.blit(font_h3.render("Cancel", True, config.COLORS["WHITE"]), (174, 413))

pg.draw.rect(self.screen, (110, 110, 110), repeat_box, border_radius=12)
self.screen.blit(font_h3.render("Reset", antialias=True, color=config.COLORS["WHITE"]), (341, 413))
self.screen.blit(font_h3.render("Reset", True, config.COLORS["WHITE"]), (341, 413))
pg.display.update()

make_decision = False
Expand Down Expand Up @@ -276,5 +276,6 @@ def run(self) -> None:
self.clock.tick(self.framerate)
else:
self.draw_game_over()
except Exception: # noqa: BLE001
except Exception as exc:
self.save_game()
raise exc from None

0 comments on commit 308ea27

Please sign in to comment.