问:我尝试在 PyGame window 中显示的每条文本都有效,但在 Window 中没有显示一个文本

Q: Every text I have tried to show in the PyGame window is working but one text is not showing in the Window

好的,所以我正在尝试用 PyGame 构建剪刀石头布。我在屏幕上显示了很多文本,除了一个之外,它们都有效。 这是我的代码:

import pygame, random

pygame.init()

fps = 60
screen_width = 1200
screen_height = 500
screen = pygame.display.set_mode((screen_width, screen_height))
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
grey = (128, 128, 128)
font = pygame.font.SysFont('gabriola', 40)
pygame.display.set_caption('Rock, Paper or Scissors?')

class Button:
    def __init__(self, text, width, height, pos, elevation):
        # Core attributes
        self.pressed = False
        self.elevation = elevation
        self.dynamic_elecation = elevation
        self.original_y_pos = pos[1]

        # top rectangle
        self.top_rect = pygame.Rect(pos, (width, height))
        self.top_color = green

        # bottom rectangle
        self.bottom_rect = pygame.Rect(pos, (width, height))
        self.bottom_color = green
        # text
        self.text_surf = font.render(text, True, "#FFFFFF")
        self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)

    def draw(self):
        # elevation logic
        self.top_rect.y = self.original_y_pos - self.dynamic_elecation
        self.text_rect.center = self.top_rect.center

        self.bottom_rect.midtop = self.top_rect.midtop
        self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation

        pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
        pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
        screen.blit(self.text_surf, self.text_rect)
        self.check_click()

    def check_click(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.top_rect.collidepoint(mouse_pos):
            self.top_color = red
            if pygame.mouse.get_pressed()[0]:
                self.dynamic_elecation = 0
                self.pressed = True
            else:
                self.dynamic_elecation = self.elevation
                if self.pressed == True:
                    Rock()
        else:
            self.dynamic_elecation = self.elevation
            self.top_color = "#475F77"

class Button2:
    def __init__(self, text, width, height, pos, elevation):
        # Core attributes
        self.pressed = False
        self.elevation = elevation
        self.dynamic_elecation = elevation
        self.original_y_pos = pos[1]

        # top rectangle
        self.top_rect = pygame.Rect(pos, (width, height))
        self.top_color = green

        # bottom rectangle
        self.bottom_rect = pygame.Rect(pos, (width, height))
        self.bottom_color = green
        # text
        self.text_surf = font.render(text, True, "#FFFFFF")
        self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)

    def draw(self):
        # elevation logic
        self.top_rect.y = self.original_y_pos - self.dynamic_elecation
        self.text_rect.center = self.top_rect.center

        self.bottom_rect.midtop = self.top_rect.midtop
        self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation

        pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
        pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
        screen.blit(self.text_surf, self.text_rect)
        self.check_click()

    def check_click(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.top_rect.collidepoint(mouse_pos):
            self.top_color = red
            if pygame.mouse.get_pressed()[0]:
                self.dynamic_elecation = 0
                self.pressed = True
            else:
                self.dynamic_elecation = self.elevation
                if self.pressed == True:
                    GameLoop()
        else:
            self.dynamic_elecation = self.elevation
            self.top_color = "#475F77"
class Button_Rock:
    def __init__(self, text, width, height, pos, elevation):
        # Core attributes
        self.pressed = False
        self.elevation = elevation
        self.dynamic_elecation = elevation
        self.original_y_pos = pos[1]

        # top rectangle
        self.top_rect = pygame.Rect(pos, (width, height))
        self.top_color = green

        # bottom rectangle
        self.bottom_rect = pygame.Rect(pos, (width, height))
        self.bottom_color = green
        # text
        self.text_surf = font.render(text, True, "#FFFFFF")
        self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)

    def draw(self):
        # elevation logic
        self.top_rect.y = self.original_y_pos - self.dynamic_elecation
        self.text_rect.center = self.top_rect.center

        self.bottom_rect.midtop = self.top_rect.midtop
        self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation

        pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
        pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
        screen.blit(self.text_surf, self.text_rect)
        self.check_click()

    def check_click(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.top_rect.collidepoint(mouse_pos):
            self.top_color = red
            if pygame.mouse.get_pressed()[0]:
                self.dynamic_elecation = 0
                self.pressed = True
            else:
                self.dynamic_elecation = self.elevation
                if self.pressed == True:
                    Rock()
        else:
            self.dynamic_elecation = self.elevation
            self.top_color = "#475F77"
class Button_Paper:
    def __init__(self, text, width, height, pos, elevation):
        # Core attributes
        self.pressed = False
        self.elevation = elevation
        self.dynamic_elecation = elevation
        self.original_y_pos = pos[1]

        # top rectangle
        self.top_rect = pygame.Rect(pos, (width, height))
        self.top_color = green

        # bottom rectangle
        self.bottom_rect = pygame.Rect(pos, (width, height))
        self.bottom_color = green
        # text
        self.text_surf = font.render(text, True, "#FFFFFF")
        self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)

    def draw(self):
        # elevation logic
        self.top_rect.y = self.original_y_pos - self.dynamic_elecation
        self.text_rect.center = self.top_rect.center

        self.bottom_rect.midtop = self.top_rect.midtop
        self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation

        pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
        pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
        screen.blit(self.text_surf, self.text_rect)
        self.check_click()

    def check_click(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.top_rect.collidepoint(mouse_pos):
            self.top_color = red
            if pygame.mouse.get_pressed()[0]:
                self.dynamic_elecation = 0
                self.pressed = True
            else:
                self.dynamic_elecation = self.elevation
                if self.pressed == True:
                    Paper()
        else:
            self.dynamic_elecation = self.elevation
            self.top_color = "#475F77"

class Button_Scissors:
    def __init__(self, text, width, height, pos, elevation):
        # Core attributes
        self.pressed = False
        self.elevation = elevation
        self.dynamic_elecation = elevation
        self.original_y_pos = pos[1]

        # top rectangle
        self.top_rect = pygame.Rect(pos, (width, height))
        self.top_color = green

        # bottom rectangle
        self.bottom_rect = pygame.Rect(pos, (width, height))
        self.bottom_color = green
        # text
        self.text_surf = font.render(text, True, "#FFFFFF")
        self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)

    def draw(self):
        # elevation logic
        self.top_rect.y = self.original_y_pos - self.dynamic_elecation
        self.text_rect.center = self.top_rect.center

        self.bottom_rect.midtop = self.top_rect.midtop
        self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation

        pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=12)
        pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=12)
        screen.blit(self.text_surf, self.text_rect)
        self.check_click()

    def check_click(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.top_rect.collidepoint(mouse_pos):
            self.top_color = red
            if pygame.mouse.get_pressed()[0]:
                self.dynamic_elecation = 0
                self.pressed = True
            else:
                self.dynamic_elecation = self.elevation
                if self.pressed == True:
                    Scissors()
        else:
            self.dynamic_elecation = self.elevation
            self.top_color = "#475F77"

def Text(text:str, color:tuple, x:int, y:int):
    screenText = font.render(text, True, color)
    screen.blit(screenText, [x, y])

def WelcomeScreen(run=False):
    screen.fill(grey)
    Text('Welcome to the game! Press the Below Button to Continue.', green, 250, 150)
    welcomeButton = Button2('Continue', 150, 40, (450, 250), 5)
    while not run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = True
        welcomeButton.draw()
        pygame.display.update()

def GameLoop(run=False):
    button1 = Button('Click here!', 150, 40, (30, 100), 5)
    button2 = Button_Paper('Click here!', 150, 40, (520, 100), 5)
    button3 = Button_Scissors('Click here!', 150, 40, (980, 100), 5)
    screen.fill(grey)
    Text('Rock, Paper or Scissors?', blue, 450, 0)
    Text('Rock', grey, 250, 150)
    Text('Paper', green, 550, 50)
    Text('Scissors', red, 1000, 50)
    while not run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = True
        button1.draw()
        button2.draw()
        button3.draw()
        pygame.display.update()

def Rock(run=False):
    screen.fill(white)
    button1 = Button('Click here!', 150, 40, (30, 100), 5)
    button2 = Button('Click here!', 150, 40, (520, 100), 5)
    button3 = Button('Click here!', 150, 40, (980, 100), 5)

    while not run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = True
        pygame.display.update()

def Paper(run=False):
    pass

def Scissors(run=False):
    pass

if __name__ == "__main__":
    WelcomeScreen()

在此代码中,名为 GameLoop 的函数中的文本 'Rock' 没有显示在屏幕上,但其他所有文本都显示在 window 中。

Text('Rock', grey, 250, 150)

有人可以帮忙吗?

文字“摇滚”和背景都是一样的灰色,所以看不到文字。尝试将其更改为不同的颜色,例如白色:

def GameLoop(run=False):
    button1 = Button('Click here!', 150, 40, (30, 100), 5)
    button2 = Button_Paper('Click here!', 150, 40, (520, 100), 5)
    button3 = Button_Scissors('Click here!', 150, 40, (980, 100), 5)
    screen.fill(grey) # Same color as rock
    Text('Rock, Paper or Scissors?', blue, 450, 0)
    Text('Rock', white, 100, 50) # Change color to white and position to match other text
    Text('Paper', green, 550, 50)
    Text('Scissors', red, 1000, 50)
    ...