Aug-15-2019, 03:37 AM
I made an inventory class and was going to test it, but it doesn't draw very well. Because items are dragged around, the background has to be updated as well as the inventory boxes being drawn and doing some checks. It just doesn't work. I need a way to run my code more smoothly. Thx for any suggestions.
Draw function for the inventory; this one is most open for improvement -
Draw function and CalculatePos function for item class -
The stage class's draw function looks like this, but I don't think it can be improved
Draw function for the inventory; this one is most open for improvement -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def draw( self ): for slotY in range ( 1 , 4 ): for slotX in range ( 1 , 4 ): slotNum = slotX + ((slotY - 1 ) * 3 ) item = self .items[slotNum] #Dictionary to keep track of slots and what they are holding x = 50 + (slotX * 55 ) y = 300 + (slotY * 55 ) if item ! = None and self .carrying = = None : #self.carrying is the item being dragged item.draw() function = item.PickUp #Will change the item's slot value to None elif self .carrying ! = None : if item ! = None : item.draw() function = partial( self .SwapItems, item, self .carrying) #Will switch around the slot values to switch the items else : function = partial( self .PutDown, self .carrying, slotNum) #Change the item's slot number to the slot clicked on slotBtn = BT([x, y, 52 , 52 ], '', 16 , [ 55 , 55 , 55 ]) slotBtn.start(Screen, [ 85 , 85 , 85 ], [ 115 , 115 , 115 ], [function]) #Runs the button which includes drawing and checking for clicks |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def draw( self ): if self .slot = = None : #item's slot number mouse = pygame.mouse.get_pos() pos = [mouse[ 0 ] - 25 , mouse[ 1 ] - 25 ] #Center the thumbnail on the mouse else : pos = self .CalculatePos() #Get positions from the slot number Screen.blit(PD[ self .weapon + 'Thumbnail' ], pos) def CalculatePos( self ): slot = self .slot y = 1 count = 0 while slot - 3 > count: count + = 3 y + = 1 x = slot - ((y - 1 ) * 3 ) return 50 + (x * 55 ), 300 + (y * 55 ) |
1 2 3 4 |
def draw( self ): Screen.fill( self .background) for obst in self .obst: #There are at least 32 obstacles on each stage obst.draw() |