Bouncing Ball Program with Tkinter

Post here if you need help with creating a Graphical User Interface in Python.

Bouncing Ball Program with Tkinter

Postby costigan555 » Wed Apr 23, 2014 12:36 am

I am new to the forum just as much as I am new to programming with Python. However, I have experience with C++, but this is my first time jumping into object-oriented. I would like to learn all of the basics and everything so I am working on a program that has balls that will bounce off of each other as well as the walls. I was hoping to get some help with that and I will attach the code I already have. Thanks in advance.
Code: Select all
from Tkinter import *
import math
import random

class Jumper:

    def __init__(self, x, y):

        self.x = x
        self.y = y

        self.xChange = 0
        self.yChange = 0

        self.jumping = False
        self.startJumping = False

    def jump(self):

        self.startJumping = True

    def move(self):

        self.x += self.xChange
        self.y += self.yChange

        if self.startJumping:
            self.xChange =  0
            self.yChange -= 10

            self.startJumping = False

            self.jumping = True

        elif self.jumping:

            self.yChange += 0.2

            #self.jumping = False

class Game:

    def __init__(self): = Tk()

        self.canvas = Canvas(, width=500, height=500)
        self.jumper   = Jumper()
        self.jumperId = self.canvas.create_oval(245, 490, 255, 500)
        self.jumper.x = 250
        self.jumper.y = 495

        self.jumper.xChange = 0
        self.jumper.yChange = -3

        self.victim   = Jumper()
        self.victimId = self.canvas.create_oval(245, 245, 255, 255, fill='red')
        self.victim.x = 250
        self.victim.y = 250
        self.screenItems = {}

        self.screenItems[self.jumper] = self.jumperId
        self.screenItems[self.victim] = self.victimId

        self.canvas.bind_all('<KeyPress-space>', self.jump)

        self.screenItems = {}
        for i in range(20):

            x = random.randint(50, 450)
            y = random.randint(50, 450)

            j = Jumper(x, y)

            self.screenItems[j] = self.canvas.create_oval(x-5, y-5, x+5, y+5, fill='red')

            if i < 9:

                j.xChange = random.randint(-4, 4)
                j.yChange = random.randint(-4, 4)

    def jump(self, event):

    def detectCollision(self, left, right):

        if (math.sqrt( (right.x - left.x)**2 + (right.y - left.y)**2 ) < 10):

            theta = math.atan2( right.y - left.y, right.x - left.x )

            right.xChange = math.cos(theta) * ( math.sqrt(left.xChange**2 + left.yChange**2) )
            right.yChange = math.sin(theta) * ( math.sqrt(left.xChange**2 + left.yChange**2) )

            left.xChange = 0
            left.yChange = 0

    def run(self):
        while True:




            self.detectCollision(self.jumper, self.victim)


        while True:

            for item in self.screenItems:


                self.canvas.move(self.screenItems[item], item.xChange, item.yChange)


            for item in self.screenItems:

                for otherItem in self.screenItems:

                    if item != otherItem:

                        self.detectCollision(item, otherItem)

The program needed to be improved.
(3.53 KiB) Downloaded 101 times
Last edited by micseydel on Wed Apr 23, 2014 12:42 am, edited 1 time in total.
Reason: Locked OP, added code in code tags (from OP-provided attachment).
Posts: 2
Joined: Wed Apr 23, 2014 12:30 am

Re: Bouncing Ball Program with Tkinter

Postby micseydel » Wed Apr 23, 2014 12:51 am

Hello, welcome to the forum! Make sure to give this a read over before making more posts.

Most specifically, do you have a specific question or problem, or are you looking for general feedback?

My advice, that you don't necessarily need to follow, is to learn OOP before a GUI library such as Tkinter, rather than both at the same time. I say this because neither is trivial, and in programming and science it's easier to start to understand something new with fewer simultaneous variables. That, and I personally basically ignore anything with GUI code... so if you have any pure-OOP questions, I would separate them from your GUI code and post that code in a different subforum (I've moved your code to the GUI subforum). It's a good general rule anyway since posting the smallest runnable code that reproduces your problem makes it easier for us to help you ;)
Due to the reasons discussed here we will be moving to on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Bouncing Ball Program with Tkinter

Postby wuf » Wed Apr 23, 2014 12:39 pm

Posts: 57
Joined: Fri Feb 08, 2013 6:42 am

Re: Bouncing Ball Program with Tkinter

Postby costigan555 » Wed Apr 23, 2014 11:31 pm

Thanks. That really helped a lot.
Posts: 2
Joined: Wed Apr 23, 2014 12:30 am

Return to GUI

Who is online

Users browsing this forum: No registered users and 5 guests