2

I was wondering how could I make a start and a death screen in pygame any help is appreciated thank you!

here is my code right now script

this is a object when ever my player collides I lose -1 health and if my heatlh is > -6 I want it to load a death screen and then when you click space or something it takes my player back where I started something like that and also how could I make an opening?

   # health players
   for dice in dicing:
       if playerman.rect.colliderect(dice.hitbox):
           hurtsound.play()
           if playerman.health > -6:
               playerman.health -= 1
               playerman.x = 10

and here is my player class

class player:
   def __init__(self,x,y,height,width,color):
       self.x = x
       self.y = y
       self.color = color
       self.height  = height
       self.width = width
       self.speed = 5
       self.isJump = False
       self.JumpCount = 10
       self.fall = 0
       #hit box
       self.hitbox = (self.x + 20, self.y, 28, 60)
       self.stand = pygame.image.load("stands.png")
       self.rights = [pygame.image.load("L1.png"),
       pygame.image.load("L2.png"),
       pygame.image.load("L3.png"),
       pygame.image.load("L4.png"),
       pygame.image.load("L5.png"),
       pygame.image.load("L6.png"),
       pygame.image.load("L7.png"),
       pygame.image.load("L8.png"),
       pygame.image.load("L9.png"),
       pygame.image.load("L10.png"),
       pygame.image.load("L11.png"),
       pygame.image.load("L12.png"),
       pygame.image.load("L13.png"),
       pygame.image.load("L14.png"),
       pygame.image.load("L15.png")]
       self.lefts = [pygame.image.load("e1.png"),
       pygame.image.load("e2.png"),
       pygame.image.load("e3.png"),
       pygame.image.load("e4.png"),
       pygame.image.load("e5.png"),
       pygame.image.load("e6.png"),
       pygame.image.load("e7.png"),
       pygame.image.load("e8.png"),
       pygame.image.load("e9.png"),
       pygame.image.load("e10.png"),
       pygame.image.load("e11.png"),
       pygame.image.load("e12.png"),
       pygame.image.load("e13.png"),
       pygame.image.load("e14.png"),
       pygame.image.load("e15.png")]
       self.standingright = [pygame.image.load("d1.png"),
               pygame.image.load("d2.png"),
               pygame.image.load("d3.png"),
               pygame.image.load("d4.png"),
               pygame.image.load("d5.png"),
               pygame.image.load("d6.png"),
               pygame.image.load("d7.png"),
               pygame.image.load("d8.png"),
               pygame.image.load("d9.png"),
               pygame.image.load("d10.png"),
               pygame.image.load("d11.png"),
               pygame.image.load("d12.png"),
               pygame.image.load("d13.png"),
               pygame.image.load("d14.png"),
               pygame.image.load("d15.png")]
       self.standingleft =[pygame.image.load("s1.png"),
           pygame.image.load("s2.png"),
           pygame.image.load("s3.png"),
           pygame.image.load("s4.png"),
           pygame.image.load("s5.png"),
           pygame.image.load("s6.png"),
           pygame.image.load("s7.png"),
           pygame.image.load("s8.png"),
           pygame.image.load("s9.png"),
           pygame.image.load("s10.png"),
           pygame.image.load("s11.png"),
           pygame.image.load("s12.png"),
           pygame.image.load("s13.png"),
           pygame.image.load("s14.png"),
           pygame.image.load("s15.png")]
       self.rights = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.rights]
       self.lefts = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.lefts]
       self.standingright = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.standingright]
       self.standingleft = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.standingleft]
       self.bo_index = 0
       self.anim_index = 0
       self.stans_index = 0
       self.direction = "right"
       self.direction = "left"
       self.direction = "standright"
       self.direction = "standleft"
       self.health = 10
       self.hitbox = (self.x + -30, self.y + 0, 31, 57)
       # playerman hitbox
       self.hits = (self.x + 20, self.y, 28,60)

       self.rect = pygame.rect = pygame.Rect(self.x,self.y,width, height)



Habib Ismail
  • 69
  • 5
  • 16
  • 3
    use variale to control what to display `state = "Start"` , `state = "Game"` , `state = "Death"`. And then you can use it in main loop `if state == "Start": check_keys_and_display_start_screen()`, etc. `if state == "Game": game_code()`, `if state == "Death": death_code()`, And when you run state `game` and you get `health < -6` then set `state = "Death"` – furas Jun 02 '20 at 01:05
  • 2
    You can find some examples [here](https://stackoverflow.com/questions/14700889/pygame-level-menu-states/14727074#14727074), [here](https://stackoverflow.com/questions/59678318/how-can-i-make-my-sprite-launch-an-object-towards-the-mouse-position/59681310#59681310) and [here](https://stackoverflow.com/questions/58540537/how-to-fade-the-screen-out-and-back-in-using-pygame/58541111#58541111) – sloth Jun 02 '20 at 10:02

1 Answers1

0

This is how I do it:

I have my game in a method, probably called game(). I make a separate function for the start menu (start_menu()) which returns either "play", "quit", or "settings". This would be the code that controls everything:

while True:
    menu_result = start_menu()

    if menu_result == "start":
        game()
    elif menu_result == "settings":
        settings()
    else:  # this means it returned "quit"
        pygame.quit()
        exit()

NOTE: If you don't have a settings() function, just delete that part.

  • how can I put my whole game in a class? like do you make a class game(): and then put everything that I have in the game class? I am still confused here is my full code if you could help me https://pastebin.com/raw/wETHBK6Y – Habib Ismail Jun 05 '20 at 23:30