mirror of
https://github.com/csd4ni3l/notifplayground.git
synced 2026-01-01 04:23:47 +01:00
First version on Github
This commit is contained in:
142
game/flappy_bird.py
Normal file
142
game/flappy_bird.py
Normal file
@@ -0,0 +1,142 @@
|
||||
import arcade, arcade.gui, time, random, os, json
|
||||
|
||||
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="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.running = True
|
||||
self.should_jump = False
|
||||
self.last_update_time = time.perf_counter()
|
||||
|
||||
self.pipes = []
|
||||
self.bird_position = [0, int(ROWS / 2)]
|
||||
self.cycles = 0
|
||||
|
||||
if not os.path.exists("data.json"):
|
||||
self.data = {}
|
||||
else:
|
||||
with open("data.json", "r") as file:
|
||||
self.data = json.load(file)
|
||||
|
||||
if not "flappy_bird" in self.data:
|
||||
self.data["flappy_bird"] = {"high_score": 0}
|
||||
|
||||
self.high_score = self.data["flappy_bird"]["high_score"]
|
||||
self.score = 0
|
||||
|
||||
def create_pipe_part(self, size, pipe_type):
|
||||
if pipe_type == "bottom":
|
||||
bottom = 0
|
||||
else:
|
||||
bottom = ROWS - size
|
||||
|
||||
left = COLS
|
||||
|
||||
for pipe_y in range(bottom, bottom + size):
|
||||
self.pipes.append([left, pipe_y])
|
||||
|
||||
def create_pipe(self):
|
||||
gap_size = ROWS // 5
|
||||
gap_start = random.randint(1, ROWS - gap_size - 1)
|
||||
|
||||
bottom_height = gap_start
|
||||
self.create_pipe_part(bottom_height, "bottom")
|
||||
|
||||
top_height = ROWS - (gap_start + gap_size)
|
||||
self.create_pipe_part(top_height, "top")
|
||||
|
||||
def on_update(self, delta_time):
|
||||
if self.running and time.perf_counter() - self.last_update_time >= 0.4:
|
||||
self.pipes = [[pipe_x - 1, pipe_y] for pipe_x, pipe_y in self.pipes if pipe_x - 1 >= 0]
|
||||
|
||||
if self.should_jump:
|
||||
jumped = True
|
||||
self.bird_position[1] -= 3
|
||||
self.should_jump = False
|
||||
else:
|
||||
jumped = False
|
||||
self.bird_position[1] += 2
|
||||
|
||||
if self.bird_position in self.pipes or self.bird_position[1] >= ROWS or self.bird_position[1] <= 0:
|
||||
self.info_label.text = "Game Over.\nPress r to restart"
|
||||
self.running = False
|
||||
|
||||
notification.notify(
|
||||
title="Flappy Bird inside notifications",
|
||||
message='Game Over!'
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
for pipe in self.pipes:
|
||||
if pipe[0] == self.bird_position[0]:
|
||||
self.score += 1
|
||||
break
|
||||
|
||||
self.last_update_time = time.perf_counter()
|
||||
|
||||
text = ""
|
||||
|
||||
for y in range(ROWS):
|
||||
for x in range(COLS):
|
||||
if [x, y] in self.pipes:
|
||||
text += "|"
|
||||
elif [x, y] == self.bird_position:
|
||||
text += "^" if jumped else "^"
|
||||
else:
|
||||
text += "_"
|
||||
|
||||
text += "\n"
|
||||
|
||||
if self.score > self.high_score:
|
||||
self.high_score = self.score
|
||||
|
||||
notification.notify(
|
||||
title=f"Flappy Bird | Score: {self.score} High Score: {self.high_score}",
|
||||
message=text
|
||||
)
|
||||
|
||||
if self.cycles == 15:
|
||||
self.cycles = 0
|
||||
self.create_pipe()
|
||||
|
||||
self.cycles += 1
|
||||
|
||||
def on_show_view(self):
|
||||
super().on_show_view()
|
||||
|
||||
self.create_pipe()
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol == arcade.key.ESCAPE:
|
||||
self.data["flappy_bird"]["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.SPACE:
|
||||
self.should_jump = True
|
||||
elif symbol == arcade.key.R and not self.running:
|
||||
self.info_label.text = "Press keys inside this window to interact with the game\nYou can see the game inside notifications"
|
||||
|
||||
self.pipes.clear()
|
||||
self.create_pipe()
|
||||
|
||||
self.last_update_time = time.perf_counter()
|
||||
|
||||
self.bird_position = [0, int(ROWS / 2)]
|
||||
self.cycles = 0
|
||||
self.should_jump = False
|
||||
|
||||
self.running = True
|
||||
132
game/maze.py
Normal file
132
game/maze.py
Normal file
@@ -0,0 +1,132 @@
|
||||
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!")
|
||||
|
||||
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.\nMaze is shown in notifications.", font_size=24, multiline=True))
|
||||
|
||||
self.running = True
|
||||
self.time_start = time.perf_counter()
|
||||
|
||||
self.current_position = arcade.math.Vec2(1, 1)
|
||||
self.direction = arcade.math.Vec2()
|
||||
|
||||
def on_show_view(self):
|
||||
self.maze, start_x, start_y = self.generate_maze(int(COLS / 2), int(ROWS / 2))
|
||||
self.current_position = arcade.math.Vec2(start_x, start_y)
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
if not self.running:
|
||||
return
|
||||
|
||||
next_pos = self.current_position + self.direction
|
||||
nx, ny = int(next_pos.x), int(next_pos.y)
|
||||
|
||||
if 0 <= ny < len(self.maze) and 0 <= nx < len(self.maze[0]):
|
||||
if self.maze[ny][nx] != "#":
|
||||
self.current_position = next_pos
|
||||
if self.maze[ny][nx] == "E":
|
||||
self.running = False
|
||||
elapsed = int(time.perf_counter() - self.time_start)
|
||||
|
||||
self.info_label.text = f"Maze completed in {elapsed}s! Press r to restart."
|
||||
|
||||
notification.notify(
|
||||
title="Maze Completed!",
|
||||
message=f"You reached the end in {elapsed}s!")
|
||||
return
|
||||
|
||||
display_rows = []
|
||||
for y, row in enumerate(self.maze):
|
||||
line = ""
|
||||
for x, ch in enumerate(row):
|
||||
if (x, y) == (int(self.current_position.x), int(self.current_position.y)):
|
||||
line += "P"
|
||||
else:
|
||||
line += ch
|
||||
display_rows.append(line)
|
||||
|
||||
self.direction = arcade.math.Vec2()
|
||||
notification.notify(
|
||||
title=f"Maze | {int(time.perf_counter() - self.time_start)}s",
|
||||
message='\n'.join(display_rows),
|
||||
timeout=15
|
||||
)
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol in (arcade.key.UP, arcade.key.W):
|
||||
self.direction = arcade.math.Vec2(0, -1)
|
||||
self.update()
|
||||
elif symbol in (arcade.key.DOWN, arcade.key.S):
|
||||
self.direction = arcade.math.Vec2(0, 1)
|
||||
self.update()
|
||||
elif symbol in (arcade.key.LEFT, arcade.key.A):
|
||||
self.direction = arcade.math.Vec2(-1, 0)
|
||||
self.update()
|
||||
elif symbol in (arcade.key.RIGHT, arcade.key.D):
|
||||
self.direction = arcade.math.Vec2(1, 0)
|
||||
self.update()
|
||||
elif symbol == arcade.key.R and not self.running:
|
||||
self.running = True
|
||||
self.time_start = time.perf_counter()
|
||||
|
||||
self.info_label.text = "Use arrow keys or WASD to move.\nMaze is shown in notifications."
|
||||
|
||||
self.maze, start_x, start_y = self.generate_maze(int(COLS / 3), int(ROWS / 3))
|
||||
self.current_position = arcade.math.Vec2(start_x, start_y)
|
||||
self.update()
|
||||
|
||||
elif symbol == arcade.key.ESCAPE:
|
||||
from menus.main import Main
|
||||
self.window.show_view(Main(self.pypresence_client))
|
||||
|
||||
def generate_maze(self, width, height):
|
||||
maze = [['#' for _ in range(width)] for _ in range(height)]
|
||||
|
||||
def is_valid(x, y):
|
||||
return 1 <= x < width - 1 and 1 <= y < height - 1
|
||||
|
||||
def get_neighbors(x, y):
|
||||
neighbors = []
|
||||
|
||||
for dx, dy in [(0, 2), (2, 0), (0, -2), (-2, 0)]:
|
||||
nx, ny = x + dx, y + dy
|
||||
|
||||
if is_valid(nx, ny) and maze[ny][nx] == '#':
|
||||
neighbors.append((nx, ny))
|
||||
|
||||
return neighbors
|
||||
|
||||
def carve(x, y):
|
||||
maze[y][x] = '_'
|
||||
|
||||
neighbors = get_neighbors(x, y)
|
||||
random.shuffle(neighbors)
|
||||
|
||||
for nx, ny in neighbors:
|
||||
if maze[ny][nx] == '#':
|
||||
maze[y + (ny - y) // 2][x + (nx - x) // 2] = '_'
|
||||
carve(nx, ny)
|
||||
|
||||
start_x = 1 if width % 2 == 1 else 2
|
||||
start_y = 1 if height % 2 == 1 else 2
|
||||
|
||||
carve(start_x, start_y)
|
||||
|
||||
maze[start_y][0] = '_'
|
||||
|
||||
for y in range(height - 2, 0, -1):
|
||||
if maze[y][width - 2] == '_':
|
||||
maze[y][width - 1] = '_'
|
||||
break
|
||||
|
||||
maze[height - 2][width - 1] = 'E'
|
||||
|
||||
return maze, start_x, start_y
|
||||
142
game/pong.py
Normal file
142
game/pong.py
Normal file
@@ -0,0 +1,142 @@
|
||||
import arcade, arcade.gui, time, random, os, json
|
||||
|
||||
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="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.running = True
|
||||
self.last_update_time = time.perf_counter()
|
||||
|
||||
self.ball_position = arcade.math.Vec2(int(COLS / 2), int(ROWS / 4))
|
||||
|
||||
self.ball_direction = arcade.math.Vec2(*random.choice([(-1, 1), (1, 1), (-1, 1), (-1, -1)]))
|
||||
|
||||
self.paddle_a_position = arcade.math.Vec2(0, int(ROWS / 4))
|
||||
self.paddle_a_direction = 0
|
||||
|
||||
self.paddle_b_position = arcade.math.Vec2(COLS - 1, int(ROWS / 4))
|
||||
self.paddle_b_direction = 0
|
||||
|
||||
self.score_a = 0
|
||||
self.score_b = 0
|
||||
|
||||
if not os.path.exists("data.json"):
|
||||
self.data = {}
|
||||
else:
|
||||
with open("data.json", "r") as file:
|
||||
self.data = json.load(file)
|
||||
|
||||
if not "pong" in self.data:
|
||||
self.data["pong"] = {"high_score": 0}
|
||||
|
||||
self.high_score = self.data["pong"]["high_score"]
|
||||
|
||||
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.ball_position += self.ball_direction
|
||||
|
||||
if self.paddle_b_position.y < self.ball_position.y:
|
||||
self.paddle_b_direction = 1
|
||||
elif self.paddle_b_position.y > self.ball_position.y:
|
||||
self.paddle_b_direction = -1
|
||||
else:
|
||||
self.paddle_b_direction = 0
|
||||
|
||||
self.paddle_a_position = arcade.math.Vec2(self.paddle_a_position.x, self.paddle_a_position.y + self.paddle_a_direction)
|
||||
self.paddle_b_position = arcade.math.Vec2(self.paddle_b_position.x, self.paddle_b_position.y + self.paddle_b_direction)
|
||||
self.paddle_a_direction, self.paddle_b_direction = 0, 0
|
||||
|
||||
if self.ball_position.distance(self.paddle_a_position) <= 1:
|
||||
self.ball_direction = self.ball_direction.reflect(arcade.math.Vec2(1, 0))
|
||||
self.ball_position += self.ball_direction * 2 # needed so it doesnt get stuck in an infinite loop
|
||||
|
||||
elif self.ball_position.distance(self.paddle_b_position) <= 1:
|
||||
self.ball_direction = self.ball_direction.reflect(arcade.math.Vec2(-1, 0))
|
||||
self.ball_position += self.ball_direction * 2 # needed so it doesnt get stuck in an infinite loop
|
||||
|
||||
else:
|
||||
if self.ball_position.x <= 0:
|
||||
if self.ball_position.y <= 0 or self.ball_position.y >= int(ROWS / 2):
|
||||
self.ball_position = arcade.math.Vec2(0, self.ball_position.y)
|
||||
self.ball_direction = self.ball_direction.reflect(arcade.math.Vec2(1, 0))
|
||||
else:
|
||||
self.score_b += 1
|
||||
self.ball_position = arcade.math.Vec2(int(COLS / 2), int(ROWS / 4))
|
||||
self.ball_direction = arcade.math.Vec2(*random.choice([(-1, 1), (1, 1), (-1, 1), (-1, -1)]))
|
||||
|
||||
elif self.ball_position.x >= COLS:
|
||||
if self.ball_position.y <= 0 or self.ball_position.y >= int(ROWS / 2):
|
||||
self.ball_position = arcade.math.Vec2(COLS, self.ball_position.y)
|
||||
self.ball_direction = self.ball_direction.reflect(arcade.math.Vec2(-1, 0))
|
||||
else:
|
||||
self.score_a += 1
|
||||
self.ball_position = arcade.math.Vec2(int(COLS / 2), int(ROWS / 4))
|
||||
self.ball_direction = arcade.math.Vec2(*random.choice([(-1, 1), (1, 1), (-1, 1), (-1, -1)]))
|
||||
|
||||
if self.ball_position.y <= 0:
|
||||
self.ball_position = arcade.math.Vec2(self.ball_position.x, 0)
|
||||
self.ball_direction = self.ball_direction.reflect(arcade.math.Vec2(0, 1))
|
||||
elif self.ball_position.y >= int(ROWS / 2):
|
||||
self.ball_position = arcade.math.Vec2(self.ball_position.x, int(ROWS / 2))
|
||||
self.ball_direction = self.ball_direction.reflect(arcade.math.Vec2(0, -1))
|
||||
|
||||
text = ""
|
||||
|
||||
for y in range(int(ROWS / 2)):
|
||||
for x in range(COLS):
|
||||
if (x, y) == self.paddle_a_position or (x, y) == self.paddle_b_position:
|
||||
text += "|"
|
||||
elif self.ball_position == (x, y):
|
||||
text += "O"
|
||||
else:
|
||||
text += "_"
|
||||
|
||||
text += "\n"
|
||||
|
||||
if self.score_a > self.high_score:
|
||||
self.high_score = self.score_a
|
||||
|
||||
notification.notify(
|
||||
title=f"Pong (A: {self.score_a}, B: {self.score_b}, High Score: {self.high_score})",
|
||||
message=text
|
||||
)
|
||||
|
||||
def on_show_view(self):
|
||||
super().on_show_view()
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol == arcade.key.ESCAPE:
|
||||
self.data["pong"]["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.paddle_a_direction = 0
|
||||
self.paddle_a_position = arcade.math.Vec2(0, int(ROWS / 4))
|
||||
|
||||
self.paddle_b_direction = 0
|
||||
self.paddle_b_position = arcade.math.Vec2(COLS - 1, int(ROWS / 4))
|
||||
|
||||
self.ball_position = arcade.math.Vec2(int(COLS / 2), int(ROWS / 4))
|
||||
self.ball_direction = arcade.math.Vec2(*random.choice([(-1, 1), (1, 1), (-1, 1), (-1, -1)]))
|
||||
|
||||
self.running = True
|
||||
|
||||
elif symbol == arcade.key.UP or symbol == arcade.key.W:
|
||||
self.paddle_a_direction = -1
|
||||
elif symbol == arcade.key.DOWN or symbol == arcade.key.S:
|
||||
self.paddle_a_direction = 1
|
||||
128
game/snake.py
Normal file
128
game/snake.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import arcade, arcade.gui, time, random, os, json
|
||||
|
||||
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="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.direction = "right"
|
||||
self.running = True
|
||||
self.last_update_time = time.perf_counter()
|
||||
|
||||
self.snake = [(int(COLS / 2), int(ROWS / 2))]
|
||||
self.foods = []
|
||||
|
||||
if not os.path.exists("data.json"):
|
||||
self.data = {}
|
||||
else:
|
||||
with open("data.json", "r") as file:
|
||||
self.data = json.load(file)
|
||||
|
||||
if not "snake" in self.data:
|
||||
self.data["snake"] = {"high_score": 0}
|
||||
|
||||
self.high_score = self.data["snake"]["high_score"]
|
||||
self.score = 0
|
||||
|
||||
def spawn_food(self):
|
||||
while True:
|
||||
x, y = (random.randint(0, COLS), random.randint(0, ROWS))
|
||||
|
||||
if not (x, y) in self.snake:
|
||||
return (x, y)
|
||||
|
||||
def on_update(self, dt):
|
||||
if self.running and time.perf_counter() - self.last_update_time >= 0.4:
|
||||
self.last_update_time = time.perf_counter()
|
||||
|
||||
head_x, head_y = self.snake[0]
|
||||
|
||||
if self.direction == "right":
|
||||
new_head = (head_x + 1, head_y)
|
||||
elif self.direction == "left":
|
||||
new_head = (head_x - 1, head_y)
|
||||
elif self.direction == "up":
|
||||
new_head = (head_x, head_y - 1)
|
||||
elif self.direction == "down":
|
||||
new_head = (head_x, head_y + 1)
|
||||
|
||||
if new_head in self.snake or not (0 <= new_head[0] < COLS and 0 <= new_head[1] < ROWS):
|
||||
self.info_label.text = "Game Over.\nPress r to restart"
|
||||
self.running = False
|
||||
|
||||
notification.notify(
|
||||
title="Snake inside notifications",
|
||||
message='Game Over!'
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
if new_head in self.foods:
|
||||
self.score += 1
|
||||
|
||||
self.foods.remove(new_head)
|
||||
self.snake = [new_head] + self.snake
|
||||
self.foods.append(self.spawn_food())
|
||||
else:
|
||||
self.snake = [new_head] + self.snake[:-1]
|
||||
|
||||
text = ""
|
||||
|
||||
for y in range(ROWS):
|
||||
for x in range(COLS):
|
||||
if (x, y) == self.snake[0]:
|
||||
text += "H"
|
||||
elif (x, y) in self.snake[1:]:
|
||||
text += "o"
|
||||
elif (x, y) in self.foods:
|
||||
text += "*"
|
||||
else:
|
||||
text += "_"
|
||||
|
||||
text += "\n"
|
||||
|
||||
if self.score > self.high_score:
|
||||
self.high_score = self.score
|
||||
|
||||
notification.notify(
|
||||
title=f"Snake | Score: {self.score} High Score: {self.high_score}",
|
||||
message=text
|
||||
)
|
||||
|
||||
def on_show_view(self):
|
||||
super().on_show_view()
|
||||
|
||||
self.foods = [self.spawn_food() for _ in range(3)]
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol == arcade.key.LEFT or symbol == arcade.key.A:
|
||||
self.direction = "left"
|
||||
elif symbol == arcade.key.RIGHT or symbol == arcade.key.D:
|
||||
self.direction = "right"
|
||||
elif symbol == arcade.key.UP or symbol == arcade.key.W:
|
||||
self.direction = "up"
|
||||
elif symbol == arcade.key.DOWN or symbol == arcade.key.S:
|
||||
self.direction = "down"
|
||||
elif symbol == arcade.key.R and not self.running:
|
||||
self.info_label.text = "Press keys inside this window to interact with the game.\nYou can see the game inside notifications."
|
||||
self.info_label.fit_content()
|
||||
|
||||
self.snake = [(int(COLS / 2), int(ROWS / 2))]
|
||||
self.foods = [self.spawn_food() for _ in range(3)]
|
||||
self.running = True
|
||||
elif symbol == arcade.key.ESCAPE:
|
||||
self.data["snake"]["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))
|
||||
166
game/space_invaders.py
Normal file
166
game/space_invaders.py
Normal file
@@ -0,0 +1,166 @@
|
||||
import arcade, arcade.gui, time, random, os, json
|
||||
|
||||
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="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.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 "space_invaders" in self.data:
|
||||
self.data["space_invaders"] = {"high_score": 0}
|
||||
|
||||
self.high_score = self.data["space_invaders"]["high_score"]
|
||||
self.score = 0
|
||||
|
||||
self.ship_position = arcade.math.Vec2(0, int(ROWS / 2) - 1)
|
||||
|
||||
self.ship_direction = arcade.math.Vec2(0, 0)
|
||||
|
||||
self.enemies = []
|
||||
self.enemy_bullets = []
|
||||
self.your_bullets = []
|
||||
|
||||
self.last_enemy_shoot = time.perf_counter()
|
||||
|
||||
def summon_enemies(self):
|
||||
self.enemies = [[x, y] for x in range(int(COLS)) for y in range(int(int(ROWS / 2) / 5))]
|
||||
|
||||
def update_enemies(self):
|
||||
columns = list(range(COLS))
|
||||
random.shuffle(columns)
|
||||
|
||||
for x in columns:
|
||||
max_y = -99999
|
||||
for y in range(int(int(ROWS / 2) / 3)):
|
||||
if not [x, y] in self.enemies:
|
||||
continue
|
||||
|
||||
if y > max_y:
|
||||
max_y = y
|
||||
|
||||
if time.perf_counter() - self.last_enemy_shoot >= 1:
|
||||
self.last_enemy_shoot = time.perf_counter()
|
||||
self.enemy_bullets.append([x, max_y])
|
||||
|
||||
def on_show_view(self):
|
||||
super().on_show_view()
|
||||
|
||||
self.summon_enemies()
|
||||
|
||||
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.ship_position += self.ship_direction
|
||||
|
||||
if self.ship_position.x < 0:
|
||||
self.ship_position = arcade.math.Vec2(0, self.ship_position.y)
|
||||
elif self.ship_position.x > COLS:
|
||||
self.ship_position = arcade.math.Vec2(COLS, self.ship_position.y)
|
||||
|
||||
self.ship_direction = arcade.math.Vec2(0, 0)
|
||||
|
||||
self.update_enemies()
|
||||
|
||||
new_enemy = [(x, y + 1) for (x, y) in self.enemy_bullets]
|
||||
new_yours = [(x, y - 1) for (x, y) in self.your_bullets]
|
||||
|
||||
if tuple(self.ship_position) in new_enemy:
|
||||
self.info_label.text = "Game Over.\nPress r to restart"
|
||||
self.running = False
|
||||
|
||||
notification.notify(
|
||||
title="Space Invaders inside notifications",
|
||||
message='Game Over!'
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
bullets_to_remove = set()
|
||||
for b in new_yours:
|
||||
if [b[0], b[1]] in self.enemies:
|
||||
self.enemies.remove([b[0], b[1]])
|
||||
bullets_to_remove.add(b)
|
||||
elif b in new_enemy:
|
||||
bullets_to_remove.add(b)
|
||||
elif b[1] <= 0:
|
||||
bullets_to_remove.add(b)
|
||||
|
||||
for b in new_enemy:
|
||||
if b[1] > int(ROWS / 2):
|
||||
bullets_to_remove.add(b)
|
||||
|
||||
self.your_bullets = [b for b in new_yours if b not in bullets_to_remove]
|
||||
self.enemy_bullets = [b for b in new_enemy if b not in bullets_to_remove]
|
||||
|
||||
text = ""
|
||||
|
||||
for y in range(int(ROWS / 2)):
|
||||
for x in range(COLS):
|
||||
if [x, y] in self.enemies:
|
||||
text += "~"
|
||||
elif (x, y) in self.enemy_bullets or (x, y) in self.your_bullets:
|
||||
text += "|"
|
||||
elif arcade.math.Vec2(x, y) == self.ship_position:
|
||||
text += "^"
|
||||
else:
|
||||
text += "_"
|
||||
|
||||
text += "\n"
|
||||
|
||||
if self.score > self.high_score:
|
||||
self.high_score = self.score
|
||||
|
||||
notification.notify(
|
||||
title=f"Space Invaders | Score: {self.score} High Score: {self.high_score}",
|
||||
message=text
|
||||
)
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol == arcade.key.ESCAPE:
|
||||
self.data["space_invaders"]["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.SPACE:
|
||||
self.last_you_shoot = time.perf_counter()
|
||||
self.your_bullets.append([self.ship_position.x, self.ship_position.y - 1])
|
||||
elif symbol == arcade.key.R:
|
||||
self.score = 0
|
||||
|
||||
self.ship_position = arcade.math.Vec2(0, int(ROWS / 2) - 1)
|
||||
self.ship_direction = arcade.math.Vec2(0, 0)
|
||||
|
||||
self.enemies = []
|
||||
self.enemy_bullets = []
|
||||
self.your_bullets = []
|
||||
|
||||
self.last_enemy_shoot = time.perf_counter()
|
||||
self.last_you_shoot = time.perf_counter()
|
||||
|
||||
self.summon_enemies()
|
||||
|
||||
self.running = True
|
||||
elif symbol == arcade.key.LEFT or symbol == arcade.key.A:
|
||||
self.ship_direction = arcade.math.Vec2(-1, 0)
|
||||
elif symbol == arcade.key.RIGHT or symbol == arcade.key.D:
|
||||
self.ship_direction = arcade.math.Vec2(1, 0)
|
||||
124
game/wps_test.py
Normal file
124
game/wps_test.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import arcade, arcade.gui, time, random, os, json
|
||||
|
||||
from plyer import notification
|
||||
|
||||
from utils.constants import ROWS, COLS
|
||||
from utils.preload import words
|
||||
|
||||
class Game(arcade.gui.UIView):
|
||||
def __init__(self, pypresence_client):
|
||||
super().__init__()
|
||||
|
||||
self.pypresence_client = pypresence_client
|
||||
self.pypresence_client.update(state="Doing a WPS Test 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 app\nYou can see the app inside notifications!", font_size=24, multiline=True))
|
||||
|
||||
self.running = True
|
||||
|
||||
if not os.path.exists("data.json"):
|
||||
self.data = {}
|
||||
else:
|
||||
with open("data.json", "r") as file:
|
||||
self.data = json.load(file)
|
||||
|
||||
if not "wps_test" in self.data:
|
||||
self.data["wps_test"] = {"highest_wpm": 0}
|
||||
|
||||
self.highest_wpm = self.data["wps_test"]["highest_wpm"]
|
||||
|
||||
self.words = [random.choice(words) for _ in range(500)]
|
||||
self.time_start = time.perf_counter()
|
||||
self.last_manual_notification_update = time.perf_counter()
|
||||
|
||||
self.current_typing = ""
|
||||
self.wpm = 0
|
||||
self.accuracy = 100
|
||||
self.current_index = 0
|
||||
|
||||
self.words_done = 0
|
||||
|
||||
self.total_chars = 0
|
||||
self.valid_chars = 0
|
||||
|
||||
self.text = " ".join(self.words[0:15])
|
||||
|
||||
self.trigger_notification()
|
||||
|
||||
def on_update(self, delta_time):
|
||||
if self.running and time.perf_counter() - self.time_start >= 60:
|
||||
self.running = False
|
||||
self.wpm = self.words_done
|
||||
|
||||
if self.wpm > self.highest_wpm:
|
||||
self.highest_wpm = self.wpm
|
||||
|
||||
self.trigger_notification()
|
||||
self.info_label.text = f"WPS Test Over.\nWPM: {self.wpm}\nAccuracy: {self.accuracy}%\nHigh Score: {self.highest_wpm}\nPress r to restart."
|
||||
|
||||
if self.running and time.perf_counter() - self.last_manual_notification_update >= 1:
|
||||
self.last_manual_notification_update = time.perf_counter()
|
||||
self.trigger_notification()
|
||||
|
||||
def trigger_notification(self):
|
||||
if self.running:
|
||||
notification.notify(
|
||||
title=f"Accuracy: {self.accuracy}% | Time left: {60 - int(time.perf_counter() - self.time_start)}",
|
||||
message=self.text
|
||||
)
|
||||
else:
|
||||
notification.notify(
|
||||
title="WPS Test done!",
|
||||
message=f"WPM: {self.wpm}\nAccuracy: {self.accuracy}%\nHigh Score: {self.highest_wpm}"
|
||||
)
|
||||
|
||||
def update_words(self):
|
||||
self.trigger_notification()
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol == arcade.key.ESCAPE:
|
||||
self.data["wps_test"]["highest_wpm"] = self.highest_wpm
|
||||
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 self.running and 97 <= symbol <= 122:
|
||||
self.total_chars += 1
|
||||
|
||||
if chr(symbol) == self.words[0][self.current_index]:
|
||||
self.current_typing += chr(symbol)
|
||||
self.current_index += 1
|
||||
self.valid_chars += 1
|
||||
self.text = self.text[1:]
|
||||
self.update_words()
|
||||
|
||||
self.accuracy = int((self.valid_chars / self.total_chars) * 100)
|
||||
elif self.running and symbol == 32 and self.current_typing == self.words[0]:
|
||||
self.words = self.words[1:]
|
||||
self.current_typing = ""
|
||||
self.words_done += 1
|
||||
self.text = " ".join(self.words[0:15])
|
||||
self.current_index = 0
|
||||
|
||||
self.update_words()
|
||||
elif not self.running and symbol == arcade.key.R:
|
||||
self.words = [random.choice(words) for _ in range(500)]
|
||||
self.time_start = time.perf_counter()
|
||||
|
||||
self.current_typing = ""
|
||||
self.wpm = 0
|
||||
self.accuracy = 100
|
||||
self.current_index = 0
|
||||
|
||||
self.words_done = 0
|
||||
|
||||
self.total_chars = 0
|
||||
self.valid_chars = 0
|
||||
|
||||
self.text = " ".join(self.words[0:15])
|
||||
|
||||
self.trigger_notification()
|
||||
|
||||
self.running = True
|
||||
Reference in New Issue
Block a user