Tkinter移动球程序创建4个球而不是2个 [英] Tkinter moving balls program creating 4 balls instead of 2

查看:72
本文介绍了Tkinter移动球程序创建4个球而不是2个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,该程序使球在画布上移动并反弹画布的边缘。效果非常好,所以我尝试添加另一个球,但没有创建2个球,而是创建了4个球,其中2个球移动了,其他两个球保持静止。
这是代码:

I have created a program that made a ball move around a canvas and bounce of the edge of the canvas. This worked very well so I tried to add another ball but instead of creating 2 balls it created 4, 2 of the balls move and the other two stay still. This is the code:

#imports
from tkinter import *
import random
from random import randint



#Creating random x and y for the balls to move along
x = random.randint(1,10)
y = random.randint(1,10)
print(x, " Ball 1y")
print(y, " Ball 1x")

x0 = random.randint(1,10)
y0 = random.randint(1,10)
print(y0," Ball2y")
print(x0, " Ball2x")


class Ball:

    #creates balls
    def __init__(self, canvas, x1,y1,x2,y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.canvas = canvas
        self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,         fill="red")
        self.ball2 = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,     fill="red")


    def move_ball(self, x, y):
        #function to move ball

        coords = canvas.coords(self.ball)
        #Because coord is stored in a list I need to call which coord is bigger than the edge of the canvas
        if coords[0] >= 280:
            #multiplying by a negative number makes the ball "bounce"
            x *= -1
        if coords[0] <= 0:
            x *= -1
        if coords[3] <= 20:
            y *= -1
        if coords[3] >= 300:
            y *= -1
        print(coords, " Ball1")
        #makes ball move
        self.canvas.move(self.ball, x, y)
        self.canvas.after(50, self.move_ball, x, y)


    def move_ball2(self, x0, y0):

        #same as previous different variables
        coords2 = canvas.coords(self.ball2)

        if coords2[0] >= 280:
            x0 *= -1
        if coords2[0] <= 0:
            x0 *= -1
        if coords2[3] <= 20:
            y0 *= -1
        if coords2[3] >= 300:
            y0 *= -1
        print(coords2, " Ball2")
        self.canvas.move(self.ball2, x0, y0)
        self.canvas.after(50, self.move_ball2, x0, y0)



#changes window titles etc.
root = Tk()
root.title("Balls")
root.resizable(False, False)
canvas = Canvas(root, width = 300, height = 300)
canvas.pack()

#creates ball with dimensions of the ball
ball1 = Ball(canvas, 10, 10, 30, 30)
ball2 = Ball(canvas, 60, 60, 80, 80)
#calls move ball function
ball1.move_ball(x, y)
ball2.move_ball2(x0,y0)

root.mainloop()


推荐答案

问题是,除了添加第二个球。面向对象编程(即简单的python中的类)的想法是创建一个类,这里​​是Ball,它定义一个通用球的工作方式。您可以从此类创建所需的任意多个对象(此处为ball1,ball2等)。

The problem is that you changed the class Ball in addition to adding a second ball. The idea of object oriented programming (i.e. in simple words classes in python) is to create a class, here Ball, that defines how ONE generic ball works. You can from this class create as many objects (here ball1, ball2, etc.) as you want.

在您的代码中,您只需


  1. 删除该行

  1. remove the line

self.ball2 = canvas.create_oval(self.x1,self.y1,self.x2,self.y2,fill = red)

self.ball2 = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red")

删除完整的move_ball2函数

remove the complete move_ball2 function

更改

ball2.move_ball2(x0,y0)

ball2.move_ball2(x0,y0)

ball2.move_ball(x0,y0)

它将按预期工作。

这篇关于Tkinter移动球程序创建4个球而不是2个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆