2017-11-25 4 views
0

나는 받아들이고 있는데, button_01은 정의되지 않은 오류입니다. 내 버튼이 어떻게 만들어 지는지 정의하는 버튼 클래스와 defMouseButtonDown과 같은 특정 기능을 정의합니다. 나는 또한 나의 사건을 조직하는 장면 수업도한다.개체를 만들었지 만 내 단추가 정의되지 않았습니까?

내 titleScreen의 processInput에서 mouseButtonDown (button_01)을 호출했으며 이미 내 titleScreen의 렌더링에서 객체를 만들었습니다. 이 문제를 어떻게 해결할 수 있습니까?

import pygame 
import os 


WHITE = (255, 255, 255) 
GREY = (200, 200, 200) 
BLACK = (0, 0, 0) 

screen = pygame.display.set_mode((800, 400)) 

############################### 
class Button(): 
    def __init__(self, txt, location, action, bg=WHITE, fg=BLACK, size=(80, 30), font_name="Segoe Print", font_size=16): 
     self.color = bg # the static (normal) color 
     self.bg = bg # actual background color, can change on mouseover 
     self.fg = fg # text color 
     self.size = size 

     self.font = pygame.font.SysFont(font_name, font_size) 
     self.txt = txt 
     self.txt_surf = self.font.render(self.txt, 1, self.fg) 
     self.txt_rect = self.txt_surf.get_rect(center=[s//2 for s in self.size]) 

     self.surface = pygame.surface.Surface(size) 
     self.rect = self.surface.get_rect(center=location) 

     self.call_back_ = action 

    def draw(self): 
     self.mouseover() 

     self.surface.fill(self.bg) 
     self.surface.blit(self.txt_surf, self.txt_rect) 
     screen.blit(self.surface, self.rect) 

    def mouseover(self): 
     self.bg = self.color 
     pos = pygame.mouse.get_pos() 
     if self.rect.collidepoint(pos): 
      self.bg = GREY # mouseover color 

    def call_back(self): 
     self.call_back_() 


def my_great_function(): 
    print("Great! " * 5) 


def my_fantastic_function(): 
    print("Fantastic! " * 4) 


def mousebuttondown(button): 
    pos = pygame.mouse.get_pos() 
    #for button in buttons: 
     #if button.rect.collidepoint(pos): 
      #button.call_back() 

    if button.rect.collidepoint(pos): 
     button.call_back() 
######################### 


class SceneBase: 
    def __init__(self): 
     self.next = self 

    def ProcessInput(self, events, pressed_keys): 
     print("uh-oh, you didn't override this in the child class") 

    def Update(self): 
     print("uh-oh, you didn't override this in the child class") 

    def Render(self, screen): 
     print("uh-oh, you didn't override this in the child class") 

    def SwitchToScene(self, next_scene): 
     self.next = next_scene 

    def Terminate(self): 
     self.SwitchToScene(None) 

def run_game(width, height, fps, starting_scene): 
    pygame.init() 
    screen = pygame.display.set_mode((width, height)) 
    clock = pygame.time.Clock() 

    active_scene = starting_scene 


    while active_scene != None: 
     pressed_keys = pygame.key.get_pressed() 

     # Event filtering 
     filtered_events = [] 
     for event in pygame.event.get(): 
      quit_attempt = False 
      if event.type == pygame.QUIT: 
       quit_attempt = True 
      elif event.type == pygame.KEYDOWN: 
       alt_pressed = pressed_keys[pygame.K_LALT] or \ 
           pressed_keys[pygame.K_RALT] 
       if event.key == pygame.K_ESCAPE: 
        quit_attempt = True 
       elif event.key == pygame.K_F4 and alt_pressed: 
        quit_attempt = True 

      if quit_attempt: 
       active_scene.Terminate() 
      else: 
       filtered_events.append(event) 

     active_scene.ProcessInput(filtered_events, pressed_keys) 
     active_scene.Update() 
     active_scene.Render(screen) 

     active_scene = active_scene.next 

     pygame.display.flip() 
     clock.tick(fps) 

# The rest is code where you implement your game using the Scenes model 

class TitleScene(SceneBase): 
    def __init__(self): 
     SceneBase.__init__(self) 

    def ProcessInput(self, events, pressed_keys): 
     for event in events: 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: 
       # Move to the next scene when the user pressed Enter 
       self.SwitchToScene(GameScene()) 

      if event.type == pygame.KEYUP: 
       print("You are hitting up!") 
       print(self.next) 

      if event.type == pygame.MOUSEBUTTONDOWN: 
       mousebuttondown(button_01) 

    def Update(self): 
     pass 

    def Render(self, screen): 
     # For the sake of brevity, the title scene is a blank red screen 
     screen.fill((255, 0, 0)) 

     #Title Creation 
     myfont = pygame.font.SysFont(("Moyko"), 50) 
     textImage = myfont.render("Anime Pong", True, (0, 255, 0)) 
     screen.blit(textImage, (100,100)) 

     #Button Creation 

     button_01 = Button("Great!", (60, 30), my_great_function) 
     button_01.draw() 



def my_great_function(): 
    print("Great! " * 5) 


def my_fantastic_function(): 
    print("Fantastic! " * 4) 


class GameScene(SceneBase): 
    def __init__(self): 
     SceneBase.__init__(self) 

    def ProcessInput(self, events, pressed_keys): 
     pass 

    def Update(self): 
     pass 

    def Render(self, screen): 
     # The game scene is just a blank blue screen 
     screen.fill((0, 0, 255)) 

run_game(800, 400, 60, TitleScene()) 

답변

2

당신은 로컬 변수로 Render 방법에 button_01 인스턴스를 생성하고 그는 ProcessInput 방법이 변수에 액세스 할 수 없습니다 의미합니다. __init__ 메서드 self.button_01 = Button("Great!", (60, 30), my_great_function)에 버튼 인스턴스를 만들어야 다른 모든 메서드에서이 특성에 액세스 할 수 있습니다. 뭔가 글꼴 초기화 문제 갔기 때문에

class TitleScene(SceneBase): 
    def __init__(self): 
     SceneBase.__init__(self) 
     # Create the button and the font instance here. 
     self.button_01 = Button("Great!", (60, 30), my_great_function) 
     self.myfont = pygame.font.SysFont("Moyko", 50) 

    def ProcessInput(self, events, pressed_keys): 
     for event in events: 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: 
       self.SwitchToScene(GameScene()) 
      if event.type == pygame.KEYUP: 
       print("You are hitting up!") 
       print(self.next) 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       mousebuttondown(self.button_01) 

    def Update(self): 
     pass 

    def Render(self, screen): 
     screen.fill((100, 0, 0)) 

     textImage = self.myfont.render("Anime Pong", True, (0, 255, 0)) 
     screen.blit(textImage, (100,100)) 
     # Just draw the button here 
     self.button_01.draw() 

은 또한, 프로그램 (수입 아래)의 상단에 pygame.init() 전화를 이동했다.

+0

감사합니다. 그게 잘 풀렸다. 앞으로 다른 클래스를 초기화 할 때 다른 객체를 생성 할 것입니다. – turtlefish12