Advertisement
Guest User

Untitled

a guest
Oct 27th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. import pygame
  2. import math
  3. from pygame import *
  4.  
  5. DISPLAY = (800, 640)
  6. DEPTH = 32
  7. FLAGS = 0
  8.  
  9. # in blocks, 25 x 20
  10.  
  11. def main():
  12.     pygame.init()
  13.     screen = display.set_mode(DISPLAY, FLAGS, DEPTH)
  14.     display.set_caption("Use arrows to move!")
  15.     timer = time.Clock()
  16.    
  17.     up = down = left = right = False
  18.     bg = Surface((32,32))
  19.     bg.convert()
  20.     bg.fill(Color("#000000"))
  21.     entities = pygame.sprite.Group()
  22.     player = Player(32, 32)
  23.     platforms = []
  24.    
  25.     x = y = 0
  26.     level = [
  27.     "PPPPPPPPPPPPPPPPPPPPPPPPP",
  28.     "P  P                    P",
  29.     "P  PPPPPP               P",
  30.     "P  P     P    P         P",
  31.     "P  P      P   P         P",
  32.     "P  P       P  P         P",
  33.     "P  P         PP    PPPPPP",
  34.     "P  P        P P         P",
  35.     "P  P       P  P         P",
  36.     "P  P      P   PPPPPP    P",
  37.     "P  P     P    PP        P",
  38.     "P  P PP       PPP      PP",
  39.     "P  PPPPP      PPPP    PPP",
  40.     "P       P     PPPPP  PPPP",
  41.     "P        PP   P         P",
  42.     "P            PP         P",
  43.     "P          PP P         P",
  44.     "P       PP    P         P",
  45.     "P      PPPP   PE        P",
  46.     "PPPPPPPPPPPPPPPPPPPPPPPPP",]
  47.     # build the level
  48.     for row in level:
  49.         for col in row:
  50.             if col == "P":
  51.                 p = Platform(x, y)
  52.                 platforms.append(p)
  53.                 entities.add(p)
  54.             if col == "E":
  55.                 e = ExitBlock(x, y)
  56.                 platforms.append(e)
  57.                 entities.add(e)
  58.             x += 32
  59.         y += 32
  60.         x = 0
  61.    
  62.     entities.add(player)
  63.    
  64.     while 1:
  65.         timer.tick(60)
  66.         for e in pygame.event.get():
  67.             if e.type == QUIT: raise SystemExit, "QUIT"
  68.             if e.type == KEYDOWN and e.key == K_ESCAPE:
  69.                 raise SystemExit, "ESCAPE"
  70.             if e.type == KEYDOWN and e.key == K_UP:
  71.                 up = True
  72.             if e.type == KEYDOWN and e.key == K_DOWN:
  73.                 down = True
  74.             if e.type == KEYDOWN and e.key == K_LEFT:
  75.                 left = True
  76.             if e.type == KEYDOWN and e.key == K_RIGHT:
  77.                 right = True
  78.            
  79.             if e.type == KEYUP and e.key == K_UP:
  80.                 up = False
  81.             if e.type == KEYUP and e.key == K_DOWN:
  82.                 down = False
  83.             if e.type == KEYUP and e.key == K_LEFT:
  84.                 left = False
  85.             if e.type == KEYUP and e.key == K_RIGHT:
  86.                 right = False
  87.        
  88.         # draw background
  89.         for y in range(20):
  90.             for x in range(25):
  91.                 screen.blit(bg, (x * 32, y * 32))
  92.        
  93.         # update player, draw everything else
  94.         player.update(up, down, left, right, platforms)
  95.         entities.draw(screen)
  96.        
  97.         pygame.display.flip()
  98.  
  99. class Entity(pygame.sprite.Sprite):
  100.     def __init__(self):
  101.         pygame.sprite.Sprite.__init__(self)
  102.  
  103. class Player(Entity):
  104.     def __init__(self, x, y):
  105.         Entity.__init__(self)
  106.         self.xvel = 0
  107.         self.yvel = 0
  108.         self.onGround = False
  109.         self.image = Surface((32, 32))
  110.         self.image.convert()
  111.         self.image.fill(Color("#FF0000"))
  112.         self.rect = Rect(x, y, 32, 32)
  113.    
  114.     def update(self, up, down, left, right, platforms):
  115.         if up:
  116.             # only jump if on the ground
  117.             if self.onGround: self.yvel -= 7
  118.         if down:
  119.             pass
  120.         if left:
  121.             self.xvel = -5
  122.         if right:
  123.             self.xvel = 5
  124.         if not self.onGround:
  125.             # only accelerate with gravity if in the air
  126.             self.yvel += 0.3
  127.             # max falling speed
  128.             if self.yvel > 30: self.yvel = 30
  129.         if not(left or right):
  130.             self.xvel = 0
  131.         # increment in x direction
  132.         self.rect.left += self.xvel
  133.         # do x-axis collisions
  134.         self.collide(self.xvel, 0, platforms)
  135.         # increment in y direction
  136.         self.rect.top += self.yvel
  137.         # assuming we're in the air
  138.         self.onGround = False;
  139.         # do y-axis collisions
  140.         self.collide(0, self.yvel, platforms)
  141.    
  142.     def collide(self, xvel, yvel, platforms):
  143.         for p in platforms:
  144.             if sprite.collide_rect(self, p):
  145.                 if isinstance(p, ExitBlock):
  146.                     event.post(event.Event(QUIT))
  147.                 if xvel > 0: self.rect.right = p.rect.left
  148.                 if xvel < 0: self.rect.left = p.rect.right
  149.                 if yvel > 0:
  150.                     self.rect.bottom = p.rect.top
  151.                     self.onGround = True
  152.                     self.yvel = 0
  153.                 if yvel < 0: self.rect.top = p.rect.bottom
  154.  
  155. class Platform(Entity):
  156.     def __init__(self, x, y):
  157.         Entity.__init__(self)
  158.         self.image = Surface((32, 32))
  159.         self.image.convert()
  160.         self.image.fill(Color("#DDDDDD"))
  161.         self.rect = Rect(x, y, 32, 32)
  162.    
  163.     def update(self):
  164.         pass
  165.  
  166. class ExitBlock(Platform):
  167.     def __init__(self, x, y):
  168.         Platform.__init__(self, x, y)
  169.         self.image.fill(Color("#0033FF"))
  170.        
  171. if(__name__ == "__main__"):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement