如何让这个程序在Python中绘制10个椭圆?

如何让这个程序在Python中绘制10个椭圆?,python,list,class,loops,pygame,Python,List,Class,Loops,Pygame,我正在计划第12章课程练习第11步 街机游戏,我在显示10个椭圆时遇到了问题 正在尝试通过矩形的父类执行此操作。我只是想 现在有10个矩形。我知道问题出在第34-37行, 60-62或78-81。整个问题在下面的链接中列出 从技术上讲,您展示的代码确实绘制了10个椭圆,但它们是0×0像素的椭圆,因此您实际上看不到任何东西 您需要更改设置代码,以将在循环中创建的椭圆实例的属性设置为有用的属性,就像上一个循环对其创建的10个矩形所做的那样。没有设置,您只使用默认值(大部分为零) import py

我正在计划第12章课程练习第11步 街机游戏,我在显示10个椭圆时遇到了问题 正在尝试通过矩形的父类执行此操作。我只是想 现在有10个矩形。我知道问题出在第34-37行, 60-62或78-81。整个问题在下面的链接中列出


从技术上讲,您展示的代码确实绘制了10个椭圆,但它们是0×0像素的椭圆,因此您实际上看不到任何东西

您需要更改设置代码,以将在循环中创建的
椭圆
实例的属性设置为有用的属性,就像上一个循环对其创建的10个矩形所做的那样。没有设置,您只使用默认值(大部分为零)

import pygame
from random import randrange 

#Colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)

pygame.init()

#Set width and height of screen
size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Classes")

class Rectangle():

    def __init__(self):
        self.x = 0
        self.y = 0
        self.change_x = 0
        self.change_y = 0
        self.width = 0
        self.height = 0
        self.color = [0, 255, 0]
    def draw(self, screen):
        pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])
    def move(self):
        self.x = self.x + self.change_x
        self.y = self.y + self.change_y

class Ellipse(Rectangle):

    def draw(self, screen):
        pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])



#Loop until user clicks close
done = False

#Manage how fast screen updates
clock = pygame.time.Clock()

my_list = []
for i in range(10):

    my_object = Rectangle()
    my_object.x = randrange(0, 701)
    my_object.y = randrange(0, 501)
    my_object.change_x = randrange(-3, 3)
    my_object.change_y = randrange(-3, 3)
    my_object.width = randrange(20, 71)
    my_object.height = randrange(20, 71)
    my_object.color = [0, 255, 0]
    my_list.append(my_object)

for i in range(10):

    my_ellipse = Ellipse()
    my_list.append(my_ellipse)

#Main Program Loop
while not done:
    #Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    #Game logic goes here

    #Screen clearing code or background image goes here
    screen.fill(black)

    #drawing code goes here
    for my_object in my_list:
        my_object.draw(screen)
        my_ellipse.draw(screen)
        my_object.move()


    #update and display drawn screen
    pygame.display.flip()

    #limit to 60 frames per second
    clock.tick(60)

#close the window and quit
pygame.quit()