On rend la partie équitable en mettant le bot a la même vitesse que le joueur #10

Merged
CoCo_Sol merged 3 commits from fair-game into main 2024-01-07 09:45:57 +00:00
9 changed files with 76 additions and 18 deletions
Showing only changes of commit caafd80a8c - Show all commits

Binary file not shown.

Before

Width:  |  Height:  |  Size: 218 KiB

After

Width:  |  Height:  |  Size: 218 KiB

6
launch.py Normal file
View file

@ -0,0 +1,6 @@
import subprocess as sp
import os
folder_path = __file__.replace("\\", "/")[:-10]
os.chdir(folder_path)
sp.call(f'python "{folder_path}/src/main.py"')

View file

@ -60,6 +60,14 @@ class AABB:
self.min += movement
self.max += movement
def collide(self, other: "AABB"):
return (
self.min.x < other.max.x
and self.max.x > other.min.x
and self.min.y < other.max.y
and self.max.y > other.min.y
)
def line_to_line(sa: Vec2, ea: Vec2, sb: Vec2, eb: Vec2):
"""
@ -117,6 +125,8 @@ def aabb_to_aabb(moving: AABB, static: AABB, movement: Vec2):
"""
Renvoie la collision entre deux AABB.
"""
if moving.collide(static):
return 1.0, Vec2(0, 0)
size = (moving.max - moving.min) / 2
static = AABB(static.min - size, static.max + size, static.entity)
start_pos = moving.min + size

View file

@ -18,6 +18,7 @@ from plugins.render import (
Text,
TextSize,
)
from plugins.sound import Sound
from plugins.timing import Delta, Time
import random
from plugins.physics import CollisionHandler, Solid, Velocity
@ -25,6 +26,12 @@ from plugins.physics import CollisionHandler, Solid, Velocity
from scenes import game_over
class RestrictToScene:
"""
Composant marquant une entitée comme ne pouvant pas sortir de la scène.
"""
class GameMode(Enum):
"""
Ressource qui definit le game mode choisi par l'utilisateur.
@ -213,7 +220,7 @@ def __spawn_ellements(world: World):
world.new_entity().set(
Origin(Vec2(1, 0)),
Scale(Vec2(10, render.HEIGHT)),
Position(Vec2(70, 0)),
Position(Vec2(0, 0)),
Solid(),
LeftWall(),
CollisionHandler(__bounce_on_left_wall),
@ -223,7 +230,7 @@ def __spawn_ellements(world: World):
world.new_entity().set(
Origin(Vec2(0, 0)),
Scale(Vec2(10, render.HEIGHT)),
Position(Vec2(render.WIDTH - 70, 0)),
Position(Vec2(render.WIDTH, 0)),
Solid(),
RightWall(),
CollisionHandler(__bounce_on_right_wall),
@ -266,6 +273,7 @@ def __spawn_ellements(world: World):
DownKey("s"),
Speed(500),
LastPlayerTurn(),
RestrictToScene(),
)
# Joueur 2
@ -282,6 +290,7 @@ def __spawn_ellements(world: World):
(UpKey("up"), DownKey("down"), Speed(500))
if world[GameMode] == GameMode.TWO
else Speed(500),
RestrictToScene(),
)
__spawn_ball(world)
@ -358,6 +367,8 @@ def __spawn_ball(world: World):
def __collision_with_ball(a: Entity, b: Entity):
if Ball in a:
a.world.new_entity().set(Sound("bound.mp3"))
if Player1 in b or Player2 in b:
for player in a.world.query(LastPlayerTurn):
del player[LastPlayerTurn]
@ -541,6 +552,7 @@ def _update_bot(world: World):
better_distance = None
for offset in [-100, -50, 0, 50, 100]:
bot[Position].y = right_touch_height + offset
__restrict_to_scene(world)
player.remove(Solid)
left_wall_ball = __simulate_wall_position(ball, LeftWall)
player.set(Solid())
@ -558,8 +570,27 @@ def _update_bot(world: World):
# On se déplace vers la meilleure option
diff = target - bot[Position].y
if abs(diff) > 10:
if abs(diff) > bot[Speed] * world[Delta]:
bot[Position].y += (diff / abs(diff)) * bot[Speed] * world[Delta]
else:
bot[Position].y = target
# On restrict l'entité a la scène
__restrict_to_scene(world)
def __restrict_to_scene(world: World):
"""
Restrinct dans la scène toutes les entitées ayant le composant RestrictToScene.
"""
for entity in world.query(RestrictToScene):
y = entity[Position].y - (entity[Origin].y * entity[Scale].y)
if y < 0:
entity[Position].y = entity[Origin].y * entity[Scale].y
if y + entity[Scale].y > render.HEIGHT:
entity[Position].y = (
render.HEIGHT - entity[Scale].y + (entity[Origin].y * entity[Scale].y)
)
def __check_bonus_collision(world: World):
@ -663,7 +694,13 @@ def __update_bonus_time(world: World):
__SCENE = Scene(
[__spawn_ellements],
[__update_move, __check_bonus_collision, __update_animation, __update_bonus_time],
[
__update_move,
__check_bonus_collision,
__update_animation,
__update_bonus_time,
__restrict_to_scene,
],
[],
)

View file

@ -45,10 +45,12 @@ def __create_button(world: World, i: int, name: str):
"""
Ajoute un bouton au monde.
"""
if i == 0:
i = -1
world.new_entity().set(
SpriteBundle(
f"button_{name}.png",
position=Vec2(450 + 540 * i, render.HEIGHT / 2 + render.HEIGHT / 8),
position=Vec2(render.WIDTH / 2 + 300 * i, 800),
order=1,
origin=Vec2(0.5),
),

View file

@ -30,10 +30,12 @@ def __create_button(world: World, i: int, name: str):
"""
Ajoute un bouton au monde.
"""
if i == 0:
i = -1
world.new_entity().set(
SpriteBundle(
f"button_{name}.png",
position=Vec2(450 + 540 * i, 11 * render.HEIGHT / 16),
position=Vec2(render.WIDTH / 2 + 300 * i, 800),
order=1,
origin=Vec2(0.5),
),

View file

@ -20,7 +20,6 @@ def new_score(world: World, e: Entity):
try:
post = {"name": name[Text], "score": world[game.Player1Score]}
print(post)
rq.post(f"https://{IP}/new_score", post)
world.new_entity().set(
TimedEvent(
@ -28,15 +27,15 @@ def new_score(world: World, e: Entity):
lambda world, entity: world.set(CurrentScene(thanks.THANKS)),
)
)
except:
print("Error with the serveur")
except Exception as error:
print("Error with the serveur:", error)
def get_scores():
try:
return rq.get(f"https://{IP}/data").json()
except:
print("Error with the serveur")
except Exception as error:
print("Error with the serveur:", error)
def __spawn_elements(world: World):
@ -44,7 +43,7 @@ def __spawn_elements(world: World):
Ajoute les éléments du menu dans le monde.
"""
world.new_entity().set(SpriteBundle("background.jpg", -5))
world.new_entity().set(SpriteBundle("background.png", -5))
world.new_entity().set(
TextBundle(
"Quel est votre pseudo ?",
@ -71,7 +70,7 @@ def __spawn_elements(world: World):
world.new_entity().set(
SpriteBundle(
f"button_one_player.png",
position=Vec2(render.WIDTH / 2, 600),
position=Vec2(render.WIDTH / 2, 800),
order=1,
origin=Vec2(0.5),
),

View file

@ -8,7 +8,7 @@ from scenes import menu
def __spawn_elements(world: World):
world.new_entity().set(SpriteBundle("background.jpg", -5))
world.new_entity().set(SpriteBundle("background.png", -5))
world.new_entity().set(
TextBundle(
"Merci,",
@ -23,7 +23,7 @@ def __spawn_elements(world: World):
TextBundle(
"Votre score a bien été envoyé !",
0,
100,
80,
position=Vec2(render.WIDTH / 2, render.HEIGHT / 2 + 75),
origin=Vec2(0.5),
),

View file

@ -18,10 +18,12 @@ def __create_button(world: World, i: int, name: str):
"""
Ajoute un bouton au monde.
"""
if i == 0:
i = -1
world.new_entity().set(
SpriteBundle(
f"button_{name}.png",
position=Vec2(450 + 540 * i, render.HEIGHT / 2),
position=Vec2(render.WIDTH / 2 + 300 * i, 800),
order=1,
origin=Vec2(0.5),
),
@ -52,10 +54,10 @@ def __spawn_elements(world: World):
Ajoute les éléments du menu dans le monde.
"""
world.new_entity().set(SpriteBundle("background.jpg", -5))
world.new_entity().set(SpriteBundle("background.png", -5))
world.new_entity().set(
TextBundle(
"Voulez vous changer\nde mode de jeu ?",
"Voulez vous changer de mode de jeu ?",
0,
position=Vec2(render.WIDTH / 2, 350),
origin=Vec2(0.5),