2014-01-21 591 views

回答

1

在您的遊戲循環中,在繪製新框架之前,請使用背景顏色填充框架。

實施例:

ball = pygame.Rect(0,0,10,10) 
while True: 
    mainSurface.fill((0,0,0)) 
    pygame.draw.circle(display,(255,255,255),ball.center,5) 
    ball.move_ip(1,1) 
    pygame.display.update() 

的關鍵點是mainSurface.fill這將清除以前的幀。

4

首先,我建議你去PyGame文檔並閱讀一下PyGame。 (Link

但是爲了節省您的時間,您必須在屏幕上繪製新的形狀/文字之前,必須使用功能screen.fill(#Your chosen colour)。這是PyGame中的功能,它可以擺脫舊屏幕,並允許您將新項目繪製到清晰的屏幕上,而不需要在那裏留下透視圖。

實施例:

import pygame 
import sys 
from pygame.locals import * 

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

class Pane(object): 
    def __init__(self): 
     pygame.init() 
     self.font = pygame.font.SysFont('Arial', 25) 
     pygame.display.set_caption('Box Test') 
     self.screen = pygame.display.set_mode((600,400), 0, 32) 
     self.screen.fill((white)) 
     pygame.display.update() 


    def addRect(self): 
     self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2) 
     pygame.display.update() 

    def addText(self): 
     self.screen.blit(self.font.render('Hello!', True, black), (200, 100)) 
     pygame.display.update() 

    def addText2(self): 
     self.screen.blit(self.font.render('Hello!', True, red), (200, 100)) 
     pygame.display.update() 


    def functionApp(self): 
     if __name__ == '__main__': 
      self.addRect() 
      self.addText() 
      while True: 
       for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
         pygame.quit(); sys.exit(); 

        if event.type == pygame.KEYDOWN: 
         if event.key == pygame.K_ESCAPE: 
          self.screen.fill(white) 
          self.addRect() 
          self.addText2() #i made it so it only changes colour once. 



display = Pane() 
display.functionApp()