Refactor entity and player classes to implement friction and velocity management

This commit is contained in:
2026-06-22 16:37:30 +02:00
parent 04a83c5cc2
commit 40306d9441
3 changed files with 61 additions and 16 deletions
+10 -5
View File
@@ -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):...
def move(self, deltatime: float):...
def apply_friction(self, deltatime: float):...
+10
View File
@@ -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()
+41 -11
View File
@@ -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
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]