Enhance Ball and Player classes for AI integration; update game logic for player and bot interactions
This commit is contained in:
+34
-11
@@ -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
|
||||
@@ -43,5 +43,4 @@ class Entity:
|
||||
self.screen: pygame.Surface = screen_manager.get_screen()
|
||||
|
||||
def draw(self):...
|
||||
def move(self, deltatime: float):...
|
||||
def apply_friction(self, deltatime: float):...
|
||||
+26
-2
@@ -14,13 +14,24 @@ class Game:
|
||||
self.running = True
|
||||
|
||||
self.player: Player = Player(self.screen_manager)
|
||||
self.ball: Ball = Ball(self.screen_manager, self.player)
|
||||
self.bot: Player = Player(self.screen_manager, is_ai=True)
|
||||
self.ball: Ball = Ball(self.screen_manager, self.player, self.bot)
|
||||
|
||||
self.my_font:pygame.font.Font = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
|
||||
def restart(self):
|
||||
self.running = True
|
||||
|
||||
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)
|
||||
|
||||
def run(self):
|
||||
while (self.running):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
return -1
|
||||
|
||||
self.screen.fill((0, 0, 0))
|
||||
|
||||
@@ -28,8 +39,21 @@ class Game:
|
||||
dt_ms: int = self.clock.tick(self.FPS_MAX)
|
||||
dt: float = dt_ms / 1000.0
|
||||
|
||||
self.player.move(dt)
|
||||
self.player.move(dt, self.ball)
|
||||
self.ball.move(dt)
|
||||
self.bot.move(dt, self.ball)
|
||||
self.player.draw()
|
||||
self.bot.draw()
|
||||
self.ball.draw()
|
||||
|
||||
text_surface: pygame.Surface | None = None
|
||||
if (self.ball.as_winner() == 0):
|
||||
text_surface = self.my_font.render('Player Won', False, (125, 125, 255))
|
||||
elif (self.ball.as_winner() == 1):
|
||||
text_surface = self.my_font.render('Bot Won', False, (255, 125, 125))
|
||||
|
||||
if (text_surface):
|
||||
self.screen.blit(text_surface, (self.screen.get_width() / 2 - (text_surface.get_width() / 2), self.screen.get_height() / 2 - (text_surface.get_height() / 2)))
|
||||
self.running = False
|
||||
|
||||
pygame.display.flip()
|
||||
+12
-3
@@ -3,7 +3,16 @@ from game import Game
|
||||
|
||||
pygame.init()
|
||||
|
||||
game = Game()
|
||||
game.run()
|
||||
launch_game = True
|
||||
|
||||
pygame.quit()
|
||||
game = Game()
|
||||
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT: pygame.quit()
|
||||
elif event.type == pygame.KEYDOWN: launch_game = True
|
||||
|
||||
if launch_game:
|
||||
launch_game = False
|
||||
game.restart()
|
||||
if game.run() == -1: pygame.quit()
|
||||
|
||||
+25
-14
@@ -1,17 +1,24 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pygame
|
||||
from typing import Tuple
|
||||
from typing import Tuple, TYPE_CHECKING
|
||||
from entity import Entity
|
||||
from screen import Screen
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ball import Ball
|
||||
|
||||
class Player(Entity):
|
||||
def __init__(self, screen_manager: Screen):
|
||||
self.size = (150, 15)
|
||||
def __init__(self, screen_manager: Screen, is_ai: bool = False):
|
||||
self.is_ai = is_ai
|
||||
|
||||
self.size = (150, 10)
|
||||
color_player = (255, 255, 255, 255)
|
||||
|
||||
image:pygame.Surface = pygame.Surface(self.size)
|
||||
image.fill(color_player)
|
||||
|
||||
origin: Tuple[int, int] = (round(screen_manager.get_screen().get_size()[0] / 2) - round(self.size[0] / 2), 675)
|
||||
origin: Tuple[int, int] = (round(screen_manager.get_screen().get_size()[0] / 2) - round(self.size[0] / 2), 105) if is_ai else (round(screen_manager.get_screen().get_size()[0] / 2) - round(self.size[0] / 2), 675)
|
||||
|
||||
super().__init__("Player", origin, image, screen_manager)
|
||||
|
||||
@@ -26,18 +33,22 @@ class Player(Entity):
|
||||
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):
|
||||
keys = pygame.key.get_pressed()
|
||||
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 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
|
||||
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_friction(deltatime)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user