0

i am new to programming and am currently trying to get a chemistry stimulation with beaker which produces bubbles. after a lot of research I have managed to get a circle moving and also created a rectangle representing a beaker. however I would like the circles to be formed at the top of the beaker and move upwards in random and destroy itself when it meets the top which I just cant figure out. I would be very grateful if anyone can help me. thank you in advance.

my code is:

from tkinter import *

x = 10
y = 10
a = 50
b = 50

x_vel = 5
y_vel = 5

def move():

    global x
    global y
    global x_vel
    global y_vel
    if x < 0:
        x_vel = 5
    if x > 350:
        x_vel = -5
    if y < 0:
        y_vel = 5
    if y > 150:
        y_vel = -5
    canvas1.move(circle, x_vel, y_vel)
    coordinates = canvas1.coords(circle)
    x = coordinates[0]
    y = coordinates[1]
    window.after(33, move)

window   = Tk()
window.geometry("1000x1000")

canvas1=Canvas(window, height = 1000, width= 1000)
canvas1.grid (row=0, column=0, sticky=W)
coord = [x, y, a, b ]
circle = canvas1.create_oval(coord, outline="red", fill="red")


coord = [230, 270, 270, 310]
rect2 = canvas1.create_rectangle(coord, outline="Blue", fill="Blue")

move()

window.mainloop ()
tamilini
  • 53
  • 1
  • 2
  • 10
  • not tkinter, but something similar as inspiration... [mpl rain animation](http://matplotlib.org/gallery/animation/rain.html#sphx-glr-gallery-animation-rain-py). – ahed87 Dec 16 '17 at 20:22
  • example [canvas-solar-system](https://github.com/furas/python-examples/tree/master/tkinter/__canvas__/canvas-solar-system) – furas Dec 16 '17 at 20:41
  • use button `{}` to correctly format code. Now you have wrong indentions and we can't run it. – furas Dec 16 '17 at 20:42
  • I corrected my indentions – tamilini Dec 16 '17 at 21:10

1 Answers1

3

Using random you can move it randomly.

If y < -height then object left screen and you can move it to start position.

import tkinter as tk
import random

def move():
    global x
    global y
    global x_vel
    global y_vel

    # get random move
    x_vel = random.randint(-5, 5)
    y_vel = -5

    canvas1.move(circle, x_vel, y_vel)
    coordinates = canvas1.coords(circle)

    x = coordinates[0]
    y = coordinates[1]

    # if outside screen move to start position
    if y < -height:
        x = start_x
        y = start_y
        canvas1.coords(circle, x, y, x+width, y+height)

    window.after(33, move)

# --- main ---

start_x = 230
start_y = 270

x = start_x
y = start_y

width  = 50
height = 50

x_vel = 5
y_vel = 5

window = tk.Tk()
window.geometry("1000x1000")

canvas1 = tk.Canvas(window, height=1000, width=1000)
canvas1.grid(row=0, column=0, sticky='w')

coord = [x, y, x+width, y+height]
circle = canvas1.create_oval(coord, outline="red", fill="red")

coord = [x, y, x+40, y+40]
rect2 = canvas1.create_rectangle(coord, outline="Blue", fill="Blue")

move()

window.mainloop ()

EDIT: using class you can easily have many items

enter image description here

import tkinter as tk
import random

class Bubble():

    def __init__(self, canvas, x, y, size, color='red'):
        self.canvas = canvas

        self.x = x
        self.y = y

        self.start_x = x
        self.start_y = y

        self.size = size
        self.color = color

        self.circle = canvas.create_oval([x, y, x+size, y+size], outline=color, fill=color)

    def move(self):
        x_vel = random.randint(-5, 5)
        y_vel = -5

        self.canvas.move(self.circle, x_vel, y_vel)
        coordinates = self.canvas.coords(self.circle)

        self.x = coordinates[0]
        self.y = coordinates[1]

        # if outside screen move to start position
        if self.y < -self.size:
            self.x = self.start_x
            self.y = self.start_y
            self.canvas.coords(self.circle, self.x, self.y, self.x + self.size, self.y + self.size)

def move():
    for item in bubbles:
        item.move()

    window.after(33, move)

# --- main ---

start_x = 230
start_y = 270

window = tk.Tk()
window.geometry("1000x1000")

canvas = tk.Canvas(window, height=1000, width=1000)
canvas.grid(row=0, column=0, sticky='w')

bubbles = []
for i in range(5):
    offset = random.randint(10, 20)
    b = Bubble(canvas, start_x+10, start_y-offset, 20, 'red')
    bubbles.append(b)
for i in range(5):
    offset = random.randint(0, 10)
    b = Bubble(canvas, start_x+10, start_y-offset, 20, 'green')
    bubbles.append(b)

coord = [start_x, start_y, start_x+40, start_y+40]
rect = canvas.create_rectangle(coord, outline="Blue", fill="Blue")

move()

window.mainloop ()
furas
  • 134,197
  • 12
  • 106
  • 148
  • new version on GitHub: [canvas-bubbles](https://github.com/furas/python-examples/tree/master/tkinter/__canvas__/canvas-bubbles) – furas Dec 16 '17 at 21:47