Ajout des bonus #6
|
@ -3,7 +3,6 @@ Le jeux principale.
|
|||
"""
|
||||
|
||||
from enum import Enum
|
||||
from telnetlib import DO
|
||||
from engine import Plugin, Scene
|
||||
from engine.ecs import Entity, World
|
||||
from engine.math import Vec2
|
||||
|
@ -109,6 +108,54 @@ 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] /= 2
|
||||
case Bonus.REVERSE:
|
||||
entity[UpKey], entity[DownKey] = entity[DownKey], entity[UpKey]
|
||||
|
||||
|
||||
class Bonus(Enum):
|
||||
MULTI = 0
|
||||
BIG = 1
|
||||
|
@ -120,13 +167,13 @@ class Bonus(Enum):
|
|||
type = random.randint(0, 3)
|
||||
match type:
|
||||
case 0:
|
||||
return Bonus.FAST
|
||||
return Bonus.MULTI
|
||||
case 1:
|
||||
return Bonus.FAST
|
||||
return Bonus.BIG
|
||||
case 2:
|
||||
return Bonus.FAST
|
||||
case _:
|
||||
return Bonus.FAST
|
||||
return Bonus.REVERSE
|
||||
|
||||
|
||||
def __spawn_ellements(world: World):
|
||||
|
@ -136,6 +183,10 @@ def __spawn_ellements(world: World):
|
|||
|
||||
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)),
|
||||
|
@ -186,6 +237,7 @@ def __spawn_ellements(world: World):
|
|||
UpKey("z"),
|
||||
DownKey("s"),
|
||||
Speed(1000),
|
||||
LastPlayerTurn(),
|
||||
)
|
||||
|
||||
# Joueur 2
|
||||
|
@ -201,11 +253,10 @@ def __spawn_ellements(world: World):
|
|||
Player2(),
|
||||
(UpKey("up"), DownKey("down"), Speed(1000))
|
||||
if world[GameMode] == GameMode.TWO
|
||||
else None,
|
||||
else Speed(300),
|
||||
)
|
||||
|
||||
__spawn_ball(world)
|
||||
__spawn_bonus(world)
|
||||
|
||||
# Initialisation des scores
|
||||
world.set(Player1Score(0), Player2Score(0))
|
||||
|
@ -267,33 +318,38 @@ def __spawn_ball(world: World):
|
|||
|
||||
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)
|
||||
elif Bonus in b:
|
||||
return __bonus_touched(a, b)
|
||||
if Ball in a:
|
||||
return __bonus_touched(a, b)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def __bonus_touched(ball: Entity, bonus: Entity):
|
||||
if ball[physics.Velocity].x > 0:
|
||||
player = ball.world.query(Player1).pop()
|
||||
else:
|
||||
player = ball.world.query(Player2).pop()
|
||||
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] *= 2
|
||||
player.set(HasBonus(Bonus.FAST, 10, bonus.world))
|
||||
case Bonus.REVERSE:
|
||||
if ball[physics.Velocity].x < 0:
|
||||
player = ball.world.query(Player1).pop()
|
||||
player[UpKey], player[DownKey] = player[DownKey], player[UpKey]
|
||||
else:
|
||||
player = ball.world.query(Player2).pop()
|
||||
player[UpKey], player[DownKey] = player[DownKey], player[UpKey]
|
||||
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
|
||||
|
@ -440,7 +496,7 @@ 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 __update_scores(world: World, ball: Entity):
|
||||
|
@ -492,9 +548,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_ellements],
|
||||
[__update_move, __update_animation],
|
||||
[__update_move, __update_animation, __update_bonus_time],
|
||||
[],
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue