📜 ⬆️ ⬇️

Doodle Jump on pygame

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




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:

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: # do something if event.type == MOUSEMOTION: for btn in self.buttons: if btn.rect.collidepoint(pygame.mouse.get_pos()): #pass btn.changeState(1) 

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




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.

Source: https://habr.com/ru/post/130726/


All Articles