Kind…
I want to tell and show you my experience of acquaintance with the pygame library - presumably an excellent library for the implementation of graphical applications (mostly arcade games) in the python language. For example, the Doodle Jump game was implemented (partially).
Disclaimer
- Yes, I used other people's pictures - I am ashamed and I will fix it soon.
- The character can now only jump, but I plan to finish the game to the end.
- Yes, the idea is not new, but it turned out cool.
- Yes, most likely nonsense - use an interpreted language to write games.
- I am not a professional programmer and this is not my main activity.
Instead of introducing
Python. Python is great in terms of documentation. She is, she is understandable, she is not without excellent examples. Pygame is not an exception, so I suggest not to dwell on the details of the implementation of this library, but it should be noted that its tools allow you to load an image, and simply move it along x and y. Nothing extra.
')
Architecture
We will try to develop the application architecture in the best
OOP traditions. We have the following class diagram:

Supporters of
MVC models are already crying, because and logic and representation are stuck in the body of the class.
We have 3 locations:
- StartLocation - here the menu and invitation to the game is spinning.
- GameLocation - the actual subject.
- ExitLocation - output, results, fanfare-ovation.
The base object Sprite, from which all game objects are inherited - platforms, buttons, the main character, etc. The scheme is clear.
Key points
Events
def event(self, event): if event.type == QUIT: sys.exit() elif event.type == KEYUP: if event.key == K_ESCAPE:
With the mechanism of events, keystrokes and mouse manipulations are transparently intercepted.
Images
self.img_l = pygame.image.load('img/doodle_l.png').convert() self.image = self.img_l self.image.set_colorkey(self.image.get_at((0,0)), RLEACCEL) self.rect = self.image.get_rect() self.rect.center = (self.x,self.y)
We load pictures for sprites.
Collisions
self.doodle.getLegsRect().colliderect(spr.getSurfaceRect())
The coliderect method returns true if the bounding rectangles of the sprites intersect.
A couple of screenshots


ToDo
- To work gameplay
- Draw new sprites
- Add sounds
- Add statistics
- Correct platform layout algorithms
- Add arrow control
What else?
Instead of epilogue
Acquaintance with pygame can be considered complete, doodler jumps and enjoys life, but the processor load at the same time tends to 50-70%, which is not gud. What is it, a bad application architecture or language features - you decide. Thanks for attention.
Sources on github.