2017-09-05 4 views
1

私はpygameを試してみると誰かが私を助けてくれることを願っています。終了ボタンを作成するためにクラスを定義したいと思いますが、これを行うとマウスの衝突を取得できません。私は間違っている可能性が最も高いですが、本当に助けに感謝します。Pygameクラスのメニュー

私は以下の状態で動作しますが、デフに入れても動作しません!

pygame.init() 


class Window: 

    def __init__(self): 
     self.screen_width = 800 
     self.screen_height = 600 
     self.screen = (pygame.display.set_mode((self.screen_width, self.screen_height))) 
     self.FPS = 30 
     self.clock = pygame.time.Clock() 
     self.font = pygame.font.SysFont('Arial', 25) 
     self.menu_open = True 
     self.colors = {"red": (255, 0, 0), 
         "green": (0, 255, 0), 
         "blue": (0, 0, 255), 
         "white": (255, 255, 255), 
         "black": (0, 0, 0), 
         "brown": (153, 76, 0), 
         "grey": (100, 100, 100)} 

    def setup(self): 
     self.screen.fill(self.colors["black"]) 
     pygame.display.set_caption("Menu Test!") 

    def text(self, message, text_color, x_pos, y_pos): 
     text = self.font.render(message, True, (self.colors[text_color])) 
     text_rect = text.get_rect(center=(x_pos, y_pos)) 
     self.screen.blit(text, text_rect) 

    def exit(self): 
     self.screen.fill(self.colors["black"]) 
     text = self.font.render("Thank you for playing. Goodbye!", True, 
           (self.colors["white"])) 
     text_rect = text.get_rect(center=(self.screen_width/2, 
              self.screen_height/2)) 
     self.screen.blit(text, text_rect) 
     pygame.display.update() 
     sleep(3) 
     pygame.quit() 
     sys.exit() 


def main(): 
    window = Window() 
    window.setup() 

これは私がデフ

quit_button = pygame.draw.rect(window.screen, window.colors["white"], 
            (window.screen_width/2 - 100, 
            window.screen_height/1.5 - 25, 200, 50), 0) 
    window.text("QUIT", "red", window.screen_width/2, window.screen_height/1.5) 
    pygame.display.update() 

    while window.menu_open == 1: 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 

クラスに希望そして、それはRECT上でのマウスクリックのposistionを取得する場所これはビットです。

  if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: 
       pos = pygame.mouse.get_pos() 
       if quit_button.collidepoint(pos): 
        window.exit() 
       else: 
        print("Error Line 48") 

if __name__ == "__main__": 
    main() 

答えて

0

image持つクラス(この場合、Iはpygame.sprite.Spriteサブクラスを使用)とrect属性を作成し、画像上のテキストをブリット。

import sys 
import pygame as pg 


pg.init() 

class Window: 

    def __init__(self): 
     self.screen = pg.display.set_mode((800, 600)) 
     self.rect = self.screen.get_rect() 
     self.FPS = 30 
     self.clock = pg.time.Clock() 
     self.font = pg.font.SysFont("Arial", 25) 
     self.menu_open = True 
     self.colors = {"red": (255, 0, 0), 
         "green": (0, 255, 0), 
         "blue": (0, 0, 255), 
         "white": (255, 255, 255), 
         "black": (0, 0, 0), 
         "brown": (153, 76, 0), 
         "grey": (100, 100, 100)} 

    def setup(self): 
     self.screen.fill(self.colors["black"]) 
     pg.display.set_caption("Menu Test!") 

    def text(self, message, text_color, x_pos, y_pos): 
     text = self.font.render(message, True, (self.colors[text_color])) 
     text_rect = text.get_rect(center=(x_pos, y_pos)) 
     self.screen.blit(text, text_rect) 

    def exit(self): 
     self.screen.fill(self.colors["black"]) 
     text = self.font.render("Thank you for playing. Goodbye!", True, 
           (self.colors["white"])) 
     text_rect = text.get_rect(center=(self.rect.w/2, self.rect.h/2)) 
     self.screen.blit(text, text_rect) 
     pg.display.update() 
     pg.time.wait(1000) 
     pg.quit() 
     sys.exit() 


class Button(pg.sprite.Sprite): 

    def __init__(self, pos, text, window): 
     super().__init__() # Call __init__ of the parent class. 
     # Render the text. 
     self.text_surf = window.font.render(text, True, window.colors["black"]) 
     self.image = pg.Surface((self.text_surf.get_width()+40, 
           self.text_surf.get_height()+20)) 
     self.image.fill(window.colors["white"]) 
     # Now blit the text onto the self.image. 
     self.image.blit(self.text_surf, (20, 10)) 
     self.rect = self.image.get_rect(topleft=pos) 


def main(): 
    window = Window() 
    window.setup() 
    clock = pg.time.Clock() 
    # gui is a sprite group which will contain the button sprites. 
    gui = pg.sprite.Group() 
    # Instantiate some buttons. 
    quit_button = Button(
     pos=(window.rect.w/2 - 100, window.rect.h/1.5 - 25), 
     text="QUIT", 
     window=window, 
     ) 
    hello_button = Button(
     pos=(window.rect.w/8, window.rect.h/2), 
     text="hello", 
     window=window, 
     ) 
    # Add the buttons to the gui group. 
    gui.add(quit_button, hello_button) 

    while window.menu_open == True: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       window.exit() 
      if event.type == pg.MOUSEBUTTONDOWN and event.button == 1: 
       # Handle button events. 
       if quit_button.rect.collidepoint(event.pos): 
        window.exit() 
       elif hello_button.rect.collidepoint(event.pos): 
        print("hello") 

     gui.update() # Call update methods of contained sprites. 
     gui.draw(window.screen) # Draw all sprites. 
     pg.display.flip() 
     clock.tick(30) 

if __name__ == "__main__": 
    main() 

もう少し高度なソリューションは、Buttonクラスでイベントを処理した後、あなたがインスタンス化中に合格する必要があり、コールバック関数を呼び出すことであろう。

class Button(pg.sprite.Sprite): 

    def __init__(self, pos, text, window, callback): 
     super().__init__() 
     self.text_surf = window.font.render(text, True, window.colors["black"]) 
     self.image = pg.Surface((self.text_surf.get_width()+40, 
           self.text_surf.get_height()+20)) 
     self.image.fill(window.colors["white"]) 
     self.image.blit(self.text_surf, (20, 10)) 
     self.rect = self.image.get_rect(topleft=pos) 
     # The callback function will be called when 
     # the left mouse button gets pressed. 
     self.callback = callback 

    def handle_event(self, event): 
     if event.type == pg.MOUSEBUTTONDOWN and event.button == 1: 
      if self.rect.collidepoint(event.pos): 
       self.callback() # Call the callback function. 


def main(): 
    window = Window() 
    window.setup() 
    clock = pg.time.Clock() 

    gui = pg.sprite.Group() 
    quit_button = Button(
     pos=(window.rect.w/2 - 100, window.rect.h/1.5 - 25), 
     text="QUIT", 
     window=window, 
     callback=window.exit, # Pass the callback function. 
     ) 
    hello_button = Button(
     pos=(window.rect.w/8, window.rect.h/2), 
     text="hello", 
     window=window, 
     callback=lambda: print("hello"), # Pass the callback function. 
     ) 
    gui.add(quit_button, hello_button) 

    while window.menu_open == True: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       window.exit() 
      # Iterate over the gui group and pass the event to the buttons. 
      # If they collide with the mouse, call the callback func. 
      for button in gui: 
       button.handle_event(event) 

     gui.update() 
     gui.draw(window.screen) 
     pg.display.flip() 
     clock.tick(30) 

if __name__ == "__main__": 
    main() 
+0

あなたは、クラス、pygameのスプライトとスプライトグループのチュートリアル[プログラムのアーケードゲームの章12および13]のチェックアウト(http://programarcadegames.com/index.php?chapter=introduction_to_sprites&lang=en#section_13が必要な場合)。 – skrx

関連する問題