Aditya Mishra
Aditya Mishra

Reputation: 13

How do I make my object not leave a trail while moving in Pygame?

This is the image Okay so I am not able to figure out how to delete the previous render of the object every frame from screen so as to not leave a trail behind, in other words I want the object to have the appearance of Movement rather than leaving a bunch of red squares behind it. Image has been inserted for understanding what I mean by "leaving a trail" The following is my code:-

import pygame
import random

WIDTH = 1000
ROWS = 25
WIN = pygame.display.set_mode((WIDTH,WIDTH))
pygame.display.set_caption("Particle Physics")
clock = pygame.time.Clock()

RED = (255, 0, 0)
GRAVITY = 2
wid = 5

class Square:
    def __init__(self,x,y,width,color,vel_x,vel_y):
        self.x = x
        self.y = y
        self.width = width
        self.color = color
        self.velx = vel_x
        self.vely = vel_y

    def draw(self):
        pygame.draw.rect(WIN, self.color, (self.x, self.y, self.width, self.width))

    def move(self):
        self.x += self.velx
        self.y += self.vely
        self.vely += GRAVITY[enter image description here][1]    


def particles():
    pass

def main():
    run = True
    a,b = 500,300
    c = random.randint(-20,20)
    d = random.randint(-50,0)
    square = Square(a,b,wid, RED, c, d)

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        square.move()
        square.draw()

        pygame.display.update()
        pygame.time.delay(100)
        #clock.tick(60)

    pygame.quit()

main()

Upvotes: 1

Views: 962

Answers (1)

Siddharth Agrawal
Siddharth Agrawal

Reputation: 3146

In the main function add the set background color at the start so as to delete the previous frames.

def main()
   ...
   while run
     ....
     screen.fill((255,255,255))
     ...

Upvotes: 3

Related Questions