Pygame 使用 screen.blit() 时不显示文本

Pygame Text Not appearing when using screen.blit()

我有一个问题,我希望 "GAME OVER" 在可播放矩形触及边界时出现,但它不会出现。我尝试了几个可以包含 screen.blit() 命令的位置,但其中 none 有效。游戏似乎只是忽略了命令,因为其他一切都运行良好。

import pygame, sys
from pygame.locals import *
from time import*
from random import*
size = (1300, 700)
screen = pygame.display.set_mode(size)
screen.fill(Color("blue"))
pygame.display.set_caption('Game')
pygame.init()
done = True
pygame.font.init()
myfont = pygame.font.SysFont('Arial Black', 30)
textsurface = myfont.render('GAME OVER', False, Color("green"))
x1 = 310
y1 = 650
x2 = 20
y2 = 20


re1 = 200
re2 = 0
re3 = 100
re4 = 700

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

def setter():
    screen.fill(Color("blue"))
    pygame.draw.circle(screen, Color("yellow"), (circlx, circly), 20)
    pygame.draw.rect(screen, Color('red'), (re1, re2, re3, re4))


def write():
    screen.blit(textsurface, (300, 300))
    sleep(0.5)

class Cube():
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = 20
        self.y2 = 20

    def cre(self):
        pygame.draw.rect(screen, Color('green'), (self.x1, self.y1, 
self.x2, self.y2))

    def right(self):
        setter()
        self.x1 += 0.2
        self.cre()

    def left(self):
        setter()
        self.x1 -= 0.2
        self.cre()

    def up(self):
        setter()
        self.y1 -= 0.2
        self.cre()

    def down(self):
        setter()
        self.y1 += 0.2
        self.cre()

    def getx1(self):
        return self.x1

    def getx2(self):
        return self.x1 + self.x2

    def gety1(self):
        return self.y1

    def gety2(self):
        return self.y1 + self.y2



def overlap():
    if ((re1 <= cube.getx1() <= (re1 + re3)) and (re2 <= cube.gety1() <=             
(re2 + re4))) or ((re1 <= cube.getx2() <= (re1 + re3)) and (re2 <= 
cube.gety1() <= (re2 + re4))) or ((re1 <= cube.getx1() <= (re1 + re3)) and 
(re2 <= cube.gety2() <= (re2 + re4))) or ((re1 <= cube.getx2() <= (re1 + 
re3)) and (re2 <= cube.gety2() <= (re2 + re4))):
        write()
#This is where it doesn't work.
        pygame.quit()
        sys.exit()

setter()
cube = Cube(x1, y1, x2, y2)
cube.cre()
while done == True:
    keys = pygame.key.get_pressed()
    if keys[K_RIGHT]:
        overlap()
        cube.right()
    if keys[K_LEFT]:
        overlap()
        cube.left()
    if keys[K_UP]:
        overlap()
        cube.up()
    if keys[K_DOWN]:
        overlap()
        cube.down()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = False


    pygame.display.flip()
    fpsClock.tick(FPS)

pygame.quit()
sys.exit()

函数 setter()overlap() 中调用函数 write() 后立即调用(例如在 cube.right() 中)。由于 setter() 填满了整个 window,文本会立即被覆盖并且永远不会显示。

从整个代码中删除对 setter()write()self.cre() 等绘图操作的所有调用。

例如

class Cube():

    def right(self):
        # setter()   <--- remove
        self.x1 += 0.2
        # self.cre() <--- remove

将函数 overlap 更改为 returns 状态(TrueFalse)的函数:

def overlap():
    return ((re1 <= cube.getx1() <= (re1 + re3)) and (re2 <= cube.gety1() <=             
(re2 + re4))) or ((re1 <= cube.getx2() <= (re1 + re3)) and (re2 <= 
cube.gety1() <= (re2 + re4))) or ((re1 <= cube.getx1() <= (re1 + re3)) and 
(re2 <= cube.gety2() <= (re2 + re4))) or ((re1 <= cube.getx2() <= (re1 + 
re3)) and (re2 <= cube.gety2() <= (re2 + re4)))  

在主循环结束时执行所有绘图操作:

while done == True:
    keys = pygame.key.get_pressed()
    if keys[K_RIGHT]:
        cube.right()
    if keys[K_LEFT]:
        cube.left()
    if keys[K_UP]:
        cube.up()
    if keys[K_DOWN]:
        cube.down()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = False

    isOverlapping = overlap()

    setter()
    cube.cre()
    if isOverlapping:
        write()

    pygame.display.flip()
    fpsClock.tick(FPS)

您需要在 screen.blit(textsurface, (300, 300)) 之后添加 pygame.display.flip() 表示 38. 行。希望对您有所帮助。