def __init__(self, screen, img, stats): self.screen = screen if isinstance(img, str): self.img = pygame.image.load(img) else: self.img = img self.img = pygame.transform.scale(self.img, (int( self.screen.get_height() / 2), int(self.screen.get_height() / 2))) self.unit = Unit(self.screen, self.img, showRPM=False) self.unit.position = ( (((self.screen.get_width() / 2) - self.unit.width) / 2) + (self.unit.width / 2), ((self.screen.get_height() - self.unit.height) / 2) + (self.unit.height / 2), ) self.unit.angularVelocity = 100 self.unit.angularFriction = 0 # self.unit.Equip(WeaponFireStick(self.screen, self.unit)) self.weaponsTypes = [ WeaponFireStick, WeaponShield, ] # init self.current = 0 self.ready = False self.lastInteraction = pygame.time.get_ticks() self.previewUnits = { # type(weapon): Unit() } surf = pygame.Surface(self.unit.size) pygame.draw.circle(surf, (100, 100, 100), surf.get_rect().center, surf.get_width() / 2) for weaponType in self.weaponsTypes: unit = Unit(self.screen, surf, showRPM=False, bounceOffWalls=False) unit.angularFriction = 0 unit.angularVelocity = 100 unit.Equip(weaponType(self.screen, unit)) self.previewUnits[weaponType] = unit
class Equip: def __init__(self, screen, img, stats): self.screen = screen if isinstance(img, str): self.img = pygame.image.load(img) else: self.img = img self.img = pygame.transform.scale(self.img, (int( self.screen.get_height() / 2), int(self.screen.get_height() / 2))) self.unit = Unit(self.screen, self.img, showRPM=False) self.unit.position = ( (((self.screen.get_width() / 2) - self.unit.width) / 2) + (self.unit.width / 2), ((self.screen.get_height() - self.unit.height) / 2) + (self.unit.height / 2), ) self.unit.angularVelocity = 100 self.unit.angularFriction = 0 # self.unit.Equip(WeaponFireStick(self.screen, self.unit)) self.weaponsTypes = [ WeaponFireStick, WeaponShield, ] # init self.current = 0 self.ready = False self.lastInteraction = pygame.time.get_ticks() self.previewUnits = { # type(weapon): Unit() } surf = pygame.Surface(self.unit.size) pygame.draw.circle(surf, (100, 100, 100), surf.get_rect().center, surf.get_width() / 2) for weaponType in self.weaponsTypes: unit = Unit(self.screen, surf, showRPM=False, bounceOffWalls=False) unit.angularFriction = 0 unit.angularVelocity = 100 unit.Equip(weaponType(self.screen, unit)) self.previewUnits[weaponType] = unit def GetSelection(self): d = defaultdict(lambda: 0) for equip in self.unit.equipment: for key in ['attack', 'defense', 'spin']: stats = equip.GetStatDelta() d[key] += stats[key] return self.img, d, [type(e) for e in self.unit.equipment] def Event(self, event): if event.type == pygame.KEYDOWN: self.lastInteraction = pygame.time.get_ticks() if event.key in [pygame.K_KP_ENTER, pygame.K_RETURN]: self.ready = True elif event.key == pygame.K_SPACE: self.unit.ClearEquipment() print('current=', self.current) print('wetypes=', self.weaponsTypes) if 0 <= self.current <= len(self.weaponsTypes) - 1: try: weaponType = self.weaponsTypes[self.current] self.unit.Equip(weaponType(self.screen, self.unit)) except Exception as e: print(e) elif event.key in [pygame.K_UP, pygame.K_DOWN]: if event.key == pygame.K_UP: self.current += 1 elif event.key == pygame.K_DOWN: self.current -= 1 def Draw(self, clock): if pygame.time.get_ticks() - self.lastInteraction > 5000: # self.ready = True pass self.screen.fill((0, 0, 0)) self.unit.ApplyPhysics(clock) words = pygame.font.Font(None, int( self.screen.get_height() / 15)).render( f'Press SPACEBAR to equip. Press ENTER to start the battle.', True, (255, 255, 255), ) self.screen.blit(words, ( int((self.screen.get_width() - words.get_width()) / 2), int(self.screen.get_height() - words.get_height()), )) # draw the weapons x = self.screen.get_rect().centerx + ( (self.screen.get_width() - self.screen.get_rect().centerx) - self.unit.width) / 2 spacing = 200 weaponTypes = sorted(list(self.previewUnits.keys()), key=lambda t: t.NAME) for index, weaponType in enumerate(weaponTypes): previewUnit = self.previewUnits[weaponType] previewUnit.position = x, self.screen.get_rect().centery + ( spacing * (self.current - index)) previewUnit.Draw(clock) if index == self.current: words = pygame.font.Font(None, 36).render( f'{previewUnit.equipment[0].NAME}', True, (255, 255, 255)) self.screen.blit(words, previewUnit.topright) # draw the unit preview self.unit.Draw(clock)