0

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.

bappi
  • 137
  • 9
  • Is that the exact order of the if statements in your code? I notice these aren't `elif`s which mean the last one that is valid will overwrite the others. – Aaron Jul 29 '20 at 18:09
  • Also what is the value of `keys` when you press left and up together? Is it what you expect? – Aaron Jul 29 '20 at 18:10
  • The statements you provided work fine for me. Probably error is in some other place. Except the fact that you already set the `acceleration.x` and `acceleration.y` in first statements, so you don't need t do that in the last one. – Benjamin Jul 29 '20 at 18:13
  • the car is moving diagonally up left when I press the two keys, which is what I want. but the drawing stay the same – bappi Jul 29 '20 at 18:17
  • @Aaron so do I need ```if ```or ```elif ```? it is not working with both... – bappi Jul 29 '20 at 18:18
  • @babbi The way you wrote the code above is working, the problem is either somewhere else or this isn't exactly the way you wrote the code. My point about `elif` was to illustrate that if the user has the UP and LEFT key pressed, they will enter all three of your if statements above. – Aaron Jul 29 '20 at 18:23
  • Try removing the first 2 if statements so only the last one remains. See if VAN_LEFT_UP works then. – Mike67 Jul 29 '20 at 18:52
  • @Mike67 yes it works – bappi Jul 29 '20 at 18:56

2 Answers2

0

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 !

bappi
  • 137
  • 9
-1

Based on the comments discussion, this code should work. First check if both keys are pressed then check if individual keys are pressed.

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]):
    van.state = VAN_LEFT_UP
    screen.acceleration.x = screen.vitesse
    screen.acceleration.y = screen.vitesse
    
elif keys[pg.K_LEFT] or keys[pg.K_q]:
    van.state = VAN_LEFT
    screen.acceleration.x = screen.vitesse

elif keys[pg.K_UP] or keys[pg.K_z]:
    van.state = VAN_UP
    screen.acceleration.y = screen.vitesse
Mike67
  • 11,175
  • 2
  • 7
  • 15
  • Try posting your code somewhere so we can see it. Be sure it's runnable. – Mike67 Jul 29 '20 at 19:45
  • well of course its runnable I posted to code in the question – bappi Jul 29 '20 at 19:53
  • I suspect there is another part of the game\app that is causing the issue. I suggest posting the entire app somewhere so we can run it and see the issue. – Mike67 Jul 29 '20 at 19:56