我在使用 pygame 时遇到了一些问题。看,我有一个名为 blockClass 的类,但我不太确定如何创建 blockClass 的多个实例,而不创建几十个对象。有没有办法可以实例化这个类?
这是我的代码:
#imports
import pygame, sys
from pygame.locals import *
#init game
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.key.set_repeat(True)
clock = pygame.time.Clock()
#classes
class playerClass:
x = 100
y = 100
boundingBox_X = 32
boundingBox_Y = 32
sprite = pygame.image.load("Image2.png").convert_alpha()
class blockClass:
x = 0
y = 0
boundingBox_X = 32
boundingBox_y = 32
sprite = pygame.image.load("Image1.png").convert_alpha()
#objects
playerObject = playerClass()
block = blockClass()
#object variables
#colors
white = 255,255,255
#main loop
quitting = False
while quitting == False:
for event in pygame.event.get():
#event processing
if event.type == pygame.QUIT:
quitting = True
#drawing
screen.fill((white))
screen.blit(playerObject.sprite,(playerObject.x,playerObject.y))
screen.blit(blockObject.sprite,(blockObject.x,blockObject.y))
pygame.display.flip()
#ending
clock.tick(60)
谢谢你的帮助!