Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pygame 即使我创建了对象,我的按钮也没有定义?_Pygame - Fatal编程技术网

Pygame 即使我创建了对象,我的按钮也没有定义?

Pygame 即使我创建了对象,我的按钮也没有定义?,pygame,Pygame,我收到一个按钮\u 01未定义错误。我有一个button类,它定义了我的按钮是如何制作的,以及它的一些特定功能,比如defMouseButtonDown。我还有一个场景类,它组织我的活动 我在标题屏幕的processInput中调用了mouseButtonDown(button_01),我已经在标题屏幕的渲染中创建了该对象。我怎样才能解决这个问题 import pygame import os WHITE = (255, 255, 255) GREY = (200, 200, 200) B

我收到一个按钮\u 01未定义错误。我有一个button类,它定义了我的按钮是如何制作的,以及它的一些特定功能,比如defMouseButtonDown。我还有一个场景类,它组织我的活动

我在标题屏幕的processInput中调用了mouseButtonDown(button_01),我已经在标题屏幕的渲染中创建了该对象。我怎样才能解决这个问题

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

您正在将
Render
方法中的
按钮\u 01
实例创建为局部变量,这意味着
ProcessInput
方法无法访问此变量。您应该在
\uuuu init\uuuu
方法
self.button\u 01=button(“Great!”,(60,30),my\u Great\u函数)
中创建按钮实例,以便所有其他方法都可以访问此属性

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()
调用移动到程序的顶部(导入的下方),因为字体初始化出错。

您正在将
呈现
方法中的
按钮\u 01
实例创建为局部变量,这意味着
处理输入
方法无法访问此变量。您应该在
\uuuu init\uuuu
方法
self.button\u 01=button(“Great!”,(60,30),my\u Great\u函数)
中创建按钮实例,以便所有其他方法都可以访问此属性

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()
调用移到程序顶部(导入下方),因为字体初始化出现问题。

谢谢!结果很好。将来,我将在其他类hanks的init中创建其他对象!结果很好。将来,我将在其他类的init中创建其他对象