如何输出文本到屏幕上,同时也输出面向对象的按钮Pygam

2024-09-29 17:44:18 发布

您现在位置:Python中文网/ 问答频道 /正文

#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)
< >上面的代码运行一个窗口,它有0和1的随机顺序作为背景,中间的标题是我的程序。你知道吗

然后,底部应该有一个按钮,或者一个灰色的矩形。然而,当我试图.blit,或打印,一些东西到按钮框,当我把按钮画到屏幕上,它不会出现。你知道吗

有人能告诉我为什么和怎么修吗? 或任何其他方法如何将文本输出到按钮上。你知道吗

提前谢谢


Tags: selfforifhererenderwidthscreenpygame
1条回答
网友
1楼 · 发布于 2024-09-29 17:44:18

我首先创建一个单独的background曲面,并将所有永远不会改变的背景图形blit到它上面(下面示例中的数字)。在主循环中,你只需点击这个背景来清除屏幕。你知道吗

要将文本blit到按钮上,请执行以下操作:

  1. 将文本传递给__init__方法
  2. 创建按钮图像(a)pygame.表面)你知道吗
  3. 呈现文本
  4. 为图像和文本创建矩形(将图像矩形的中心传递给文本矩形以使文本居中)
  5. 将文本表面blit到图像上。你知道吗

对于按钮,我将使用sprites并将它们放入一个sprite组,这样您就可以通过调用sprite_group.draw(screen)来简单地绘制它们。精灵需要^{}^{}属性才能工作。Pygame rect有冲突检测方法,您应该使用这些方法,而不是编写自己的方法。在本例中,我们可以使用collidepoint方法并将event.pos传递给它。你知道吗

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

相关问题 更多 >

    热门问题