繁体   English   中英

Pygame文字矩形重叠

[英]Pygame Text Rectangles Overlapping

这是我的代码:

import pygame
import time
pygame.init()

display_width = 800
display_height = 600
window = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Draw')
clock = pygame.time.Clock()
p1Img = pygame.image.load('player.png')
p2Img = pygame.image.load('player2.png')
bullet = pygame.image.load('bullet.png')
id = 1

white = (255,255,255)
black = (0,0,0)






def player(x,y):
    window.blit(p1Img,(p1x,p1y))

def player2(x,y):
    window.blit(p2Img,(p2x,p2y))

def text_objects(text,font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()



def messageDisplay(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text,largeText)
    TextRect.center = (display_width/2, display_height/2)
    window.blit(TextSurf,TextRect)

    pygame.display.update()

def countDown(drawTime):
    while drawTime <= 5:
        time.sleep(1)
        pygame.display.update()
        messageDisplay(str(drawTime))
        drawTime += 1
    else:
        pygame.display.update()
        messageDisplay('Draw')



p1x = -5
p1y = 500
b1x = 56
b1y = 510

p2x = 740
p2y = 500
b2x = 740
b2y = 500

while(id != 0):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            id = 0
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                window.blit(bullet,(b1x, b1y))
                pygame.display.update()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_q:
                window.blit(bullet,(b1x, b1y))


    window.fill(white)
    player(p1x,p1y)
    player2(p2x,p2y)
    pygame.display.update()
    countDown(1)
    clock.tick(120)

pygame.quit()

问题在于文本是在其自身之上绘制的。 我已经阅读了无数有关如何重绘表面的内容,但我不确定它们的含义是什么? 我做了window.fill(white),但似乎没有做到。

您绘制while循环,并且在绘制新文本之前不会清除此位置。 在绘制新文本之前,必须用背景颜色或背景图像填充此位置。 可能您可以使用fill(white, TextRect) )仅填充(清除)部分屏幕。

您使用window.fill(white)但不使用countDownwhile循环,因此在完成计数后它会清除文本(和所有屏幕)。

redraw意味着“清除屏幕并再次绘制所有内容”。 要清除屏幕,请使用fill(white)


我不知道您要尝试做什么-但sleepwhilecountDown )可能不是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM