def __update_velocity(self): self.velocity.x = self.velocity.x + ( self.acceleration.x * Time.delta_time(self.unscaled, self.game_object.time_scale)) self.velocity.y = self.velocity.y + ( (self.acceleration.y + self.gravity) * Time.delta_time(self.unscaled, self.game_object.time_scale))
def update(self): if Time.time_scale == 0.0: #Paused game. Adjust timers self.time_of_last_invencibily += Time.delta_time(True) difference_time = Time.now() - self.time_of_last_invencibily if difference_time > self.invecible_time: for i in range(2): self.player_controller.game_object_list[ i].is_invencible = False self.get_back_to_original_colors() self.current_animation_tick_index = 0 else: value = min(difference_time / self.invecible_time, 1) # Just to convert between 0 and 1 diff = abs( value - self.animation_ticks_times[self.current_animation_tick_index]) if (diff < 0.01): self.current_animation_tick_index += 1 self.tick_colors() for obstacle in self.game_object_list: if obstacle.transform.position.y > Constants.screen_height: self.game_object_list.remove(obstacle) obstacle.destroy(obstacle) GameObject.destroy(obstacle) else: self.fall(obstacle) self.delete_power_up_text()
def update(self): if (Time.now() - self.last_update_time) * Time.time_scale >= self.time_to_update_score: self.score = self.score + self.score_per_step self.last_update_time = Time.now() Constants.current_score = self.score self.game_object_list[0].text_mesh.message = str(int(self.score))
def start_game(cls, game_settings): """ Start the game coroutine with pygame :param game_settings: settings of the game """ cls.set_game_settings(game_settings) Time.start_coroutine(cls.game) Time.start_game()
def should_spawn(self): """ :return: if it should spawn """ if Time.now() - self.time > self.period: self.time = Time.now() return True else: return False
def __update_position(self): new_position = Vector2( self.transform.position.x + (self.velocity.x * Time.delta_time(self.unscaled, self.game_object.time_scale)), self.transform.position.y + (self.velocity.y * Time.delta_time(self.unscaled, self.game_object.time_scale))) self.transform.translate(new_position)
def increase_difficult(self): if Time.now() - self.last_increases_dificculty_time > self.time_to_increase_difficult \ and self.game_difficuty < self.max_difficult: self.game_difficuty += 1 self.last_increases_dificculty_time = Time.now() self.time_to_increase_difficult *= 1.03 self.generation_obstacle_difficult = (1 - (self.game_difficuty - 1) * 0.2 / self.max_difficult) self.generate_difficulty_text() if self.game_difficuty == 2: obstacle = MiddleRectObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) if self.game_difficuty == 3: obstacle = TwoInOneSimpleObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) if self.game_difficuty == 4: obstacle = TwoSideBySideSimpleObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) if self.game_difficuty == 5: obstacle = HalfMoonSpinningRectObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) if self.game_difficuty == 6: obstacle = InvisibleMiddleObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) self.delete_object_with_specific_type(MiddleRectObstacleController) if self.game_difficuty == 7: obstacle = InvisibleSimpleObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) if self.game_difficuty == 8 and len(self.obstacle_generators) > 3: obstacle = RectTranslateXObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) self.delete_object_with_specific_type(SimpleObstacleController) self.delete_object_with_specific_type(TwoInOneSimpleObstacleController) if self.game_difficuty == 9: obstacle = SpinningMiddleRectObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) obstacle.start() self.obstacle_generators.append(obstacle) if self.game_difficuty == 10: self.delete_object_with_specific_type(TwoSideBySideSimpleObstacleController) self.delete_object_with_specific_type(InvisibleSimpleObstacleController) self.delete_object_with_specific_type(HalfMoonSpinningRectObstacleController)
def should_spawn(self): if self.num_of_periods != "inf": self.actual_period += 1 if self.num_of_periods > self.actual_period: return False if (Time.now() - self.last_time) > self.period: self.last_time = Time.now() return True else: return False
def fall(self, obstacle): new_x = obstacle.transform.position.x + self.translate_velocity \ * Time.delta_time() * obstacle.vel if new_x > Constants.screen_width - self.obstacle_size/2 \ or new_x < -self.obstacle_size/2: obstacle.vel *= -1 obstacle.transform.position = Vector2( new_x, obstacle.transform.position.y + self.fall_velocity * Time.delta_time())
def change_scene(self): """ Will fade screen out and the change it """ if self.should_fade_out: ScreenFader(fade="out", fade_duration=self.fade_out_duration) self.should_fade_out = False self.should_change_scene = True self.change_scene_timer = Time.now() Time.time_scale = 0 if self.should_change_scene and Time.now( ) - self.change_scene_timer > self.fade_out_duration + 0.2: Time.time_scale = 1.0 Scene.change_scene(2)
def fall(self, obstacle_pair): visible_condition = 0.1 * Constants.screen_height < obstacle_pair[1].transform.position.y < 0.45 * Constants.screen_height if visible_condition: obstacle_pair[1].transform.position = Vector2(obstacle_pair[1].transform.position.x, obstacle_pair[1].transform.position.y + 4 * self.fall_velocity * Time.delta_time()) else: obstacle_pair[1].transform.position = Vector2(obstacle_pair[1].transform.position.x, obstacle_pair[1].transform.position.y + self.fall_velocity * Time.delta_time()) obstacle_pair[0].transform.position = Vector2(obstacle_pair[0].transform.position.x, obstacle_pair[0].transform.position.y + self.fall_velocity * Time.delta_time())
def start(self): """ NomalBehaivor start method will be called when the object is instantiate on scene """ self.time = Time.now() self.period = 0.9 font_path = "game/assets/fonts/neuropolxrg.ttf" message_x = 10 message_y = 270 message_size = 14 title_x = 37 title_y = 200 title_size = 50 self.setup_soundtrack() BackgroundParticlesController() self.game_object_list = [ Text(Vector2(message_x, message_y), "Press arrows keys to start playing", Material(Color.white), message_size, font_path), Text(Vector2(title_x, title_y), "Balance", Material(Color.white), title_size, font_path) ] self.setup_fader()
def start(self): """ NomalBehaivor start method will be called when the object is instantiate on scene """ self.time = Time.now() self.period = 1.5 font_path = "game/assets/fonts/neuropolxrg.ttf" message_x = 15 message_y = 300 message_size = 14 score = str(int(Constants.current_score)) score_size = 28 score_x = 30 score_y = 240 title_x = 20 title_y = 180 title_size = 50 self.game_object_list = [ Text(Vector2(title_x, title_y), "You died", Material(Color.red), title_size, font_path), Text(Vector2(score_x, score_y), "Score: " + score, Material(Color.white), score_size, font_path), Text(Vector2(message_x, message_y), "Press arrows keys to try again", Material(Color.white), message_size, font_path) ] self.setup_fader() BackgroundParticlesController()
def start(self): """ Will start a particle effect """ self.physics.inst_velocity = self.inst_vel self.particle_system = ParticleSystem(self, Particle, quant=15, period=0.01, vel_min=30, vel_max=130, duration=0.9, gravity=130, layer=10, inherit_vel=True, inherit_vel_mult=0.5, unscaled=True, num_of_periods=1) self.particle_system.set_circ_gen(self.transform.position, radius=self.radius, mode="radial", direct_met=self.direct_met, ini_angle_met=self.ini_angle_met, fin_angle_met=self.fin_angle_met) self.particle_system.play() self.spawn_time = Time.now()
def __init__(self, game_object, mass=None, gravity=0, velocity=Vector2(0, 0), acceleration=Vector2(0, 0), angular_velocity=0, angular_acceleration=0, unscaled=False): super(Physics, self).__init__(game_object) gravity *= 10 self.mass = mass self.velocity = velocity self.acceleration = acceleration self.angular_velocity = angular_velocity self.angular_acceleration = angular_acceleration self.unscaled = unscaled self.gravity = gravity self.inst_velocity = velocity p = self.transform.position t = Time.delta_time(self.unscaled, self.game_object.time_scale) self.position_vect = [ Vector2(p.x, p.y), Vector2(p.x, p.y), Vector2(p.x, p.y) ] self.time_vect = [t, t, t]
def start(self): """ NormalBehavior start method will be called when the object is instantiate on scene """ self.time = Time.now() self.period = 1.5 font_path = "game/assets/fonts/neuropolxrg.ttf" message_x = 15 message_y = 300 message_size = 14 title_x = 20 title_y = 180 title_size = 50 self.game_object_list = [ Text(Vector2(title_x, title_y), "PAUSED", Material(Color.red), title_size, font_path), Text(Vector2(message_x, message_y), "Press space to keep playing", Material(Color.white), message_size, font_path), Rectangle(Vector2(0, 0), Vector2(Constants.screen_width, Constants.screen_height), Material(Color.mask)) ] self.game_object_list[2].collidable = False
def update(self): """ Will be destroyed after a time """ self.physics.inst_velocity = self.inst_vel if Time.now() - self.spawn_time > 0.01: self.destroy_me()
def initial_animation(self): if self.in_initial_animation: if self.should_play: self.should_play = False self.game_object_list[0].animator.play() self.game_object_list[1].animator.play() if Time.now() - self.initial_time > 1.0: self.in_initial_animation = False
def start(self): self.obstacle_generators = [ SimpleObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) ] self.rect_x_controller = RandomXFinalObstacleController(Vector2(0, 0), 0, Vector2(0, 0), 0) self.obstacle_geneation_delta = 1500 self.last_generation_time = 1000 * Time.now() self.game_object_list = [] self.last_increases_dificculty_time = Time.now() self.game_difficuty = 1 self.time_to_increase_difficult = 6.2 self.generation_obstacle_difficult = 1 self.max_difficult = 10 self.should_delete_difficulty_text = False self.diff_text_gen_time = 0.0 for obstacle_generator in self.obstacle_generators: obstacle_generator.start()
def fall(self): """ make the rectangle fall with constant velocity """ self.transform.translate( Vector2( self.transform.position.x, self.transform.position.y + self.fall_velocity * Time.delta_time()))
def update(self): if Time.time_scale < 0.5: #Adjust timers to new delta self.last_generation_time += 1000 * Time.delta_time(True) self.last_increases_dificculty_time += Time.delta_time(True) self.increase_difficult() self.delete_difficulty_text() if (1000 * Time.now() - self.last_generation_time) * Time.time_scale > self.obstacle_geneation_delta * \ self.generation_obstacle_difficult: self.generate_random_obstacle() for obstacle_generator in self.obstacle_generators: game_objs = obstacle_generator.game_object_list self.game_object_list.extend(game_objs)
def die(self): # TODO: change how collider works: dont use the collider list Collider.remove(self) self.circle_collider = None self.animator.play_next_animation() self.should_die = True self.die_time = Time.now()
def update(self): """ NomalBehaivor update method will be call every frame """ if self.should_spawn(): self.spawn_block() if self.pressed_button() and self.can_press_button: self.should_timer = True self.can_press_button = False if self.should_timer: ScreenFader(fade="out") self.timer = Time.now() self.should_timer = False self.should_change_scene = True if self.should_change_scene: if Time.now() - self.timer > 0.68: Scene.change_scene(1)
def start(self): self.angle = 0.0 self.angularSpeed = 5.0 self.game_object_list = [ PlayerCircle(Vector2(Constants.circCenter_x + Constants.circRadius, Constants.screen_height+15), 15, Material(Color.blue, alpha=240)), PlayerCircle(Vector2(Constants.circCenter_x - Constants.circRadius, Constants.screen_height+15), 15, Material(Color.orange, alpha=240)) ] self.in_initial_animation = True self.should_play = True self.initial_time = Time.now()
def generate_random_obstacle(self): self.last_generation_time = 1000 * Time.now() number_of_obstacles = int(min(self.game_difficuty, len(self.obstacle_generators))) random_ind = rand(0, number_of_obstacles-1) random_obstacle_generator = self.obstacle_generators[random_ind] if type(random_obstacle_generator) == RectTranslateXObstacleController: self.last_generation_time -= 300 if self.game_difficuty == self.max_difficult: self.rect_x_controller.generate_obstacle() random_obstacle_generator.generate_obstacle()
def initialize_scene(self): """ When is the correct time, initialize scene This will happen just once """ if Time.now() - self.initial_time > 0.45 and self.should_initialize: self.should_initialize = False self.background_particle_controller = BackgroundParticlesController( ) self.player_controller = PlayerController() self.obstacle_controller_wrapper = ObstacleControllerWrapper() self.items_controller = ItemsControllerWrapper() self.score_controller = ScoreController()
def __update_inst_velocity(self): del self.time_vect[0] self.time_vect.append( Time.delta_time(self.unscaled, self.game_object.time_scale)) del self.position_vect[0] self.position_vect.append( Vector2(self.transform.position.x, self.transform.position.y)) dir = self.position_vect[2] - self.position_vect[0] t = self.time_vect[0] + self.time_vect[1] + self.time_vect[2] if t == 0: self.inst_velocity = Vector2(0, 0) else: self.inst_velocity = dir / t
def get_star(self): self.sound_collect.play() obstacle = self.game_object_list[0] #plus score effect font_path = "game/assets/fonts/neuropolxrg.ttf" plus_score = Text(obstacle.transform.position, "+50", Material(Color.white, alpha=255), 15, font_path) plus_score.transform.position.x -= plus_score.text_mesh.size plus_score.animation = TextUpFadeOutAnimation(plus_score) plus_score.animator = Animator(plus_score, [plus_score.animation]) plus_score.animator.play() self.time_of_last_plus_score = Time.now() self.plus_score = plus_score self.should_delete_plus_score_text = True self.score_controller.score += self.points_per_star
def __init__(self, game_object, spawn_game_obj_class, layer=0, quant=1, quant_proport_to_len=False, period=0.05, vel_min=80, vel_max=160, duration=1.0, gravity=0, inherit_vel=False, inherit_vel_mult=1, spawn_prob="lin", vel_prob="lin", unscaled=False, num_of_periods="inf"): super().__init__(game_object) self.duration = duration self.gravity = gravity self.vel_min = vel_min self.layer = layer self.vel_max = vel_max self.inherit_vel = inherit_vel self.quant_proport_to_len = quant_proport_to_len self.inherit_vel_mult = inherit_vel_mult self.quant = quant self.turned_on = False self.period = period self.spawn_game_obj_class = spawn_game_obj_class self.last_time = Time.now() self.ini_point_method = None self.fin_point_method = None self.generation_mode = None self.obj_list = list() self.spawn_prob = None self.vel_prob = None self.define_vel_prob(vel_prob) self.define_spawn_prob(spawn_prob) self.unscaled = unscaled self.num_of_periods = num_of_periods self.actual_period = 0 if self.inherit_vel: if self.game_object.physics is None: self.game_object.physics = Physics(self.game_object)
def start(self): self.particle_system = ParticleSystem(self, Particle, quant=30, period=0.02, vel_min=40, vel_max=80, duration=2, gravity=98, layer=10 ) self.particle_system.set_circ_gen(self.transform.position, 1, mode="radial", direct_met=self.direct_met, ini_angle_met=self.ini_angle_met, fin_angle_met=self.fin_angle_met ) self.particle_system.play() self.spawn_time = Time.now()