I have made a platformer game and when i die i want there to be a game over scene. I have successfully been able to make a gameover scene appear on the screen saying gameover after my player sprite has showed its dead animation. However when the gameover screen takes me back to the main menu, and i click on play, it obviously takes me back to my sprite being dead. Basically after my sprite has been killed i want the animation to go back to normal(walking) rather than continue to be dead. The dead animation occurs upon collision with an enemy or trap. This seems like a very complex task and i just wanted someones opinion on it. Any help? Btw the top third of my code is the player class where the bottom two thirds are the game over screen etc...
class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width = 65, height = 35):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.hspeed,self.vspeed = 0,0
self.speed = 2
self.images=[]
r0 = pygame.image.load("Images\Player\i1.png")
r1 = pygame.image.load("Images\Player\i2.png")
r2 = pygame.image.load("Images\Player\i3.png")
r3 = pygame.image.load("Images\Player\i4.png")
self.hurt = pygame.image.load("Images\Player\Hurt.png")
self.images.append(r0)
self.images.append(r1)
self.images.append(r2)
self.images.append(r3)
self.rotatedimages = []
rr0 = pygame.transform.flip(r0 ,True, False)
rr1 = pygame.transform.flip(r1 ,True, False)
rr2 = pygame.transform.flip(r2 ,True, False)
rr3 = pygame.transform.flip(r3 ,True, False)
self.rotatedimages.append(rr0)
self.rotatedimages.append(rr1)
self.rotatedimages.append(rr2)
self.rotatedimages.append(rr3)
self.gravity = 0.35
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(self.x,self.y,width,height)
self.TimeNum=0
self.TimeTarget=10
self.Timer = 0
self.collision = False
def update(self, event = None):
self.calcgravity()
self.rect.x += self.hspeed
self.rect.y += self.vspeed
key = pygame.key.get_pressed()
if key[pygame.K_RIGHT]:
self.TimeNum+=1
if self.TimeNum == self.TimeTarget:
self.index +=1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
self.TimeNum = 0
print(self.Timer)
if key[pygame.K_LEFT]:
self.TimeNum+=1
if self.TimeNum == self.TimeTarget:
self.index +=1
if self.index >= len(self.rotatedimages):
self.index = 0
self.image = self.rotatedimages[self.index]
self.TimeNum = 0
def calcgravity(self):
if self.vspeed == 0:
self.vspeed = 1
else:
self.vspeed += self.gravity
#This is what i dont understand
def dead(self):
PlayerDead.play(loops = 0, maxtime = 100)
self.Timer += 1
if self.Timer >= 10:
self.images.append(self.hurt)
self.image = self.hurt
self.hspeed = 0
if self.Timer == 20:
GameOver()
def move(self, hspeed, vspeed):
self.hspeed += hspeed
self.vspeed += vspeed
def Level1PlatColl(self, BlockListGrass, TrapList, enemygroup, PowerUps):
PlatformCollision = pygame.sprite.spritecollide(self, BlockListGrass, False )
for each_object in PlatformCollision:
if self.vspeed > 0:
self.rect.bottom = each_object.rect.top
self.vspeed = 0
if self.vspeed < 0:
self.rect.top = each_object.rect.bottom
self.vspeed = 0
TrapCollision = pygame.sprite.spritecollide(self,TrapList,False )
for each_trap in TrapCollision:
self.dead()
#there might be something i could do here but i dont know what
EnemyCollision = pygame.sprite.spritecollide(self,enemygroup,False )
for each_object in EnemyCollision:
self.dead()
PowerUpsCollision = pygame.sprite.spritecollide(self,PowerUps,False )
for speedboost in PowerUpsCollision:
PowerUps.remove(speedboost)
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
def GameOver():
## PlayerDead.stop()
## SpearAttack.stop()
## SadTrombone.play()
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)
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
Buttons("BACK TO MAIN MENU",100,500,400,50,TURQUOISE,DARK_TURQUOISE,"back")
pygame.display.update()
clock.tick(15)
def Buttons(msg,x,y,w,h,inactive,active,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, active, (x,y,w,h))
if click[0]==1 and action != None:
if action == "play":
#music.stop()
main()
if action == "instructions":
Instructions()
if action == "next_level":
#music.stop()
main2()
if action == "nextlevel":
main3()
if action == "back":
#music.stop()
#this is the button where i can get back to main menu
GameIntro()
else:
pygame.draw.rect(screen, inactive , (x,y,w,h))
ButtonFont = pygame.font.SysFont("elephant",30)
TextSurf, TextRect = text_objects(msg, ButtonFont)
TextRect.center = ((x+w/2),(y+h/2))
screen.blit(TextSurf,TextRect)
Define a function that initializes the game from the very beginning. So at the first time, you start the game with calling,
After that, when game ends you can use conditions to start a new game, or create a new game_over() function to handle things that can not be handled by the new_game function.
This way, you save yourself from the trouble of reassigning all the variables to starting values in a game_over function.
Wrap all the game start variables into a function so you can call it whenever you need to start the game again. Basically what you did with the game over function. Except a start game function which resets the variables.