Software Development and Development Major: Pygame Smudge Trails

Firstly, I searched on the Internet and this site for solutions, and those that I tried do not work, so I decided to publish my individual question and code. This program was created using Python 3.2.2 and the corresponding compatible version of pygame. I also understand that a more efficient method would be to use sprites, sprite groups and a "dirty fix", but I could not convert the program, so I will continue without the additional benefits of such functions.

Problem. Traces of the tracks where the "asteroids" move remain. Hypothesis: the background is superimposed on the screen, but asteroids are scattered around the background.

Please answer - btw i'm a highschooler from AUS: D

import pygame
import random
import math
pygame.init()

height = 550
width = 750
screen = pygame.display.set_mode((width, height))
background = pygame.image.load("Planet.jpg")
Clock = pygame.time.Clock()


class asteroid(pygame.sprite.Sprite):
    def __init__(self, x, y, size):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.size = 15
        self.speed = 0.0
        self.angle = 0
        self.colour = (171, 130, 255)
        self.thickness = 0

def display(self):
     pygame.draw.circle(background, self.colour, (int(self.x),int(self.y)), self.size, self.thickness)

     pygame.draw.circle(background, (255, 255, 255), (int(self.x),int(self.y)), self.size, 1)

def move(self):
    self.x += math.sin(self.angle) * self.speed
    self.y -= math.cos(self.angle) * self.speed

def boundaries(self):
    if self.x > width - self.size:
        self.x = 0 + self.size
    elif self.x < self.size:
        self.x = width - self.size
    if self.y > height - self.size:
        self.y = 0 + self.size
    elif self.y <self.size:
        self.y = height - self.size




num_target = 5
my_particles = []
num_particles = len(my_particles)
while num_particles < 5:
    for n in range(num_target):
        size = 20
        x = random.randint(size, height - size)
        y = random.randint(size, width - size)
        target = asteroid(x, y, size)
        target.speed = random.uniform(1.0, 1.0)
        target.angle = random.uniform(0, math.pi*2)
        my_particles.append(target)
        num_particles = num_particles + 1


def main():
    pygame.display.set_caption("Anyu Game")
    screen.blit(background, (0,0))
    pygame.display.update()
    score = (pygame.time.get_ticks()/1000)
    print (score)


while True:
    pygame.display.update()
    screen.blit(background, (0,0))
    MouseP = pygame.mouse.get_pos()
    frames = Clock.get_fps
    pygame.mouse.set_visible
    score = (pygame.time.get_ticks()/1000)
    print (score)
    print (MouseP)
    for target in my_particles:
        target.move()
        target.boundaries()
        target.display()
        pygame.display.update()


    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit();

if __name__=='__main__':
    main()
+5
1

, ! , , , . /.

background screen draw. .

Sprite . , . :)

:

:

#Create a new `pygame.Surface`, and draw a circle on it, then set transparency:
circle = pygame.Surface((30,30))
circle = circle.convert()
pygame.draw.circle(circle, (171, 130, 255), (int(15),int(15)), 15, 0)
circle.set_colorkey(circle.get_at((0, 0)), pygame.RLEACCEL)

asteroid, __init__:

#Sets the asteroid image, and then the asteroids co-ords (these are in `rect`)
        self.image = circle
        self.rect = self.image.get_rect()

def move(self):

        self.rect[0] = self.x
        self.rect[1] = self.y

:

my_particles = []

#This is a special pygame container class, it has a draw() method that tracks changed areas of the screen.
my_particles = pygame.sprite.RenderUpdates()

:

my_particles.append(target)

my_particles.add(target)

:

while True:
    pygame.display.update()
    screen.blit(background, (0,0))
    MouseP = pygame.mouse.get_pos()
    frames = Clock.get_fps
    pygame.mouse.set_visible
    score = (pygame.time.get_ticks()/1000)
    print (score)
    print (MouseP)
    for target in my_particles:
        target.move()
        target.boundaries()
        target.display()
        pygame.display.update()

#initial screen draw:
screen.blit(background, (0,0))
pygame.display.update()
while True:
    #remove previous drawn sprites and replaces with background:
    my_particles.clear(screen, background)
    MouseP = pygame.mouse.get_pos()
    frames = Clock.get_fps
    pygame.mouse.set_visible
    score = (pygame.time.get_ticks()/1000)
    print (score)
    print (MouseP)
    for target in my_particles:
        target.move()
        target.boundaries()
    #draws changed sprites to the screen:
    pygame.display.update(my_particles.draw(screen))

display, .

, , , , , - !

, :)

+4

All Articles