2013-10-18 100 views
0

我不知道如何在遊戲中實現遊戲菜單,我正在考慮具有指示信用和「玩遊戲」按鈕的按鈕。那麼有人會幫助我想出如何在pygame或livewires中製作簡單的菜單嗎?感謝提前:)在Pygame中製作的遊戲菜單

這是完整的代碼到我的遊戲:

# Asteroid Dodger 
# Player must avoid asteroids 
# make the score a global variable rather than tied to the asteroid. 
import pygame 
from livewires import games, color 
import math, random 

#score 
games.init(screen_width = 640, screen_height = 480, fps = 50) 

score = games.Text(value = 0, size = 25, color = color.green, 
        top = 5, right = games.screen.width - 10) 
games.screen.add(score) 

#lives 
lives = games.Text(value = 3, size = 25, color = color.green, 
        top = 5, left = games.screen.width - 620) 
games.screen.add(lives) 

#inventory 
inventory=[] 

#Asteroid images 
images = [games.load_image("asteroid_small.bmp"), 
      games.load_image("asteroid_med.bmp"), 
      games.load_image("asteroid_big.bmp")] 


class Ship(games.Sprite): 
    """ 
    A Ship controlled by player that explodes when it by Asteroids. 
    """ 
    image = games.load_image("player.bmp") 
    VELOCITY_STEP = .05 

    def __init__(self): 
     """ Initialize Ship object """ 
     super(Ship, self).__init__(image = Ship.image, 
            bottom = games.screen.height) 




    def update(self): 
     global inventory 
     """ uses A and D keys to move the ship """ 

     if games.keyboard.is_pressed(games.K_a): 
      self.dx -= Ship.VELOCITY_STEP * 2 

     if games.keyboard.is_pressed(games.K_d): 
      self.dx += Ship.VELOCITY_STEP * 2 

     if self.left < 0: 
      self.left = 0 

     if self.right > games.screen.width: 
      self.right = games.screen.width 

     self.check_collison() 

    def ship_destroy(self): 
     self.destroy() 
     new_explosion = Explosion(x = self.x, y = self.y) 
     games.screen.add(new_explosion) 

    def check_collison(self): 
     """ Check for overlapping sprites in the ship. """ 
     global lives 
     for items in self.overlapping_sprites: 
      items.handle_caught() 
      if lives.value <=0: 
       self.ship_destroy() 

class Explosion(games.Animation): 
    sound = games.load_sound("explosion.wav") 
    images = ["explosion1.bmp", 
       "explosion2.bmp", 
       "explosion3.bmp", 
       "explosion4.bmp", 
       "explosion5.bmp", 
       "explosion6.bmp", 
       "explosion7.bmp", 
       "explosion8.bmp", 
       "explosion9.bmp"] 

    def __init__(self, x, y): 
     super(Explosion, self).__init__(images = Explosion.images, 
             x = x, y = y, 
             repeat_interval = 4, n_repeats = 1, 
             is_collideable = False) 
     Explosion.sound.play() 



class Asteroid(games.Sprite): 
    global lives 
    global score 
    global inventory 
    """ 
    A asteroid which falls through space. 
    """ 

    image = games.load_image("asteroid_med.bmp") 
    speed = 3 

    def __init__(self, x,image, y = 10): 
     """ Initialize a asteroid object. """ 
     super(Asteroid, self).__init__(image = image, 
            x = x, y = y, 
            dy = Asteroid.speed) 


    def update(self): 
     """ Check if bottom edge has reached screen bottom. """ 
     if self.bottom>games.screen.height: 
      self.destroy() 
      score.value+=10 

    def handle_caught(self): 
     if lives.value>0: 
      lives.value-=1 
      self.destroy_asteroid() 

     if lives.value <= 0: 
      self.destroy_asteroid() 
      self.end_game() 


    def destroy_asteroid(self): 
     self.destroy() 

    def die(self): 
     self.destroy() 



    def end_game(self): 
     """ End the game. """ 
     end_message = games.Message(value = "Game Over", 
            size = 90, 
            color = color.red, 
            x = games.screen.width/2, 
            y = games.screen.height/2, 
            lifetime = 5 * games.screen.fps, 
            after_death = games.screen.quit) 
     games.screen.add(end_message) 

class Spawner(games.Sprite): 
    global images 
    """ 
    Spawns the asteroids 
    """ 
    image = games.load_image("spawner.bmp") 

    def __init__(self, y = 10, speed = 5, odds_change = 50): 

     super(Spawner, self).__init__(image = Spawner.image, 
            x = games.screen.width/2, 
            y = y, 
            dx = speed) 

     self.odds_change = odds_change 
     self.time_til_drop = 0 

    def update(self): 
     """ Determine if direction needs to be reversed. """ 
     if self.left < 0 or self.right > games.screen.width: 
      self.dx = -self.dx 
     elif random.randrange(self.odds_change) == 0: 
      self.dx = -self.dx 

     self.check_drop() 
     self.check_for_lives() 


    def check_drop(self): 
     """ Decrease countdown or drop asteroid and reset countdown. """ 
     if self.time_til_drop > 0: 
      self.time_til_drop -= 0.7 
     else: 
      asteroid_size = random.choice(images) 
      new_asteroid = Asteroid(x = self.x,image = asteroid_size) 
      games.screen.add(new_asteroid) 

      # makes it so the asteroid spawns slightly below the spawner 
      self.time_til_drop = int(new_asteroid.height * 1.3/Asteroid.speed) + 1 

    def check_for_lives(self): 
     droplives = random.randrange(0, 4000) 
     if droplives == 5: 
      lifetoken = Extralives(x = self.x) 
     g ames.screen.add(lifetoken) 




class Extralives(games.Sprite): 
    global lives 

    image = games.load_image('addlives.png') 
    speed = 2 
    sound = games.load_sound("collectlives.wav") 

    def __init__(self,x,y = 10): 
     """ Initialize a asteroid object. """ 
     super(Extralives, self).__init__(image = Extralives.image, 
            x = x, y = y, 
            dy = Extralives.speed) 
    def update(self): 
     """ Check if bottom edge has reached screen bottom. """ 
     if self.bottom>games.screen.height: 
      self.destroy() 

    def handle_caught(self): 

     Extralives.sound.play() 
     lives.value+=1 
     self.destroy() 





def main(): 
    """ Play the game. """ 
    bg = games.load_image("space.jpg", transparent = False) 
    games.screen.background = bg 

    the_spawner = Spawner() 
    games.screen.add(the_spawner) 

    pygame.mixer.music.load("Jumpshot.ogg") 
    pygame.mixer.music.play()  



    the_ship = Ship() 
    games.screen.add(the_ship) 

    games.mouse.is_visible = False 

    games.screen.event_grab = True 
    games.screen.mainloop() 

#starts the game 
main() 
+0

關閉我的頭頂,我會使用外部循環(while game_running = True)來顯示菜單。如果玩家選擇「開始遊戲」,你執行你的主循環() – Alvaro

+1

看看[這篇文章](http://scripters-corner.net/2013/04/11/creating-a-menu-in-pygame /) – Azeirah

回答

0

的Pygbutton模塊提供了一種方法來創建Pygame的程序的按鈕。你可以通過「pip install pygbutton」下載它。有github上的演示:https://github.com/asweigart/pygbutton