Dans le cadre de mon option ISN je suis en train de réaliser un Bomberman-like.
Lorsque l'on presse la touche "q", le personnage pose une bombe qui explose lorsque la variable "temps" est supérieure à 3 secondes.
Mon problème ici est que la variable temps ne s'actualise que lorsqu'une touche est pressée (ce qui rend le jeu hmmm.. ennuyeux!). Ceci se situe dans la boucle du programme, celle du "for event in pygame.event.get():".
Merci d'avance pour les potentielles réponses
import pygame
import time
from pygame.locals import *
#Ouverture de la fenêtre
pygame.init()
display=pygame.display.set_mode((600,600))
#Assignation des images aux variables
fond = pygame.image.load("4040fond.png").convert() #Fond
charH = pygame.image.load("haut.png").convert_alpha() #Personnage
charB = pygame.image.load("bas.png").convert_alpha()
charG = pygame.image.load("gauche.png").convert_alpha()
charD = pygame.image.load("droite.png").convert_alpha()
blocinc = pygame.image.load("blocinc.png").convert() #Bloc Incassable
blocc = pygame.image.load("blocc.png").convert() #Bloc Cassable
blanc = pygame.image.load("blanc.png").convert()
bombe = pygame.image.load("bombe.png").convert() #Bombe
bombe2 = pygame.image.load("bombe2.png").convert()
explo = pygame.image.load("explo.png").convert()
pygame.display.flip()
#Variables
a=40
b=40
tempa=80
tempb=40
pos=(0,0)
x=0
y=0
blocI=[]
blocC=[]
blocExplo=[]
temps=0
start=0
posa=-40
posb=-40
bombeposee=False
#Décryptage et affichage de la map (map.txt)
f=open("map.txt",'r')
for i in f:
for p in i:
if p!="\n":
if int(p)==1:
display.blit(blocinc,(x,y))
blocI.append((x,y))
x+=40
elif int(p)==2:
display.blit(blocc,(x,y))
blocC.append((x,y))
x+=40
elif int(p)==0:
display.blit(fond,(x,y))
x+=40
else:
x=0
y+=40
pygame.display.flip()
#Affichage du personnage
display.blit(charB,(a,b))
pygame.display.flip()
#Boucle du programme
continuer=1
while continuer==1:
if bombeposee==True:
temps=time.time()-start
for event in pygame.event.get():
if event.type==KEYDOWN:
temps=time.time()-start
if event.key==K_RIGHT and (a+40,b) not in blocI and (a+40,b) not in blocC and a+40<600:
tempa=a
tempb=b
a+=40
display.blit(charD,(a,b))
if (tempa,tempb)!=pos:
display.blit(fond,(tempa,tempb))
elif event.key==K_LEFT and (a-40,b) not in blocI and (a-40,b) not in blocC and a-40>=0:
tempa=a
tempb=b
a-=40
display.blit(charG,(a,b))
if (tempa,tempb)!=pos:
display.blit(fond,(tempa,tempb))
elif event.key==K_UP and (a,b-40) not in blocI and (a,b-40) not in blocC and b-40>=0:
tempa=a
tempb=b
b-=40
display.blit(charH,(a,b))
if (tempa,tempb)!=pos:
display.blit(fond,(tempa,tempb))
elif event.key==K_DOWN and (a,b+40) not in blocI and (a,b+40) not in blocC and b+40<600:
tempa=a
tempb=b
b+=40
display.blit(charB,(a,b))
if (tempa,tempb)!=pos:
display.blit(fond,(tempa,tempb))
elif event.key==K_q:
start=time.time()
posa=a
posb=b
blocC.append((posa,posb))
bombeposee=True
if temps<3:
display.blit(bombe,(posa,posb))
temps=time.time()-start
elif temps>=3 and temps<3.5:
display.blit(explo,(posa,posb))
blocExplo.append((posa,posb))
if (posa-40,posb) not in blocI:
display.blit(explo,(posa-40,posb))
if (posa-40,posb) in blocC:
blocC.remove((posa-40,posb))
blocExplo.append((posa-40,posb))
if (posa+40,posb) not in blocI:
display.blit(explo,(posa+40,posb))
if (posa+40,posb) in blocC:
blocC.remove((posa+40,posb))
blocExplo.append((posa+40,posb))
if (posa,posb-40) not in blocI:
display.blit(explo,(posa,posb-40))
if (posa,posb-40) in blocC:
blocC.remove((posa,posb-40))
blocExplo.append((posa,posb-40))
if (posa,posb+40) not in blocI:
display.blit(explo,(posa,posb+40))
if (posa,posb+40) in blocC:
blocC.remove((posa,posb+40))
blocExplo.append((posa,posb+40))
if (posa,posb) in blocC:
blocC.remove((posa,posb))
else:
for element in blocExplo:
if element!=(a,b):
display.blit(fond,element)
blocExplo=[]
bombeposee=False
pygame.display.flip()
- Edité par MartinLedoux 17 mai 2015 à 13:25:51
[Pygame] Problème de temps qui ne s'actualise pas
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.