Module game_qu.getting_started.platforming game (example game)
This is an example of a simple platformer made using the game engine. Click the see 'Expand Source Code' button below to see the code.
Expand source code
"""This is an example of a simple platformer made using the game engine. Click the see 'Expand Source Code' button below
to see the code."""
# This must be before the other imports, so we can actually modify the constants. Otherwise, the constants will remain the
# same as the default values.
from game_qu.base.library_changer import LibraryChanger
from game_qu.base.colors import *
LibraryChanger.set_game_library("pygame")
LibraryChanger.set_screen_dimensions(1000, 600)
LibraryChanger.set_background_color(black)
# The rest of the imports
from game_qu.base.important_variables import *
from game_qu.base.velocity_calculator import VelocityCalculator
from game_qu.base.game_runner_function import run_game
from game_qu.base.paths import Path
from game_qu.math.point import Point
from game_qu.platformer.enemy import Enemy
from game_qu.platformer.platform import Platform
from game_qu.platformer.platformer_screen import PlatformerScreen
from game_qu.platformer.player import Player
from game_qu.gui_components.component import Component
class SimplePlayer(Player):
"""A simple player"""
eye1 = None
eye2 = None
mouth = None
def __init__(self, left_key, right_key, jump_key, down_key, attack_key):
"""Initializes the object"""
super().__init__(left_key, right_key, jump_key, down_key, attack_key)
self.set_color(gray)
self.eye1 = Component("")
self.eye2 = Component("")
self.mouth = Component("")
self.eye1.set_color(blue)
self.eye2.set_color(blue)
self.mouth.set_color(red)
def render(self):
"""Renders this object onto the screen"""
super().render()
# Settings all the dimensions of the subcomponents for rendering (eyes and mouth)
self.eye1.set_dimensions_within_component(25, 20, 20, 20, self)
self.eye2.set_dimensions_within_component(55, 20, 20, 20, self)
self.mouth.set_dimensions_within_component(10, 70, 80, 10, self)
# Rendering the subcomponents
self.eye1.render()
self.eye2.render()
self.mouth.render()
class SimpleEnemy(Enemy):
"""A simple enemy that moves back and forth across the platform"""
eye = None
def __init__(self, platform):
"""Initializes the object"""
super().__init__(30, 20, platform, "")
self.set_color(black)
self.eye = Component("")
self.eye.set_color(red)
def get_point_value(self):
return 100
def render(self):
"""Renders this component onto the game"""
super().render()
self.eye.set_dimensions_within_component(0, 30, 100, 10, self)
self.eye.render()
class SimplePlatform(Platform):
"""A simple platform"""
grass = None
def __init__(self):
"""Initializes the object"""
super().__init__()
self.set_color(brown)
self.grass = Component("")
self.grass.set_color(green)
def render(self):
"""Renders this component onto the game"""
super().render()
self.grass.set_dimensions_within_component(0, 0, 100, 20, self)
self.grass.render()
class MainScreen(PlatformerScreen):
"""The main screen of the game"""
score_to_game_difficulty = Path(Point(0, 50), [Point(1000, 70), Point(1650, 95), Point(5000, 100),
Point(float("inf"), 100)])
# Here we will be implementing the abstract methods and modifying some methods to get what we want
def get_enemy_types(self):
"""
Returns:
list[Enemy]: all the types of enemies that can be generated
"""
return [SimpleEnemy]
def get_game_difficulty(self):
"""
Returns:
float: the difficulty of the game based on the score of the player (from 0 to 100 with 100 being the hardest)
"""
return self.score_to_game_difficulty.get_y_coordinate(self.player_score)
def get_score_from_passing_platform(self):
"""
Returns:
int: the score from passing a platform
"""
return 100
def get_start_platform_coordinates(self):
"""
Returns:
list[float]: {left_edge, top_edge, length, height} of the start platform
"""
return [0,
VelocityCalculator.get_dimension(SCREEN_HEIGHT, 85),
VelocityCalculator.get_dimension(SCREEN_LENGTH, 50),
VelocityCalculator.get_dimension(SCREEN_HEIGHT, 15)]
def get_players(self):
"""
Returns:
list[Player]: the players of the game
"""
return [SimplePlayer(KEY_A, KEY_D, KEY_W, KEY_S, KEY_F)]
if __name__ == "__main__":
# Finally running the game
run_game(MainScreen())
Classes
class MainScreen-
The main screen of the game
Initializes the object
Expand source code
class MainScreen(PlatformerScreen): """The main screen of the game""" score_to_game_difficulty = Path(Point(0, 50), [Point(1000, 70), Point(1650, 95), Point(5000, 100), Point(float("inf"), 100)]) # Here we will be implementing the abstract methods and modifying some methods to get what we want def get_enemy_types(self): """ Returns: list[Enemy]: all the types of enemies that can be generated """ return [SimpleEnemy] def get_game_difficulty(self): """ Returns: float: the difficulty of the game based on the score of the player (from 0 to 100 with 100 being the hardest) """ return self.score_to_game_difficulty.get_y_coordinate(self.player_score) def get_score_from_passing_platform(self): """ Returns: int: the score from passing a platform """ return 100 def get_start_platform_coordinates(self): """ Returns: list[float]: {left_edge, top_edge, length, height} of the start platform """ return [0, VelocityCalculator.get_dimension(SCREEN_HEIGHT, 85), VelocityCalculator.get_dimension(SCREEN_LENGTH, 50), VelocityCalculator.get_dimension(SCREEN_HEIGHT, 15)] def get_players(self): """ Returns: list[Player]: the players of the game """ return [SimplePlayer(KEY_A, KEY_D, KEY_W, KEY_S, KEY_F)]Ancestors
Class variables
var score_to_game_difficulty
Inherited members
PlatformerScreen:add_game_objects_to_history_keeperbottom_edgeget_additional_generation_itemsget_code_ready_for_collisionsget_componentsget_enemy_typesget_game_difficultyget_playersget_scaled_dimensionsget_score_from_passing_platformget_start_platform_coordinatesgot_clickedhidehorizontal_midpointis_platformis_playeris_player_weaponmouse_enter_functionmouse_exit_functionnumber_set_dimensionspercentage_set_dimensionsremove_platforms_not_within_screenrenderrender_backgroundreset_gameright_edgerunrun_all_collisionsrun_collisionrun_game_coderun_platform_generationrun_player_collisionsrun_playersrun_side_scrollingset_background_colorset_colorset_dimensions_within_componentset_is_visibleset_mouse_enter_functionset_mouse_exit_functionset_mouse_functionsset_path_to_background_imagesetup_platformssetup_playersshowside_scroll_all_objectsside_scroll_objectsupdate_rightmost_platformupdate_scorevertical_midpoint
class SimpleEnemy (platform)-
A simple enemy that moves back and forth across the platform
Initializes the object
Expand source code
class SimpleEnemy(Enemy): """A simple enemy that moves back and forth across the platform""" eye = None def __init__(self, platform): """Initializes the object""" super().__init__(30, 20, platform, "") self.set_color(black) self.eye = Component("") self.eye.set_color(red) def get_point_value(self): return 100 def render(self): """Renders this component onto the game""" super().render() self.eye.set_dimensions_within_component(0, 30, 100, 10, self) self.eye.render()Ancestors
- Enemy
- WeaponUser
- GameObject
- Component
- Dimensions
- abc.ABC
Class variables
var eye
Methods
def render(self)-
Renders this component onto the game
Expand source code
def render(self): """Renders this component onto the game""" super().render() self.eye.set_dimensions_within_component(0, 30, 100, 10, self) self.eye.render()
Inherited members
Enemy:bottom_edgecause_damageget_all_componentsget_collidable_componentsget_collision_dataget_componentsget_point_valueget_scaled_dimensionsgot_clickedhorizontal_midpointmouse_enter_functionmouse_exit_functionnumber_set_dimensionspercentage_set_dimensionsreset_collision_dataright_edgerunrun_collisionsrun_enemy_collisionrun_inanimate_object_collisionrun_player_interactionsrun_upon_activationset_colorset_dimensions_within_componentset_mouse_enter_functionset_mouse_exit_functionset_mouse_functionsupdate_collision_dataupdate_for_side_scrollingupdate_platform_collision_dataupdate_top_collision_datavertical_midpoint
class SimplePlatform-
A simple platform
Initializes the object
Expand source code
class SimplePlatform(Platform): """A simple platform""" grass = None def __init__(self): """Initializes the object""" super().__init__() self.set_color(brown) self.grass = Component("") self.grass.set_color(green) def render(self): """Renders this component onto the game""" super().render() self.grass.set_dimensions_within_component(0, 0, 100, 20, self) self.grass.render()Ancestors
Class variables
var grass
Methods
def render(self)-
Renders this component onto the game
Expand source code
def render(self): """Renders this component onto the game""" super().render() self.grass.set_dimensions_within_component(0, 0, 100, 20, self) self.grass.render()
Inherited members
Platform:bottom_edgeget_all_componentsget_collidable_componentsget_scaled_dimensionsgot_clickedhorizontal_midpointmouse_enter_functionmouse_exit_functionnumber_set_dimensionspercentage_set_dimensionsright_edgerunrun_collisionsset_colorset_dimensions_within_componentset_mouse_enter_functionset_mouse_exit_functionset_mouse_functionsupdate_for_side_scrollingvertical_midpoint
class SimplePlayer (left_key, right_key, jump_key, down_key, attack_key)-
A simple player
Initializes the object
Expand source code
class SimplePlayer(Player): """A simple player""" eye1 = None eye2 = None mouth = None def __init__(self, left_key, right_key, jump_key, down_key, attack_key): """Initializes the object""" super().__init__(left_key, right_key, jump_key, down_key, attack_key) self.set_color(gray) self.eye1 = Component("") self.eye2 = Component("") self.mouth = Component("") self.eye1.set_color(blue) self.eye2.set_color(blue) self.mouth.set_color(red) def render(self): """Renders this object onto the screen""" super().render() # Settings all the dimensions of the subcomponents for rendering (eyes and mouth) self.eye1.set_dimensions_within_component(25, 20, 20, 20, self) self.eye2.set_dimensions_within_component(55, 20, 20, 20, self) self.mouth.set_dimensions_within_component(10, 70, 80, 10, self) # Rendering the subcomponents self.eye1.render() self.eye2.render() self.mouth.render()Ancestors
Class variables
var eye1var eye2var mouth
Methods
def render(self)-
Renders this object onto the screen
Expand source code
def render(self): """Renders this object onto the screen""" super().render() # Settings all the dimensions of the subcomponents for rendering (eyes and mouth) self.eye1.set_dimensions_within_component(25, 20, 20, 20, self) self.eye2.set_dimensions_within_component(55, 20, 20, 20, self) self.mouth.set_dimensions_within_component(10, 70, 80, 10, self) # Rendering the subcomponents self.eye1.render() self.eye2.render() self.mouth.render()
Inherited members
Player:acceleration_direction_is_possiblealter_player_horizontal_movementalter_player_horizontal_movement_booleansalter_player_left_edge_if_necessaryalter_player_vertical_movementbottom_edgecause_damagechange_attribute_ifcontinue_acceleration_after_partial_decelerationcreate_pathsdecelerate_playerget_all_componentsget_collidable_componentsget_collision_dataget_componentsget_deceleration_is_rightwardsget_distance_to_reach_max_velocityget_horizontal_velocityget_jumping_path_bounded_functionsget_max_time_to_top_edgeget_scaled_dimensionsget_terminal_velocity_delta_timeget_topmost_top_edgeget_vertical_accelerationget_vertical_velocitygot_clickedhorizontal_midpointhorizontal_movement_has_stoppedjumpmouse_enter_functionmouse_exit_functionnumber_set_dimensionspercentage_set_dimensionsplayer_movement_direction_is_same_as_decelerationresetreset_collision_dataright_edgerunrun_accelerationrun_bottom_edge_collisionrun_collisionsrun_decelerationrun_enemy_collisionrun_horizontal_movementrun_inanimate_object_collisionrun_jump_typerun_respawningrun_upon_activationrun_vertical_movementset_base_coordinatesset_colorset_dimensions_within_componentset_is_on_platformset_jumping_path_to_default_jumpset_jumping_path_to_falling_pathset_jumping_path_with_apexset_left_edgeset_mouse_enter_functionset_mouse_exit_functionset_mouse_functionsupdate_collision_dataupdate_falling_pathupdate_for_side_scrollingupdate_jump_typesupdate_jumping_pathupdate_platform_collision_datavertical_midpoint