diff --git a/README.md b/README.md index ef7b14a..4855442 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ -Games made inside notifications! Currently includes Flappy Bird, Maze, Pong, Space Invaders and a WPS test! -I haven't checked if it works on all platforms, only on Linux, but it still might be unusable due to notifications not being cross-platform. \ No newline at end of file +Games made inside notifications! Currently includes Flappy Bird, Maze, Pong, Space Invaders, Tetris and a WPS test! + +Some parts of Tetris are based on my other project, ShatterStack, which is a Block Blast style game. + +The whole thing is just completely unusable on Windows due to Windows having such a high animation delay for notifications. Also, on Windows, only the WPS test works, due to Windows only allowing up to 141 characters in a notification. I only tested on Arch Linux with Hyprland + ML4W dotfiles, so your mileage may differ. \ No newline at end of file diff --git a/game/flappy_bird.py b/game/flappy_bird.py index 30af932..17daa6f 100644 --- a/game/flappy_bird.py +++ b/game/flappy_bird.py @@ -12,7 +12,7 @@ class Game(arcade.gui.UIView): self.pypresence_client.update(state="Playing Flappy Bird inside notifications!") self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) - self.info_label = self.anchor.add(arcade.gui.UILabel("Press keys inside this window to interact with the game\nYou can see the game inside notifications.", font_size=24, multiline=True), anchor_x="center", anchor_y="center") + self.info_label = self.anchor.add(arcade.gui.UILabel("Use space to jump\nYou can see the game inside notifications.", font_size=24, multiline=True), anchor_x="center", anchor_y="center") self.running = True self.should_jump = False diff --git a/game/maze.py b/game/maze.py index e7fcbdf..e163d15 100644 --- a/game/maze.py +++ b/game/maze.py @@ -1,10 +1,13 @@ import arcade, arcade.gui, time, random + from plyer import notification + from utils.constants import ROWS, COLS class Game(arcade.gui.UIView): def __init__(self, pypresence_client): super().__init__() + self.pypresence_client = pypresence_client self.pypresence_client.update(state="Solving a maze inside notifications!") diff --git a/game/pong.py b/game/pong.py index c78d16d..77ff915 100644 --- a/game/pong.py +++ b/game/pong.py @@ -12,7 +12,7 @@ class Game(arcade.gui.UIView): self.pypresence_client.update(state="Playing Pong inside notifications!") self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) - self.info_label = self.anchor.add(arcade.gui.UILabel("Press keys inside this window to interact with the game\nYou can see the game inside notifications.", font_size=24, multiline=True)) + self.info_label = self.anchor.add(arcade.gui.UILabel("Use arrow keys or WASD to move\nYou can see the game inside notifications.", font_size=24, multiline=True)) self.running = True self.last_update_time = time.perf_counter() diff --git a/game/snake.py b/game/snake.py index ce781c3..bdf61b4 100644 --- a/game/snake.py +++ b/game/snake.py @@ -12,7 +12,7 @@ class Game(arcade.gui.UIView): self.pypresence_client.update(state="Playing Snake inside notifications!") self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) - self.info_label = self.anchor.add(arcade.gui.UILabel("Press keys inside this window to interact with the game.\nYou can see the game inside notifications.", font_size=24, multiline=True), anchor_x="center", anchor_y="center") + self.info_label = self.anchor.add(arcade.gui.UILabel("Use arrow keys or WASD to move.\nYou can see the game inside notifications.", font_size=24, multiline=True), anchor_x="center", anchor_y="center") self.direction = "right" self.running = True diff --git a/game/space_invaders.py b/game/space_invaders.py index 949333c..e78ff22 100644 --- a/game/space_invaders.py +++ b/game/space_invaders.py @@ -12,7 +12,7 @@ class Game(arcade.gui.UIView): self.pypresence_client.update(state="Playing Space Invaders inside notifications!") self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) - self.info_label = self.anchor.add(arcade.gui.UILabel("Press keys inside this window to interact with the game\nYou can see the game inside notifications!", font_size=24, multiline=True)) + self.info_label = self.anchor.add(arcade.gui.UILabel("Use arrow keys or WASD to move\nYou can see the game inside notifications!", font_size=24, multiline=True)) self.running = True self.last_update_time = time.perf_counter() diff --git a/game/tetris.py b/game/tetris.py new file mode 100644 index 0000000..8ed1873 --- /dev/null +++ b/game/tetris.py @@ -0,0 +1,186 @@ +import arcade, arcade.gui, time, random, os, json + +from plyer import notification + +from utils.constants import TETRIS_SHAPES + +class Game(arcade.gui.UIView): + def __init__(self, pypresence_client): + super().__init__() + + self.pypresence_client = pypresence_client + self.pypresence_client.update(state="Playing Tetris inside notifications!") + + self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) + self.info_label = self.anchor.add(arcade.gui.UILabel("Use arrow keys or WASD to move the blocks\nThe game is shown inside notifications.", font_size=24, multiline=True)) + + self.running = True + self.last_update_time = time.perf_counter() + + if not os.path.exists("data.json"): + self.data = {} + else: + with open("data.json", "r") as file: + self.data = json.load(file) + + if not "tetris" in self.data: + self.data["tetris"] = {"high_score": 0} + + self.high_score = self.data["tetris"]["high_score"] + self.score = 0 + + self.shape_to_place = random.choice(list(TETRIS_SHAPES.keys())) + self.next_shape_to_place = random.choice(list(TETRIS_SHAPES.keys())) + self.shape_data = TETRIS_SHAPES[self.shape_to_place] + + self.grid = [[0 for _ in range(10)] for _ in range(20)] + self.shape_tuple = () + + self.current_rotation_index = 0 + self.rotated_shapes = {} + self.shape_widths = {} + + self.tiles_to_destroy = [] + + def can_move(self, x, y, shape_name, dx, dy, rotation_index): + current_positions = [(x + sx, y + sy) for sx, sy in self.rotated_shapes[shape_name][rotation_index]] + + for sx, sy in self.rotated_shapes[shape_name][rotation_index]: + nx, ny = x + sx + dx, y + sy + dy + + if nx < 0 or nx >= 10 or ny < 0 or ny >= 20: + return False + + if self.grid[ny][nx] and (nx, ny) not in current_positions: + return False + + return True + + def cache_rotated_shapes(self): + for name, shape in TETRIS_SHAPES.items(): + self.rotated_shapes[name] = [] + + self.rotated_shapes[name].append(shape) + self.rotated_shapes[name].append([(y, -x) for x, y in shape]) # 90 degrees + self.rotated_shapes[name].append([(-x, -y) for x, y in shape]) # 180 degrees + self.rotated_shapes[name].append([(-y, x) for x, y in shape]) # 270 degrees + + def list_get(self, lst, index, default=None): + return lst[index] if 0 <= index < len(lst) else default + + def create_shape(self, start_x, start_y, shape_name, rotation_index=0): + for x, y in self.rotated_shapes[shape_name][rotation_index]: + self.grid[start_y + y][start_x + x] = 1 + + return (start_x, start_y, shape_name) + + def move_shape(self, start_x, start_y, shape_name, change_x, change_y): + if not self.can_move(start_x, start_y, shape_name, change_x, change_y, self.current_rotation_index): + self.shape_to_place = self.next_shape_to_place + self.next_shape_to_place = random.choice(list(TETRIS_SHAPES.keys())) + self.spawn_shape() + + return + + for x, y in self.rotated_shapes[shape_name][self.current_rotation_index]: + self.grid[start_y + y][start_x + x] = 0 + + for x, y in self.rotated_shapes[shape_name][self.current_rotation_index]: + self.grid[start_y + change_y + y][start_x + change_x + x] = 1 + + self.shape_tuple = (start_x + change_x, start_y + change_y, shape_name) + + def rotate_shape(self, start_x, start_y, shape_name): + for x, y in self.rotated_shapes[shape_name][self.current_rotation_index]: + self.grid[start_y + y][start_x + x] = 0 + + self.current_rotation_index += 1 + + if self.current_rotation_index == 4: + self.current_rotation_index = 0 + + return self.create_shape(start_x, start_y, shape_name, self.current_rotation_index) + + def on_show_view(self): + super().on_show_view() + + self.cache_rotated_shapes() + self.spawn_shape() + + def spawn_shape(self): + self.shape_tuple = self.create_shape(int(10 / 2), 0, self.shape_to_place) + + def on_update(self, delta_time): + if self.running and time.perf_counter() - self.last_update_time >= 0.4: + self.last_update_time = time.perf_counter() + + self.move_shape(*self.shape_tuple, 0, 1) + + if any(all(self.grid[row][col] for row in range(5)) for col in range(10)): # check if any columns are all full + self.info_label.text = "Game Over.\nPress r to restart" + self.running = False + + notification.notify( + title="Tetris inside notifications", + message="Game Over!" + ) + + return + + for row in range(20): + if all(self.grid[row]): + self.grid[row] = [0] * 10 + self.score += 10 * 10 + + text = "" + + for y in range(20): + for x in range(10): + if self.grid[y][x]: + text += "#" + else: + text += "_" + + text += "\n" + + if self.score > self.high_score: + self.high_score = self.score + + notification.notify( + title=f"Tetris | Score: {self.score} High Score: {self.high_score}", + message=text + ) + + def on_key_press(self, symbol, modifiers): + if symbol == arcade.key.ESCAPE: + self.data["tetris"]["high_score"] = self.high_score + with open("data.json", "w") as file: + file.write(json.dumps(self.data, indent=4)) + + from menus.main import Main + self.window.show_view(Main(self.pypresence_client)) + elif symbol == arcade.key.R and not self.running: + self.score = 0 + + self.shape_to_place = random.choice(list(TETRIS_SHAPES.keys())) + self.next_shape_to_place = random.choice(list(TETRIS_SHAPES.keys())) + self.shape_data = TETRIS_SHAPES[self.shape_to_place] + + self.grid = [[0 for _ in range(10)] for _ in range(20)] + self.shape_tuple = () + + self.current_rotation_index = 0 + + self.tiles_to_destroy = [] + + self.spawn_shape() + + self.running = True + elif symbol == arcade.key.SPACE: + self.shape_tuple = self.rotate_shape(*self.shape_tuple) + elif symbol == arcade.key.LEFT and self.can_move(*self.shape_tuple, -1, 0, self.current_rotation_index): + self.move_shape(*self.shape_tuple, -1, 0) + elif symbol == arcade.key.RIGHT and self.can_move(*self.shape_tuple, 1, 0, self.current_rotation_index): + self.move_shape(*self.shape_tuple, 1, 0) + elif symbol == arcade.key.DOWN and self.can_move(*self.shape_tuple, 0, 1, self.current_rotation_index): + self.move_shape(*self.shape_tuple, 0, 1) \ No newline at end of file diff --git a/menus/main.py b/menus/main.py index ce28ab4..cc0d747 100644 --- a/menus/main.py +++ b/menus/main.py @@ -52,25 +52,28 @@ class Main(arcade.gui.UIView): self.title_label = self.box.add(arcade.gui.UILabel(text="NotifPlayground", font_name="Roboto", font_size=48)) - self.snake_button = self.box.add(arcade.gui.UITextureButton(text="Snake", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.snake_button = self.box.add(arcade.gui.UITextureButton(text="Snake", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.snake_button.on_click = lambda event: self.snake() - self.flappy_bird_button = self.box.add(arcade.gui.UITextureButton(text="Flappy Bird", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.flappy_bird_button = self.box.add(arcade.gui.UITextureButton(text="Flappy Bird", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.flappy_bird_button.on_click = lambda event: self.flappy_bird() - self.pong_button = self.box.add(arcade.gui.UITextureButton(text="Pong", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.pong_button = self.box.add(arcade.gui.UITextureButton(text="Pong", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.pong_button.on_click = lambda event: self.pong() - self.space_invaders_button = self.box.add(arcade.gui.UITextureButton(text="Space Invaders", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.space_invaders_button = self.box.add(arcade.gui.UITextureButton(text="Space Invaders", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.space_invaders_button.on_click = lambda event: self.space_invaders() - self.wps_test_button = self.box.add(arcade.gui.UITextureButton(text="WPS Test", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.wps_test_button = self.box.add(arcade.gui.UITextureButton(text="WPS Test", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.wps_test_button.on_click = lambda event: self.wps_test() - self.maze_button = self.box.add(arcade.gui.UITextureButton(text="Maze", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.maze_button = self.box.add(arcade.gui.UITextureButton(text="Maze", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.maze_button.on_click = lambda event: self.maze() - self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.tetris_button = self.box.add(arcade.gui.UITextureButton(text="Tetris", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) + self.tetris_button.on_click = lambda event: self.tetris() + + self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.settings_button.on_click = lambda event: self.settings() def pong(self): @@ -97,6 +100,10 @@ class Main(arcade.gui.UIView): from game.maze import Game self.window.show_view(Game(self.pypresence_client)) + def tetris(self): + from game.tetris import Game + self.window.show_view(Game(self.pypresence_client)) + def settings(self): from menus.settings import Settings self.window.show_view(Settings(self.pypresence_client)) diff --git a/utils/constants.py b/utils/constants.py index 47a1493..16fac37 100644 --- a/utils/constants.py +++ b/utils/constants.py @@ -7,8 +7,21 @@ menu_background_color = (30, 30, 47) log_dir = 'logs' discord_presence_id = 1417184787936055336 -ROWS = 30 -COLS = 35 +ROWS = 20 +COLS = 25 + +TETRIS_SHAPES = { + "I": [(0, 0), (1, 0), (2, 0), (3, 0)], + "I_R1": [(0, 0), (0, 1), (0, 2), (0, 3)], + "O": [(0, 0), (1, 0), (0, 1), (1, 1)], + "L": [(0, 0), (0, 1), (0, 2), (1, 2)], + "L_R1": [(0, 1), (1, 1), (2, 1), (2, 0)], + "L_R2": [(1, 0), (1, 1), (1, 2), (0, 0)], + "L_R3": [(0, 0), (1, 0), (2, 0), (0, 1)], + "LINE1": [(0, 0)], + "LINE2": [(0, 0), (1, 0)], + "LINE3": [(0, 0), (1, 0), (2, 0)], +} button_style = {'normal': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK), 'hover': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK), 'press': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK), 'disabled': UITextureButtonStyle(font_name="Roboto", font_color=arcade.color.BLACK)}