How to properly apply OOP in the bouncing ball code

Tommy C tommyc168168 at gmail.com
Mon May 11 11:22:05 EDT 2015


Thanks for your help.

I have updated the code as follows, there are no more errors but the images will not move at all, as all the images are staying at the upper left corner. Please advice, thanks.


import sys, pygame

pygame.init()

size   = width,  height = 800, 600
black  = [0,0,0]
screen = pygame.display.set_mode(size)

class BALL:
    def __init__(self,image):
        self.ball     = pygame.image.load(image)
        self.ballrect = self.ball.get_rect()
        self.speed    = [2, 2]

    def control(self):
        ballmove = self.ballrect.move(self.speed)

        if ballmove.left < 0 or ballmove.right > width:
            self.speed[0] = -self.speed[0]

        if ballmove.top < 0  or ballmove.bottom > height:
            self.speed[1] = -self.speed[1]

    def settings(self):
        clock  = pygame.time.Clock()
        screen.fill(black)
        screen.blit(self.ball, self.ballrect)
        pygame.display.flip()
        clock.tick(60)
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT: sys.exit()


bob = BALL("spongebob.png")
bob.control()
bob.settings()

patrick = BALL("patrick.jpg")
patrick.speed[0] = 5
patrick.speed[1] = 8
patrick.control()
patrick.settings()

jaws = BALL("jaws.jpg")
jaws.speed[0] = 1
jaws.speed[1] = 10
jaws.control()
jaws.settings()


On Friday, May 8, 2015 at 11:40:46 AM UTC-4, Tommy C wrote:
> I'm trying to apply OOP in this bouncing ball code in order to have multiple balls bouncing around the screen. The objective of this code is to create a method called settings, which controls all the settings for the screen and the bouncing behaviour of multiple balls, under the class Ball. However, I keep on getting errors related to attributes (e.g., speed). I'm new to OOP in Python so your help will be much appreciated. Thanks in advance.
> 
> 
> import pygame
> import random
> import sys
> 
> pygame.init()
> 
> class Ball:
>     def __init__(self, X, Y):
>         self.velocity = [1,1]
>         self.ball_image = pygame.image.load ("ball.jpg")
>         self.ball_boundary = self.ball_image.get_rect ()
>         self.black = [0,0,0]
>         self.width = 800
>         self.height = 600
>         self.num = 8
>         self.X = random.randint(0, self.width)
>         self.Y = random.randint(0, self.height)
> 
>     def settings(self):
>         #X = random.randint(0, self.width)
>         #Y = random.randint(0, self.height)
>         clock = pygame.time.Clock()
>         size = self.width, self.height
>         screen = pygame.display.set_mode(size)
>         ball_boundary = self.ball_image.get_rect()
>         speed = self.velocity
>         pic = self.ball_image
>         pygame.display.set_caption("Balls")
>         num_balls = self.num
>         ball_list = []
> 
>         for i in range(num_balls):
>            ball_list.append( Ball(random.randint(10, self.width-10),random.randint(10, self.height-10)) )
> 
>         while 1:
>             for event in pygame.event.get():
>                 if event.type == pygame.QUIT:
>                     sys.exit(0)
> 
>             screen.fill(self.black)
>             for balls in ball_list:
>                 if balls.ball_boundary.left < 0 or balls.ball_boundary.right > self.width:
>                     balls.speed[0] = -balls.speed[0]
>                 if balls.ball_boundary.top < 0 or balls.ball_boundary.bottom > self.height:
>                     balls.speed[1] = -balls.speed[1]
>                 balls.ball_boundary = balls.ball_boundary.move (self.velocity)
>                 screen.blit (balls.ball_image, balls.ball_boundary)
>             pygame.display.flip()
> 
> play = Ball(random.randint(0, 800), random.randint(0, 600))
> 
> play.settings()
> 
> 
> 
> 
> Message	File Name	Line	Position	
> Traceback				
>     <module>	C:\....\Multiple_balls_TC.py	63		
>     settings	C:\....\Multiple_balls_TC.py	56		
> AttributeError: Ball instance has no attribute 'speed'




More information about the Python-list mailing list