我正试图通过以下方式在屏幕上创建新对象:
spawnedObjectDict = dict()
while True: # main loop
if mouseClicked == True:
RectangleName = "Rectangle" + str((len(spawnedObjectDict)))
spawnedObjectDict[RectangleName] = SpawnedRectangle
spawnedObjectDict[RectangleName].positionX = mouseX
spawnedObjectDict[RectangleName].positionY = mouseY
这应该是创建新对象并为它们指定与鼠标相等的坐标。但是,它会不断地为它们分配新的鼠标坐标,所以它们只是堆叠在一起。起初,我以为它只是画了一个,或者因为某种原因,dict中只有一个对象,但我添加了这个来确保:
def drawRectCoords(RectName, theDict, x, y, size_x, size_y):
for i in iter(theDict):
BASICFONT = pygame.font.Font('freesansbold.ttf', 20)
textSurf = BASICFONT.render(str(theDict['Rectangle0'].positionX) + ", " + \
str(theDict['Rectangle0'].positionY), True, (255, 255, 255), (0, 0, 0))
textRect = textSurf.get_rect()
textRect.topleft = (x, y)
textSurf2 = BASICFONT.render(str(len(theDict)) + ", " + RectName, True, (255, 255, 255), (0, 0, 0))
textRect2 = textSurf2.get_rect()
textRect2.topleft = (150, (20*len(theDict)))
DISPLAYSURF.blit(textSurf, textRect)
DISPLAYSURF.blit(textSurf2, textRect2)
果然,矩形0的坐标每次都在变化,但是textSurf2每次都会更新,以显示矩形名称在变化,spawnedObjectDict的长度在增加。你知道吗
要创建
SpawnedRectangle
的新实例,请执行以下操作:您当前所做的是将类
SpawnedRectangle
分配给字典中的不同键。你知道吗相关问题 更多 >
编程相关推荐