0
\$\begingroup\$

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
        else:
            self.rect.y += dy

    def update(self):
        self.move(self.velocity)


class Game(object):
    def __init__(self):
        pygame.init()

        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))
        self.background.fill((255,255,255))

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

        pygame.display.flip()

        # 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.sprites.add(self.leftpaddle)
        self.rightpaddle = Paddle((750,200))
        self.sprites.add(self.rightpaddle)

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

        running = True

        while running:
            self.clock.tick(60)

            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:
                sprite.update()

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

            # blit 
            pygame.display.update(dirty)


        print('Quitting, thanks for playing!')
        pygame.quit()
        sys.exit()

    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:
                    self.rightpaddle.up()
                if event.key == K_DOWN:
                    self.rightpaddle.down()
                if event.key == K_w:
                    self.leftpaddle.up()
                if event.key == K_s:
                    self.leftpaddle.down()
            elif event.type == KEYUP:
                # paddle control
                if event.key == K_w:
                    self.leftpaddle.down()
                if event.key == K_s:
                    self.leftpaddle.up()

                if event.key == K_UP:
                    self.rightpaddle.down()
                if event.key == K_DOWN:
                    self.rightpaddle.up()

        return True

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

Can anyone help me?

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\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\$ 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\$ May 3 '13 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .