Enhance Ball and Player classes for AI integration; update game logic for player and bot interactions

This commit is contained in:
2026-06-22 19:11:32 +02:00
parent ee0ea0206b
commit 208fbe6cfe
5 changed files with 97 additions and 31 deletions
+34 -11
View File
@@ -1,14 +1,19 @@
from __future__ import annotations
import pygame
import random
import math
from typing import Tuple
from typing import Tuple, TYPE_CHECKING
from entity import Entity
from screen import Screen
from player import Player
if TYPE_CHECKING:
from player import Player
class Ball(Entity):
def __init__(self, screen_manager: Screen, player:Player):
self.player = player
def __init__(self, screen_manager: Screen, player:Player, bot:Player):
self.player: Player = player
self.bot: Player = bot
self.size = 30
color_ball = (255, 200, 200, 255)
@@ -22,20 +27,22 @@ class Ball(Entity):
self.speed: int = 25
self.friction.x = 1 # le vrai speed est la différence entre friction.x et speed
self.velocity.max = 4 ** 2 # Mettre au carré pour économiser des calcules plus tard (sur la normalisation du mouvement)
self.velocity.max = 10 ** 2 # Mettre au carré pour économiser des calcules plus tard (sur la normalisation du mouvement)
self.velocity.min = 2
self.collide: bool = False
def draw(self):
self.screen.blit(self.image, (self.pos.x, self.pos.y))
def speed_up(self):
self.velocity.x += .5
self.velocity.y += .5
self.velocity.x = self.velocity.x + .75 if (self.velocity.x > 0) else self.velocity.x - .25
self.velocity.y = self.velocity.y + .75 if (self.velocity.y > 0) else self.velocity.y - .25
def move(self, deltatime: float):
if (self.velocity.x == 0 and self.velocity.y == 0): # si la balle est a l'arret lui donner un mouvement aléatoire
self.velocity.x = random.randint(1, int(self.velocity.max))
self.velocity.y = random.randint(1, int(self.velocity.max))
self.velocity.x = random.randint(1, int(self.velocity.min))
self.velocity.y = random.randint(1, int(self.velocity.min))
norm: float = self.velocity.x ** 2 + self.velocity.y ** 2 # norme du vecteur mouvement
@@ -50,11 +57,27 @@ class Ball(Entity):
if (self.pos.x < 0 or (self.pos.x + round(self.size / 2)) > self.screen.get_size()[0]): self.velocity.x = -self.velocity.x # Collision avec le mur
# collision avec le joueur
if ((self.pos.y + self.size / 2) >= self.player.pos.y - self.player.size[1] and self.pos.y <= self.player.pos.y):
if (self.pos.x + self.size >= self.player.pos.x and self.pos.x - self.size <= self.player.pos.x + self.player.size[0]):
self.velocity.y = -self.velocity.y
self.speed_up()
if not self.collide: self.speed_up()
self.collide = True
# collision avec le bot
elif (self.pos.y - round(self.size / 2) <= self.bot.pos.y and self.pos.y >= self.bot.pos.y + self.bot.size[1]):
if (self.pos.x + self.size >= self.bot.pos.x and self.pos.x - self.size <= self.bot.pos.x + self.bot.size[0]):
self.velocity.y = -self.velocity.y
if not self.collide: self.speed_up()
self.collide = True
else: self.collide = False
self.pos.x += round(self.velocity.x)
self.pos.y += round(self.velocity.y)
self.pos.y += round(self.velocity.y)
def as_winner(self) -> int:
if (self.pos.y < 0): return 0 # Joueur a gagner
if (self.pos.y > self.screen.get_size()[1]): return 1 # Bot a gagner
return -1