2016-08-08 12 views
2

私は自分のゲームのメインコードを完成しました。私はメニュー画面の作成を始めました。私はうまく画面上のボタンを表示することができますが、私はどこかをクリックしたときに、私はこのErrorを得る:Pygame Button getRect Collidepointが機能していませんか?

私はこれを修正については行くことができますどのように?この質問で私が何かを明確にしなかった場合は、私に教えてください。ありがとう!あなたは、クラス、単にローカル変数のいずれかの属性を宣言していない

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() 

#Fonts 

smallfont = pygame.font.SysFont("comicsansms", 25) 
medfont = pygame.font.SysFont("comicsansms", 50) 
largefont = pygame.font.SysFont("comicsansms", 75) 

#Sprites 

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) 
pygame.display.update() 


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

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

     pygame.display.update() 

     selfrect = image.get_rect() 

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

def gameIntro(): 
    buttons = pygame.sprite.Group() 
    button_start = Button(img_button_start, 27, 0) 
    button_options = Button(img_button_options, 27, 500) 
    buttons.add(button_start) 
    buttons.add(button_options) 

    print(buttons) 


    #main game loop 
    running = True 
    while running: 
     for event in pygame.event.get(): 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       print(event.pos) 
       #check for every button whether it was clicked 
       for btn in buttons: 
        print('forbtninbuttons') 
        if btn.wasClicked(): 
         print('clicked!') 

      if event.type == pygame.QUIT: 
       pygame.quit() 
+0

今後、エラーの画像にリンクする代わりに、質問にエラーを書き留めてください。画像にリンクすると検索能力が低下し、スクリーンリーダーを持っている人がエラーを聞くことができなくなります。 http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors –

答えて

2

はここにmenuscreenのための私のコードです。あなたの初期化子で、あなたwasClicked(event)方法でself.selfrect = image.get_rect()をやってみません:

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

それはしかし、あなたのRECT変数だけRECTに名前を付けるために、通常は慣例です。

class Button(pygame.sprite.Sprite): 
    def __init__(self, image, buttonX, buttonY): 
     super().__init__() 
     # This code doesn't make sense here. It should be inside your game loop. 
     # gameDisplay.blit(image, (buttonX, buttonY)) 
     # pygame.display.update() 

     self.image = image # It's usually good to have a reference to your image. 
     self.rect = image.get_rect() 

    def wasClicked(self, event): 
     if self.rect.collidepoint(event.pos): 
      return True 
     else: 
      return False 
+0

お返事ありがとうございます。 "NameError:name 'self'は" NameError:name 'の代わりに定義されていません "selfrect'は定義されていません" –

+0

ああ、申し訳ありません、* self *を入れるのを忘れてしまったメソッドパラメータ。今すぐ動作するはずです:) –

+0

メソッドパラメータに "self"を入れましたが、今はこのエラーが出ます: "TypeError:wasClicked()が欠けています1必要な位置引数: 'self'" –

関連する問題