- adaptation en float de la position (meilleur precision pour les collisions)

-  maj du bot (difficulté normal)
- refact leger du init du Game
- update todo
This commit is contained in:
2026-06-27 13:54:17 +02:00
parent 7533c1191c
commit a404dab474
4 changed files with 69 additions and 37 deletions
+2 -2
View File
@@ -6,8 +6,8 @@ from screen import Screen
@dataclass
class Pos:
x: int
y: int
x: float
y: float
@dataclass
class Velocity:
+5 -9
View File
@@ -5,26 +5,22 @@ from ball import Ball
class Game:
def __init__(self):
self.screen_manager = Screen()
self.screen = self.screen_manager.get_screen()
self.screen_manager: Screen = Screen()
self.screen: pygame.Surface = self.screen_manager.get_screen()
self.clock: pygame.time.Clock = pygame.time.Clock()
self.FPS_MAX: int = 60
self.running = True
self.difficulty = 0 # 0: facile, 1: normal, 2: difficile, change uniquement l'intelligence du bot
self.player: Player = Player(self.screen_manager)
self.bot: Player = Player(self.screen_manager, is_ai=True)
self.ball: Ball = Ball(self.screen_manager, self.player, self.bot)
self.restart()
self.my_font:pygame.font.Font = pygame.font.SysFont('Comic Sans MS', 30)
def restart(self):
self.running = True
self.difficulty = 1 # 0: facile, 1: normal, 2: difficile, change uniquement l'intelligence du bot
self.player: Player = Player(self.screen_manager)
self.bot: Player = Player(self.screen_manager, is_ai=True)
self.bot: Player = Player(self.screen_manager, difficulty=self.difficulty, is_ai=True)
self.ball: Ball = Ball(self.screen_manager, self.player, self.bot)
def run(self):
+2 -1
View File
@@ -1,7 +1,8 @@
"""
TODO
- améliorer l'ia
- rendre plus realiste les rebomd
- rendre plus realiste les rebond
- modifier déplacement joueur & balle pour utiliser le deltatime
- faire un menu style arcade
- rendre le jeu plus arcade
- ajouter des sons style arcade
+60 -25
View File
@@ -9,12 +9,15 @@ if TYPE_CHECKING:
from ball import Ball
class Player(Entity):
def __init__(self, screen_manager: Screen, is_ai: bool = False):
self.is_ai = is_ai
def __init__(self, screen_manager: Screen, difficulty:int = 0, is_ai: bool = False):
self.size = (150, 10)
color_player = (255, 255, 255, 255)
self.difficulty: int = difficulty
self.is_ai = is_ai
self.end_pos_is_calculate = False
self.end_ballx: float = (screen_manager.get_screen().get_size()[0] / 2)
image:pygame.Surface = pygame.Surface(self.size)
image.fill(color_player)
@@ -27,34 +30,18 @@ class Player(Entity):
self.velocity.max = 10
def draw(self):
self.screen.blit(self.image, (self.pos.x, self.pos.y))
self.screen.blit(self.image, (round(self.pos.x), round(self.pos.y)))
def apply_friction(self, deltatime: float):
if (self.velocity.x > 0): self.velocity.x -= self.friction.x * deltatime
elif (self.velocity.x < 0): self.velocity.x += self.friction.x * deltatime
def move(self, deltatime: float, ball: Ball):
if (self.is_ai):
if (ball.pos.x < self.pos.x + self.size[0] / 2): self.velocity.x -= self.speed * deltatime
elif (ball.pos.x > self.pos.x + self.size[0] / 2): self.velocity.x += self.speed * deltatime
else:
keys = pygame.key.get_pressed()
# if keys[pygame.K_z] or keys[pygame.K_UP]: self.velocity.y -= self.speed * deltatime
# if keys[pygame.K_s] or keys[pygame.K_DOWN]: self.velocity.y += self.speed * deltatime
if keys[pygame.K_q] or keys[pygame.K_LEFT]: self.velocity.x -= self.speed * deltatime
if keys[pygame.K_d] or keys[pygame.K_RIGHT]: self.velocity.x += self.speed * deltatime
if (self.velocity.x > self.velocity.max): self.velocity.x = self.velocity.max
elif (-self.velocity.x > self.velocity.max): self.velocity.x = -self.velocity.max
# if (self.velocity.y > self.velocity.max): self.velocity.y = self.velocity.max
# elif (-self.velocity.y > self.velocity.max): self.velocity.y = -self.velocity.max
def apply_move(self, deltatime:float):
self.apply_friction(deltatime)
temps_pos: Tuple[int, int] = (
self.pos.x + round(self.velocity.x),
self.pos.y + round(self.velocity.y)
temps_pos: Tuple[float, float] = (
self.pos.x + self.velocity.x,
self.pos.y + self.velocity.y
)
# les collisions
@@ -66,4 +53,52 @@ class Player(Entity):
self.velocity.x = 0
else:
self.pos.x = temps_pos[0]
self.pos.y = temps_pos[1]
self.pos.y = temps_pos[1]
def ia_move(self, deltatime:float, ball:Ball):
# IA en mode facile
if (self.difficulty == 0):
if (ball.pos.x < self.pos.x + self.size[0] / 2): self.velocity.x -= self.speed * deltatime
elif (ball.pos.x > self.pos.x + self.size[0] / 2): self.velocity.x += self.speed * deltatime
# IA en mode normal && difficile
if (self.difficulty > 0):
if (ball.velocity.y < 0): # Si la balle va vers le bot (en gros, monte)
coef_dir: float = ball.velocity.y / ball.velocity.x # Calculer le coef directeur de la droite qui suit le vecteur mouvement de la balle
dist_boty: float = (self.pos.y - self.size[1]) - (ball.pos.y - (ball.size / 2)) # La distance en yqui séparre le bot et la balle
self.end_ballx = ball.pos.x + (dist_boty / coef_dir) # déterminer ou la ball devrais arriver
if (self.end_ballx < 0): # Si la balle devrait collisionner avec le mur de gauche
collisiony: float = ball.pos.y + (coef_dir * (ball.pos.x - (ball.size / 2))) # Calculer a quelle hauteur la balle va collisionner avec le mur
dist_boty = (self.pos.y - self.size[1]) - (collisiony - (ball.size / 2))
self.end_ballx = ball.pos.x + (dist_boty / -coef_dir) # déterminer ou la ball devrais arriver
elif (self.end_ballx > self.screen.get_width() + (ball.size / 2)): # Si la balle devrait collisionner avec le mur de droite
collisiony: float = ball.pos.y + (coef_dir * (self.screen.get_width() - (ball.pos.x - (ball.size / 2)))) # Calculer a quelle hauteur la balle va collisionner avec le mur
self.end_ballx = ball.pos.x + (dist_boty / -coef_dir) # déterminer ou la ball devrais arriver
if (self.end_ballx < self.pos.x + self.size[0] / 2): self.velocity.x -= self.speed * deltatime
elif (self.end_ballx > self.pos.x + self.size[0] / 2): self.velocity.x += self.speed * deltatime
self.apply_move(deltatime)
def player_move(self, deltatime:float):
keys = pygame.key.get_pressed()
# if keys[pygame.K_z] or keys[pygame.K_UP]: self.velocity.y -= self.speed * deltatime
# if keys[pygame.K_s] or keys[pygame.K_DOWN]: self.velocity.y += self.speed * deltatime
if keys[pygame.K_q] or keys[pygame.K_LEFT]: self.velocity.x -= self.speed * deltatime
if keys[pygame.K_d] or keys[pygame.K_RIGHT]: self.velocity.x += self.speed * deltatime
if (self.velocity.x > self.velocity.max): self.velocity.x = self.velocity.max
elif (-self.velocity.x > self.velocity.max): self.velocity.x = -self.velocity.max
# if (self.velocity.y > self.velocity.max): self.velocity.y = self.velocity.max
# elif (-self.velocity.y > self.velocity.max): self.velocity.y = -self.velocity.max
self.apply_move(deltatime)
def move(self, deltatime: float, ball: Ball):
if (self.is_ai):
self.ia_move(deltatime, ball)
else:
self.player_move(deltatime)