def draw(self, surface, player_money): if self.upgrades >= self.max_upgrades and self.max_upgrades != 0: text1, text1_pos = FONT.render( f'{self.text}: {convert_to_prefix(round(getattr(self.class_instance, self.attribute_attached), 4))}{self.attribute_extra} |', (255, 255, 255), size=30) text2, text2_pos = FONT.render(f' UPGRADE COST: MAX LVL', (255, 0, 0), size=30) else: text1, text1_pos = FONT.render( f'{self.text}: {convert_to_prefix(round(getattr(self.class_instance, self.attribute_attached)), 4)}{self.attribute_extra} |', (255, 255, 255), size=30) text2, text2_pos = FONT.render( f' UPGRADE COST: ${self.upgrade_cost_text}', (0, 255, 0) if player_money >= self.upgrade_cost else (255, 0, 0), size=30) self.rect = pygame.Rect( self.x, self.y, text2_pos.width + self.gap * 2 + text1_pos.width + self.gap + 10, 50) pygame.draw.rect(surface, (0, 0, 0), self.rect) pygame.draw.rect(surface, (255, 255, 255), self.rect, 4) surface.blit(text1, (self.x + self.gap * 2, self.y + self.rect.height / 2 - text1_pos.height / 2)) surface.blit(text2, (self.x + text1_pos.width + self.gap * 2, self.y + self.rect.height / 2 - text2_pos.height / 2))
def show_message(self, message, rect): #Set the game's messages in bottom strip self.window.fill((0, 0, 0), rect) text = message text_render = FONT.render(text, True, (0, 255, 0)) self.window.blit(text_render, rect) pygame.display.update(rect)
def draw_text(self, surface): # Text drawing if self.attached_text != '': font_surface, font_pos = FONT.render(self.attached_text, size=25, fgcolor=(255, 255, 255)) surface.blit(font_surface, (self.x - font_pos.width / 3, self.y - 25))
def __init__(self, pos, text, class_instance, attribute_attached, attribute_ratio, attribute_type, upgrade_cost, upgrade_ratio, upgrade_type, attribute_extra='', gap=15, max_upgrades=0): self.x, self.y = pos self.text = text self.class_instance = class_instance self.attribute_attached = attribute_attached self.attribute_ratio = attribute_ratio self.attribute_type = attribute_type self.upgrade_cost = float(upgrade_cost) self.upgrade_cost_text = convert_to_prefix(self.upgrade_cost) self.upgrade_ratio = float(upgrade_ratio) self.upgrade_type = upgrade_type self.attribute_extra = attribute_extra self.gap = gap self.upgrades = 0 self.max_upgrades = max_upgrades _, text1_pos = FONT.render( f'{self.text}: {getattr(self.class_instance, self.attribute_attached)}{self.attribute_extra}', (255, 255, 255), size=30) _, text2_pos = FONT.render(f'UPGRADE COST: ${self.upgrade_cost_text}', (255, 255, 255), size=30) self.rect = pygame.Rect(self.x + text1_pos.width + self.gap, self.y, text2_pos.width + self.gap * 2, 50)
def draw(self, surface): if self.border is True: pygame.draw.rect(surface, 0, self.rect) pygame.draw.rect(surface, (255, 255, 255), self.rect, 4) font, font_pos = FONT.render(f'{self.text} {self.cost_text}', fgcolor=(255, 255, 255), size=30) surface.blit( font, (self.rect.x - font_pos.width / 2 + self.rect.width / 2, self.rect.y - font_pos.height / 2 + self.rect.height / 2))
def adjust_text(self): text_split = self.text.split(' ') offset_x, offset_y = 75, 45 self.surfaces = [] self.surfaces_pos = [] for x in text_split: surface, pos = FONT.render(x, size=25, fgcolor=(25, 25, 25)) if offset_x + pos.width > self.width - 30: offset_x = 25 offset_y += 40 self.surfaces.append(surface) self.surfaces_pos.append((self.x + offset_x, self.y + offset_y - pos.height)) offset_x += 15 offset_x += pos.width
def write(text, y): surface.blit(FONT.render(text, True, COL_PLAYER), (0, y))
def shop_screen2(surface, tick, clicked): shop2 = True shop1 = False def not_enough(): hitmarkers_shop.clear() hitmarkers_shop.append( HitMarker(pos=pygame.mouse.get_pos(), indicator=0, extra='NOT ENOUGH MONEY!', color=(255, 0, 0), size=30, angle_variation=15)) def enough(): hitmarkers_shop.clear() hitmarkers_shop.append( HitMarker(pos=pygame.mouse.get_pos(), indicator=0, extra='BOUGHT!', color=(0, 255, 0), size=40, angle_variation=15)) def max_level(): hitmarkers_shop.clear() hitmarkers_shop.append( HitMarker(pos=pygame.mouse.get_pos(), indicator=0, extra='MAX LVL!', color=(255, 0, 0), size=40, angle_variation=15)) if clicked: if back_button.clicked(): shop1 = True shop2 = False for button in buttons_p2: if button.clicked(): money = button.upgrade(money=player.money) if money is False: max_level() elif money != player.money: player.money = money enough() else: not_enough() # Move for hitmarker in hitmarkers_shop: hitmarker.move() # Draw Screen surface.blit(BACKGROUND_IMG, (0, 0)) back_button.draw(surface) font, font_pos = FONT.render(f'Money: ${convert_to_prefix(player.money)}', fgcolor=(255, 255, 255), size=30) surface.blit( font, (WINDOW_WIDTH - font_pos.width - font_pos.height, font_pos.height)) for button in buttons_p2: button.draw(surface, player.money) for i, hitmarker in enumerate(hitmarkers_shop): hitmarker.draw(surface) if hitmarker.delay < 0: hitmarkers_shop.pop(i) pygame.display.flip() return tick, shop1, shop2
def game(surface, tick, clicked): cheats = True shop_background.current_img = 0 shop1 = False player.repair = False if player.health_bar.health < player.health_bar.max_health: player.repair = True hive.attempt_enemy_spawn(tick) if pygame.key.get_pressed()[pygame.K_d]: player.right() if pygame.key.get_pressed()[pygame.K_a]: player.left() if pygame.key.get_pressed()[pygame.K_z] and cheats: player.money += 1e6 if pygame.key.get_pressed()[pygame.K_SPACE] and player.delay1 == 0: player.shoot() player.delay1 += player.delay1_delay elif player.delay1 != 0: player.delay1 -= 1 for i, enemy in enumerate(hive.enemies): for j, bullet in enumerate(player.bullets): if bullet.get_rect().colliderect(enemy.get_rect()): player.bullets.pop(j) hitmarkers.append( HitMarker(pos=(bullet.x + bullet.width / 2, bullet.y + bullet.height / 2), indicator=-bullet.damage, color=(255, 255, 0), crit=bullet.crit, crit_color=(255, 0, 0), decimal=False)) try: dead = hive.enemies[i].hit(bullet.damage) if dead: if enemy.spaceship_type == 0: for _ in range(randint(1, 5)): money_orbs.append( Orb((enemy.x + enemy.width / 2, enemy.y + enemy.height / 2), player, reward=enemy.reward, reward_ratio=hive.reward_ratio, deviation=5)) hive.enemies.pop(i) except IndexError: pass if enemy.get_rect().colliderect(player.get_rect()): player.hit(1) hitmarkers.append( HitMarker(pos=(player.x + player.width / 2, player.y + player.height / 2), indicator=-1, color=(255, 0, 0), spread=80, decimal=False)) dead = hive.enemies[i].hit(1) if dead: hive.enemies.pop(i) for i, bullet in enumerate(player.bullets): if bullet.chase: best_distance = 10e9 enemy_to_attack = None for j, enemy in enumerate(hive.enemies): x1, y1 = bullet.x, bullet.y x2, y2 = enemy.x + enemy.width / 2, enemy.y + enemy.height / 2 if distance_two_points(x1, y1, x2, y2) < best_distance: best_distance = distance_two_points(x1, y1, x2, y2) enemy_to_attack = j if enemy_to_attack is not None: temp_enemy = hive.enemies[enemy_to_attack] bullet.set_target(temp_enemy.x + temp_enemy.width / 2, temp_enemy.y + temp_enemy.height / 2) else: bullet.set_target() for i, orb in enumerate(money_orbs): cash_hitbox = player.get_rect() cash_hitbox.x += cash_hitbox.width / 2 cash_hitbox.y += cash_hitbox.height / 2 cash_hitbox.width /= 4 cash_hitbox.height /= 4 if orb.get_rect().colliderect(cash_hitbox): hitmarkers.append( HitMarker(pos=(WINDOW_WIDTH - 150, 65), indicator=orb.reward, extra='$', color=(0, 255, 0), spread=40, y_vel=-1)) player.money += orb.reward money_orbs.pop(i) if clicked: if shop_button1.clicked(): shop1 = True if player.repair and repair_button.clicked( ) and (player.health_bar.max_health - player.health_bar.health) * player.repair_cost <= player.money: player.money -= (player.health_bar.max_health - player.health_bar.health) * player.repair_cost for i in range(player.health_bar.max_health - player.health_bar.health): hitmarkers.append( HitMarker(pos=(player.x + player.width / 2, player.y + player.height / 2), indicator=0, extra='+', color=(0, 255, 0), spread=40)) player.health_bar.health = player.health_bar.max_health player.repair = False # Move background.move() player.move() for hitmarker in hitmarkers: hitmarker.move() for enemy in hive.enemies: enemy.move() for orb in money_orbs: orb.move() # Draw Screen win.fill((0, 0, 0)) background.draw(surface) player.draw(surface) for i, enemy in enumerate(hive.enemies): enemy.draw(surface) if enemy.y > WINDOW_HEIGHT: hive.enemies.pop(i) for i, hitmarker in enumerate(hitmarkers): hitmarker.draw(surface) if hitmarker.delay < 0: hitmarkers.pop(i) for orb in money_orbs: orb.draw(surface) font, font_pos = FONT.render(f'Money: ${convert_to_prefix(player.money)}', fgcolor=(255, 255, 255), size=30) surface.blit( font, (WINDOW_WIDTH - font_pos.width - font_pos.height, font_pos.height)) shop_button1.draw(surface) if player.repair: repair_button.text = f' REPAIR (${(convert_to_prefix(player.health_bar.max_health - player.health_bar.health)*player.repair_cost)})' repair_button.draw(surface) pygame.display.flip() tick += 1 return tick, shop1