Merge branch 'main' into score

This commit is contained in:
Tipragot 2024-01-07 08:32:23 +01:00
commit 1e02eda918
93 changed files with 208 additions and 27 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 213 KiB

BIN
assets/textures/big.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 125 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

BIN
assets/textures/fast.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
assets/textures/multi.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
assets/textures/reverse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

View file

@ -6,7 +6,8 @@ from enum import Enum
from engine import CurrentScene, KeepAlive, Plugin, Scene
from engine.ecs import Entity, World
from engine.math import Vec2
from plugins import physics, render
from plugins import render
from plugins import physics
from plugins.inputs import Held
from plugins.render import (
Origin,
@ -19,6 +20,7 @@ from plugins.render import (
)
from plugins.timing import Delta, Time
import random
from plugins.physics import CollisionHandler, Solid, Velocity
from scenes import game_over
@ -116,21 +118,105 @@ class Wall:
"""
def __spawn_elements(world: World):
class TimeUntilBonus:
"""
ressource qui represente le temps restant avant d'avoir le bonus
"""
def __init__(self, time: float, world: World):
self.time = time
self.started_time = int(world[Time])
def is_ended(self, world: World):
return world[Time] - self.started_time >= self.time
def start(self, world: World):
self.started_time = world[Time]
class LastPlayerTurn:
"""
un composant qui represente le dernier joueur qui a joué.
"""
class HasBonus:
"""
un composant qui represente si l'entité a un bonus
"""
def __init__(self, bonus: "Bonus", time: float, world: World):
self.bonus = bonus
self.time = time
self.start_time = world[Time]
def is_ended(self, world: World):
return world[Time] - self.start_time >= self.time
def suppr_bonus_from_entity(self, entity: Entity):
match self.bonus:
case Bonus.MULTI:
pass
case Bonus.BIG:
entity[Scale] /= 2
case Bonus.FAST:
entity[Speed] /= 1.5
case Bonus.REVERSE:
entity[UpKey], entity[DownKey] = entity[DownKey], entity[UpKey]
class Bonus(Enum):
MULTI = 0
BIG = 1
FAST = 2
REVERSE = 3
@staticmethod
def aleatoire():
type = random.randint(0, 3)
match type:
case 0:
return Bonus.MULTI
case 1:
return Bonus.BIG
case 2:
return Bonus.FAST
case _:
return Bonus.REVERSE
@staticmethod
def get_texture(bonus: "Bonus"):
match bonus:
case Bonus.MULTI:
return "multi.png"
case Bonus.BIG:
return "big.png"
case Bonus.FAST:
return "fast.png"
case _:
return "reverse.png"
def __spawn_ellements(world: World):
"""
La fonction permet de initializer les ellements de la scene.
"""
world.new_entity().set(SpriteBundle(("background.jpg"), -5))
world.set(
TimeUntilBonus(5, world),
)
# Mon mur de gauche
world.new_entity().set(
Origin(Vec2(1, 0)),
Scale(Vec2(10, render.HEIGHT)),
Position(Vec2(70, 0)),
physics.Solid(),
Solid(),
LeftWall(),
physics.CollisionHandler(__bounce_on_left_wall),
CollisionHandler(__bounce_on_left_wall),
)
# Mon mur du RN
@ -138,9 +224,9 @@ def __spawn_elements(world: World):
Origin(Vec2(0, 0)),
Scale(Vec2(10, render.HEIGHT)),
Position(Vec2(render.WIDTH - 70, 0)),
physics.Solid(),
Solid(),
RightWall(),
physics.CollisionHandler(__bounce_on_right_wall),
CollisionHandler(__bounce_on_right_wall),
)
# Mon mur du bas
@ -148,10 +234,10 @@ def __spawn_elements(world: World):
Origin(Vec2(0, 0)),
Scale(Vec2(render.WIDTH, 10)),
Position(Vec2(0, render.HEIGHT)),
physics.Solid(),
(Wall(), physics.CollisionHandler(__bounce_on_top_or_bot_wall))
if world[GameMode] == GameMode.ONE
else None,
Solid(),
)
# Mon mur du haut
@ -159,10 +245,10 @@ def __spawn_elements(world: World):
Origin(Vec2(0, 1)),
Scale(Vec2(render.WIDTH, 10)),
Position(Vec2(0, 0)),
physics.Solid(),
(Wall(), physics.CollisionHandler(__bounce_on_top_or_bot_wall))
if world[GameMode] == GameMode.ONE
else None,
Solid(),
)
# Joueur 1
@ -174,11 +260,12 @@ def __spawn_elements(world: World):
Vec2(44, 250),
Vec2(0.5),
),
physics.Solid(),
Solid(),
Player1(),
UpKey("z"),
DownKey("s"),
Speed(1000),
LastPlayerTurn(),
)
# Joueur 2
@ -190,11 +277,11 @@ def __spawn_elements(world: World):
Vec2(44, 250),
Vec2(0.5),
),
physics.Solid(),
Solid(),
Player2(),
(UpKey("up"), DownKey("down"), Speed(1000))
if world[GameMode] == GameMode.TWO
else None,
else Speed(300),
)
__spawn_ball(world)
@ -227,6 +314,23 @@ def __spawn_elements(world: World):
)
def __spawn_bonus(world: World):
bonus = Bonus.aleatoire()
world.new_entity().set(
SpriteBundle(
Bonus.get_texture(bonus),
3,
Vec2(
random.randint(200, render.WIDTH - 200),
random.randint(100, render.HEIGHT - 100),
),
Vec2(70),
Vec2(0.5),
),
bonus,
)
def __spawn_ball(world: World):
"""
Fonction qui fait apparaitre une balle avec une velocitée aleatoire
@ -248,12 +352,45 @@ def __spawn_ball(world: World):
Vec2(0.5),
),
Ball(),
#
physics.Velocity(velocity),
physics.CollisionHandler(__bounce_on_player),
Velocity(velocity),
CollisionHandler(__collision_with_ball),
)
def __collision_with_ball(a: Entity, b: Entity):
if Player1 in b or Player2 in b:
for player in a.world.query(LastPlayerTurn):
del player[LastPlayerTurn]
b.set(LastPlayerTurn())
return __bounce_on_player(a, b)
return True
def __bonus_touched(ball: Entity, bonus: Entity):
player = ball.world.query(LastPlayerTurn).pop()
match bonus[Bonus]:
case Bonus.MULTI:
__spawn_ball(bonus.world)
__spawn_ball(bonus.world)
ball.world[TimeUntilBonus].start(ball.world)
case Bonus.BIG:
player[Scale] *= 2
player.set(HasBonus(Bonus.BIG, 10, bonus.world))
case Bonus.FAST:
player[Speed] *= 1.5
player.set(HasBonus(Bonus.FAST, 10, bonus.world))
case Bonus.REVERSE:
for entity in ball.world.query(UpKey):
if LastPlayerTurn in entity:
continue
entity[UpKey], entity[DownKey] = entity[DownKey], entity[UpKey]
entity.set(HasBonus(Bonus.REVERSE, 10, bonus.world))
bonus.destroy()
return False
def __bounce_on_player(a: Entity, b: Entity):
"""
Fonction qui decrit se qui se passe lorque la ball entre en collision avec un joueur
@ -360,17 +497,17 @@ def __simulate_wall_position(entity: Entity, component_type: type):
simulation_entity = entity.world.new_entity()
def __collision_handler(a: Entity, b: Entity):
entity[physics.CollisionHandler].callback(a, b)
entity[CollisionHandler].callback(a, b)
return component_type not in b
simulation_entity.set(
Position(entity[Position]),
Scale(entity[Scale]),
physics.Velocity(entity[physics.Velocity]),
Velocity(entity[Velocity]),
Origin(entity[Origin]),
physics.CollisionHandler(__collision_handler),
CollisionHandler(__collision_handler),
)
physics.move_entity(simulation_entity, entity[physics.Velocity] * 500)
physics.move_entity(simulation_entity, entity[Velocity] * 500)
return simulation_entity
@ -379,7 +516,7 @@ def _update_bot(world: World):
Fonction qui update les mouvement du bot
"""
# On récupère la balle la plus proche du bot
ball_query = world.query(Position, physics.Velocity, physics.CollisionHandler)
ball_query = world.query(Position, Velocity, CollisionHandler)
if ball_query == set():
return None
ball = max(ball_query, key=lambda entity: entity[Position].y)
@ -389,24 +526,24 @@ def _update_bot(world: World):
player = world.query(Player1).pop()
# On trouve l'endroit ou la balle va arriver sur le mur de droite
bot.remove(physics.Solid)
bot.remove(Solid)
right_wall_ball = __simulate_wall_position(ball, RightWall)
right_touch_height = right_wall_ball[Position].y
right_wall_ball.destroy()
bot.set(physics.Solid())
bot.set(Solid())
# On teste différentes possitions pour voir laquelle la plus éloigné du joueur
# Mais seulement si la balle vas vers la droite car sinon elle touchera le mur
# de gauche sans intervention du bot
if ball[physics.Velocity].x > 0:
if ball[Velocity].x > 0:
bot_base_y = bot[Position].y
target: float = right_touch_height
better_distance = None
for offset in [-100, -50, 0, 50, 100]:
bot[Position].y = right_touch_height + offset
player.remove(physics.Solid)
player.remove(Solid)
left_wall_ball = __simulate_wall_position(ball, LeftWall)
player.set(physics.Solid())
player.set(Solid())
left_touch_height = left_wall_ball[Position].y
left_wall_ball.destroy()
if (
@ -422,7 +559,35 @@ def _update_bot(world: World):
# On se déplace vers la meilleure option
diff = target - bot[Position].y
if abs(diff) > 10:
bot[Position].y += (diff / abs(diff)) * 300 * world[Delta]
bot[Position].y += (diff / abs(diff)) * bot[Speed] * world[Delta]
def __check_bonus_collision(world: World):
"""
Fonction qui permet de voir si un bonus est entrée en collision avec une entité.
"""
def __collision_handler(a: Entity, b: Entity):
if Bonus in b:
__bonus_touched(a, b)
return False
return True
for entity in world.query(Bonus):
entity.set(Solid())
for entity in world.query(Ball):
simulated_ball = world.new_entity()
simulated_ball.set(
Position(entity[Position]),
Scale(entity[Scale]),
Velocity(entity[Velocity]),
Origin(entity[Origin]),
CollisionHandler(__collision_handler),
)
physics.move_entity(simulated_ball, entity[Velocity] * world[Delta])
simulated_ball.destroy()
for entity in world.query(Bonus):
entity.remove(Solid)
def __update_scores(world: World, ball: Entity):
@ -480,9 +645,25 @@ def __update_animation(world: World):
animation.destroy()
def __update_bonus_time(world: World):
"""
Fonction qui permet de mettre à jour les bonus.
"""
for player in world.query(HasBonus):
if not player[HasBonus].is_ended(world):
return None
player[HasBonus].suppr_bonus_from_entity(player)
del player[HasBonus]
world[TimeUntilBonus].start(world)
if world.query(Bonus) == set() and world.query(HasBonus) == set():
if world[TimeUntilBonus].is_ended(world):
__spawn_bonus(world)
__SCENE = Scene(
[__spawn_elements],
[__update_move, __update_animation],
[__spawn_ellements],
[__update_move, __check_bonus_collision, __update_animation, __update_bonus_time],
[],
)