How can I make this 9 different balls with different colors and sizes in python

您所在的位置:网站首页 randrande How can I make this 9 different balls with different colors and sizes in python

How can I make this 9 different balls with different colors and sizes in python

2023-02-06 14:20| 来源: 网络整理| 查看: 265

If you get random value always from the same list then items can repeate. And sometimes you can randomly get the same color for all items. Better get colors from list one-by-one, not randomly.

for color in COLORS: ball_id = canvas.create_oval(..., fill=color)

You select SIZE only once - at start - and later you use the same value from SIZE. You should select random size inside loop for _ in range (10)

Every ball starts in the same place (0,0) and use the same speedx, speedy so it may move the same way. They should start in different places. And every ball should have own variable for speed.

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) from tkinter import * 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, balls): self.balls = balls self.ball_update() def ball_update(self): for ball in self.balls: #ball_id, speedx, speedy = ball #canvas.move(ball_id, speedx, speedy) canvas.move(ball[0], ball[1], ball[2]) pos = canvas.coords(ball[0]) if pos[2] >= WIDTH or pos[0] = HEIGHT or pos[1] = WIDTH or pos[0] = HEIGHT or pos[1]


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3