Ajout des bonus #6

Merged
raphael merged 4 commits from bonus into main 2024-01-06 14:20:07 +00:00
89 changed files with 184 additions and 5 deletions

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

View file

@ -108,6 +108,86 @@ class StartAnimation(float):
""" """
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): def __spawn_ellements(world: World):
""" """
La fonction permet de initializer les ellements de la scene. La fonction permet de initializer les ellements de la scene.
@ -115,6 +195,10 @@ def __spawn_ellements(world: World):
world.new_entity().set(SpriteBundle(("background.jpg"), -5)) world.new_entity().set(SpriteBundle(("background.jpg"), -5))
world.set(
TimeUntilBonus(5, world),
)
# Mon mur de gauche # Mon mur de gauche
world.new_entity().set( world.new_entity().set(
Origin(Vec2(1, 0)), Origin(Vec2(1, 0)),
@ -165,6 +249,7 @@ def __spawn_ellements(world: World):
UpKey("z"), UpKey("z"),
DownKey("s"), DownKey("s"),
Speed(1000), Speed(1000),
LastPlayerTurn(),
) )
# Joueur 2 # Joueur 2
@ -180,7 +265,7 @@ def __spawn_ellements(world: World):
Player2(), Player2(),
(UpKey("up"), DownKey("down"), Speed(1000)) (UpKey("up"), DownKey("down"), Speed(1000))
if world[GameMode] == GameMode.TWO if world[GameMode] == GameMode.TWO
else None, else Speed(300),
) )
__spawn_ball(world) __spawn_ball(world)
@ -200,6 +285,23 @@ def __spawn_ellements(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): def __spawn_ball(world: World):
""" """
Fonction qui fait apparaitre une balle avec une velocitée aleatoire Fonction qui fait apparaitre une balle avec une velocitée aleatoire
@ -221,12 +323,45 @@ def __spawn_ball(world: World):
Vec2(0.5), Vec2(0.5),
), ),
Ball(), Ball(),
#
physics.Velocity(velocity), physics.Velocity(velocity),
physics.CollisionHandler(__bounce_on_player), physics.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): def __bounce_on_player(a: Entity, b: Entity):
""" """
Fonction qui decrit se qui se passe lorque la ball entre en collision avec un joueur Fonction qui decrit se qui se passe lorque la ball entre en collision avec un joueur
@ -368,7 +503,35 @@ def _update_bot(world: World):
# On se déplace vers la meilleure option # On se déplace vers la meilleure option
diff = target - bot[Position].y diff = target - bot[Position].y
if abs(diff) > 10: 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(physics.Solid())
for entity in world.query(Ball):
simulated_ball = world.new_entity()
simulated_ball.set(
Position(entity[Position]),
Scale(entity[Scale]),
physics.Velocity(entity[physics.Velocity]),
Origin(entity[Origin]),
physics.CollisionHandler(__collision_handler),
)
physics.move_entity(simulated_ball, entity[physics.Velocity] * world[Delta])
simulated_ball.destroy()
for entity in world.query(Bonus):
entity.remove(physics.Solid)
def __update_scores(world: World, ball: Entity): def __update_scores(world: World, ball: Entity):
@ -420,9 +583,25 @@ def __update_animation(world: World):
animation.destroy() 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( __SCENE = Scene(
[__spawn_ellements], [__spawn_ellements],
[__update_move, __update_animation], [__update_move, __check_bonus_collision, __update_animation, __update_bonus_time],
[], [],
) )