- Read the guideline before start
- Pygame primer
- Pygame sprites and groups
Games! Games are a perfect example to show OOP principles at work. You could play the game Bomberman in your youth or childhood, now it is time to write your own a little modified version. You already have an initial structure of the game Bomberman.
There are player
(green rectangle) and walls
(white squares).
You should implement these features:
- Player movements. The player should be able to move left, right, up, down.
Sprites
update
method is called every frame, so useself.rect.move_ip
the method insideupdate
to move the player. Movement direction depends on the pressed key by the user. Usepygame.key.get_pressed
to get the keys that are currently pressed. Use arrows to move the player. Player - Player cannot walk away from the field. Use player's
self.rect.left
,self.rect.right
,self.rect.top
,self.rect.bottom
- coordinates of player borders and compare them to field bordersSCREEN_WIDTH
,SCREEN_HEIGHT
. If the player coordinate is out of bound, it should change it to field border. - Player cannot walk over the walls. Walls are stored in group
walls
. Usepygame.sprite.spritecollideany
with the player and walls group to check if the player is colliding with any sprite in that group. (If afterself.rect.move_ip
player starts colliding - just move it back). Collision detection - Player should plant the bomb when the user presses the space button.
Implement class
Bomb
that should be a sprite. Bomb should be planted on the current player's coordinates. But the player cannot walk over the bomb, think about how the player gets off the bomb after the planting. - Player cannot put bomb if less than 1 second has passed from last planting
(the logic of the explosion you will implement later). There are
60
frames per second. You can store timeout in the attribute and reduce it inupdate
method. - Replace simple colored rectangles with images given in
"app/images/
. Usepygame.image.load("source.png").convert_alpha()
instead ofSurface
. The player image should change when he goes in different directions. Sprite images
All information you can find in guides links on the top of the tasks.
Also, it is a bad idea to store full application logic in main.py
.
- Implement your architecture ideas
Hint: it will be good to have a class that stores and handles
such things as: clock
, screen
, running
, events
, groups
etc.
If sprites store link to this class, they can interact with these
things in methods instead of module level.