【发布时间】:2018-12-10 17:33:33
【问题描述】:
这是我 12 年级编码课的一项作业,重点是类和继承:
你要稍微改变一下。你的工作是使用基本形状来创建一些“面孔”。请记住,除了圆形和正方形之外,您还可以创建许多不同的基本形状。但是,一旦创建了面,您将创建继承形状但会创建新颜色的其他面的类。但是,与之前的分配不同,您的输出应该被组织,以便面部清晰地一行一行地排列,直到屏幕被填满。
问题
我正在尝试做的是制作几个形状的面孔,为每个部分设置半随机颜色,并在 pygame 中水平重复它们,并重复到下一行直到填满屏幕。我不是在寻找对所有事情的完整答案,所以关于我可以尝试的想法可能会奏效。我坚持可能创建一个循环,该循环将从我的 Face 类中获取当前 x 和 y 并将其添加到它,让它以更高的 x 值重复,在屏幕上重复面部。
到目前为止我所做的尝试
我画了第一张脸,让它出现在我想要的位置。我正在努力使用 __init__ 中的 x 和 y 坐标并更改它们。
当前代码
import pygame
from random import randint
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# defining random colors to work with
randomColor = (randint(0, 255), randint(0, 255), randint(0, 255))
randomColor1 = (randint(0, 255), randint(0, 255), randint(0, 255))
randomColor2 = (randint(0, 255), randint(0, 255), randint(0, 255))
randomColor3 = (randint(0, 255), randint(0, 255), randint(0, 255))
randomColor4 = (randint(0, 255), randint(0, 255), randint(0, 255))
randomColor5 = (randint(0, 255), randint(0, 255), randint(0, 255))
# creating first face (head, eyes, mouth) at the starting point 0, 0
class Face:
def __init__(self):
self.x = 0
self.y = 0
def head(self, screen):
pygame.draw.rect(screen, GREEN, (self.x, self.y, 50, 50))
def eyes(self, screen):
pygame.draw.ellipse(screen, randomColor1, [self.x + 5, self.y + 15, 10, 10])
pygame.draw.ellipse(screen, randomColor2, [self.x + 25, self.y + 15, 10, 10])
def mouth(self, screen):
pygame.draw.rect(screen, randomColor, (self.x + 15, self.y + 35, 30, 7))
# creating second face with more colors
class Face2(Face):
def head2(self, screen):
pygame.draw.ellipse(screen, randomColor3, [self.x + 5, self.y + 15, 10, 10])
def eyes2(self, screen):
pygame.draw.rect(screen, randomColor4, (self.x + 5, self.y, 10, 10))
pygame.draw.rect(screen, randomColor4, (self.x + 25, self.y, 10, 10))
def mouth2(self, screen):
pygame.draw.rect(screen, randomColor5, (self.x, self.y, 30, 7))
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Game logic should go here
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(WHITE)
# --- Drawing code should go here
my_face = Face()
my_face.head(screen)
my_face.eyes(screen)
my_face.mouth(screen)
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()
【问题讨论】:
标签: python class inheritance pygame