I've been following a tutorial to make my first Pong clone. Here is the tutorial: http://muagames.com/tutorials/pygame-3-pong/pygame-3-pong-step-1/

When I try to run it I get this error message:

Traceback (most recent call last): File "/Users//Documents/Pygame/pong2.py", line 133, in game = Game() File "/Users//Documents/Pygame/pong2.py", line 66, in init self.sprites.add(self.leftpaddle) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/sprite.py", line 319, in add sprite.add_internal(self) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/sprite.py", line 147, in add_internal self.__g[group] = 0 AttributeError: 'Paddle' object has no attribute 'Sprite_g'

I could not figure out why, my code looks pretty much the same as the one in the tutorial.

    # Pong V2 OOP
import pygame, sys, random, os, math
from pygame.locals import *

class Paddle(pygame.sprite.Sprite):
    def __init__(self, xy):
        #initialize the pygame sprite part
        self.image = pygame.image.load(os.path.join('images','pong_paddle.gif'))
        self.rect = self.image.get_rect()

        #set position
        self.rect.centerx, self.rect.centery = xy

        #movement speed of paddle
        self.movementspeed = 5

        #current velocity
        self.velocity = 0

    def up(self):
        self.velocity -= self.movementspeed

    def down(self):
        self.velocity += self.movementspeed

    def move(self, dy):
        if self.rect.bottom + dy > 400:
            self.rect.bottom = 400
        elif self.rect.top + dy < 0:
            self.rect.top = 0
            self.rect.y += dy

    def update(self):

class Game(object):
    def __init__(self):

        self.screen = pygame.display.set_mode((800,400),0,32)

        pygame.display.set_caption('Pong V2')
        self.clock = pygame.time.Clock()

        pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])

        # set background:
        self.background = pygame.Surface((800,400))

        # draw line in middle
        pygame.draw.line(self.background, (0,0,0), (400,0), (400,400), 2)
        self.screen.blit(self.background, (0,0))


        # a sprite rendering group for our ball and paddles
        self.sprites = pygame.sprite.RenderUpdates()

        # create our paddles and add to sprite group
        self.leftpaddle = Paddle((50,200))
        self.rightpaddle = Paddle((750,200))

    def run(self):
        print('Starting Event Loop')

        running = True

        while running:

            running = self.handleEvents()

            #Update title with fps
            pygame.display.set_caption('Pygame Tutorial 2 - Basic   %d fps' % self.clock.get_fps())

            # update sprites
            for sprite in self.sprites:

            # render sprites
            self.sprites.clear(self.screen, self.background)
            dirty = self.sprites.draw(self.screen)

            # blit 

        print('Quitting, thanks for playing!')

    def handleEvents(self):

        # poll for events
        for event in pygame.event.get():
            if event.type == QUIT:
                return False
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return False

                # paddle control
                if event.key == K_UP:
                if event.key == K_DOWN:
                if event.key == K_w:
                if event.key == K_s:
            elif event.type == KEYUP:
                # paddle control
                if event.key == K_w:
                if event.key == K_s:

                if event.key == K_UP:
                if event.key == K_DOWN:

        return True

if __name__ == '__main__':
    game = Game()

Can anyone help me?


Look here: http://www.nongnu.org/pygsear/doc/api_html/private/pygame.sprite.Sprite-class.html

When you subclass Sprite, you must call this pygame.sprite.Sprite.__init__(self) before you add the sprite to any groups, or you will get an error.

That seems to be what is missing in order to make Paddle a proper subclass of Sprite.

  • \$\begingroup\$ I tried adding pygame.sprite.Sprite.__init__(self), but still got an error: line 64, in init pygame.sprite.Sprite.__init__(Paddle) TypeError: unbound method __init__() must be called with Sprite instance as first argument (got type instance instead) \$\endgroup\$ – rcs May 3 '13 at 0:10
  • 1
    \$\begingroup\$ In order to complete the usefulness of this question for others could you please write out exactly what you did to make it work? \$\endgroup\$ – aaaaaaaaaaaa May 3 '13 at 6:13
  • \$\begingroup\$ +1 to @eBusiness - please show us the working code to exapnd on the solution and accept eBusiness' answer if it helped find it. \$\endgroup\$ – Tom 'Blue' Piddock May 3 '13 at 14:20

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.