所以,我制作的游戏选择一个随机数,你必须猜测它是什么,但是,当它说你赢了或者你丢失了文本只是留在那里我可以& #39;弄清楚如何删除旧文本。我试过做screen.blit(背景,(0,0)),但它并没有改变一件事。 (是的,我确实做了pygame.display.flip())我已经浏览了整个网络(我先查看了堆栈溢出),但没有成功。帮助!
import random, pygame, pygbutton
from pygame.locals import *
pygame.init()
dice = random.randint(1, 6)
def window():
width = 600
height = 600
# background_color = (0, 0, 0)
WH = (width, height)
screen = pygame.display.set_mode(WH)
background = pygame.image.load("wood.jpg").convert()
background=pygame.transform.scale(background,(600, 600))
pygame.display.set_caption("Dice Game!")
screen.blit(background, (600, 600))
num1_button = pygbutton.PygButton((0, 0, 100,50), '1')
num2_button = pygbutton.PygButton((0, 50, 100, 50), '2')
# screen.fill(background_color)
font = pygame.font.Font(None, 100)
running = True
while running == True:
dice = random.randint(1, 6)
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit()
if 'click' in num1_button.handleEvent(event):
if dice == 1:
text = font.render("You Win!", 1, (0, 0, 0))
background.blit(text, (155, 255))
screen.blit(background, (0, 0))
else:
Text = font.render("You Lose!", 1, (0, 0, 0))
background.blit(Text, (155, 255))
screen.blit(background, (0, 0))
cliq = True
if 'click' in num2_button.handleEvent(event):
if dice == 2:
text = font.render("You Win!", 1, (0, 0, 0))
background.blit(text, (155, 255))
screen.blit(background, (0, 0))
else:
Text = font.render("You Lose!", 1, (0, 0, 0))
background.blit(Text, (155, 255))
screen.blit(background, (0, 0))
cliq=True
num1_button.draw(screen)
num2_button.draw(screen)
pygame.display.flip()
window()
如果你告诉我把改变放在哪里,我也会喜欢它。谢谢你提前!
答案 0 :(得分:0)
您的问题是您将文本blitting到背景表面。这个文字一直存在,直到你重新加载表面。重新加载表面是一个坏主意。这需要大量的cpu功能,你不需要花费。相反,只需将文本直接插入屏幕即可。
if 'click' in num1_button.handleEvent(event):
if dice == 1:
text = font.render("You Win!", 1, (0, 0, 0))
screen.blit(text, (155, 255))