Refactor entity and player classes to implement friction and velocity management
This commit is contained in:
+10
-5
@@ -13,12 +13,19 @@ class Pos:
|
|||||||
class Velocity:
|
class Velocity:
|
||||||
x: float
|
x: float
|
||||||
y: float
|
y: float
|
||||||
|
max: float = 50
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Friction: # les forces de frottement
|
||||||
|
x: float
|
||||||
|
y: float
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Entity:
|
class Entity:
|
||||||
name: str
|
name: str
|
||||||
pos: Pos
|
pos: Pos
|
||||||
velocity: Velocity
|
velocity: Velocity
|
||||||
|
friction: Friction
|
||||||
image: pygame.Surface
|
image: pygame.Surface
|
||||||
|
|
||||||
screen_manager: Screen
|
screen_manager: Screen
|
||||||
@@ -28,14 +35,12 @@ class Entity:
|
|||||||
self.name:str = name
|
self.name:str = name
|
||||||
self.pos = Pos(*origin)
|
self.pos = Pos(*origin)
|
||||||
self.velocity = Velocity(0, 0)
|
self.velocity = Velocity(0, 0)
|
||||||
|
self.friction = Friction(0, 0) # Par defaut on est dans le vide
|
||||||
|
|
||||||
self.image: pygame.Surface = image
|
self.image: pygame.Surface = image
|
||||||
self.screen_manager = screen_manager
|
self.screen_manager = screen_manager
|
||||||
self.screen: pygame.Surface = screen_manager.get_screen()
|
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 draw(self):...
|
||||||
def move(self, event:pygame.event.Event):...
|
def move(self, deltatime: float):...
|
||||||
|
def apply_friction(self, deltatime: float):...
|
||||||
+10
@@ -7,6 +7,9 @@ class Game:
|
|||||||
self.screen_manager = Screen()
|
self.screen_manager = Screen()
|
||||||
self.screen = self.screen_manager.get_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.running = True
|
||||||
|
|
||||||
self.player = Player(self.screen_manager)
|
self.player = Player(self.screen_manager)
|
||||||
@@ -17,5 +20,12 @@ class Game:
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.running = False
|
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()
|
self.player.draw()
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
+41
-11
@@ -1,26 +1,56 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
from typing import Tuple
|
||||||
from entity import Entity
|
from entity import Entity
|
||||||
from screen import Screen
|
from screen import Screen
|
||||||
|
|
||||||
class Player(Entity):
|
class Player(Entity):
|
||||||
def __init__(self, screen_manager: Screen):
|
def __init__(self, screen_manager: Screen):
|
||||||
size = (150, 5)
|
self.size = (150, 5)
|
||||||
color_player = (255, 255, 255, 255)
|
color_player = (255, 255, 255, 255)
|
||||||
|
|
||||||
image:pygame.Surface = pygame.Surface(size)
|
image:pygame.Surface = pygame.Surface(self.size)
|
||||||
image.fill(color_player)
|
image.fill(color_player)
|
||||||
|
|
||||||
super().__init__("Player", (50, 50), image, screen_manager)
|
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):
|
def draw(self):
|
||||||
self.screen.blit(self.image, (self.pos.x, self.pos.y))
|
self.screen.blit(self.image, (self.pos.x, self.pos.y))
|
||||||
|
|
||||||
def move(self, event:pygame.event.Event):
|
def apply_friction(self, deltatime: float):
|
||||||
if (event.key == pygame.K_z or event.key == pygame.K_UP):
|
if (self.velocity.x > 0): self.velocity.x -= self.friction.x * deltatime
|
||||||
pass
|
elif (self.velocity.x < 0): self.velocity.x += self.friction.x * deltatime
|
||||||
if (event.key == pygame.K_s or event.key == pygame.K_DOWN):
|
|
||||||
pass
|
def move(self, deltatime: float):
|
||||||
if (event.key == pygame.K_q or event.key == pygame.K_LEFT):
|
keys = pygame.key.get_pressed()
|
||||||
pass
|
|
||||||
if (event.key == pygame.K_d or event.key == pygame.K_RIGHT):
|
# if keys[pygame.K_z] or keys[pygame.K_UP]: self.velocity.y -= self.speed * deltatime
|
||||||
pass
|
# 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]
|
||||||
Reference in New Issue
Block a user