Search code examples
pythonpygamekey

Issue with keys events using key.get_pressed()


I have a car that I want to move left/right/up/down but also diagonally. The first four work perfectly, but when I make It move diagonally, the image doesn't change.

keys = pg.key.get_pressed()

if keys[pg.K_LEFT] or keys[pg.K_q]:
    van.state = VAN_LEFT
    screen.acceleration.x = screen.vitesse

if keys[pg.K_UP] or keys[pg.K_z]:
    van.state = VAN_UP
    screen.acceleration.y = screen.vitesse


if (keys[pg.K_LEFT] or keys[pg.K_q]) and (keys[pg.K_UP] or keys[pg.K_z]):
    van.state = VAN_LEFT_UP
    screen.acceleration.x = screen.vitesse
    screen.acceleration.y = screen.vitesse

if I add in the main loop print(van.state), it will never be van_left_up.

Does anyone know what I'm doing wrong ?

Here is the whole code

VAN_HAUT = 0
VAN_DROITE_HAUT = 1
VAN_DROITE = 2
VAN_DROITE_BAS = 3
VAN_BAS = 4
VAN_GAUCHE_BAS = 5
VAN_GAUCHE = 6
VAN_GAUCHE_HAUT = 7

class Van(object):
    def __init__(self, pos, etat, couleur):
        self.pos = pos
        self.etat = etat
        self.couleur = couleur

    def draw(self, win):
        if self.etat == VAN_HAUT:
            if self.couleur is "bleu":
                win.blit(van_haut_bleu, (self.pos.x, self.pos.y))

        if self.etat == VAN_DROITE_HAUT:
            if self.couleur is "bleu":
                win.blit(van_droite_haut_bleu, (self.pos.x, self.pos.y))

        if self.etat == VAN_DROITE:
            if self.couleur is "bleu":
                win.blit(van_droite_bleu, (self.pos.x, self.pos.y))

                win.blit(van_droite_violet, (self.pos.x, self.pos.y))
        if self.etat == VAN_DROITE_BAS:
            if self.couleur is "bleu":
                win.blit(van_droite_bas_bleu, (self.pos.x, self.pos.y))

        if self.etat == VAN_BAS:
            if self.couleur is "bleu":
                win.blit(van_bas_bleu, (self.pos.x, self.pos.y))
 
        if self.etat == VAN_GAUCHE_BAS:
            if self.couleur is "bleu":
                win.blit(van_gauche_bas_bleu, (self.pos.x, self.pos.y))
 
        if self.etat == VAN_GAUCHE:
            if self.couleur is "bleu":
                win.blit(van_gauche_bleu, (self.pos.x, self.pos.y))

        if self.etat == VAN_GAUCHE_HAUT:
            if self.couleur is "bleu":
                win.blit(van_gauche_haut_bleu, (self.pos.x, self.pos.y))

ECRAN_MAP = 3

class Ecran(object):
    def __init__(self, affiche, position, position_map, velocite, vitesse, acceleration, friction, niveau):
        self.affiche = affiche
        self.position = position
        self.position_map = position_map
        self.velocite = velocite
        self.vitesse = vitesse
        self.acceleration = acceleration
        self.friction = friction

        self.nombre_ecran_intro = 0
        
    def draw(self, win):

        elif self.affiche == ECRAN_MAP:
            win.blit(ecran_carte, (self.position_map.x, self.position_map.y))
            ecran_pos_x = police_du_jeu.render((str(self.position.x)), True, NOIR)
            win.blit(ecran_pos_x, (0, 0))
            ecran_pos_y = police_du_jeu.render((str(self.position.y)), True, NOIR)
            win.blit(ecran_pos_y, (0, 50))
            van.draw(win)

        self.acceleration += self.velocite * self.friction
        self.velocite += self.acceleration
        self.position += self.velocite + 0.5 * self.acceleration
        self.position_map += self.velocite + 0.5 * self.acceleration
        
                

def rgw():
    ecran.draw(win)
    pg.display.update()

ecran = Ecran(ECRAN_INTRO, vec(0, 0), vec(-11810, -2640), vec(0, 0), 0.75, vec(0, 0), -0.12, NIVEAU_NEWYORK)
van = Van(vec(0, 0), VAN_GAUCHE, "bleu")



run = True
while run:

    ecran.acceleration = vec(0, 0)

    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False

       
    keys = pg.key.get_pressed()

    if (keys[pg.K_LEFT] or keys[pg.K_q]) and (keys[pg.K_UP] or keys[pg.K_z]):
        if ecran.affiche == ECRAN_MAP:
            van.etat = VAN_GAUCHE_HAUT
            ecran.acceleration.x = ecran.vitesse
            ecran.acceleration.y = ecran.vitesse
    
    if keys[pg.K_LEFT] or keys[pg.K_q]:
        if ecran.affiche == ECRAN_JEU:
            joueur.acceleration.x = -joueur.vitesse
        elif ecran.affiche == ECRAN_MAP:
            van.etat = VAN_GAUCHE
            ecran.acceleration.x = ecran.vitesse
            
    if keys[pg.K_RIGHT] or keys[pg.K_d]:
        if ecran.affiche == ECRAN_JEU:
            joueur.acceleration.x = joueur.vitesse
        elif ecran.affiche == ECRAN_MAP:
            van.etat = VAN_DROITE
            ecran.acceleration.x = -ecran.vitesse
    
    if keys[pg.K_UP] or keys[pg.K_z]:
        if ecran.affiche == ECRAN_JEU:
            if joueur.collide is True or joueur.side_collide is True:
                joueur.collide = False
                joueur.side_collide = False
                joueur.velocite.y = -19
        elif ecran.affiche == ECRAN_MAP:
            van.etat = VAN_HAUT
            ecran.acceleration.y = ecran.vitesse
            
    if keys[pg.K_DOWN] or keys[pg.K_s]:
        if ecran.affiche == ECRAN_MENU:
            ecran.affiche = ECRAN_SHOP
        elif ecran.affiche == ECRAN_JEU:

            joueur.action = EST_BAISSE
        elif ecran.affiche == ECRAN_MAP:
            van.etat = VAN_BAS
            ecran.acceleration.y = -ecran.vitesse

    rgw()
    clock.tick(IPS)
    
pg.quit()

I removed all the modules imported, the images loaded and the classes that doesn't concern the van and the screen. What is left is the only things that concern directly the van and the map that moves.


Solution

  • I finally find how to do it work. It is maybe not the best solution, but it works exactly as I wanted. Instead or having only if keys[pygame.K_UP] and if keys[pygame.K_LEFT], I wrote it like this:

    if keys[pygame.K_UP] and not keys[pygame.K_LEFT]:
        ...
    if keys[pygame.K_LEFT] and not keys[pygame.K_UP]:
        ...
    if keys[pygame.K_LEFT] and keys[pygame.K_UP]:
        ...
    

    So it knows when I press the two keys at the time. Thanks for those who tried to help me !