即使我制作了这个物体,我的按钮也没有定义?

时间:2017-11-25 14:43:30

标签: pygame

我正在接收一个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())

1 个答案:

答案 0 :(得分:2)

您在button_01方法中创建Render实例作为局部变量,这意味着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()调用移到程序顶部(导入下面),因为字体初始化出了问题。