为什么圆圈在被绘制后不会消失?

时间:2014-09-22 17:35:43

标签: python pygame

当我运行此代码时,它只是创建一个圆圈而不是一个圆圈移动。代码如下:

import pygame, sys, random
from pygame.locals import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
WHITE = (255, 255, 255)

circX = 250 
circY= 250
diffX= 0
diffY=0

while True:
 for event in pygame.event.get():
  if event.type == QUIT:
   pygame.quit()
   sys.exit()
 diffX += random.randint(-1,1)
 diffY += random.randint(-1,1)
 circX += diffX
 circY += diffY
 circLocate = (circX,circY)
 pygame.draw.circle(windowSurface, WHITE, circLocate, 10, 0)
 pygame.display.flip()

2 个答案:

答案 0 :(得分:0)

每次要移动圆圈时,此添加应清除屏幕

while True:
 for event in pygame.event.get():
  if event.type == QUIT:
   pygame.quit()
   sys.exit()
 windowSurface.fill(WHITE) //add this line to clear the screen
 diffX += random.randint(-1,1)
 diffY += random.randint(-1,1)
 circX += diffX
 circY += diffY
 circLocate = (circX,circY)
 pygame.draw.circle(windowSurface, WHITE, circLocate, 10, 0)
 pygame.display.flip()

答案 1 :(得分:0)

您必须清除屏幕,使其看起来像物体在移动。

windowSurface.fill(WHITE)

因此:

import pygame, sys, random
from pygame.locals import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
WHITE = (255, 255, 255)

circX = 250 
circY= 250
diffX= 0
diffY=0

while True:
 for event in pygame.event.get():
  if event.type == QUIT:
   pygame.quit()
   sys.exit()
 diffX += random.randint(-1,1)
 diffY += random.randint(-1,1)
 circX += diffX
 circY += diffY
 circLocate = (circX,circY)
 windowSurface.fill(WHITE)
 pygame.draw.circle(windowSurface, WHITE, circLocate, 10, 0)
 pygame.display.flip()

但是,请确保windowSurface.fill() 之前<{strong>} <{strong>},否则,它只会显示白屏。