Problème Pygame Event.key

Résolu
Bonjour,

Je suis entrain de programmer un jeu (amateur) et j'utilise la fonctionnalité des event.key, afin de faire avancer mon jeu.

Cependant, à un moment de mon programme, je souhaite faire plusieurs fois dans une même boucle la comparaison event.key == .....
Voilà mon code:

if event.key == pygame.K_RIGHT:
---print("Test Droite")
---if event.key == pygame.K_RIGHT:
------print("Test Droite")
---if event.key == pygame.K_LEFT:
------print("Test Gauche")
if event.key == pygame.K_LEFT:
---print("Test GAUCHE")
---if event.key == pygame.K_RIGHT:
------print("Test Droite")
---if event.key == pygame.K_LEFT:
------print("Test Gauche")

Quand j'appuye sur la flèche droite, les 2 print de font instantanément, mais j'aimerais que après avoir fais le premier :

if event.key == pygame.K_RIGHT:
---print("Test Droite")

Je puisse encore faire un choix entre Droite et gauche encore une fois.
J'ai essayé plusieurs choses mais rien n'a fonctionné.

Merci de votre aide en avance,
Cordialement, Martin
Configuration: Windows / Opera 80.0.4170.48

1 réponse

  1. Contributeur
    bonjour,
    tu dois modifier d'autres parties de ton code pour obtenir le résultat attendu.
    1. Soit, mais pouvez vous me donnez un exemple de comment je pourrais régler le problème?
      J'imagine bien que je dois changer quelque chose mais je ne sais pas quoi.
    2. Contributeur
      @Martinle point de départ, c'est que tu nous montres ton code.
    3. @yg_beLe voici au total:

      import pygame
      import time
      pygame.init()

      #générer la fenêtre
      pygame.display.set_caption("Emperor")
      screen = pygame.display.set_mode((1080,720))

      #variables des jauges
      influencej = 50
      peuplej = 50
      armeej = 50
      argentj = 50

      #variables des personnages
      oracle_debut=0
      chefgarde_debut=0
      oracle_perso=0
      stratege_perso=0
      gaulois_perso=0
      jesus_perso=0
      peuple_perso=0
      reine_perso=0
      chef_garde_perso=0
      dirigeant_perso=0
      comptable_perso=0
      espion_perso=0
      explorateur_perso=0
      random_dialogue = 0

      WIDTH, HEIGHT = 1080, 720
      fleche_droit = pygame.image.load('assets/fleche_droit.png')
      fleche_gauche = pygame.image.load('assets/fleche_gauche.png.')
      background = pygame.image.load('assets/bg.jpg')
      oracle = pygame.image.load('assets/oracle.png')
      stratege = pygame.image.load('assets/stratege.png')
      gaulois = pygame.image.load('assets/gaulois.png')
      jesus = pygame.image.load('assets/jesus.png')
      peuple = pygame.image.load('assets/peuple.png')
      reine = pygame.image.load('assets/reine.png')
      chef_garde = pygame.image.load('assets/chef garde.png')
      dirigeant = pygame.image.load('assets/dirigeant.png')
      comptable = pygame.image.load('assets/comptable.png')
      espion = pygame.image.load('assets/espion.png')
      explorateur = pygame.image.load('assets/explorateur.png')
      zone_texte = pygame.Surface((500,200))
      zone_jauge = pygame.Surface((150,150))
      debut_commence=0

      dialogue_FONT = pygame.font.SysFont('comicsans',30)

      def jauges(influencej, peuplej, armeej, argentj):
      draw_influence = dialogue_FONT.render("Influence : " + str(influencej), 1, (255,255,255))
      screen.blit(draw_influence, (WIDTH // 2 - draw_influence.get_width() / 2 + 450, HEIGHT / 2 - draw_influence.get_height() / 2 - 240))
      draw_peuple = dialogue_FONT.render("Peuple : " + str(peuplej), 1, (255,255,255))
      screen.blit(draw_peuple, (WIDTH // 2 - draw_peuple.get_width() / 2 + 450, HEIGHT / 2 - draw_peuple.get_height() / 2 - 270))
      draw_armee = dialogue_FONT.render("Armée : " + str(armeej), 1, (255,255,255))
      screen.blit(draw_armee, (WIDTH // 2 - draw_armee.get_width() / 2 + 450, HEIGHT / 2 - draw_armee.get_height() / 2 - 300))
      draw_argent = dialogue_FONT.render("Argent : " + str(argentj), 1, (255,255,255))
      screen.blit(draw_argent, (WIDTH // 2 - draw_argent.get_width() / 2 + 450, HEIGHT / 2 - draw_argent.get_height() / 2 - 330))

      def dialogue_debut(text,text2,text3,text4):
      draw_texte = dialogue_FONT.render(text, 1, (0,0,0))
      screen.blit(draw_texte, (WIDTH//2 - draw_texte.get_width()/2, HEIGHT/2 - draw_texte.get_height()/2 + 10))
      draw_texte2 = dialogue_FONT.render(text2, 1, (0, 0, 0))
      screen.blit(draw_texte2, (WIDTH // 2 - draw_texte2.get_width() / 2, HEIGHT / 2 - draw_texte2.get_height() / 2 + 40))
      draw_texte3 = dialogue_FONT.render(text3, 1, (0, 0, 0))
      screen.blit(draw_texte3, (WIDTH // 2 - draw_texte3.get_width() / 2 - 190, HEIGHT / 2 - draw_texte3.get_height() / 2 + 150))
      draw_texte4 = dialogue_FONT.render(text4, 1, (0, 0, 0))
      screen.blit(draw_texte4, (WIDTH // 2 - draw_texte4.get_width() / 2 + 180, HEIGHT / 2 - draw_texte4.get_height() / 2 + 150))
      pygame.display.update()

      running = True
      #appliquer l'arrière plan
      screen.blit(background, (-427,-250))
      zone_texte.fill((243, 156, 31))
      screen.blit(zone_texte, (287, 350))
      zone_jauge.fill((30, 61, 237))
      screen.blit(zone_jauge, (913, 0))
      jauges(influencej, peuplej, armeej, argentj)
      screen.blit(fleche_gauche, (290, 410))
      screen.blit(fleche_droit, (720, 410))

      #boucle tant que running est True
      while running:

      #mettre a jour l'écran
      if debut_commence == 0:
      zone_texte.fill((243, 156, 31))
      screen.blit(zone_texte, (287, 350))
      zone_jauge.fill((30, 61, 237))
      screen.blit(zone_jauge, (913, 0))
      jauges(influencej, peuplej, armeej, argentj)
      screen.blit(fleche_gauche, (290, 410))
      screen.blit(fleche_droit, (720, 410))
      dialogue_debut("Bonjour et bienvenue dans Emperor","Choissisez votre première rencontre:","Oracle","Chef garde")
      pygame.display.flip()

      #si le joueur femre la fenêtre
      for event in pygame.event.get():
      #que l'evenement est fermeture de cette fenetre
      if event.type == pygame.QUIT:
      running = False
      pygame.quit()
      if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_RIGHT and oracle_debut == 0 and chefgarde_debut == 0:
      debut_commence=1
      chefgarde_debut=1
      screen.blit(chef_garde, (407, 100))
      zone_texte.fill((243, 156, 31))
      screen.blit(zone_texte, (287, 350))
      zone_jauge.fill((30, 61, 237))
      screen.blit(zone_jauge, (913, 0))
      screen.blit(fleche_gauche, (290, 410))
      screen.blit(fleche_droit, (720, 410))
      jauges(influencej, peuplej, armeej, argentj)
      draw_texte = dialogue_FONT.render("Bonsoir mon roi, je suis venu vous informer", 1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 10))
      draw_texte = dialogue_FONT.render("de l'arrivée d'un chef indigène.", 1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 30))
      draw_texte = dialogue_FONT.render("Souhaitez vous le rencontrer ?", 1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 50))
      draw_texte = dialogue_FONT.render("Pourquoi pas", 1, (0, 0, 0))
      screen.blit(draw_texte, (WIDTH // 2 - draw_texte.get_width() / 2 + 180, HEIGHT / 2 - draw_texte.get_height() / 2 + 150))
      draw_texte = dialogue_FONT.render("Non.", 1, (0, 0, 0))
      screen.blit(draw_texte, (WIDTH // 2 - draw_texte.get_width() / 2 - 180, HEIGHT / 2 - draw_texte.get_height() / 2 + 150))
      pygame.display.flip()
      print("tt")
      print(event.key)
      event.key = 0
      print(event.key)

      if event.key == pygame.K_RIGHT:
      print("tttt")
      gaulois_perso = 1
      if event.key == pygame.K_LEFT:
      print("faaa")
      gaulois_perso = 0
      random_dialogue = 1

      if event.key == pygame.K_RIGHT and gaulois_perso == 1:
      screen.blit(gaulois, (407, 100))
      zone_texte.fill((243, 156, 31))
      screen.blit(zone_texte, (287, 350))
      zone_jauge.fill((30, 61, 237))
      screen.blit(zone_jauge, (913, 0))
      screen.blit(fleche_gauche, (290, 410))
      screen.blit(fleche_droit, (720, 410))
      jauges(influencej, peuplej, armeej, argentj)
      draw_texte = dialogue_FONT.render("Bonsoir mon roi, je suis venu vous informer", 1, (0, 0, 0))
      screen.blit(draw_texte, (
      WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 10))
      draw_texte = dialogue_FONT.render("de l'arrivée d'un chef indigène.", 1, (0, 0, 0))
      screen.blit(draw_texte, (
      WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 30))
      draw_texte = dialogue_FONT.render("Souhaitez vous le rencontrer ?", 1, (0, 0, 0))
      screen.blit(draw_texte, (
      WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 50))
      draw_texte = dialogue_FONT.render("Pourquoi pas", 1, (0, 0, 0))
      screen.blit(draw_texte, (
      WIDTH // 2 - draw_texte.get_width() / 2 + 180, HEIGHT / 2 - draw_texte.get_height() / 2 + 150))
      draw_texte = dialogue_FONT.render("Non.", 1, (0, 0, 0))
      screen.blit(draw_texte, (
      WIDTH // 2 - draw_texte.get_width() / 2 - 180, HEIGHT / 2 - draw_texte.get_height() / 2 + 150))

      pygame.display.flip()

      if event.key == pygame.K_LEFT and chefgarde_debut == 0 and oracle_debut == 0:
      oracle_debut=1
      debut_commence = 1
      screen.blit(oracle, (407, 100))
      zone_texte.fill((243, 156, 31))
      screen.blit(zone_texte, (287, 350))
      zone_jauge.fill((30, 61, 237))
      screen.blit(zone_jauge, (913, 0))
      screen.blit(fleche_gauche, (290, 410))
      screen.blit(fleche_droit, (720, 410))
      jauges(influencej, peuplej, armeej, argentj)
      draw_texte = dialogue_FONT.render("Bien le bonjour, je prédis l'arrivée d'une", 1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 10))
      draw_texte = dialogue_FONT.render("grande reine Egyptienne.",1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 30))
      draw_texte = dialogue_FONT.render("Souhaitez vous la rencontrer ?",1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2, HEIGHT / 2 - draw_texte.get_height() / 2 + 50))
      draw_texte = dialogue_FONT.render("Avec plaisir !",1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2 + 180, HEIGHT / 2 - draw_texte.get_height() / 2 + 150))
      draw_texte = dialogue_FONT.render("Non merci.",1, (0, 0, 0))
      screen.blit(draw_texte,(WIDTH // 2 - draw_texte.get_width() / 2 - 180, HEIGHT / 2 - draw_texte.get_height() / 2 + 150))
      pygame.display.flip()

      if event.key == pygame.K_RIGHT:
      reine_perso = 1
      if event.key == pygame.K_LEFT:
      reine_perso = 0
      random_dialogue = 1
    4. @MartinBon, en essayant quelque chose j'ai réussi a faire fonctionner ce que je voulais....
      J'ai l'air bête mais bon.. Désolé du dérangement et merci de l'aide.
      Bonne journée.