如何在pygame中水平重复绘图(以类方法完成绘图)?

时间:2018-12-10 17:33:33

标签: python class inheritance pygame

这是我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()

1 个答案:

答案 0 :(得分:0)

您可以使用嵌套的for循环来创建面网格。修改__init__方法,使其采用xy坐标,并将它们分配给self.xself.y。将Face实例放入列表中而不是一直在主while循环中重新创建它们也是一个好主意。

class Face:
    def __init__(self, x, y):  # Pass the x, y coordinates ...
        self.x = x  # and assign them.
        self.y = y

faces = []  # Put all face instances into this group.
for x in range(0, size[0], 60):  # 0, 60, 120, ..., up to size[0] (exclusive).
    for y in range(0, size[1], 60):
        faces.append(Face(x, y))  # Append the next face instance.


while not done:
    # ...
    # Iterate over the faces to draw them.
    for face in faces:
        face.head(screen)
        face.eyes(screen)
        face.mouth(screen)