2016-07-20 11 views
0

私のゲームのメニュー画面を作成しようとしています。現在は、スプライトをボタンに使用していて、すべてがうまくいき、無限のボタンを作成することができます(現在は開始とオプションがあります)。私はそれがボタンクラスのwhileループと関係があると思いますが、それを修正する方法はわかりません。私はおそらくあなたが私が何かを明確にするために必要な場合、ここでは意味がありません。ありがとう!Pygame - メニュー画面のボタンが正しく動作しない

import pygame 
import random 
import time 

pygame.init() 

#colours 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,155,0) 
blue = (50,50,155) 

display_width = 800 
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('Numeracy Ninjas') 

clock = pygame.time.Clock() 

img_button_start = pygame.image.load('Sprites/Buttons/button_start.png') 
img_button_options = pygame.image.load('Sprites/Buttons/button_options.png') 

gameDisplay.fill(white) 

class Button(pygame.sprite.Sprite): 
    def __init__(self, sprite, buttonX, buttonY): 
     super().__init__() 

     gameDisplay.blit(sprite, (buttonX, buttonY)) 

     pygame.display.update() 

     running = True 
     while (running): 
      for event in pygame.event.get(): 
       if event.type == pygame.MOUSEBUTTONDOWN: 
        # Set the x, y postions of the mouse click 
        x, y = event.pos 
        print(x, y) 
        if x <= (150 + buttonX) and x >=(0 + buttonX) and y <= (75 + buttonY) and y >= (0 + buttonY): 
         print('clicked on button') 

def gameIntro():     
    button_start = Button(img_button_start, 27, 0) 
    button_options = Button(img_button_options, 27, 500) 

gameIntro() 

答えて

0

ボタンクラスのコンストラクタでは、無限ループが発生します。これは、2番目のボタンを作成するコード部分に決して到達しないことを意味します。

def gameIntro():     
    button_start = Button(img_button_start, 27, 0) #stuck in infinite loop 
    print('This print statement is never reached') 
    button_options = Button(img_button_options, 27, 500) 

代わりに、何がやりたいことは2つのボタンが初期化され、その後、イベントをチェックし、あなたのgameIntro()方法でゲームのメインループを持っています。 mousebuttownownイベントが発生した場合、このボタンのインスタンスがクリックされたかどうかをチェックしてから、そのボタンがクリックされたかどうかをチェックするボタンの機能に、イベント(またはマウスのボタンがクリックされていない場合はイベント位置)入力(おそらくあなたがメインのゲームループで扱うものを返すことによって)。

私は、次のコードを実行していないことに注意してください、私はあなたにそれが構造化すべきかのアイデアを与えるためにしようとしている:フィードバックのための

class Button(pygame.sprite.Sprite): 
    def __init__(self, image, buttonX, buttonY): 
     super().__init__() 
     self.image = image 
     self.rect = image.getRect() 
     self.rect.x = buttonX 
     self.rect.y = buttonY 

    def wasClicked(event): 
     if self.rect.collidepoint(event.pos): 
      return True 

def gameIntro(): 
    #initialize buttons 
    buttons = pygame.sprite.Group() #make a group to make drawing easier 
    button_start = Button(img_button_start, 27, 0) 
    button_options = Button(img_button_options, 27, 500) 
    #draw buttons to display 
    buttons.draw(gameDisplay) 
    pygame.display.update() 

    #main game loop 
    running = True 
    while (running): 
     for event in pygame.event.get(): 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       #check for every button whether it was clicked 
       for btn in buttons: 
        if btn.wasClicked(): 
         #do something appropriate 
      if event.type == pygame.QUIT: 
       pygame.quit() 
+0

おかげで、私はそれが無限に起因した疑いがありますループは、しかし、私はあなたのコードを使用して、それがどのように動作するかを見てみましたが、 "期待インデントブロック"と言って続けた。私はプログラミングに慣れていないので、メインループにイベントチェックメソッドを置くのが実際に私にとってはうまくいくかどうかはわかりません。buttonX、buttonYなどのボタンクラス内の変数はクラス内でしか呼び出せないので、しかし、再びself.rect.collidepointを使用しているので、わかりません。私は可能な場合は、同時にループを実行する方法を把握しようとしているが、私は知らない。 –

関連する問題