Aller au contenu

Simulation : déplacement du personnage

Pour le moment, la simulation ne va comporter que le personnage, avec une position. La taille du personnage et sa vitesse de déplacement, en pixels, seront réglées par une constante.

VITESSE_PERSONNAGE = 5
TAILLE_PERSONNAGE = 10

Classe Simulation

class Personnage:

    def __init__(self, position):
        self.position = position

On définit aussi l'état de la simulation :

class Simulation:

    def __init__(self, personnage):
        self.personnage = personnage

On ajoute bien entendu l'objet correspondant au gamestate par le biais de la fonction d'initialisation. On place le personnage au centre de l'écran.

def initialiser_simulation(etat):
    pos = Vector2(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/2)
    p = Personnage(pos)
    etat.simulation = Simulation(p)

Déplacement personnage.

Pour mettre à jour la simulation, on va déplacer le personnage. Pour ça, on va multiplier le vecteur déplacement du personnage par sa vitesse, et l'ajouter à sa position.

def avancer_simulation(etat):
    direction = etat.controles.direction
    etat.simulation.personnage.position += direction * VITESSE_PERSONNAGE

Vérifions que notre joueur se déplace en affichant sa position dans la boucle principale :

while not etat.controles.quitter:

    clock.tick(FPS)

    traiter_controles(etat)

    avancer_simulation(etat)

    afficher_rendu(etat)

    print(etat.simulation.personnage.position)

En exécutant le code, vous devriez pouvoir voir en console les coordonnées du personnage changer si vous appuyez sur les flèches directionnelles :

[515, 920]
[510, 925]
[505, 930]
[500, 935]
[495, 940]
[490, 945]
[485, 950]
[480, 955]
[475, 960]
[470, 965]
[465, 970]

Le code complet à cet endroit du projet se trouve dans le dépliant ci dessous :

Code Complet
import pygame
from pygame.math import Vector2

####################
####   CONFIG   ####
####################

## Général 

FPS = 60

LARGEUR_FENETRE = 1280
HAUTEUR_FENETRE = 720


## Controles

TOUCHE_QUITTER = pygame.K_ESCAPE
TOUCHE_HAUT = pygame.K_UP  
TOUCHE_BAS = pygame.K_DOWN 
TOUCHE_DROITE = pygame.K_RIGHT 
TOUCHE_GAUCHE = pygame.K_LEFT 

## Simulation

VITESSE_PERSONNAGE = 5
TAILLE_PERSONNAGE = 10

## Rendu

####################
####  GENERAL   ####
####################

class GameState :

    def __init__(self):
        self.controles = None # on met a None parce que ces attributs seront initialisés plus tard
        self.simulation = None
        self.rendu = None

####################
#### CONTROLEUR ####
####################

class Controles :
    def __init__(self):
        self.quitter = False
        self.direction = Vector2(0,0)

def initialiser_controles(etat):
    etat.controles = Controles()

def traiter_controles(etat):

    for e in pygame.event.get():
            if e.type == pygame.KEYDOWN: 
                if e.key == TOUCHE_QUITTER:
                    etat.controles.quitter = True

    direction = Vector2(0, 0)

    clavier = pygame.key.get_pressed()
    if clavier[TOUCHE_HAUT]:
        direction.y += 1.0
    if clavier[TOUCHE_BAS]:
        direction.y -= 1.0
    if clavier[TOUCHE_DROITE]:
        direction.x += 1.0
    if clavier[TOUCHE_GAUCHE]:
        direction.x -= 1.0

    etat.controles.direction = direction

####################
#### SIMULATION ####
####################

class Personnage:

    def __init__(self, position):
        self.position = position

class Simulation:

    def __init__(self, personnage):
        self.personnage = personnage


def initialiser_simulation(etat):
    pos = Vector2(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/2)
    p = Personnage(pos)
    etat.simulation = Simulation(p)

def avancer_simulation(etat):
    direction = etat.controles.direction
    etat.simulation.personnage.position += direction * VITESSE_PERSONNAGE

####################
####   RENDU    ####
####################

def initialiser_rendu(etat):
    pass

def afficher_rendu(etat):
    pass

####################
####    MAIN    ####
####################

def main():
    pygame.init()

    etat = GameState()

    fenetre = pygame.display.set_mode([LARGEUR_FENETRE, HAUTEUR_FENETRE])

    initialiser_controles(etat)
    initialiser_simulation(etat)
    initialiser_rendu(etat)

    clock = pygame.time.Clock()

    while not etat.controles.quitter:

        clock.tick(FPS)

        traiter_controles(etat)

        avancer_simulation(etat)

        afficher_rendu(etat)

        print(etat.simulation.personnage.position)

    pygame.quit()

if __name__ == "__main__":
    main()