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


Groups > comp.lang.python > #90466

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

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'method.': 0.07; 'subject:code': 0.07; 'screen.': 0.09; 'sys,': 0.09; 'runs': 0.10; 'subject:How': 0.10; 'def': 0.12; '[2,': 0.16; 'advice,': 0.16; 'balls': 0.16; 'loop.': 0.16; 'pygame': 0.16; 'pygame.': 0.16; 'quit.': 0.16; 'subject:OOP': 0.16; 'to:name:python list': 0.16; 'tommy': 0.16; 'width,': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'thanks.': 0.20; 'help.': 0.21; 'import': 0.22; 'class.': 0.26; 'skip:_ 20': 0.27; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'errors': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'ball': 0.31; 'clock': 0.31; 'staying': 0.31; 'class': 0.32; 'moment': 0.34; 'screen': 0.34; 'updated': 0.34; 'sense': 0.34; 'subject:the': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'animation': 0.36; 'height': 0.36; 'method': 0.36; 'thanks': 0.36; 'should': 0.36; 'level': 0.37; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'black': 0.61; 'more': 0.64; '600': 0.68; 'upper': 0.74; '2015': 0.84; 'oscar': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Tqt4SUrh/jr9jzZuiM74foinuD6sSnBELaXFW0+6KVs=; b=0E1NwAJyXOn+g2D/jo4m/6K0bL1e1dJxOMLsYa55zzybAeNhIOrRQnpc+ZvsQ0LKBT ffmCOWd3OmW81PKxBS5vofFrDK1yD1Zst/HyIYJ2E4CqpZ9cCdtLN8iaMEvMM6m8/Y3k 9NQ1M0rGRKdMntv3Tv8rD88wLU2I7bUFK3exOjQF6WvArvoc3jHWcJDNn1oCNw8jw5j0 vWF5cG7f+VjsyW91kHkXGGrW2AcjYa21jt2SYszJAxHXId76fMhitqlW1O+ByFScmMLd YogvBmdEIJ1gYdnHhwXZ3jcqt3pdRi295CoJ9CDvgA/WmTgHG4IERIgA4l9W5mys0pOR gCvA==
X-Received by 10.180.212.106 with SMTP id nj10mr6897452wic.53.1431449624281; Tue, 12 May 2015 09:53:44 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <e841eeeb-7a36-47fe-bd21-bfd7b53a9588@googlegroups.com>
References <009ceef8-066d-4d92-a6c9-761e86584e75@googlegroups.com> <e841eeeb-7a36-47fe-bd21-bfd7b53a9588@googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Tue, 12 May 2015 17:53:23 +0100
Subject Re: How to properly apply OOP in the bouncing ball code
To Python List <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.404.1431449631.12865.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431449631 news.xs4all.nl 2952 [2001:888:2000:d::a6]:46285
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90466

Show key headers only | View raw


On 11 May 2015 at 16:22, Tommy C <tommyc168168@gmail.com> wrote:
> 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]

The function below should not be a method on a particular ball object
as it is the global event loop for pygame. This needs to be a top
level function that updates the animation for all balls.

>     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()

In this loop you need to add code that will call the control() method
of each ball and then redraw the screen. At the moment you only draw
the objects once at the top of the settings() method. Then the loop
runs until you quit. So it should be something like:

# The functions below would make sense as methods of an "App" class.

def run():
    while 1:
        for event in pygame.event.get():
            if event.type = pygame.QUIT:
                sys.exit()
        update()
        draw()

def update():
    for ball in balls:
        ball.control()

def draw():
    screen.fill(black)
    for ball in balls:
        ball.draw()  # You need to add this method to BALL
    pygame.display.flip()

balls = [BALL("spongebob.png"), BALL("jaws.jpg")]

run()  # Actually runs the program's event loop.


--
Oscar

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