def __init__(self): self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height # scaling parameters with display resolution self.PIPE_INIT_X = int(self.PIPE_INIT_X * self.display_width) self.PIPE_MIN_SPACING_X = int(self.PIPE_MIN_SPACING_X * self.display_height) self.PIPE_MIN_SPACING_Y = int(self.PIPE_MIN_SPACING_Y * self.display_height) self.PIPE_MAX_SPACING_X = int(self.PIPE_MAX_SPACING_X * self.display_height) self.PIPE_MAX_SPACING_Y = int(self.PIPE_MAX_SPACING_Y * self.display_height) self.PIPE_SPACING_DECREASE = int(self.PIPE_SPACING_DECREASE * self.display_height) self.pipe_spacing_x = self.PIPE_MAX_SPACING_X self.pipe_spacing_y = self.PIPE_MAX_SPACING_Y self.pipe_width, self.pipe_height = Assets.get().PIPE_BOTTOM_IMAGE.get_size() debugger('PipeManager: __init__: pipe_width = {}, pipe_height = {}'.format(self.pipe_width, self.pipe_height)) # initialising parameters for randomizing top/bottom pipe y positions self.rand_from = int(self.display_height * self.SPACING_FROM_Y) self.rand_to = int(self.display_height * self.SPACING_TO_Y) debugger('PipeManager: __init__: (rand_from, rand_to) = {}'.format((self.rand_from, self.rand_to))) # calculate the number of pipes needed and generate them self.no_of_pipes = self.display_width / (self.pipe_width + self.PIPE_MIN_SPACING_X) + 2 debugger('PipeManager: __init__: no_of_pipes = {}'.format(self.no_of_pipes)) self.pipes_list = self.generate_pipes() self.pipes_group = pygame.sprite.RenderUpdates(self.pipes_list) self.difficulty_counter = self.no_of_pipes
def __init__(self, scale_pos_x, scale_pos_y): super(Flappy, self).__init__() self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height self.default_position = (int(scale_pos_x * self.display_width), int(scale_pos_y * self.display_height)) debugger("Flappy: __init__: default_position = {}".format(self.default_position)) # scaling physics with display resolution self.ACCELERATION *= self.display_height self.MAX_VELOCITY *= self.display_height self.JUMP_VELOCITY *= self.display_height self.IDLE_ANIMATION_MAX_Y *= self.display_height self.IDLE_ANIMATION_SPEED *= self.display_height # loading flappy assets self.FLAPPY_UP_IMAGE = Assets.get().FLAPPY_UP_IMAGE self.FLAPPY_IMAGES = Assets.get().FLAPPY_IMAGES # the actual image that gets rendered and collision detection mask self.image = self.FLAPPY_IMAGES[0] self.rect = self.image.get_rect() self.hitmask = get_alpha_hitmask(self.image, self.rect) # setting Flappy's default values self.position = self.default_position self.velocity = 0 self.idle_direction = -1 self.animation_ms = 0 self.flappy_flap_listeners = list()
def __init__(self, scale_pos_x, scale_pos_y): super(Flappy, self).__init__() self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height self.default_position = (int(scale_pos_x * self.display_width), int(scale_pos_y * self.display_height)) debugger("Flappy: __init__: default_position = {}".format( self.default_position)) # scaling physics with display resolution self.ACCELERATION *= self.display_height self.MAX_VELOCITY *= self.display_height self.JUMP_VELOCITY *= self.display_height self.IDLE_ANIMATION_MAX_Y *= self.display_height self.IDLE_ANIMATION_SPEED *= self.display_height # loading flappy assets self.FLAPPY_UP_IMAGE = Assets.get().FLAPPY_UP_IMAGE self.FLAPPY_IMAGES = Assets.get().FLAPPY_IMAGES # the actual image that gets rendered and collision detection mask self.image = self.FLAPPY_IMAGES[0] self.rect = self.image.get_rect() self.hitmask = get_alpha_hitmask(self.image, self.rect) # setting Flappy's default values self.position = self.default_position self.velocity = 0 self.idle_direction = -1 self.animation_ms = 0 self.flappy_flap_listeners = list()
def __init__(self, position): super(Ground, self).__init__() self.position = position # scaling speed with display resolution self.MOVING_SPEED *= Assets.get().PIPE_TOP_IMAGE.get_width() self.image = Assets.get().GROUND_IMAGE self.rect = self.image.get_rect() self.hitmask = get_full_hitmask(self.image, self.rect)
def __init__(self, position, is_top=False): super(Pipe, self).__init__() self.position = position # use different images for top and bottom pipes if is_top: self.image = Assets.get().PIPE_TOP_IMAGE else: self.image = Assets.get().PIPE_BOTTOM_IMAGE # scaling speed with display resolution self.MOVING_SPEED *= self.image.get_width() self.rect = self.image.get_rect() self.hitmask = get_alpha_hitmask(self.image, self.rect)
def __init__(self, display): self.display = display self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height # Font downloaded from: http://www.fontsaddict.com/font/zx-spectrum-7-bold.html self.font_path = os.path.join(fonts_path, 'zx_spectrum-7_bold.ttf') self.setup_fonts() self.controls_hint_text = self.hint_renderer.render(_("[SPACE] or [UP] to Jump"), 1, self.WHITE) self.controls_hint_text_x = self.display_width * self.CONTROLS_HINT_X self.controls_hint_text_y = self.display_height * self.CONTROLS_HINT_Y self.quit_hint_text = self.hint_renderer.render(_("[ESC] or [Q] to Quit"), 1, self.WHITE) self.quit_hint_text_x = self.display_width * self.CONTROLS_HINT_X self.quit_hint_text_y = self.display_height * self.CONTROLS_HINT_Y + 20
def __init__(self): self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height # scaling ground position with display resolution self.GROUND_Y *= self.display_height debugger("GroundManager: __init__: GROUND_Y = {}".format(self.GROUND_Y)) self.ground_width = Assets.get().GROUND_IMAGE.get_width() debugger("GroundManager: __init__: ground_width = {}".format(self.ground_width)) # calculate the number of ground sprites needed and generate them self.no_of_grounds = self.display_width / self.ground_width + 2 debugger("GroundManager: __init__: no_of_grounds = {}".format(self.no_of_grounds)) self.ground_list = self.generate_ground() self.ground_group = pygame.sprite.RenderUpdates(self.ground_list)
def randomise_background(self): # pick a random background from the list index = random.randint(0, len(Assets.get().BACKGROUND_IMAGES) - 1) background_image = Assets.get().BACKGROUND_IMAGES[index] background_color = Assets.get().BACKGROUND_COLORS[index] # compose the background by filling the surface with a color self.background = pygame.Surface((self.WIDTH, self.HEIGHT)) self.background.fill(background_color) # and draw as many background assets one after another as needed no_of_background_images = int(self.WIDTH / background_image.get_width()) + 1 y = self.HEIGHT * 0.88 - background_image.get_width() / 2.0 for i in xrange(no_of_background_images): x = int(i * background_image.get_width()) self.background.blit(background_image, (x, y))
def randomise_background(self): # pick a random background from the list index = random.randint(0, len(Assets.get().BACKGROUND_IMAGES) - 1) background_image = Assets.get().BACKGROUND_IMAGES[index] background_color = Assets.get().BACKGROUND_COLORS[index] # compose the background by filling the surface with a color self.background = pygame.Surface((self.WIDTH, self.HEIGHT)) self.background.fill(background_color) # and draw as many background assets one after another as needed no_of_background_images = int( self.WIDTH / background_image.get_width()) + 1 y = self.HEIGHT * 0.88 - background_image.get_width() / 2.0 for i in xrange(no_of_background_images): x = int(i * background_image.get_width()) self.background.blit(background_image, (x, y))
def get_flappys_next_pipes(self): ''' This method returns the two pipes just in front of Flappy. It is called by FlappyFlyingState in its update_state method. ''' for i in xrange(0, len(self.pipes_list), 2): # TODO: better way of using flappy's position (.. > s.d_w * 0.1) if self.pipes_list[i].position[0] > self.display_width * 0.2 - Assets.get().FLAPPY_UP_IMAGE.get_width(): return self.pipes_list[i], self.pipes_list[i + 1]
def __init__(self, display): self.display = display self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height # Font downloaded from: http://www.fontsaddict.com/font/zx-spectrum-7-bold.html self.font_path = os.path.join(fonts_path, 'zx_spectrum-7_bold.ttf') self.setup_fonts() self.controls_hint_text = self.hint_renderer.render( _("[SPACE] or [UP] to Jump"), 1, self.WHITE) self.controls_hint_text_x = self.display_width * self.CONTROLS_HINT_X self.controls_hint_text_y = self.display_height * self.CONTROLS_HINT_Y self.quit_hint_text = self.hint_renderer.render( _("[ESC] or [Q] to Quit"), 1, self.WHITE) self.quit_hint_text_x = self.display_width * self.CONTROLS_HINT_X self.quit_hint_text_y = self.display_height * self.CONTROLS_HINT_Y + 20
def __init__(self): self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height # scaling ground position with display resolution self.GROUND_Y *= self.display_height debugger("GroundManager: __init__: GROUND_Y = {}".format( self.GROUND_Y)) self.ground_width = Assets.get().GROUND_IMAGE.get_width() debugger("GroundManager: __init__: ground_width = {}".format( self.ground_width)) # calculate the number of ground sprites needed and generate them self.no_of_grounds = self.display_width / self.ground_width + 2 debugger("GroundManager: __init__: no_of_grounds = {}".format( self.no_of_grounds)) self.ground_list = self.generate_ground() self.ground_group = pygame.sprite.RenderUpdates(self.ground_list)
def get_ground_under_flappy(self): ''' This method returns the Ground object that is directly under Flappy. It is called by FlappyFlyingState in its update_state method. ''' for ground in self.ground_list: # TODO: better way of using flappy's position (.. > s.d_w * 0.2) if ground.position[0] + Assets.get().GROUND_IMAGE.get_width() / 2.0 > self.display_width * 0.2: return ground debugger("FATAL ERROR: GroundManager: get_ground_under_flappy:" \ " Did not find any ground under Flappy!", fatal=True)
def get_flappys_next_pipes(self): ''' This method returns the two pipes just in front of Flappy. It is called by FlappyFlyingState in its update_state method. ''' for i in xrange(0, len(self.pipes_list), 2): # TODO: better way of using flappy's position (.. > s.d_w * 0.1) if self.pipes_list[i].position[ 0] > self.display_width * 0.2 - Assets.get( ).FLAPPY_UP_IMAGE.get_width(): return self.pipes_list[i], self.pipes_list[i + 1]
def __init__(self): self.display_width = Assets.get().display_width self.display_height = Assets.get().display_height # scaling parameters with display resolution self.PIPE_INIT_X = int(self.PIPE_INIT_X * self.display_width) self.PIPE_MIN_SPACING_X = int(self.PIPE_MIN_SPACING_X * self.display_height) self.PIPE_MIN_SPACING_Y = int(self.PIPE_MIN_SPACING_Y * self.display_height) self.PIPE_MAX_SPACING_X = int(self.PIPE_MAX_SPACING_X * self.display_height) self.PIPE_MAX_SPACING_Y = int(self.PIPE_MAX_SPACING_Y * self.display_height) self.PIPE_SPACING_DECREASE = int(self.PIPE_SPACING_DECREASE * self.display_height) self.pipe_spacing_x = self.PIPE_MAX_SPACING_X self.pipe_spacing_y = self.PIPE_MAX_SPACING_Y self.pipe_width, self.pipe_height = Assets.get( ).PIPE_BOTTOM_IMAGE.get_size() debugger( "PipeManager: __init__: pipe_width = {}, pipe_height = {}".format( self.pipe_width, self.pipe_height)) # initialising parameters for randomizing top/bottom pipe y positions self.rand_from = int(self.display_height * self.SPACING_FROM_Y) self.rand_to = int(self.display_height * self.SPACING_TO_Y) debugger("PipeManager: __init__: (rand_from, rand_to) = {}".format( (self.rand_from, self.rand_to))) # calculate the number of pipes needed and generate them self.no_of_pipes = self.display_width / (self.pipe_width + self.PIPE_MIN_SPACING_X) + 2 debugger("PipeManager: __init__: no_of_pipes = {}".format( self.no_of_pipes)) self.pipes_list = self.generate_pipes() self.pipes_group = pygame.sprite.RenderUpdates(self.pipes_list) self.difficulty_counter = self.no_of_pipes
def get_ground_under_flappy(self): ''' This method returns the Ground object that is directly under Flappy. It is called by FlappyFlyingState in its update_state method. ''' for ground in self.ground_list: # TODO: better way of using flappy's position (.. > s.d_w * 0.2) if ground.position[0] + Assets.get().GROUND_IMAGE.get_width( ) / 2.0 > self.display_width * 0.2: return ground debugger("FATAL ERROR: GroundManager: get_ground_under_flappy:" \ " Did not find any ground under Flappy!", fatal=True)
def __init__(self): # if the resolution was not changed through cmdline args, use the default scaling if self.WIDTH < 1 and self.HEIGHT < 1: monitor_info = pygame.display.Info() debugger("Display: __init__: Monitor resolution is {}x{}".format( monitor_info.current_w, monitor_info.current_h)) self.WIDTH = int(self.WIDTH * monitor_info.current_w) self.HEIGHT = int(self.HEIGHT * monitor_info.current_h) # TODO: remove this if font sizes in HUDs are scaled if self.HEIGHT < 400: debugger( "WARNING: flappy-judoka: main: Height resolution is below the recommended value 400!" ) self.display = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.NOFRAME) debugger("Display: __init__: Setting game resolution {}x{}".format( self.WIDTH, self.HEIGHT)) # setting the window name (this is for the window manager to have a reference) pygame.display.set_caption(self.GAME_TITLE) # hiding mouse pointer when the game runs pygame.mouse.set_visible(False) # grabing all keyboard and mouse inputs, apps behind the game won't get input pygame.event.set_grab(True) # initialising and loading assets based on screen resolution Assets(self.WIDTH, self.HEIGHT) # compose and draw the background self.randomise_background() self.draw_background()
def __init__(self): self.game_over_sound = Assets.get().GAME_OVER_SOUND self.start_game_sound = Assets.get().START_GAME_SOUND self.flappy_flap_sound = Assets.get().FLAPPY_FLAP_SOUND self.gained_point_sound = Assets.get().GAINED_POINT_SOUND