Pygame文本按钮错了

时间:2018-03-28 21:38:38

标签: pygame

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

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

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

BTW要求这是最后的手段。我花了差不多三个小时试图解决这个问题,这对我来说很难盯着屏幕看那么久。



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)




2 个答案:

答案 0 :(得分:0)

这是一个按钮类:

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)

答案 1 :(得分:0)

虽然您的问题仍然有点令人困惑,但我可以告诉您如何在按钮附近或按钮中显示文字。所以你要做的只是将文本的位置放在按钮附近,将文本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.

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