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

标签 python list class loops pygame

I'm working on this Chapter 12 Classes Exercise Step #11 in Program Arcade games and I'm having trouble displaying 10 ellipses which I'm trying to do through the parent class of Rectangle. I'm just getting 10 rectangles right now. I know the problem is either in lines 34-37, 60-62, or 78-81. The whole problem is laid out in the link below

http://www.programarcadegames.com/index.php?chapter=lab_classes_and_graphics&lang=en

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()

最佳答案

您在技术上展示的代码确实绘制了10个椭圆,但它们是零乘零像素的,所以您实际上看不到任何东西。

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

关于python - 如何让这个程序在 Python 中绘制 10 个椭圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34445718/

相关文章:

Java + 运算符行为未按预期继承

java - 重复值

c# - CodedUI [测试方法] 与 [CodedUITest]

python - Kivy错误(python 2.7): sdl2 import error

python - 导入错误: cannot import name 'SkipWrapper'

c# - datagridview 数据列表,然后将列表框绑定(bind)到列表,不起作用?

python - 使用给定项目拆分列表

javascript - 使用 Jscript 创建列表菜单运行时

python - 运行 subprocess.call 会导致错误

python - 在 Python 中通过 TCP 套接字发送 gzip 压缩数据