object - Python dict entries not being referenced properly -
i'm trying create new objects on screen new data way:
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
this should creating new objects , assigning them coordinates equal mouse. however, keeps assigning of them new mouse coordinates, stack on top of each other. @ first assumed drawing one, or 1 object in dict reason, added make sure:
def drawrectcoords(rectname, thedict, x, y, size_x, size_y): 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)
sure enough, coordinates of rectangle0 changing each time, textsurf2 updates each time show rectanglename changing , length of spawnedobjectdict increasing.
to create new instance of spawnedrectangle
, this:
spawnedobjectdict[rectanglename] = spawnedrectangle()
what doing assigns class spawnedrectangle
different keys in dictionary.
Comments
Post a Comment