python - GameOver screen not appearing when i want - pygame -
i'm having problem finding way blit gameover screen @ right time. made platformer on pygame , want game on screen appear after animation of character dying has occurred , when enemy has had attack animation. right bypasses animation , collision detection between sprites true, says game over. ideas?
thanks
def gameover(): intro = true while intro: event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() quit() screen.blit(pygame.image.load("background0.jpg").convert(), (0,0)) largetext = pygame.font.sysfont("elephant",60) textsurf, textrect = text_objects("gameover", largetext) textrect.center = ((screen_width/2),(screen_height/2)) screen.blit(textsurf, textrect) pygame.display.update() clock.tick(15) class enemy: def __init__(self,x,y): self.x=x self.y=y self.width=40 self.height = 40 self.speed=1 self.s0 = pygame.image.load("images/enemy/s0.png") self.s1 = pygame.image.load("images/enemy/s1.png") self.s2 = pygame.image.load("images/enemy/s2.png") self.s3 = pygame.image.load("images/enemy/s3.png") self.attack = pygame.image.load("attack.png") self.rotatedattack = pygame.transform.flip(self.attack ,true,false) self.rotateds0 = pygame.transform.flip(self.s0 ,true, false) self.rotateds1 = pygame.transform.flip(self.s1 ,true, false) self.rotateds2 = pygame.transform.flip(self.s2 ,true, false) self.rotateds3 = pygame.transform.flip(self.s3 ,true, false) self.collision = false self.collision1 = false self.timetarget=10 self.timenum=0 self.currentimage=0 def move(self,player): if self.x > player.x: self.x -= self.speed if self.currentimage > 3: self.currentimage = 0 if self.collision == true: if self.currentimage < 4: #image of him attacking self.currentimage = 8 spearattack.play(loops = 0, maxtime = 10) elif self.x < player.x: self.x += self.speed if self.currentimage < 4: self.currentimage = 4 if self.collision == true: if player.y > 487: if self.currentimage == 4 or 5 or 6 or 7: #image of him attacking flipped self.currentimage = 9 spearattack.play(loops = 0, maxtime = 10) #tried putting gameover here skips animation if self.x < player.x: if self.x > player.x: if self.currentimage > 3: self.currentimage = 0 def update(self,collisiondetect,player,enemy2): self.timenum+=1 if self.timenum == self.timetarget: if self.currentimage ==0: self.currentimage=1 elif self.currentimage ==1: self.currentimage=2 elif self.currentimage == 2: self.currentimage=3 elif self.currentimage ==3: self.currentimage =0 elif self.currentimage ==4: self.currentimage=5 elif self.currentimage ==5: self.currentimage=6 elif self.currentimage == 6: self.currentimage=7 elif self.currentimage ==7: self.currentimage = 4 self.timenum=0 if self.currentimage==0: screen.blit(self.s0, (self.x,self.y)) elif self.currentimage==1: screen.blit(self.s1, (self.x,self.y)) elif self.currentimage==2: screen.blit(self.s2, (self.x,self.y)) elif self.currentimage ==3: screen.blit(self.s3, (self.x,self.y)) elif self.currentimage==4: screen.blit(self.rotateds0, (self.x,self.y)) elif self.currentimage==5: screen.blit(self.rotateds1, (self.x,self.y)) elif self.currentimage==6: screen.blit(self.rotateds2, (self.x,self.y)) elif self.currentimage ==7: screen.blit(self.rotateds3, (self.x,self.y)) elif self.currentimage ==8: screen.blit(self.attack, (self.x,self.y)) #tried putting gameover() here skips blit. elif self.currentimage ==9: screen.blit(self.rotatedattack, (self.x,self.y)) self.collision = collisiondetect(self.x,self.y,self.width,self.height,player.x,player.y,player.width,player.height)
i'd have delay animation. seems pygame doesn't have default animation queue, therefore you'll have take care of it. there few things work:
using
time.sleep()- wont work if animation part of single thread, since entire thread stopped (and animation should freeze). therefore i'll recommend second option since sounds more reasonable me.using pygame timer
pygame.time.set_timer(), should thread , therefore not freeze spear-animation. you'll have send gameover event caught inside gameover function , reset timer event 0 (to stop loop) once it's caught. https://www.pygame.org/docs/ref/time.html#comment_pygame_time_set_timerusing thread (see: python threading.timer - repeat function every 'n' seconds) , calling thread timer after spear-animation
the following has nothing error code poorly designed in general might help: trying call function game (in case gameover) instance of class (in case enemy), bad practice , semantically incorrect. enemy-class shouldn't know game @ all, should used game - not other way around. can fire events, different, global handler should take care of when game on (and similar game-related events), not single instance of class.
also, instead of having endless if-else conditions should consider using kind of mapping - example dictionary. it'll safe many lines of code, it's easier maintain , more readable.
Comments
Post a Comment