Пример #1
0
    def __init__(self, asteroids_amount):
        # Calls the screen and sets its borders
        self.__screen = Screen()
        self.__screen_max_x = Screen.SCREEN_MAX_X
        self.__screen_max_y = Screen.SCREEN_MAX_Y
        self.__screen_min_x = Screen.SCREEN_MIN_X
        self.__screen_min_y = Screen.SCREEN_MIN_Y

        # Randomizes coordinates for the ship in each of the axis
        random_x = random.randint(self.__screen_min_x, self.__screen_max_x)
        random_y = random.randint(self.__screen_min_y, self.__screen_max_y)
        self.__ship = Ship((random_x, random_y), (0, 0), 0)  # Our ship
        self.__asteroid_list = []  # The list of asteroids the are in the game
        # Handles the initialization of the asteroids when the game starts
        for current_asteroid_i in range(asteroids_amount):
            location = random.randint(self.__screen_min_x,
                                      self.__screen_max_x), random.randint(
                                          self.__screen_min_x,
                                          self.__screen_max_x)
            velocity = random.choice(SPEED_RANGE), random.choice(SPEED_RANGE)
            asteroid = Asteroid(location, velocity, INITIAL_AS_SIZE)
            while asteroid.has_intersection(self.__ship):
                asteroid = Asteroid(location, velocity, INITIAL_AS_SIZE)
            self.__asteroid_list.append(asteroid)
        for asteroid in self.__asteroid_list:
            self.__screen.register_asteroid(asteroid, INITIAL_AS_SIZE)
        self.__torpedo_list = []  # The list which stores shouted torpedoes
        self.__points = 0  # stores the points of the player
        self.__screen.set_score(self.__points)  # Register the intialized
    def __init__(self, asteroids_amnt):
        """
        Constructor
        :param asteroids_amnt: The amount of asteroids the game will start with
        """
        self.__screen = Screen()
        # every game has one screen object he works with

        self.screen_max_x = Screen.SCREEN_MAX_X
        self.screen_max_y = Screen.SCREEN_MAX_Y
        self.screen_min_x = Screen.SCREEN_MIN_X
        self.screen_min_y = Screen.SCREEN_MIN_Y

        self.__ship = Ship(self.__screen, self.get_rand_location())
        # every game contains only one ship object

        # initiation of asteroids objects
        self.__asteroids = []
        for asteroid in range(asteroids_amnt):
            while True:
                # this loop makes sure that the asteroid is not generated
                # at the current location of the ship
                asteroid = Asteroid(self.__screen, self.get_rand_location(),
                                    GameRunner.START_AST_SIZE)
                if not asteroid.has_intersection(self.__ship):
                    break
            self.__asteroids.append(asteroid)

        self.__torpedos = []

        self.__score = 0
Пример #3
0
 def __generate_asteroids(self, size):
     while len(self.__asteroids_lst) < DEFAULT_ASTEROIDS_NUM:
         # TODO: edit asteroid speeds
         possible_asteroid_speeds = [
             speed for speed in range(-4, 4) if speed != 0
         ]
         now_asteroid = Asteroid(
             randint(self.__screen_min_x, self.__screen_max_x),
             randint(self.__screen_min_y, self.__screen_max_y),
             choice(possible_asteroid_speeds),
             choice(possible_asteroid_speeds), size)
         if not now_asteroid.has_intersection(self.__ship):
             self.__asteroids_lst.append(now_asteroid)
             self.__screen.register_asteroid(now_asteroid, size)
Пример #4
0
 def __create_asteroid(self, size):
     """
     This method creates one asteroid in the game. Coordinates and speed
     are chosen randomly from constant parameters of the class GameRunner.
     :param size: integer - size of the asteroid that we gonna create
     :return: object of type Asteroid - new asteroid in the game
     """
     loc_x = rndm.randint(self.screen_min_x, self.screen_max_x)
     loc_y = rndm.randint(self.screen_min_y, self.screen_max_y)
     speed_x = rndm.choice(self.ASTEROID_SPEEDS_LST)
     speed_y = rndm.choice(self.ASTEROID_SPEEDS_LST)
     asteroid = Asteroid(loc_x, speed_x, loc_y, speed_y, size)
     while asteroid.has_intersection(self.ship):  # check the place if it's
         # possible to draw asteroid there
         loc_x = rndm.randint(self.screen_min_x, self.screen_max_x)
         loc_y = rndm.randint(self.screen_min_y, self.screen_max_y)
     asteroid = Asteroid(loc_x, speed_y, loc_y, speed_y, size)
     return asteroid
 def add_asteroids(self):
     """
     The following adds astorid
     to the current game,
     it adds the amount of astroid
     which was given in the constructor
     :return: None
     """
     for i in range(self.__asteroids_amount):
         asteroid_point = self.random_point()
         speed = Speed2D(random.randint(1, 4), random.randint(1, 4))
         new_asteroid = Asteroid(asteroid_point, speed, 3)
         while new_asteroid.has_intersection(self._ship):
             asteroid_point = self.random_point()
             speed = random.randint(1, 4)
             new_asteroid = Asteroid(asteroid_point, speed, 3)
         self.__asteroids.append(new_asteroid)
         self.__screen.register_asteroid(new_asteroid, 3)
         self.__screen.draw_asteroid(new_asteroid, asteroid_point.get_x(),
                                     asteroid_point.get_y())
Пример #6
0
 def add_asteroids(self, asteroids_amount):
     ast = set()
     ast_num = 0
     while ast_num < asteroids_amount:
         x = randint(-500, 500)
         while True:
             x_speed = randint(-4, 4)
             if x_speed != 0:
                 break
         y = randint(-500, 500)
         while True:
             y_speed = randint(-4, 4)
             if x != 0:
                 break
         current_ast = Asteroid(x, y, x_speed, y_speed)
         if not current_ast.has_intersection(self.__x_wing):
             self.__screen.register_asteroid(current_ast, 3)
             ast_num += 1
             ast.add(current_ast)
     self.__asteroids = ast
 def __add_asteroids(self, asteroids_amount):
     """
     Adds an amount of asteroids objects with random coordinates, random
     speed and default size, make sure that their is enough distance between
     the asteroids and the ship, and put the asteroids on the screen.
     :param asteroids_amount: the amount of the asteroids you want to add to
     the game.
     :return: None
     """
     for ast in range(asteroids_amount):
         x = random.randint(self.__screen_min_x, self.__screen_max_x)
         y = random.randint(self.__screen_min_y, self.__screen_max_y)
         x_speed = random.randint(self.AST_MIN_SPEED, self.AST_MAX_SPEED)
         y_speed = random.randint(self.AST_MIN_SPEED, self.AST_MAX_SPEED)
         asteroid_to_add = Asteroid(x, x_speed, y, y_speed, DEF_AST_SIZE)
         # The while loop makes sure that the asteroid is not on the ship.
         while asteroid_to_add.has_intersection(self.__ship):
             x = random.randint(self.__screen_min_x, self.__screen_max_x)
             y = random.randint(self.__screen_min_y, self.__screen_max_y)
             asteroid_to_add = Asteroid(x, x_speed, y, y_speed,
                                        DEF_AST_SIZE)
         self.__add_ast_on_screen(asteroid_to_add)
Пример #8
0
 def __init__(self, asteroids_amnt):
     self._screen = Screen()
     self.screen_max_x = Screen.SCREEN_MAX_X
     self.screen_max_y = Screen.SCREEN_MAX_Y
     self.screen_min_x = Screen.SCREEN_MIN_X
     self.screen_min_y = Screen.SCREEN_MIN_Y
     place_x = random.randrange(1, self.screen_max_x)
     place_y = random.randrange(1, self.screen_max_y)
     self.ship1 = sh(place_x, place_y)
     self.asteroids_amnt = asteroids_amnt
     self.ast_lst = []
     self.torp_lst = []
     i = 0
     self.__value = 0
     #creat asteroid
     while i < asteroids_amnt:
         place_x = random.randrange(1, self.screen_max_x)
         place_y = random.randrange(1, self.screen_max_y)
         ast = Asteroid(place_x, place_y, ASTERIODS_SIZE)
         if ast.has_intersection(self.ship1) == False:
             self.ast_lst.append(ast)
             self._screen.register_asteroid(self.ast_lst[i], ASTERIODS_SIZE)
             i += 1