user3009695
user3009695

Reputation: 3

Why do the circles not disappear after being drawn?

When I run this code it just make a trail of circles instead of one circle moving. The code is as follows:

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()

Upvotes: 0

Views: 289

Answers (2)

Michael
Michael

Reputation: 719

This addition should clear your screen each time you want the circle to move

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()

Upvotes: 0

A.J. Uppal
A.J. Uppal

Reputation: 19284

You have to clear the screen so it appears like the object is moving.

windowSurface.fill(WHITE)

As such:

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()

However, make sure that the windowSurface.fill() is before the pygame.draw.circle(), else, it will only show a white screen.

Upvotes: 1

Related Questions