通过循环创建多个类实例

时间:2020-01-26 22:55:32

标签: python

我试图通过for循环在pygame中创建多个矩形,并使用名为XButton和YButtons的列表迭代矩形的位置。我当前面临的问题是Xbuttons [i]尚未定义,并且我不确定是什么原因造成的。

from _tkinter import *
import pygame
from math import *
import random

pygame.init()
width = 500
win = pygame.display.set_mode((width, width))

# Buttons
Buttons = ["Meow", "Woof"]
Buttonsname = ["Rufus", "Jonathan"]
XButtons = [10, 10]
YButtons = [10, 10]
rows = 30
columns = 30


class Bes(object):
    def __init__(self, name):
        self.name = name

    def click(self):
        pygame.draw.rect(win, (000, 255, 000), (XButtons[i], YButtons[i], 10, 10))
        print("Meow")


def Loop():
    for i in range(len(Buttons)):
        Buttons[i] = Bes(Buttonsname[i])
        print(Buttons[i].name)
        Buttons[i].click()


Loop()


def Mainfunction():
    run = True
    print(Buttons)
    while run:
        for event in pygame.event.get():
            pygame.display.update()
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()


Mainfunction()

0 个答案:

没有答案