Problème Pygame 3 touche en même temps

Résolu/Fermé
Flayme - Modifié le 25 juil. 2022 à 18:34
 Flayme - 28 juil. 2022 à 13:55

Bonjour,

Je suis entrain de créer un jeu vidéo mais je ne comprend pas pourquoi quand j'appuye sur la touche haut,gauche et espace (quand j'appuye sur haut,droite et espace le programme tir) le programme n'essaye même pas de tirer.

Voici mon code:

import os os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame LARGEUR = 1300
HAUTEUR = 700
TAILLE = 60
VITESSE = 10
TIREVIT = 30
FPS = 60
play = 1
action = [] x = LARGEUR//2
y = HAUTEUR//2
dir = 0
haut = False
bas = False
droite = False
gauche = False
tir = False
def avhaut(): global y if y > 0: y -= VITESSE def avbas(): global y if y < HAUTEUR-TAILLE: y += VITESSE def avdroite(): global x if x < LARGEUR-TAILLE: x += VITESSE def avgauche(): global x if x > 0: x -= VITESSE def tirer(): action.append(["tir",dir,x,y]) pygame.init() pygame.display.set_caption('LE CARRÉ') ecran = pygame.display.set_mode((LARGEUR,HAUTEUR)) clock = pygame.time.Clock() while play: for event in pygame.event.get(): if event.type == pygame.QUIT: play = 0
        if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: haut = True
                print('haut') if event.key == pygame.K_DOWN: bas = True
                print('bas') if event.key == pygame.K_RIGHT: droite = True
                print('droite') if event.key == pygame.K_LEFT: gauche = True
                print('gauche') if pygame.key.get_pressed()[pygame.K_DOWN]: tir = True
            print('tir') if event.type == pygame.KEYUP: if event.key == pygame.K_UP: haut = False
            if event.key == pygame.K_DOWN: bas = False
            if event.key == pygame.K_RIGHT: droite = False
            if event.key == pygame.K_LEFT: gauche = False
            if event.key == pygame.K_SPACE: tir = False
    if (haut and bas) or not (haut or bas): if not (droite and gauche): if droite: dir = 0
            if gauche: dir = 4
    elif (droite and gauche) or not (droite or gauche): if not (haut and bas): if haut: dir = 2
            if bas: dir = 6
    else: if haut: if droite: dir = 1
            if gauche: dir = 3
        if bas: if droite: dir = 7
            if gauche: dir = 5
    if haut: avhaut() if bas: avbas() if droite: avdroite() if gauche: avgauche() if tir: tirer() #print(dir)
    clock.tick(FPS) ecran.fill((0, 0, 0)) pygame.draw.rect(ecran, (255, 255, 255), pygame.Rect(x, y, TAILLE, TAILLE)) for act in action: # 321
        # 4X0
        # 567
        if act[0]=='tir': pygame.draw.rect(ecran, (255, 0, 0), pygame.Rect(act[2], act[3], TAILLE//3, TAILLE//3)) if act[1] == 0: act[2] = act[2] + TIREVIT try: if act[1] == 1: act[3] = act[3] - TIREVIT//2
                    act[2] = act[2] + TIREVIT//2
            except: pass
            try: if act[1] == 2: act[3] = act[3] - TIREVIT except NameError: pass
            try: if act[1] == 3: act[3] = act[3] + TIREVIT//2
                    act[2] = act[2] - TIREVIT//2
            except: pass
            try: if act[1] == 4: act[2] = act[2] - TIREVIT except NameError: pass
            try: if act[1] == 6: act[3] = act[3] + TIREVIT except NameError: pass
    pygame.display.flip() pygame.quit() quit()

merci pour votre aide

3 réponses

yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471
25 juil. 2022 à 18:58

bonjour,

comme tu peux le voir, ton programme n'est pas correctement partagé.  Peux-tu adapter cela?

0

Bonsoir, il semblerait que (bug de la sdl ?) up + left + space ne fonctionnent pas, le space n'est pas ajouté dans la liste d'events si les touches up et left sont pressées.

Un simple test avec le fichier pour tester les événements permet de s'en rendre compte.

from pygame.examples import eventlist
eventlist.main()

Solution : utiliser une autre touche, le Ctrl gauche (K_LCTRL) est adapté, pour ma part je préfère cette touche à la barre d'espace =)

0

Merci drustro tu as régler mon problème!

0