ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:pygame:step002

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
en:pygame:step002 [2020/05/14 16:04]
Horst JENS
en:pygame:step002 [2020/05/14 16:08] (current)
Horst JENS
Line 115: Line 115:
 To run both variants of this example you need: To run both variants of this example you need:
  
-^  file  ^  in folder ​ ^  download ​ ^  comment ​ ^ 
-|  [[https://​github.com/​horstjens/​ThePythonGameBook/​raw/​master/​pygame/​002_display_fps.py|002_display_fps.py]] ​ |  pygame ​ |  Download the whole Archive with all files from Github: ​ \\  https://​github.com/​horstjens/​ThePythonGameBook/​archives/​master ​ |  simple version ​ | 
-|  [[https://​github.com/​horstjens/​ThePythonGameBook/​raw/​master/​pygame/​002_display_fps_pretty.py|002_display_fps_pretty.py]] ​ |  pygame ​ |  :::  |  OOP version ​ | 
  
 There exist 2 code examples, a simple example from [[en:​secret:​people:​jens_horst:​start|Horst]] and an object oriented variant of [[en:​secret:​people:​yipyip:​start|yipyip]]. Both versions have a similar functionality but implement this in a different manner. If you are new to python/​pygame programming and have not yet worked with the **class** command you may want to try out the simple variant and come back if you are more familiar with classes. ​ There exist 2 code examples, a simple example from [[en:​secret:​people:​jens_horst:​start|Horst]] and an object oriented variant of [[en:​secret:​people:​yipyip:​start|yipyip]]. Both versions have a similar functionality but implement this in a different manner. If you are new to python/​pygame programming and have not yet worked with the **class** command you may want to try out the simple variant and come back if you are more familiar with classes. ​
Line 126: Line 123:
  
 This is the most basic meaningful pygame program. No fancy stuff. This is the most basic meaningful pygame program. No fancy stuff.
- 
-View/​Edit/​Download the file directly in Github: https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​002_display_fps.py ​ 
- 
-//click reload in your browser if you see no code here:// 
-<​html>​ 
- <​script src="​http://​gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​002_display_fps.py">​ 
- </​script></​html>​ 
- 
-\\ 
-==== OOP Version ==== 
- 
-An object orientated version, which uses the pygame.font module for displaying 
-frame rate and game duration as text. \\ 
-(See also [[step012|→pygame text]]) 
  
 <code python; title:​002_display_fps.py>​ <code python; title:​002_display_fps.py>​
Line 212: Line 195:
 </​code>​ </​code>​
  
-\\ 
  
-^ [[:​en:​pygame:​step001| ← previous]] ^ [[en:​pygame:​start| ↑ Overview]] ^ [[:​en:​pygame:​step003| → next ]] ^+==== OOP Version ====
  
-\\+An object orientated version, which uses the pygame.font module for displaying 
 +frame rate and game duration as text. \\ 
 +(See also [[step012|→pygame text]]) 
 + 
 + 
 + 
 + 
 +<code python>​ 
 +#​!/​usr/​bin/​env python 
 + 
 +"""​ 
 +002_display_fps_pretty.py 
 + 
 +Display framerate and playtime. 
 +Works with Python 2.7 and 3.3+. 
 + 
 +URL:     ​http://​thepythongamebook.com/​en:​part2:​pygame:​step002 
 +Author: ​ yipyip 
 +License: Do What The Fuck You Want To Public License (WTFPL) 
 +         See http://​sam.zoy.org/​wtfpl/​ 
 +"""​ 
 + 
 +#### 
 + 
 +import pygame 
 + 
 + 
 +#### 
 + 
 +class PygView(object):​ 
 + 
 + 
 +    def __init__(self,​ width=640, height=400, fps=30): 
 +        """​Initialize pygame, window, background, font,... 
 +        """​ 
 +        pygame.init() 
 +        pygame.display.set_caption("​Press ESC to quit"​) 
 +        self.width = width 
 +        self.height = height 
 +        #​self.height = width // 4 
 +        self.screen = pygame.display.set_mode((self.width,​ self.height),​ pygame.DOUBLEBUF) 
 +        self.background = pygame.Surface(self.screen.get_size()).convert() 
 +        self.clock = pygame.time.Clock() 
 +        self.fps = fps 
 +        self.playtime = 0.0 
 +        self.font = pygame.font.SysFont('​mono',​ 20, bold=True) 
 + 
 + 
 +    def run(self):​ 
 +        """​The mainloop 
 +        """​ 
 +        running = True 
 +        while running: 
 +            for event in pygame.event.get():​ 
 +                if event.type == pygame.QUIT:​ 
 +                    running = False 
 +                elif event.type == pygame.KEYDOWN:​ 
 +                    if event.key == pygame.K_ESCAPE:​ 
 +                        running = False 
 + 
 +            milliseconds = self.clock.tick(self.fps) 
 +            self.playtime += milliseconds / 1000.0 
 +            self.draw_text("​FPS:​ {:​6.3}{}PLAYTIME:​ {:6.3} SECONDS"​.format( 
 +                           ​self.clock.get_fps(),​ " "*5, self.playtime)) 
 + 
 +            pygame.display.flip() 
 +            self.screen.blit(self.background,​ (0, 0)) 
 + 
 +        pygame.quit() 
 + 
 + 
 +    def draw_text(self,​ text): 
 +        """​Center text in window 
 +        """​ 
 +        fw, fh = self.font.size(text) # fw: font width, ​ fh: font height 
 +        surface = self.font.render(text,​ True, (0, 255, 0)) 
 +        # // makes integer division in python3 
 +        self.screen.blit(surface,​ ((self.width - fw) // 2, (self.height - fh) // 2)) 
 + 
 +#### 
 + 
 +if __name__ == '​__main__':​ 
 + 
 +    # call with width of window and fps 
 +    PygView(640,​ 400).run() 
 +</​code>​ 
 + 
 + 
 +^ [[:​en:​pygame:​step001| ← previous]] ^ [[en:​pygame:​start| ↑ Overview]] ^ [[:​en:​pygame:​step003| → next ]] ^
  
-====== Comment this Page ====== 
-~~DISQUS~~ 
-\\ 
  
/var/www/horst/thepythongamebook.com/data/pages/en/pygame/step002.txt · Last modified: 2020/05/14 16:08 by Horst JENS