如何用python制作这9个不同颜色和大小的球

如何用python制作这9个不同颜色和大小的球,python,tkinter,Python,Tkinter,所以我试着让9个不同的球以不同的颜色、大小和不同的动作出现。到目前为止,我有9个球,但它们有时颜色相同,大小相同,运动轨迹几乎相同。 不知道我该换什么 from tkinter import * import time import random WIDTH = 800 HEIGHT = 500 SIZE = random.randrange(10,100,10) tk = Tk() canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="gr

所以我试着让9个不同的球以不同的颜色、大小和不同的动作出现。到目前为止,我有9个球,但它们有时颜色相同,大小相同,运动轨迹几乎相同。 不知道我该换什么


from tkinter import *
import time
import random

WIDTH = 800
HEIGHT = 500
SIZE = random.randrange(10,100,10)
tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="grey")
canvas.pack()
colors = ['black', 'blue', 'yellow','orange','green','purple', 'maroon', 'teal', 'brown']
balls = []

for _ in range (10):
    balls.append(canvas.create_oval(0, 0, SIZE, SIZE, fill=random.choice(colors)))

class Ball:
    def __init__(self):
        for self.shape in balls:
            self.speedx = 9 # changed from 3 to 9
            self.speedy = 9 # changed from 3 to 9
            self.active = True
            self.move_active()

    def ball_update(self):
        for self.shape in balls:
            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

    def move_active(self):
        if self.active:
            self.ball_update()
            tk.after(40, self.move_active) # changed from 10ms to 30ms


ball = Ball()
tk.mainloop()


如果您总是从同一个列表中获得随机值,则项目可以重复。有时,你可以随机获得所有物品的相同颜色。最好一个接一个地从列表中获取颜色,而不是随机获取

for color in COLORS:
     ball_id = canvas.create_oval(..., fill=color)
仅在开始时选择一次“大小”,然后使用“大小”中的相同值。您应该为范围为10的uu选择循环内的随机大小

每个球在相同的位置0,0开始,使用相同的speedx,speedx,因此它可以以相同的方式移动。他们应该从不同的地方开始。每个球都应该有自己的速度变量

for color in COLORS:
    size = random.randrange(10, 50, 5)

    x = random.randrange(0, WIDTH, 10)
    y = random.randrange(0, HEIGHT, 10)

    speedx = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`
    speedy = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`

    ball_id = canvas.create_oval(x, y, x+size, y+size, fill=color)

    ball = [ball_id, speedx, speedy]

    balls.append(ball)
顺便说一句:职业球应该只保留关于一个球的信息,并且只移动这个球。你们的班级舞会很像应用程序,所以我改了名字

import tkinter as tk # `import *` is not preferd
import time
import random

# --- constants ---

WIDTH = 800
HEIGHT = 500
COLORS = ['black', 'blue', 'yellow','orange','green','purple', 'maroon', 'teal', 'brown']

# --- classes ---

class Ball:

    def __init__(self, canvas, color):
        self.canvas = canvas
        self.color = color

        self.size = random.randrange(10, 50, 5)

        self.x = random.randrange(self.size, WIDTH-self.size, 10) # uses "size" to stop jamming the edge
        self.y = random.randrange(self.size, HEIGHT-self.size, 10) # uses "size" to stop jamming the edge

        self.speedx = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`
        self.speedy = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`

        self.id = self.canvas.create_oval(self.x, self.y, self.x+self.size, self.y+self.size, fill=self.color)

    def move(self):
        self.canvas.move(self.id, self.speedx, self.speedy)

        x1, y1, x2, y2 = self.canvas.coords(self.id)
        if x1 <= 0 or x2 >= WIDTH:
            self.speedx *= -1
        if y1 <= 0 or y2 >= HEIGHT:
            self.speedy *= -1

class App:

    def __init__(self, root):

        self.root = root

        self.canvas = tk.Canvas(self.root, width=WIDTH, height=HEIGHT, bg="grey")
        self.canvas.pack()

        self.create()

        self.move()

        self.root.mainloop()

    def create(self):

        self.balls = []

        for color in COLORS:
            ball = Ball(self.canvas, color)
            self.balls.append(ball)


    def move(self):
        for ball in self.balls:
            ball.move()

        self.root.after(50, self.move) # changed from 10ms to 30ms

# --- functions ---

# empty

# --- main ---

root = tk.Tk()
App(root)
root.mainloop()

如果您总是从同一个列表中获得随机值,则项目可以重复。有时,你可以随机获得所有物品的相同颜色。也许你不应该随机获得颜色,而应该在颜色中使用颜色:balls.appendcanvas.create_oval。。。fill=color仅选择一次大小,以后使用与“大小”相同的值。你应该在圈内为u10:`选择随机大小:`每个球在相同的位置0,0开始,因此它可以以相同的方式移动。他们应该从不同的地方开始。每个球都应该有自己的速度变量。我建议阅读下面的文章:。因为我将继续使用你的格式,当它位于一个布局中时,学习编码会容易得多,我可以使用它来保持一切井然有序。有一次我把两者并排比较,我真的明白你在说什么。
import tkinter as tk # `import *` is not preferd
import time
import random

# --- constants ---

WIDTH = 800
HEIGHT = 500
COLORS = ['black', 'blue', 'yellow','orange','green','purple', 'maroon', 'teal', 'brown']

# --- classes ---

class App:

    def __init__(self, root):

        self.root = root

        self.canvas = tk.Canvas(self.root, width=WIDTH, height=HEIGHT, bg="grey")
        self.canvas.pack()

        self.ball_create()

        self.ball_update()

        self.root.mainloop()

    def ball_create(self):

        self.balls = []

        for color in COLORS:
            size = random.randrange(10, 50, 5)
            x = random.randrange(size, WIDTH-size, 10) # uses "size" to stop jamming the edge
            y = random.randrange(size, HEIGHT-size, 10) # uses "size" to stop jamming the edge
            speedx = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`
            speedy = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`
            ball_id = self.canvas.create_oval(x, y, x+size, y+size, fill=color)

            ball = [ball_id, speedx, speedy]

            self.balls.append(ball)

    def ball_update(self):
        for ball in self.balls:
            #ball_id, speedx, speedy = ball
            #self.canvas.move(ball_id, speedx, speedy)

            self.canvas.move(ball[0], ball[1], ball[2])

            pos = self.canvas.coords(ball[0])
            if pos[2] >= WIDTH or pos[0] <= 0:
                ball[1] *= -1
            if pos[3] >= HEIGHT or pos[1] <= 0:
                ball[2] *= -1

        self.root.after(50, self.ball_update) # changed from 10ms to 30ms

# --- functions ---

# empty

# --- main ---

root = tk.Tk()
App(root)
root.mainloop()
import tkinter as tk # `import *` is not preferd
import time
import random

# --- constants ---

WIDTH = 800
HEIGHT = 500
COLORS = ['black', 'blue', 'yellow','orange','green','purple', 'maroon', 'teal', 'brown']

# --- classes ---

class Ball:

    def __init__(self, canvas, color):
        self.canvas = canvas
        self.color = color

        self.size = random.randrange(10, 50, 5)

        self.x = random.randrange(self.size, WIDTH-self.size, 10) # uses "size" to stop jamming the edge
        self.y = random.randrange(self.size, HEIGHT-self.size, 10) # uses "size" to stop jamming the edge

        self.speedx = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`
        self.speedy = random.choice([-9, -6, -3, 3, 6, 9]) # skip `0`

        self.id = self.canvas.create_oval(self.x, self.y, self.x+self.size, self.y+self.size, fill=self.color)

    def move(self):
        self.canvas.move(self.id, self.speedx, self.speedy)

        x1, y1, x2, y2 = self.canvas.coords(self.id)
        if x1 <= 0 or x2 >= WIDTH:
            self.speedx *= -1
        if y1 <= 0 or y2 >= HEIGHT:
            self.speedy *= -1

class App:

    def __init__(self, root):

        self.root = root

        self.canvas = tk.Canvas(self.root, width=WIDTH, height=HEIGHT, bg="grey")
        self.canvas.pack()

        self.create()

        self.move()

        self.root.mainloop()

    def create(self):

        self.balls = []

        for color in COLORS:
            ball = Ball(self.canvas, color)
            self.balls.append(ball)


    def move(self):
        for ball in self.balls:
            ball.move()

        self.root.after(50, self.move) # changed from 10ms to 30ms

# --- functions ---

# empty

# --- main ---

root = tk.Tk()
App(root)
root.mainloop()