我是编程的新手,目前正在尝试用烧杯产生化学刺激,产生气泡。经过大量的研究,我设法得到一个圆圈移动,并创建了一个代表烧杯的矩形。但是我想要在烧杯顶部形成圆圈并随机向上移动并在遇到顶部时将其自身摧毁,我无法弄清楚。如果有人能帮助我,我将非常感激。先感谢您。
我的代码是:
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 ()
答案 0 :(得分:1)
使用random
,您可以随机移动它。
如果y < -height
然后对象离开了屏幕,您可以将其移动到开始位置。
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 ()
编辑:使用课程可以轻松拥有多项内容
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 ()