Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在屏幕上弹起30个球_Python_Class - Fatal编程技术网

Python 在屏幕上弹起30个球

Python 在屏幕上弹起30个球,python,class,Python,Class,我的任务是在创建的窗口上显示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

我的任务是在创建的窗口上显示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()
#/usr/bin/env蟒蛇3
#多尔西·佩特里克
#在屏幕上显示30个弹跳球
从图形导入*
从时间上导入睡眠
从随机导入*
班级舞会:
定义初始化(自):
self.dx=1
self.dy=1
def抽签(自我,赢):
self.ball=圆(点(25,60),3)
self.ball.setFill('blue'))
赛尔夫。球。平局(胜利)
def移动(自我):
self.ball.move(self.dx,self.dy)
xValue=self.ball.getCenter().getX()
yValue=self.ball.getCenter().getY()
如果550xValue:
self.dx=-self.dx
如果500yValue:
self.dy=-self.dy
def main():
win=GraphWin(“bouncy.py”,550500)
ball=ball()
球。平局(胜利)
计数器=[]
尽管如此:
对于范围(30)内的i:
球移动
计数器=计数器()
计数器。设置计数器ID(i+1)
balls.append(ball)
win.getMouse()
赢
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

考虑到您的球类,您可以:

编辑以修复球类:

随机导入
班级舞会:
定义初始化(自):
self.dx=1
self.dy=1
self.ball=圆(点(random.randint(0550),random.randint(0500)),3)#使用random随机显示球
self.ball.setFill('blue'))
def抽签(自我,赢):
赛尔夫。球。平局(胜利)
def移动(自我):
self.ball.move(self.dx,self.dy)
#睡眠(0.005)#不需要。。。
xValue=self.ball.getCenter().getX()
yValue=self.ball.getCenter().getY()
如果550xValue:
self.dx=-self.dx
如果500yValue:
self.dy=-self.dy
def main():
win=GraphWin(“bouncy.py”,550500)
ball_lst=[ball()代表范围(30)内的i]#初始化30个球
对于球中球:
球。平局(胜利)
尽管如此:
对于球中球:#对于我们球列表中的每个球
球。移动()#移动球
win.getMouse()
赢

这应该可以完成任务。

您每次都将相同的
球添加到
。每次执行for-loop时,都需要启动一个新的
Ball()
对象。在代码检查方面:-尝试不要硬编码屏幕宽度等值(将其作为参数传递或声明为全局变量)。-不惜一切代价避免从X导入的
*
,你的主函数有点没用(除了C相似性之外,没有任何理由改变声明)-你的
睡眠(0.05)
球内。移动()
以后会有问题(移动的球越多,睡眠时间越长!),考虑在主事件循环中移动它。玩得开心:)还有,
计数器中的缩进问题。考虑在CoDeVIEW站点上发布成功代码=)我不确定什么是<代码>计数器>代码>!p那也是。我刚刚意识到,无论
ball.x
,你的绘图函数都在同一个位置绘图:(25,60)我似乎得到了一个AttributeError,因为“ball”对象没有属性“ball”。@Dorthy你在
ball.draw
中创建了
self.ball
,所以当你试图在
ball.move
中调用它时,它还不存在。尝试将其移动到
\uuuuu init\uuuuuu
方法。我当前收到一个关于图形的较大错误。第396行,在draw if self.canvas而非self.canvas.isClosed()中:提升GraphicsError(对象已绘制)graphics.GraphicsError:当前对象drawn@Dorthy,试试最后一个,draw方法应该只调用一次,不管怎样,要注意所有的球都是用相同的值实例化的,因此,他们每个人都是一对other@DanielSanchez有关于如何为圆创建随机点的提示吗?
import random
class Ball:
    def __init__(self):
        self.dx = 1
        self.dy = 1
        self.ball = Circle(Point(random.randint(0, 550), random.randint(0, 500)), 3) # use random to randomly display the balls
        self.ball.setFill('blue')

    def draw(self, win):
        self.ball.draw(win)

    def move(self):
        self.ball.move(self.dx,self.dy)
        #sleep(0.005) #not needed...

        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_lst = [Ball() for i in range(30)] #Initialize 30 balls
    for ball in ball_lst:
        ball.draw(win)
    while True:
        for ball in ball_lst: # for each ball in our balls list
            ball.move() #move the ball
    win.getMouse()
    win.close()