Python version: 3.12 A guide to install python3.12 on Linux
How to setup a virtual environment:
// create a virtual environment under a folder named 'venv'
$ python3.12 -m venv venv
// activate the venv (command for Linux, Windows' command for activation might differ)
$ source ./venv/bin/activate
Required packages installation: pip3.12 install -r requirements.txt
How to run: python3.12 src/Game.py
Helpful info:
- python3.12 venv (if not installed):
sudo apt install python3.12-venv
- if you face an error:
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
, trypython3.12 -m pip install --upgrade setuptools
If you have Python < 3.12
- switch to the branch
master-python-3.10
- if you will copy some code from this file while doing tasks, remove all
@override
statements from that code
How to generate a level: see python src/level_editor/level_generator.py
PRESENTATION https://docs.google.com/presentation/d/1yjGLa3u4cL7nUUC8xHlvnIIPi8vCxuQttyZGKqyvmGI
TASKS
-
Creating the
Enemy
class and placing an enemy on the level- Copy the file src/actors/characters/Fox.py into src/actors/characters/enemy dir and rename it into
Enemy.py
, also rename the classFox
intoEnemy
in the new file - The enemy should use animation sprites provided under assets/sprites/enemy
- Place 1 enemy on the Level1 on
PLAYABLEGROUND
(see src/levels/level1/Level1.py) at position (500, 500) - Take a screenshot of the game for UPEL where the enemy can be seen on the screen
- Copy the file src/actors/characters/Fox.py into src/actors/characters/enemy dir and rename it into
-
Adding a non-physical collision component to the
Enemy
class- uncomment src/actors/characters/enemy/PlayerDetectorComponent.py
- inside of the
Enemy
__init__
add aPlayerDetectorComponent
instance toself.components
. Keep in mind to import thePlayerDetectorComponent
class at the end of the file to prevent circular import issue - in src/constants.py you can set
SHOW_COLLISIONS
toTrue
to see collision boxes on the screen when the game is running - set width and relative offset of the
PlayerDetectorComponent
so that it's at the center of the enemy and it's wide - something like this (if it takes too much time see the end of the README file): - take a similar screenshot for UPEL
-
Making the enemy chase the player if the player gets close
- copy the following overridden methods into the
PlayerDetectorComponent.py
:
@override def tick(self): super().tick() @override def check_and_process_collision(self, other: CollisionComponent): super().check_and_process_collision(other)
- add next 2 lines into the
__init__
of thePlayerDetectorComponent
:
self.collided_player_this_tick: bool = False self.collided_player_position: Vector = Vector(0, 0)
-
write a logic so that in the
check_and_process_collision
method we check if the instance of theother.owned_by
isPlayer.PlayerCharacter
and if theother
is collidingself
Hints:- use Python's
isinstance
function - use
colliderect
, example is also provided in the src/components/CharacterCollisionComponent.py
If both conditions are
True
then setself.collided_player_this_tick
toTrue
andself.collided_player_position
to the current position of theother.owned_by
- use Python's
-
write a logic so that in the
tick
method we check ifself.collided_player_this_tick
isTrue
then we useself.owned_by.movement_component.set_x_direction
method so that the enemy goes in the direction of the player (based on their positions). Otherwise, set the x direction to 0 so that the enemy doesn't move. Don't forget to reset thecollided_player_this_tick
toFalse
at the end of thetick
method Hint: useint(some_number)
to convertfloat
toint
-
send a screenshot to UPEL where the enemy is running in the direction of the player like this:
- copy the following overridden methods into the
-
Make player disappear from the screen if it collided with an enemy:
- go to src/Player.py and in the
PlayerCharacterCollisionComponent
in thecheck_and_process_collision
write a condition that checks if the player collides with an instance of anEnemy
andother.isPhysical
isTrue
and then if the conditions is met paste this under the if statement:self.owned_by.die() # type:ignore
- send a screenshot like this to UPEL where the player is off the screen:
PlayerDetectorComponent width and offset respectively are: 300; (-80. 20)