我的任务是在创建的窗口上显示30个弹跳球。我刚开始学习课程,我似乎不知道如何显示30个球弹跳。我能把一个球从四面墙上弹开。在#! /usr/bin/env python3
# Dorthy Petrick
# Display 30 bouncing balls bouncing around the screen
from graphics import *
from time import sleep
from random import *
class Ball:
def __init__(self):
self.dx = 1
self.dy = 1
def draw(self, win):
self.ball = Circle(Point(25, 60), 3)
self.ball.setFill('blue')
self.ball.draw(win)
def move(self):
self.ball.move(self.dx,self.dy)
xValue = self.ball.getCenter().getX()
yValue = self.ball.getCenter().getY()
if 550 < xValue:
self.dx = -self.dx
if -xValue > xValue:
self.dx = -self.dx
if 500 < yValue:
self.dy = -self.dy
if -yValue > yValue:
self.dy = -self.dy
def main():
win = GraphWin("bouncy.py", 550, 500)
ball = Ball()
ball.draw(win)
counters = []
while True:
for i in range(30):
ball.move()
counter = Counter()
counter.setCounterId(i + 1)
balls.append(ball)
win.getMouse()
win.close()
if __name__ == '__main__':
main()