Python Pygame:从不同模块绘制类多边形

Python Pygame:从不同模块绘制类多边形,python,pygame,Python,Pygame,历史记录: 我不明白我的代码有什么问题,第一次使用类创建图形时 我有一个模块,其中是多边形类,它有以下代码: 我有一个包含以下代码的内核: 您需要创建Polygon类的实例,而不仅仅是尝试直接调用该类上的方法: poly = Polygon() # call the class to create an instance; put this in your setup code poly.draw() # put this in your main loop 类仅为特定类型的对象定义“模板

历史记录: 我不明白我的代码有什么问题,第一次使用类创建图形时

我有一个模块,其中是多边形类,它有以下代码:

我有一个包含以下代码的内核:


您需要创建
Polygon
类的实例,而不仅仅是尝试直接调用该类上的方法:

poly = Polygon() # call the class to create an instance; put this in your setup code 

poly.draw() # put this in your main loop
类仅为特定类型的对象定义“模板”。要实际创建该类型的对象(或“实例”),必须调用该类并将完成其定义所需的任何数据作为参数传递。不将数据硬编码到类本身将允许您创建多个类实例,并且每个实例可以具有不同的属性,具体取决于创建时给定的值(例如颜色、形状、位置等)

考虑到这一点,我将更改您的代码如下。它仍然只有一个多边形,但现在只需多次调用
polygon()
并存储返回的实例,就可以轻松添加更多多边形,以便在必要时调用它们的
draw()
方法

文件
Objects.py

import pygame

class Polygon:
    def __init__(self, *puntos):
        self.puntos = puntos

    def draw(self, surface):
        """ Draw the shape on the surface. """
        pygame.draw.polygon(surface, (255, 0, 255), self.puntos)

    def cursors(self):
        pass
主要游戏模块文件:

from Objects import Polygon
import pygame
import sys

class Kernel:
    def __init__(self):
         pygame.init()
         self.screen_size = (800,  600)
         self.bg_color = (120, 120, 250)
         self.fps = 60
         self.screen = pygame.display.set_mode(self.screen_size)
         self.clock = pygame.time.Clock()
         self.polygon = Polygon((200, 30), (250, 80), (225, 130), (175, 130),
                                (150 ,80))  # define shape here, not in class

    def handle_input(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

    def update(self):
        capition = "Music Art - FPS: {:.2f}".format(self.clock.get_fps())
        pygame.display.set_caption(capition)

    def render(self):
        self.screen.fill(self.bg_color)

    def main_loop(self):
        while True:
            self.handle_input()
            self.update()
            self.render()
            self.polygon.draw(self.screen)
            pygame.display.flip()
            self.clock.tick(self.fps)

if __name__ == '__main__':
    module_game = Kernel()
    module_game.main_loop()
import pygame

class Polygon:
    def __init__(self, *puntos):
        self.puntos = puntos

    def draw(self, surface):
        """ Draw the shape on the surface. """
        pygame.draw.polygon(surface, (255, 0, 255), self.puntos)

    def cursors(self):
        pass
from Objects import Polygon
import pygame
import sys

class Kernel:
    def __init__(self):
         pygame.init()
         self.screen_size = (800,  600)
         self.bg_color = (120, 120, 250)
         self.fps = 60
         self.screen = pygame.display.set_mode(self.screen_size)
         self.clock = pygame.time.Clock()
         self.polygon = Polygon((200, 30), (250, 80), (225, 130), (175, 130),
                                (150 ,80))  # define shape here, not in class

    def handle_input(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

    def update(self):
        capition = "Music Art - FPS: {:.2f}".format(self.clock.get_fps())
        pygame.display.set_caption(capition)

    def render(self):
        self.screen.fill(self.bg_color)

    def main_loop(self):
        while True:
            self.handle_input()
            self.update()
            self.render()
            self.polygon.draw(self.screen)
            pygame.display.flip()
            self.clock.tick(self.fps)

if __name__ == '__main__':
    module_game = Kernel()
    module_game.main_loop()