skip to Main Content

I’m creating a ping pong game in pygame and trying to draw the paddles and a ball within a class.

When I attempt to draw it within a normal function it works fine, but when I try to implement it with a class I get a load of errors.

What am I doing wrong? Would it just be easier for me to create an image in photoshop and import it? Also, do I draw the paddles within the init method or a separate method.

Here’s my code, the class is in a separate file.

paddles.py:

import pygame    
import sys
class Paddles():

    def __init__(self, screen):
        self.screen = screen
        self.paddle_l = pygame.draw.rect(screen, (255, 255, 255), [15, 250, 10, 100])
        self.paddle_r = pygame.draw.rect(screen, (255, 255, 255), [780, 250, 10, 100])

program.py:

 import sys
 import pygame

 from settings import Settings
 from paddles import Paddles
 import game_functions as gf


 def run_game():
     # Initialise pygame, settings and screen object.
     pygame.init()
     ai_settings = Settings()
     screen =     pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
     pygame.display.set_caption('Ping Pong')

     #Make paddles
     paddles = Paddles(screen)
     paddles.paddles()

     # Start the main loop for the game.
     while True:
         gf.check_events()
         gf.update_screen(ai_settings,screen,paddles)


 run_game()

The error message I get is:

Traceback (most recent call last):   File "C:/Users/PycharmProjects/pingpong/.idea/program.py", line 27, in <module>
    run_game()   
File "C:/Users/PycharmProjects/pingpong/.idea/program.py", line 19, in run_game
    paddles.paddles() 
AttributeError: 'Paddles' object has no attribute 'paddles'

2

Answers


  1. Just as it says in the error message, your Paddle class has no function named paddles. Your class should look like:

    class Paddles():
        def __init__(self, screen):
            self.screen = screen
            self.paddle_l = pygame.draw.rect(screen, (255, 255, 255), [15, 250, 10, 100])
            self.paddle_r = pygame.draw.rect(screen, (255, 255, 255), [780, 250, 10, 100])
    
        def paddles(self):
            # DO STUFF WITH PADDLES
    

    I’m not entirely sure what you intend this function to do, but that’s up to you.

    Login or Signup to reply.
  2. The error you’re getting it’s because you’re trying to use a method from Paddles which would be paddles, but it doesn’t exist.

    Your file, for what I got, is called paddles.py (I can guess for your code), so it doesn’t mean your class Paddles has this paddles method.

    To fix it, just add the paddles method inside your Paddles class, such as:

    import pygame    
    import sys
    
    class Paddles():
        def __init__(self, screen):
            self.screen = screen
            self.paddle_l = pygame.draw.rect(screen, (255, 255, 255), [15, 250, 10, 100])
            self.paddle_r = pygame.draw.rect(screen, (255, 255, 255), [780, 250, 10, 100])
        def paddles():
            # Method operations 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search