首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与tkinter-canvas交互的球

与tkinter-canvas交互的球
EN

Stack Overflow用户
提问于 2020-05-03 17:42:24
回答 1查看 112关注 0票数 1

我可以用tkinter编程一些移动的球。我用了两种不同的颜色。现在我想让白色的球在击中红色的球时变成红色。有人能帮帮我吗?

(额外但不太重要的一点是,如果一个球撞到另一个球,他们改变方向,它应该是随机的哪个方向,但这不是那么重要)

总的来说,我有一个问题要弄清楚,球是如何相互作用的。下面是我的代码:

代码语言:javascript
复制
from tkinter import *
import time
import random

WIDTH = 800
HEIGHT = 500

tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="black")
tk.title("Drawing")
canvas.pack()

colors = ['red', 'green', 'blue', 'orange', 'yellow', 'cyan', 'magenta',
          'dodgerblue', 'turquoise', 'grey', 'gold', 'pink']

class Ball:
    def __init__(self, color):
        self.size = random.randrange(5, 10)
        #color = random.choice(colors)
        self.xpos = random.randrange(0,WIDTH)
        self.ypos =random.randrange(0,HEIGHT)
        self.shape = canvas.create_oval(self.xpos-self.size, self.ypos -self.size,self.xpos +self.size,self.ypos+self.size, fill=color)
        self.speedx = random.randrange(-5, 5)
        self.speedy = random.randrange(-5, 5)

    def update(self):
        canvas.move(self.shape, self.speedx, self.speedy)
        pos = canvas.coords(self.shape)
        if pos[2] >= (WIDTH) or (pos[0] ) <= 0:
            self.speedx *= -1
        if pos[3] >= (HEIGHT) or (pos[1]) <= 0:
            self.speedy *= -1


ball_list = []
Infiziert_list =[]
for i in range(100):
    ball_list.append(Ball('white'))
for j in range(3):
    ball_list.append(Ball('red'))
while True:
    for ball in ball_list:
        ball.update()
    tk.update()
    #time.sleep(0.001)

非常感谢

EN

回答 1

Stack Overflow用户

发布于 2020-05-03 19:46:00

您需要执行类似以下代码的操作。

如果你想写你自己的代码。这里有一个很好的参考资料,请检查Ball collisions部件。

运行此代码以获得碰撞球的完整工作示例,这些球在碰撞时会改变颜色。

代码语言:javascript
复制
from tkinter import *
import random

class Balls:
    color = ["red", "orange", "yellow", "green", "blue", "violet"]    
    v1x,v2x,v1y,v2y=0.1*random.randint(-10, 10),0.1*random.randint(-10, 10),0.1*random.randint(-10, 10),0.1*random.randint(-10, 10)
    def __init__(self, canvas,R,x1,y1,x2,y2):
        self.R=R
        self.x1 = x1
        self.y1=y1
        self.x2=x2
        self.y2 = y2
        self.canvas = canvas
        self.ball1 = canvas.create_oval(self.x1-self.R, self.y1-self.R,self.x1+self.R,self.y1+self.R, fill=random.choice(self.color))
        self.ball2 = canvas.create_oval(self.x2-self.R, self.y2-self.R,self.x2+self.R,self.y2+self.R, fill=random.choice(self.color))


    def callback(self):
        color=["red", "orange", "yellow", "green", "blue", "violet"] 
        self.AA=canvas.create_oval(self.x-10, self.y-10,self.x+10,self.y+10, fill=random.choice(color))
        self.vx,self.vy=0.5*random.randint(-10, 10),0.5*random.randint(-10, 10)
        canvas.move(self.AA, self.vx,self.vy)



    def move_balls(self):
        self.canvas.move(self.ball1, self.v1x,self.v1y)
        self.canvas.move(self.ball2, self.v2x,self.v2y)
        self.canvas.after(5, self.move_balls)

    def eventt(self):            
        A=[(canvas.coords(self.ball1)[0]-canvas.coords(self.ball2)[0]),
           (canvas.coords(self.ball1)[1]-canvas.coords(self.ball2)[1])]
        if ((A[0])**2+(A[1])**2)**0.5<=2*self.R:
            self.v1x,self.v1y,self.v2x,self.v2y=self.v2x,self.v2y,self.v1x,self.v1y
            canvas.itemconfig(self.ball1,fill=random.choice(self.color)) 
            canvas.itemconfig(self.ball2,fill=random.choice(self.color))
        if canvas.coords(self.ball1)[0]<=0 or canvas.coords(self.ball1)[2]>=400:
            self.v1x=-self.v1x
        if canvas.coords(self.ball1)[1]<=0 or canvas.coords(self.ball1)[3]>=400:
            self.v1y=-self.v1y
        if canvas.coords(self.ball2)[0]<=0 or canvas.coords(self.ball2)[2]>=400:
            self.v2x=-self.v2x
        if canvas.coords(self.ball2)[1]<=0 or canvas.coords(self.ball2)[3]>=400:
            self.v2y=-self.v2y
        self.canvas.after(10,self.eventt)


# initialize root Window and canvas
root = Tk()
root.title("Balls")
root.resizable(False,False)
canvas = Canvas(root, width = 400, height = 400)
canvas.bind("<Button-1>", Balls.callback)
canvas.pack()

# create two ball objects and animate them
ball = Balls(canvas ,20, 100, 200, 200, 300)
ball.move_balls()
ball.eventt()
root.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61572410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档