Advertisement
Guest User

mario double jump

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