Advertisement
Guest User

mario jump

a guest
Mar 17th, 2013
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. WINDOW_TITLE = "mario jump"
  2.  
  3. import pygame
  4. from pygame.locals import Rect, Color
  5. from pygame.sprite import Sprite
  6. from pygame import *
  7.  
  8. class Mario(Sprite):
  9.     def __init__(self):
  10.         """
  11.        hold key = move
  12.        tap = jumping"""
  13.         Sprite.__init__(self)
  14.         self.image = Surface([40,80])
  15.         self.image.fill(Color("gray80"))
  16.         self.rect = self.image.get_rect()        
  17.         self.screen = pygame.display.get_surface()
  18.  
  19.         # start mario centered on bot
  20.         self.rect.midbottom = self.screen.get_rect().midbottom
  21.  
  22.         self.velx, self.vely = 0., 0.
  23.  
  24.     def update(self):
  25.         # gravity
  26.         self.vely += 4
  27.  
  28.         # units update velocity, even if you don't have gravity
  29.         self.rect.left += self.velx
  30.         self.rect.top += self.vely
  31.  
  32.         # don't go off screen
  33.         if self.rect.bottom > self.screen.get_rect().bottom:
  34.             self.rect.bottom = self.screen.get_rect().bottom
  35.             self.vely = 0
  36.  
  37.    
  38.     def draw(self):        
  39.         self.screen.blit(self.image, self.rect)      
  40.  
  41.     def jump(self):
  42.         """jump if able considering cooldown and state"""
  43.         self.vely -= 50
  44.         print "jump"
  45.  
  46.  
  47. class Game(object):
  48.     """game Main entry point. handles intialization of game and graphics, as well as game loop"""    
  49.     done = False
  50.    
  51.     def __init__(self, width=800, height=600):
  52.         """Initialize PyGame window. boilerplate stuff.
  53.        
  54.        variables:
  55.            width, height = screen width, height
  56.            screen = main video surface, to draw on
  57.            
  58.            fps_max     = framerate limit to the max fps
  59.            limit_fps   = boolean toggles capping FPS, to share cpu, or let it run free.
  60.            color_bg    = backround color, accepts many formats. see: pygame.Color() for details
  61.        """
  62.         pygame.init()
  63.  
  64.         # save w, h, and screen
  65.         self.width, self.height = width, height
  66.         self.screen = pygame.display.set_mode(( self.width, self.height ))
  67.         pygame.display.set_caption( WINDOW_TITLE )        
  68.  
  69.         # fps clock, limits max fps
  70.         self.clock = pygame.time.Clock()
  71.         self.fps_max = 40        
  72.  
  73.         self.mario = Mario()
  74.  
  75.     def main_loop(self):
  76.         """Game() main loop goes like this:
  77.        
  78.            1. player input
  79.            2. move stuff
  80.            3. draw stuff
  81.        """
  82.         while not self.done:
  83.             self.handle_events()                                
  84.             self.update()
  85.             self.draw()
  86.            
  87.             # cap FPS if: limit_fps == True
  88.             self.clock.tick( self.fps_max )
  89.    
  90.     def draw(self):
  91.         """draw screen"""
  92.         self.screen.fill(Color("gray20"))
  93.         self.mario.draw()
  94.         pygame.display.flip()
  95.        
  96.     def update(self):
  97.         """physics. collisions."""
  98.         self.mario.update()
  99.  
  100.     def handle_events(self):
  101.         """handle events: keyboard, mouse, etc."""
  102.         events = pygame.event.get()
  103.        
  104.         for event in events:
  105.             if event.type == pygame.QUIT: self.done = True
  106.  
  107.             elif event.type == KEYDOWN:
  108.                 if event.key == K_ESCAPE:
  109.                     self.done = True
  110.                 elif event.key == K_SPACE:
  111.                     self.mario.jump()
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.                    
  121. if __name__ == "__main__":        
  122.     print """Keys:
  123.    ESC    = quit
  124. """
  125.    
  126.     game = Game()
  127.     game.main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement