From 40306d944132ce42c19f12284ad214dba192db99 Mon Sep 17 00:00:00 2001 From: popkex Date: Mon, 22 Jun 2026 16:37:30 +0200 Subject: [PATCH] Refactor entity and player classes to implement friction and velocity management --- src/entity.py | 15 ++++++++++----- src/game.py | 10 ++++++++++ src/player.py | 52 ++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/src/entity.py b/src/entity.py index 4c89fb3..8fcaa50 100644 --- a/src/entity.py +++ b/src/entity.py @@ -13,12 +13,19 @@ class Pos: class Velocity: x: float y: float + max: float = 50 + +@dataclass +class Friction: # les forces de frottement + x: float + y: float @dataclass class Entity: name: str pos: Pos velocity: Velocity + friction: Friction image: pygame.Surface screen_manager: Screen @@ -28,14 +35,12 @@ class Entity: self.name:str = name self.pos = Pos(*origin) self.velocity = Velocity(0, 0) + self.friction = Friction(0, 0) # Par defaut on est dans le vide self.image: pygame.Surface = image self.screen_manager = screen_manager self.screen: pygame.Surface = screen_manager.get_screen() - def apply_move(self): - self.pos.x += int(self.velocity.x) - self.pos.y += int(self.velocity.y) - def draw(self):... - def move(self, event:pygame.event.Event):... \ No newline at end of file + def move(self, deltatime: float):... + def apply_friction(self, deltatime: float):... \ No newline at end of file diff --git a/src/game.py b/src/game.py index b73aa63..96c95b1 100644 --- a/src/game.py +++ b/src/game.py @@ -7,6 +7,9 @@ class Game: self.screen_manager = Screen() self.screen = self.screen_manager.get_screen() + self.clock: pygame.time.Clock = pygame.time.Clock() + self.FPS_MAX: int = 60 + self.running = True self.player = Player(self.screen_manager) @@ -17,5 +20,12 @@ class Game: if event.type == pygame.QUIT: self.running = False + self.screen.fill((0, 0, 0)) + + # Determiner le deltaTime + dt_ms: int = self.clock.tick(self.FPS_MAX) + dt: float = dt_ms / 1000.0 + + self.player.move(dt) self.player.draw() pygame.display.flip() \ No newline at end of file diff --git a/src/player.py b/src/player.py index c94b06c..6f64c61 100644 --- a/src/player.py +++ b/src/player.py @@ -1,26 +1,56 @@ import pygame +from typing import Tuple from entity import Entity from screen import Screen class Player(Entity): def __init__(self, screen_manager: Screen): - size = (150, 5) + self.size = (150, 5) color_player = (255, 255, 255, 255) - image:pygame.Surface = pygame.Surface(size) + image:pygame.Surface = pygame.Surface(self.size) image.fill(color_player) super().__init__("Player", (50, 50), image, screen_manager) + self.speed: int = 25 + self.friction.x = 15 # le vrai speed est la différence entre friction.x et speed + self.velocity.max = 10 + def draw(self): self.screen.blit(self.image, (self.pos.x, self.pos.y)) - def move(self, event:pygame.event.Event): - if (event.key == pygame.K_z or event.key == pygame.K_UP): - pass - if (event.key == pygame.K_s or event.key == pygame.K_DOWN): - pass - if (event.key == pygame.K_q or event.key == pygame.K_LEFT): - pass - if (event.key == pygame.K_d or event.key == pygame.K_RIGHT): - pass \ No newline at end of file + 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): + 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_friction(deltatime) + + temps_pos: Tuple[int, int] = ( + self.pos.x + round(self.velocity.x), + self.pos.y + round(self.velocity.y) + ) + + # les collisions + if (temps_pos[0] < 0): + self.velocity.x = 0 + self.pos.x = 0 + elif ((temps_pos[0] + self.size[0]) > self.screen.get_size()[0]): + self.pos.x = self.screen.get_size()[0] - self.size[0] + self.velocity.x = 0 + else: + self.pos.x = temps_pos[0] + self.pos.y = temps_pos[1] \ No newline at end of file