mellowmelody355
mellowmelody355

Reputation: 13

Problem with overlapping (like afterimage!) pygame sprites

Pygame sprites overlap and look like afterimages Since Pygame operates like an animation in the while statement, I thought the ball would fall normally, but for some reason, an afterimage remained. like this image:afterimages

I typed this code(58 lines), The code consists of three main parts: setting part, class part, and while part. In the setting part, set the game screen, In the class part, I made a physics engine for green circle, and in the while statement part, I drew sprites on the screen.

import pygame as pg
import numpy as np

pg.init()
# screen = X_X
screen = pg.display.set_mode((400, 400))
pg.display.set_caption('movingcircle')
# width, height = oMo
width, height = pg.display.get_surface().get_size()
# running = P~~
running = True
# clock = X_X
clock = pg.time.Clock()
# wind, gravity = X_X
wind = np.array([1, 0])
gravity = np.array([0, 1])

class Circle:
    def __init__(self):
        #self.pos,vel = ~II
        self.surf = pg.image.load('movingcircle\circle.png').convert_alpha()
        self.pos = np.array([width / 2, height / 2])
        self.vel = np.array([0, 0])
        self.acc = np.array([0, 0])
    def applyForce(self, force):
        if force == 'wind':
            self.acc += wind
        if force == 'gravity':
            self.acc += gravity
    def calculate(self):
        self.vel += self.acc
        self.pos += self.vel
        self.rect = self.surf.get_rect(midbottom = (self.pos[0], self.pos[1]))

    #def show(self):
        #screen.blit(self.surf, (self.rect.x, self.rect.y))

circle = Circle()

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
    circle.applyForce(force = 'gravity')
    circle.calculate()
    #circle.show()
    print(circle.rect)
    screen.blit(circle.surf, circle.rect)
    pg.display.update()
    clock.tick(60)

Upvotes: 1

Views: 42

Answers (1)

Rabbid76
Rabbid76

Reputation: 211268

You8 need to clear the display in every frame:

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
    circle.applyForce(force = 'gravity')
    circle.calculate()
    #circle.show()
    print(circle.rect)

    screen.fill("black")                    # <---
    screen.blit(circle.surf, circle.rect)
    pg.display.update()
    clock.tick(60)

Upvotes: 1

Related Questions