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


Groups > comp.lang.python > #99996

Re: How to bounce the ball forever around the screen

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: How to bounce the ball forever around the screen
Date Fri, 04 Dec 2015 09:09:05 +0100
Organization None
Lines 51
Message-ID <mailman.190.1449216556.14615.python-list@python.org> (permalink)
References <ee38a39b-5d03-4715-b9d8-23ca24e91866@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de JIwNHlgInfjCb24pQ1O2jQIjc590FywHW1CtKmaz1oCw==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'canvas': 0.07; 'subject:How': 0.09; 'it;': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'hits': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:screen': 0.16; 'tk()': 0.16; 'wrote:': 0.16; 'tkinter': 0.22; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'horizontal': 0.29; 'vertical': 0.29; 'code': 0.30; 'window': 0.30; 'handle': 0.34; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'email addr:gmail.com': 0.62; 'wall': 0.63; 'moves': 0.84; 'subject:around': 0.84; 'bounce': 0.91; 'forever.': 0.93; 'walls': 0.93
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9f5c.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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>
Xref csiph.com comp.lang.python:99996

Show key headers only | View raw


phamtony33@gmail.com wrote:

> from Tkinter import *
> window = Tk()
> canvas = Canvas(window, width=500, height=500, background="green")
> canvas.pack()
> 
> def move_ball(speed_x, speed_y):
>         box = canvas.bbox("ball")
>         x1 = box[0]
>         y1 = box[1]
>         x2 = box[2]
>         y2 = box[3]
>         
>         if x1 <= 0:
>                 speed_x = 0
>                 speed_y = 0
>                 
>         canvas.move("ball", speed_x, speed_y)
>         canvas.after(30, move_ball, speed_x, speed_y)
>         
> canvas.create_oval(225, 225, 275, 275, fill="blue", tags="ball")
> 
> move_ball(-10, 7)
> 
> mainloop()
> 
>  where in the code should i change to make the ball bounce around forever. 
is it the x and y?

When the ball hits a wall you have to "reflect" it; for a vertical wall 
speed_x has to change:

    if x1 <= 0: # ball hits left wall
        speed_x = -speed_x
    elif ... # ball hits right wall
        speed_x = -speed_x

Can you come up with the correct check for the right wall?
To try if this part works correctly temporarily change

> move_ball(-10, 7)

to

move_ball(-10, 0)

so that the ball only moves in horizontal direction. Once this works handle 
the vertical walls and speed_y the same way.

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


Thread

How to bounce the ball forever around the screen phamtony33@gmail.com - 2015-12-03 20:19 -0800
  Re: How to bounce the ball forever around the screen Peter Otten <__peter__@web.de> - 2015-12-04 09:09 +0100

csiph-web