Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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
Python 如何将文本输出到屏幕,同时也输出面向对象的按钮-Pygame #导入模块 导入pygame 导入系统 随机输入 #所有的游戏都在这下面 pygame.init() #字体定义 backFont=pygame.font.SysFont(“monospace”,40) titleFont=pygame.font.SysFont(“garamond”,100) buttonFont=pygame.font.SysFont(“garamond”,25) bigFont=pygame.font.SysFont(“garamond”,100) #颜色定义 BackGray=pygame.Color('gray60') screenGray=pygame.Color('gray80') buttonGray1=pygame.Color('gray40') buttonGray2=pygame.Color('gray50') buttonGray3=pygame.Color('gray30')) textColour=pygame.Color('navy') #屏幕大小设置 screen=pygame.display.set_模式((800600)) #为此处设置的按钮初始化 类别按钮: 定义初始(自、x、y、宽度、高度、颜色、表面): self.x=x self.y=y 自我高度=高度 self.width=宽度 颜色 self.surface=曲面 def已压缩(自): mouse\u position=pygame.mouse.get\u pos() 鼠标位置x=鼠标位置[0] 鼠标位置y=鼠标位置[1] 如果鼠标_x>self.x: 如果鼠标_xself.y: 如果鼠标y_Python_Oop_Pygame - Fatal编程技术网

Python 如何将文本输出到屏幕,同时也输出面向对象的按钮-Pygame #导入模块 导入pygame 导入系统 随机输入 #所有的游戏都在这下面 pygame.init() #字体定义 backFont=pygame.font.SysFont(“monospace”,40) titleFont=pygame.font.SysFont(“garamond”,100) buttonFont=pygame.font.SysFont(“garamond”,25) bigFont=pygame.font.SysFont(“garamond”,100) #颜色定义 BackGray=pygame.Color('gray60') screenGray=pygame.Color('gray80') buttonGray1=pygame.Color('gray40') buttonGray2=pygame.Color('gray50') buttonGray3=pygame.Color('gray30')) textColour=pygame.Color('navy') #屏幕大小设置 screen=pygame.display.set_模式((800600)) #为此处设置的按钮初始化 类别按钮: 定义初始(自、x、y、宽度、高度、颜色、表面): self.x=x self.y=y 自我高度=高度 self.width=宽度 颜色 self.surface=曲面 def已压缩(自): mouse\u position=pygame.mouse.get\u pos() 鼠标位置x=鼠标位置[0] 鼠标位置y=鼠标位置[1] 如果鼠标_x>self.x: 如果鼠标_xself.y: 如果鼠标y

Python 如何将文本输出到屏幕,同时也输出面向对象的按钮-Pygame #导入模块 导入pygame 导入系统 随机输入 #所有的游戏都在这下面 pygame.init() #字体定义 backFont=pygame.font.SysFont(“monospace”,40) titleFont=pygame.font.SysFont(“garamond”,100) buttonFont=pygame.font.SysFont(“garamond”,25) bigFont=pygame.font.SysFont(“garamond”,100) #颜色定义 BackGray=pygame.Color('gray60') screenGray=pygame.Color('gray80') buttonGray1=pygame.Color('gray40') buttonGray2=pygame.Color('gray50') buttonGray3=pygame.Color('gray30')) textColour=pygame.Color('navy') #屏幕大小设置 screen=pygame.display.set_模式((800600)) #为此处设置的按钮初始化 类别按钮: 定义初始(自、x、y、宽度、高度、颜色、表面): self.x=x self.y=y 自我高度=高度 self.width=宽度 颜色 self.surface=曲面 def已压缩(自): mouse\u position=pygame.mouse.get\u pos() 鼠标位置x=鼠标位置[0] 鼠标位置y=鼠标位置[1] 如果鼠标_x>self.x: 如果鼠标_xself.y: 如果鼠标y,python,oop,pygame,Python,Oop,Pygame,然后,底部应该有一个按钮,或者一个灰色矩形。但是,当我尝试在按钮框上打印一些东西时,在我将按钮绘制到屏幕上之后,它不会出现 有人能告诉我为什么以及如何修理它吗? 或者选择如何将文本输出到按钮上 提前感谢我首先创建一个单独的背景曲面,并将所有从未更改过的背景图形(以下示例中的数字)显示在该曲面上。在主循环中,您只需点击此背景即可清除屏幕 要在按钮上闪烁文本,请执行以下操作: 将文本传递给\uuuuu init\uuuu方法 创建按钮图像(pygame.Surface) 渲染文本 为图像和文本创建矩

然后,底部应该有一个按钮,或者一个灰色矩形。但是,当我尝试在按钮框上打印一些东西时,在我将按钮绘制到屏幕上之后,它不会出现

有人能告诉我为什么以及如何修理它吗? 或者选择如何将文本输出到按钮上


提前感谢

我首先创建一个单独的
背景
曲面,并将所有从未更改过的背景图形(以下示例中的数字)显示在该曲面上。在主循环中,您只需点击此背景即可清除屏幕

要在按钮上闪烁文本,请执行以下操作:

  • 将文本传递给
    \uuuuu init\uuuu
    方法
  • 创建按钮图像(pygame.Surface)
  • 渲染文本
  • 为图像和文本创建矩形(将图像矩形的中心传递到文本矩形以使文本居中)
  • 将文本表面点到图像上
  • 对于我使用的按钮,将它们放入一个sprite组,这样您只需调用
    sprite\u group.draw(screen)
    即可绘制它们。精灵需要一个和一个属性才能工作。Pygame Rect具有碰撞检测方法,您应该使用这些方法,而不是编写自己的方法。在这种情况下,我们可以使用
    collidepoint
    方法并将
    事件.pos
    传递给它

    #Importing Modules
    import pygame
    import sys
    import random
    
    #All pygame stuff under here
    pygame.init()
    
    #Font definitions
    backFont = pygame.font.SysFont("monospace",40)
    titleFont = pygame.font.SysFont("garamond", 100)
    buttonFont = pygame.font.SysFont("garamond", 25)
    bigFont = pygame.font.SysFont("garamond",100)
    
    #Colour definitions
    BackGray = pygame.Color('gray60')
    screenGray = pygame.Color('gray80')
    buttonGray1 = pygame.Color('gray40')
    buttonGray2 = pygame.Color('gray50')
    buttonGray3 = pygame.Color('gray30')
    textColour = pygame.Color('navy')
    
    #Screen size set
    screen = pygame.display.set_mode((800, 600))
    
    #Class for the buttons set here
    class Button:
        def __init__(self, x, y, width, height, colour, surface):
            self.x = x
            self.y = y
            self.height = height
            self.width = width
            self.colour = colour
            self.surface = surface
    
        def isPressed(self):
            mouse_position = pygame.mouse.get_pos()
            mouse_x = mouse_position[0]
            mouse_y = mouse_position[1]
            if mouse_x > self.x:
                if mouse_x < self.x + self.width:
                    if mouse_y > self.y:
                        if mouse_y < self.y + self.height:
                            mouse_click = pygame.mouse.get_pressed()
                            left_click = mouse_click[0]
                            if left_click:
                                self.colour = (0,0,0)
                                return True
            self.colour = (230, 230, 230)
            return False
    
        def drawButton(self):
            pygame.draw.rect(self.surface, self.colour,  (self.x, self.y, self.width, self.height))
            pygame.display.flip()
    
    def FrontPage():
        #Back screen
        screen.fill(screenGray)
    
        #The background is defined here
        BinaryPage = []
        for i in range(0,15):
            BinaryString = ""
            for j in range(0,33):
                BinaryString += random.choice(["0","1"])
            BinaryPage.append(BinaryString)
    
        for i in range(0,15):
            screen.blit(backFont.render(BinaryPage[i], 1, BackGray), (0,i * 40))
    
        #The title is defined and printed here
        Title1 = titleFont.render("The Six", 10, textColour)
        Title2 = titleFont.render("Cipher", 10, textColour)
        Title3 = titleFont.render("Simulator", 10, textColour)
        screen.blit(Title1, (100, 100))
        screen.blit(Title2, (100, 200))
        screen.blit(Title3, (100, 300))
    
        #Text for the button
        buttonText = buttonFont.render("Continue", 10, textColour)
        screen.blit(buttonText, (115,405))
    
        #Where the buttons are defined and drawn
        ButtonBig = Button(100, 450, 120, 60, buttonGray1, screen)
        ButtonSmall = Button(105, 455, 110, 50, buttonGray2, screen)
        ButtonBig.drawButton()
        ButtonSmall.drawButton()
    
    
        #Pygame While loop
        clock = pygame.time.Clock()
        while True:
            for event in pygame.event.get():
                if ButtonBig.isPressed():
                    print("Hello")
                    break
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
    
            clock.tick(60)
    

    这是一个例子。或者,您可以使用列表而不是精灵组,并在包含的按钮上进行迭代。谢谢您的帮助。但是,我是否需要在类定义中添加一些内容,以便从这些面向对象的按钮之一调用函数?由于我已经在我的程序中实现了上述代码,现在我似乎无法让它调用函数,它只能打印到解释器中。如果
    按钮被按下(event)
    为True,则可以在事件循环中调用函数。在实际的GUI框架中可以看到,一种更复杂的方法是在实例化过程中将函数(回调函数)传递给按钮,然后在按下
    的方法中调用它。如果你愿意的话,我可以添加一个例子。是的,我做的是尝试调用函数,这是另一个pygame代码位-这将导致喜欢下一个屏幕。我在while循环的event部分调用了它,它返回了一个黑屏,即使在调用的函数中,我填充了屏幕并显示了其他按钮,您现在明白了吗,还是仍然需要帮助?也许最好再贴一个问题,这样我可以看看你最近的代码。
    
    import random
    import pygame as pg
    
    
    pg.init()
    
    screen = pg.display.set_mode((800, 600))
    
    FONT = pg.font.SysFont('garamond', 25)
    SCREEN_GRAY = pg.Color('gray80')
    BUTTON_GRAY = pg.Color('gray40')
    TEXT_COLOUR = pg.Color('navy')
    
    
    # Inherit from pg.sprite.Sprite. That will allow you to
    # put the instances into a sprite group.
    class Button(pg.sprite.Sprite):
    
        def __init__(self, text, x, y, width, height, colour):
            super().__init__()
            # Create the image of the button.
            self.image = pg.Surface((width, height))
            self.image.fill(colour)
            # A pygame.Rect will serve as the blit position.
            self.rect = self.image.get_rect()
            # Render the text.
            txt = FONT.render(text, True, TEXT_COLOUR)
            # This txt_rect is used to center the text on the image.
            txt_rect = txt.get_rect(center=self.rect.center)
            self.image.blit(txt, txt_rect)
            # Set the position of the image rect.
            self.rect.topleft = x, y
    
        def is_pressed(self, event):
            if event.type == pg.MOUSEBUTTONDOWN:
                # MOUSE... events have an event.pos attribute (the mouse position)
                # which you can pass to the collidepoint method of the rect.
                if self.rect.collidepoint(event.pos):
                    return True
            return False
    
    
    def main():
        button_big = Button('big button', 100, 250, 120, 60, BUTTON_GRAY)
        button_small = Button('small button', 105, 455, 120, 50, BUTTON_GRAY)
        buttons_group = pg.sprite.Group(button_big, button_small)
    
        background = pg.Surface(screen.get_size())
        background.fill(SCREEN_GRAY)
    
        for i in range(screen.get_height()//40):
            for j in range(screen.get_width()//40):
                n = random.choice(['0', '1'])
                background.blit(FONT.render(n, True, BUTTON_GRAY), (j*40, i*40))
    
        clock = pg.time.Clock()
        while True:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    return
                if button_big.is_pressed(event):
                    print('big')
                elif button_small.is_pressed(event):
                    print('small')
    
            screen.blit(background, (0, 0))
            # Blit the images of the contained sprites onto the screen.
            buttons_group.draw(screen)
    
            pg.display.flip()
            clock.tick(60)
    
    
    if __name__ == '__main__':
        main()
        pg.quit()