【问题标题】:Pygame Text Button Gone wrongPygame文本按钮出错了
【发布时间】:2018-03-28 21:38:37
【问题描述】:

所以我在 Pygame 中制作了一个 rpg 项目,我需要一个包含文本的按钮类。到目前为止,这是我的代码。我试图在网上和这个网站上使用一些代码示例,但我无法让它们以我想要的方式工作。 ;-;

我想要的是一个可以绘制到包含文本的 GameWindow 的按钮。稍后我会弄清楚事件处理。

如果有人能给我解释一下使用文本的按钮类如何在 pygame 中工作,并以我可以在我的按钮类中实现的方式进行解释,我将不胜感激。以前,我尝试通过将宽度和高度除以 2 并将彩色矩形与文本相邻放置以尝试标记矩形,以便我可以将它们用作按钮,从而将文本简单地放置在屏幕的中心。但是我意识到这不是一个实用的解决方案,因为我在整个游戏过程中需要很多按钮,而且这种方法占据了我屏幕的大部分。 我不明白如何使用类将消息粘贴到矩形上。下面的 Button 类是我尝试将文本放在矩形顶部的地方,但我发现这很难。 理想情况下,我的目标是能够调用我可以用作按钮的按钮类的实例。

顺便说一句,在这里询问是最后的手段。我花了将近三个小时试图弄清楚这一点,长时间盯着屏幕对我不利。

import pygame, random, sys, math, time
from pygame.locals import *

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

GameWindow = pygame.display.set_mode((650,520))

#Variables
Blue = (0,0,255)
Green = (0,255,0)
Red = (255,0,0)
White = (255,255,255)
Black = (0,0,0)

def Button():
    def__init__(self, surface, x, y, width, height, colour, message, action=None)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.font = pygame.font.Font(None, 20)
    self.message = message
    
                
background_image = pygame.image.load('map.JPG')
title_image = pygame.image.load('title.PNG')

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        GameWindow.blit(background_image, [0,0])
        GameWindow.blit(title_image, [100,0])
        pygame.display.flip()
        fpsClock.tick(FPS)

【问题讨论】:

  • 请准确告诉我们您想要什么。告诉我们“帮我完成我的 Button 课程”是没有帮助的。另外,请告诉我们您的尝试以及出了什么问题,或者您对什么感到困惑。谢谢。
  • 好点哈哈。我的问题现在好了吗? :)
  • 看看examples here,尤其是第二个面向对象的示例。或者您可以使用其中一个 GUI 库,例如 SGC

标签: pygame


【解决方案1】:

这是一个按钮类:

class Button(object):
    global screen_width,screen_height,screen
    def __init__(self,x,y,width,height,text_color,background_color,text):
        self.rect=pygame.Rect(x,y,width,height)
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.text=text
        self.text_color=text_color
        self.background_color=background_color
        self.angle=0

    def check(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def draw(self):
        pygame.draw.rect(screen, self.background_color,(self.rect),0)
        drawTextcenter(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)  
        pygame.draw.rect(screen,self.text_color,self.rect,3)

使用检查功能查看您的按钮是否被点击,并使用绘图功能来绘制您的按钮。

在你的主循环中实现:

button=Button(x,y,width,height,text_color,background_color,text)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type==pygame.MOUSEBUTTONDOWN:
            if button.check()==True:       
                #do what you want to do when button is pressed  
    GameWindow.blit(background_image, [0,0])
    GameWindow.blit(title_image, [100,0])
    pygame.display.flip()
    fpsClock.tick(FPS)

我也推荐使用这些函数来绘制文本:

def drawTextcenter(text,font,screen,x,y,color):
    textobj=font.render(text,True,color)
    textrect=textobj.get_rect(center=(x,y))
    screen.blit(textobj,textrect)

def drawText(text, font, surface, x, y,color):
    textobj=font.render(text, 1, color)
    textrect=textobj.get_rect()
    textrect.topleft=(x, y)
    surface.blit(textobj, textrect)

【讨论】:

    【解决方案2】:

    虽然您的问题仍然有点令人困惑,但我可以告诉您,您是如何在按钮附近或按钮中添加文本的。因此,您只需将文本的位置放在按钮附近,将文本 x 和 y 变量基于按钮 x 和 y 变量。

    从您的代码中复制:

    def Button():
        def__init__(self, surface, x, y, width, height, colour, message, action=None)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.font = pygame.font.Font(None, 20)
        self.message = message           
        self.font = pygame.font.SysFont('Comic Sans MS', 30) #Example Font
    
        def draw_button(self):
             pygame.draw.rect(GameWindow, Red, (self.x, self.y, self.width, self.height))
             self.text = myfont.render(message, False, (0, 0, 0))
             GameWindow.blit(self.text, (self.x + self.width/2, self.y + self.height/2)) #Displays text at coordinates at middle of the button.
    

    这会绘制按钮(它仍然不执行任何操作),但也会显示按钮中的文本。 但是,由于文本显示在其所在表面的左上角,它不会正好在中间,而且看起来很奇怪。如果需要,您可以修改确切的位置。 我希望这能回答你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2012-11-20
      • 2013-09-27
      • 1970-01-01
      相关资源
      最近更新 更多