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


Groups > comp.lang.python > #90269

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

References <009ceef8-066d-4d92-a6c9-761e86584e75@googlegroups.com>
Date 2015-05-08 10:22 -0600
Subject Re: How to properly apply OOP in the bouncing ball code
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.258.1431102150.12865.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On May 8, 2015 9:46 AM, "Tommy C" <tommyc168168@gmail.com> 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.

As the error says, the attribute does not exist.

> class Ball:
>     def __init__(self, X, Y):
>         self.velocity = [1,1]

Here where you set it, you call it "velocity".

>         speed = self.velocity

Here you create a local variable called "speed", which you never use.

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

And here you look up an attribute of Ball called "speed", which doesn't
match the name you used in __init__.

This is a muddled design overall. Your Ball class represents the individual
balls bouncing around the screen. It should not also contain details about
window size and the logic for the event loop. Use a separate class for that.

Similarly, if the purpose of your settings method is to manage settings,
why does it also contain all the bouncing logic? These should probably be
separate methods.

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