2
\$\begingroup\$

I am trying to show the motion of some basketballs towards walle using the pygae sprites. The idea is that I don't know how to remove the old ones so they won't show. Is there a move method so that it would know it has to draw it again at the specified location? I created a sprite class. Then I have a Character class that uses the sprite class and threads to calculate the intermediary coordinates for them. But I don't want them to show on the screen forever, like the pictures show. Thanks!

 import pygame

 class CharacterSprite(pygame.sprite.Sprite):

   def __init__(self, location, image, surface):
       pygame.sprite.Sprite.__init__(self)
       self.image = pygame.image.load(image).convert_alpha()
       self.rect = self.image.get_rect()
       self.rect.center = location
       self.surface = surface

    def draw(self):
       self.surface.blit(self.image, self.rect)

   def update(self, location):
      self.rect.center = location
      self.draw()


import threading
import pygame
import os
from random import randint
from Sprites.CharacterSprite import CharacterSprite

LOCK = threading.Lock()


class StudentCharacter(threading.Thread):
   NRSTEPS = 3
   YPOS = 440
   TEACHERPOS = 40
   IMAGE_PATH = os.path.join('images', 'ball.png')

def __init__(self, screen, xpos, interval):
    threading.Thread.__init__(self)
    self.screen, self.xpos, self.ypos, self.interval = screen, xpos, self.YPOS, interval
    self.draw(self.xpos, self.ypos)
    self.configure()


def draw(self, x, y):
    self.circle = CharacterSprite([x, y], self.IMAGE_PATH, self.screen)
    self.circle.draw()


def configure(self):
    (left, right) = self.interval
    self.target = randint(left, right)
    xdif = self.target - self.xpos
    ydif = self.TEACHERPOS - self.ypos
    self.normal = {'x': int(xdif / self.NRSTEPS), 'y': int(ydif / self.NRSTEPS)}
    print self.target, self.normal['x'], self.normal['y']


def update(self):
    self.circle.update([self.xpos, self.ypos])

def run(self):
    i = 0
    while i < self.NRSTEPS:
        LOCK.acquire()
        pygame.time.wait(100)
        self.xpos += self.normal['x']
        self.ypos += self.normal['y']
        self.update()
        print self.xpos, self.ypos
        pygame.display.update()
        i += 1
        LOCK.release()
    print "Done"

enter image description here

enter image description here

\$\endgroup\$
  • \$\begingroup\$ You need to redraw the background each frame, otherwise everything that you have drawn on the screen stays there. \$\endgroup\$ – Chachmu Oct 12 '14 at 18:14
  • \$\begingroup\$ So there's no easier and elegant way? This is so annoying ... I heard about this but thought that the update method will do this for me somehow ... Is there a way let's say to redraw only the old basketball with the background color and not the whole background? \$\endgroup\$ – user3009269 Oct 12 '14 at 18:19
  • \$\begingroup\$ Yes, thats called dirty blitting and Newbie Guide to pygame touches on this. The steps they say to follow to implement it are: Blit a piece of the background over the sprite’s current location, erasing it. Append the sprite’s current location rectangle to a list called dirty_rects. Move the sprite. Draw the sprite at it’s new location. Append the sprite’s new location to my dirty_rects list. Call display.update(dirty_rects) You can read more about it here: pygame.org/docs/tut/newbieguide.html \$\endgroup\$ – Chachmu Oct 12 '14 at 18:21
  • \$\begingroup\$ Thanks! It worked! Maybe you could write it as an answer so that I could mark it as correct... \$\endgroup\$ – user3009269 Oct 12 '14 at 18:41
3
\$\begingroup\$

Yes, thats called dirty blitting. The Newbie Guide to pygame touches on this.

The steps they recommend to implement it are:

  1. Blit a piece of the background over the sprite’s current location, erasing it.
  2. Append the sprite’s current location rectangle to a list called dirty_rects.
  3. Move the sprite. Draw the sprite at it’s new location.
  4. Append the sprite’s new location to my dirty_rects list. Call display.update(dirty_rects)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.