2015-12-11 104 views
0

因此,我正在製作一個隨機數的遊戲,你必須猜測它是什麼,但是,當它說你贏了,或者你失去了文本就停留在那裏,我不能弄清楚如何刪除舊文本。我試圖做screen.blit(背景,(0,0)),但它不會改變一件事。 (是的,我做了pygame.display.flip())我看遍了整個網絡(我首先檢查了堆棧溢出),但沒有成功。幫幫我!Pygame文本重疊

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

你的問題是,你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))