def angle(self, angle: float) -> None: """ Change angle of the cannon. Change the angle by value of angle :param angle: value of changed angle """ if Config( )['max_shooting_angle'] > angle + self.shooting_angle > Config( )['min_shooting_angle']: self.shooting_angle += angle
def __get_random_position(self): x_from = 150 x_to = Config()['windows_size'][0] - 50 y_from = 150 y_to = Config()['windows_size'][1] - 50 position = Position(random.randint(x_from, x_to), random.randint(y_from, y_to)) return position
def rescale(number: float) -> float: """ Rescale size of progress bar :param number: :return: """ result = (((MAX_PROGRESS - MIN_PROGRESS) * (number - Config()['cannon_min_strength'])) / (Config()['cannon_max_strength'] - Config()['cannon_min_strength'])) + MIN_PROGRESS return result
def strength(self, value: float): """ Cannon strength setter, min and max defined by config cannon_min_strength, cannon_max_strength :param value: value of new strength """ if value < Config()['cannon_min_strength']: self.__strength = Config()['cannon_min_strength'] elif value > Config()['cannon_max_strength']: self.__strength = Config()['cannon_max_strength'] else: self.__strength = value
def shoot(self) -> tuple: """ Shoot one missile :return: tuple with only one missile """ x = self.__cannon.position.x_position + 105 y = self.__cannon.position.y_position + 55 - self.__cannon.shooting_angle pygame.mixer.Channel(0).play(pygame.mixer.Sound(os.path.join(Config()['root_dir'], 'resources', 'sounds', 'shoot.wav'))) missile = CreationFactory().create_missile(Position(x, y), self.__cannon.strength, self.__cannon.shooting_angle) return missile,
def move(self, angle, distance) -> None: """ Move the cannon by distance in direction of angle + -> up - -> down :param angle: +/- 90 up or down :param distance: distance of move """ if 10 < self.position.y_position + ( distance * -angle / 90) < Config()['windows_size'][1] - 100: self.position.move(angle, distance)
def initialize(self): """ Set up the pygame graphical display and loads graphical resources. """ # result = pygame.init() pygame.font.init() pygame.display.set_caption('Angry tux') self.__screen = pygame.display.set_mode(Config()['windows_size']) self.__clock = pygame.time.Clock() self.__screen.fill((255, 255, 255)) pygame.display.update()
def out_of_window(self) -> bool: """ Check, if position is out of window :return: True if out of window, False otherwise """ x, y = Config()['windows_size'] if self.__x_position < 0: return True if self.__x_position > x or self.__y_position > y: return True return False
def move(self) -> bool: """ Move enemy across the screen down and up :return: True """ x, y = self._enemy.position.to_rect() # y += self.__operator if y > Config()['windows_size'][1] - 20 or y < 70: self.__operator = -self.__operator self._enemy.position.move(self.__operator, 3) return True
def run(): parser = argparse.ArgumentParser(description='AngryTux game') parser.add_argument('-l', '--level', dest='level', action='store', default=1, help='Set play level') parser.add_argument('-f', '--factory', dest='factory', action='store', default='simple', help='Set creation factory (simple/realistic)') args = parser.parse_args() # init pygame pygame.mixer.pre_init(44100, -16, 2, 512) pygame.mixer.init() pygame.init() pygame.key.set_repeat(300, 40) # Init controller controller = Keyboard() # Init view view = View() view.initialize() if args.factory == 'simple': CreationFactory(SimpleFactory()) elif args.factory == 'realistic': CreationFactory(RealisticFactory()) else: print('Please use only simple or realistic mode, if you want some more, you can create it') quit(10) # Init model model = GameModel() if int(args.level) == 1: model.model_builder(Level1Creator()) elif int(args.level) == 2: model.model_builder(Level2Creator()) else: print('Only 1 and 2 level is complete, you can create some more!') quit(10) clock = pygame.time.Clock() while 1: model.tick() controller.tick() clock.tick(Config()['fps']) / 1000
def shoot(self) -> tuple: """ Shoot two missiles from cannon. Each another angle :return: tuple of two missiles """ x = self.__cannon.position.x_position + 105 y = self.__cannon.position.y_position + 55 - self.__cannon.shooting_angle pygame.mixer.Channel(0).play( pygame.mixer.Sound( os.path.join(Config()['root_dir'], 'resources', 'sounds', 'shoot.wav'))) missile1 = CreationFactory().create_missile( Position(x, y), self.__cannon.strength, self.__cannon.shooting_angle - 20) missile2 = CreationFactory().create_missile( Position(x, y), self.__cannon.strength, self.__cannon.shooting_angle + 20) return missile1, missile2
def get_background(self): size = Config()['windows_size'] image_name = 'background.jpg' return size, self.__return_cached('background', image_name, size)
import os import pygame from angrytux.config.Config import Config IMAGES_PATH = os.path.join(Config()['root_dir'], 'resources', 'images') def load_image(image: str, size: tuple): """ Function for load an image :param image: image name :param size: size of image :return: loaded and resized image object """ tux = pygame.image.load(os.path.join(IMAGES_PATH, image)) tux = pygame.transform.scale(tux, size) return tux class ImageLoader: """ Class for loading images """ def __init__(self): self.__loaded = {} def __return_cached(self, name: str, image_name: str, size: tuple): """
def __init__(self, position: Position): super().__init__(position) self.__shooting_angle = Config()['cannon_initial_shooting_angle'] self.__strength = Config()['cannon_initial_strength'] self.__state = SingleShoot(self)
def test_strength_setter_min_value(): cannon = Cannon(Position(50, 50)) assert cannon.strength == Config()['cannon_initial_strength'] cannon.strength = 0 assert cannon.strength == Config()['cannon_min_strength']
import pygame import pytest from angrytux.config.Config import Config from angrytux.model.abstract_facotry.CreationFactory import CreationFactory from angrytux.model.abstract_facotry.RealisticFactory import RealisticFactory from angrytux.model.game_objects.Cannon import Cannon from angrytux.model.game_objects.Position import Position INIT_ANGLE = Config()['cannon_initial_shooting_angle'] def test_cannon_moving(): cannon = Cannon(Position(50, 50)) old_position = cannon.position.to_rect() cannon.move(90, 10) assert old_position != cannon.position.to_rect() def test_cannon_change_state(utils): cannon = Cannon(Position(50, 50)) old_state = cannon.state cannon.change_state() new_state = cannon.state assert not utils.compare_classes(old_state, new_state)