Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90396

Re: How to properly apply OOP in the bouncing ball code

X-Received by 10.66.170.234 with SMTP id ap10mr19131493pac.0.1431357725848; Mon, 11 May 2015 08:22:05 -0700 (PDT)
X-Received by 10.182.137.137 with SMTP id qi9mr54414obb.36.1431357725792; Mon, 11 May 2015 08:22:05 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!m20no1963293iga.0!news-out.google.com!kd3ni14103igb.0!nntp.google.com!m20no10304739iga.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Mon, 11 May 2015 08:22:05 -0700 (PDT)
In-Reply-To <009ceef8-066d-4d92-a6c9-761e86584e75@googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=209.183.142.170; posting-account=vnV9NQoAAAA6LXzGehW7wALi50V_ol9e
NNTP-Posting-Host 209.183.142.170
References <009ceef8-066d-4d92-a6c9-761e86584e75@googlegroups.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <e841eeeb-7a36-47fe-bd21-bfd7b53a9588@googlegroups.com> (permalink)
Subject Re: How to properly apply OOP in the bouncing ball code
From Tommy C <tommyc168168@gmail.com>
Injection-Date Mon, 11 May 2015 15:22:05 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Lines 132
Xref csiph.com comp.lang.python:90396

Show key headers only | View raw


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'

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to properly apply OOP in the bouncing ball code Tommy C <tommyc168168@gmail.com> - 2015-05-08 08:40 -0700
  Re: How to properly apply OOP in the bouncing ball code Mel Wilson <mwilson@the-wire.com> - 2015-05-08 18:44 +0000
  Re: How to properly apply OOP in the bouncing ball code Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-08 10:22 -0600
  Re: How to properly apply OOP in the bouncing ball code Tommy C <tommyc168168@gmail.com> - 2015-05-11 08:22 -0700
    Re: How to properly apply OOP in the bouncing ball code Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-05-12 17:53 +0100
  Re: How to properly apply OOP in the bouncing ball code zipher <dreamingforward@gmail.com> - 2015-05-11 08:33 -0700
    Re: How to properly apply OOP in the bouncing ball code Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-05-11 20:24 -0400
      Re: How to properly apply OOP in the bouncing ball code zipher <dreamingforward@gmail.com> - 2015-05-11 17:42 -0700
        Re: How to properly apply OOP in the bouncing ball code Terry Reedy <tjreedy@udel.edu> - 2015-05-12 11:45 -0400
          Re: How to properly apply OOP in the bouncing ball code Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-13 20:20 +1200

csiph-web