1
votes

I'm making a pygame program to animate a sprite, but the problem isn't a particular error:

import sys
import os
import pygame
from pygame.locals import *

pygame.init ()

WHITE = ( 255, 255, 255 )
BLACK = (   0,   0,   0 )

Surface = pygame.display.set_mode ((400, 400), 0, 32)
pygame.display.set_caption ('Animation Test')




class Processes (object):
    @staticmethod
    def load_image (imagefile):
        image = pygame.image.load (imagefile)   #Remember to call imagefile as a string statement

        return image, image.get_rect ()



class Sprite (pygame.sprite.Sprite):

    def __init__ (self):
        pygame.sprite.Sprite.__init__ (self)

        self.image, self.rect = Processes.load_image ('frame1.jpg')

        self.blindrect = pygame.Rect ((self.rect.top - 289, self.rect.left - 289), (self.rect.width, self.rect.height))




   def animation (self, key_event = None, mouse_event = None):

       blindpass = 0

       if key_event == K_LEFT:
       self.rect.centerx = self.rect.centerx - 10

       if key_event == K_RIGHT:
        self.rect.centerx = self.rect.centerx + 10

       if key_event == K_UP:
       self.rect.centery = self.rect.centery - 10

       if key_event == K_DOWN:
       self.rect.centery = self.rect.centery + 10


       if mouse_event == MOUSEBUTTONDOWN:
        self.rect.centerx, self.rect.centery = pygame.mouse.get_pos ()


    if blindpass > 0:

        if key_event == K_LEFT:
            self.blindrect.centerx = self.blindrect.centerx - 10

        if key_event == K_RIGHT:
            self.blindrect.centerx = self.blindrect.centerx + 10

        if key_event == K_UP:
            self.blindrect.centery = self.blindrect.centery - 10

        if key_event == K_DOWN:
            self.blindrect.centery = self.blindrect.centery + 10


        if mouse_event == MOUSEBUTTONDOWN:
            self.rect.centerx, self.rect.centery = pygame.mouse.get_pos () 




mecha = Sprite ()
allsprites = pygame.sprite.RenderPlain ((mecha, ))



while True:
Surface.fill (WHITE)

for event in pygame.event.get ():
    if event.type == KEYDOWN:
    mecha.animation (key_event = event.key)

    if event.type == MOUSEBUTTONDOWN:
    mecha.animation (mouse_event = event.type)

    if event.type == QUIT:
    pygame.quit ()
    sys.exit (0)



allsprites.draw (Surface)
pygame.display.update ((mecha.rect, mecha.blindrect))

Now, the error is a bit weird. The output is that the sprite does appear on the screen, and it moves when I provide key and mouse input, but the problem is is that it's leaving trails. Which is why I created a rectangle to trail the sprite rectangle which is supposed to be filled with white every time the game loop loops. But it doesn't. At least, not until I minimize the window, and pull it up again, all the trails disappear and the background becomes white instead of black. But when I move the image, the trails start forming again. I minimize, they disappear as supposed to.

2

2 Answers

0
votes

If your render function does:

screen.fill(white)
# blit or sprite group drawing
pygame.display.flip()

It will clear the entire screen in white, draw the sprites once. There won't be a trail.

  • Your code didn't pasted correctly. fLike your if QUIT statement has no indention, for a multi statement. The same with class Sprite
  • You might get naming problems if you use Surface or Sprite as your class/variable names.
0
votes
  1. You're not initializing and updating the blindrect correctly. That's why the correct portion of the screen is not being updated and your sprite is leaving a trail.
  2. In your update loop, since you're only updating the portion of the display that changed, you need to update the entire screen once to initialize it before you go into the update loop. That's why the screen is not initially white.

See the updated source below (only the Sprite class and later was modified):

class Sprite (pygame.sprite.Sprite):
    def __init__ (self):
        pygame.sprite.Sprite.__init__ (self)
        self.image, self.rect = Processes.load_image ('frame1.jpg')
        #initialize blindrect to be the same rectangle as rect
        self.blindrect = pygame.Rect ((self.rect.top, self.rect.left), (self.rect.width, self.rect.height))

    def animation (self, key_event = None, mouse_event = None):
        print(self.rect)
        print(self.blindrect)

        #save the current sprite position before moving the sprite
        self.blindrect.centerx = self.rect.centerx
        self.blindrect.centery = self.rect.centery

        if key_event == K_LEFT:
            self.rect.centerx = self.rect.centerx - 10

        if key_event == K_RIGHT:
            self.rect.centerx = self.rect.centerx + 10

        if key_event == K_UP:
            self.rect.centery = self.rect.centery - 10

        if key_event == K_DOWN:
            self.rect.centery = self.rect.centery + 10

        if mouse_event == MOUSEBUTTONDOWN:
            self.rect.centerx, self.rect.centery = pygame.mouse.get_pos ()

        print(self.rect)
        print(self.blindrect)

mecha = Sprite ()
allsprites = pygame.sprite.RenderPlain ((mecha, ))

#need to initially fill the entire surface with white
Surface.fill (WHITE)
pygame.display.update ((pygame.Rect((0,0),(400,400))))

while True:
    Surface.fill (WHITE)

    for event in pygame.event.get ():
        if event.type == KEYDOWN:
            mecha.animation (key_event = event.key)

        if event.type == MOUSEBUTTONDOWN:
            mecha.animation (mouse_event = event.type)

        if event.type == QUIT:
            pygame.quit ()
            sys.exit (0)

    allsprites.draw (Surface)
    #update only the previous sprite position and new sprite position
    pygame.display.update ((mecha.rect, mecha.blindrect))