mixster

ManFlardin

Apr 5th, 2010
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.61 KB | None | 0 0
  1. #!user/bin/env python
  2.  
  3.  
  4. import sys, pygame
  5. from pygame.locals import *
  6.  
  7.  
  8. #--- Our Variables
  9. black = (0,0,0)
  10. size = (320, 17*16)
  11. speed = [0,5]
  12.  
  13. #--- Init pygame
  14. pygame.init
  15.  
  16. #--- Display
  17. pygame.display.set_caption("Rectangle Man: Story of a Square")
  18. screen = pygame.display.set_mode(size)
  19.  
  20. class Player(object):
  21.    
  22.     def __init__(self):
  23.         self.rect = pygame.Rect(0, 0, 15, 16)
  24.  
  25.     def move(self, dx, dy):
  26.         if dx != 0:
  27.             self.move_single_axis(dx, 0)
  28.         if dy != 0:
  29.             self.move_single_axis(0, dy)
  30.    
  31.     def move_single_axis(self, dx, dy):
  32.         self.rect.x += dx
  33.         self.rect.y += dy
  34.        
  35.         tx, ty = int(player.rect.x / 16), int(player.rect.y / 16)
  36.         rects = [[tx, ty],[tx + 1, ty],[tx, ty + 1],[tx + 1, ty + 1]]
  37.  
  38.         for h in rects:
  39.             if walls[int(h[1])][int(h[0])] == 1:
  40.                 wam = pygame.Rect(h[0] * 16, h[1] * 16, 16, 16)
  41.                 if self.rect.colliderect(wam):
  42.                     if dx > 0:
  43.                         self.rect.right = wam.left
  44.                     if dx < 0:
  45.                         self.rect.left = wam.right
  46.                     if dy > 0:
  47.                         self.rect.bottom = wam.top
  48.                         global jump
  49.                         jump = 0
  50.                         speed[1] = 0
  51.                     if dy < 0:
  52.                         self.rect.top = wam.bottom
  53.                         speed[1] = 0
  54.  
  55. class Wall(object):
  56.    
  57.     def __init__(self, pos, tile):
  58.         tNum = tile
  59.        
  60. #--- Variables
  61. clock = pygame.time.Clock()
  62. #ball = pygame.image.load("ball.jpg").convert()
  63. jumping = False
  64. jump, jumpNum, inc, jHeight = 0, 2, 1, 10
  65.  
  66. level = ["""WWWWWWWWWWWWWWWWWWWW.WWWWWWWWWWWWWWWWWWWW.WS                 W.W         WWWWWW   W.W   WWWW       W   W.W   W        WWWW  W.W WWW  WWWW        W.W   W     W W      W.W   W     W   WWW WW.W   WWW WWW   W W  W.W     W   W   W W  W.WWW   W   WWWWW W  W.W W      WW        W.W W   WWWW   WWW   W.W     W    W   W   W.WWWWWWWWWWWWWWWWWWWW.WWWWWWWWWWWWWWWWWWWW"""
  67. ]
  68.            
  69. walls = []
  70. walls.append([])
  71. player = Player()
  72.  
  73. def getEvents():  
  74.     for event in pygame.event.get():    
  75.         if event.type == KEYDOWN:
  76.             if event.key == K_UP:
  77.                 global jumping, jump
  78.                 if jumping == False:
  79.                     jumping = True
  80.                     jump += 1
  81.                     speed[1] = -jHeight
  82.                     global inc
  83.                     inc = 1
  84.             if event.key == K_DOWN:
  85.                 player.rect = pygame.Rect(player.rect.x, player.rect.y + 8,16,8)
  86.                 pygame.draw.rect(screen, (0,0,0), ballbuff)
  87.             if event.key == K_LEFT:
  88.                 speed[0] = -4
  89.             if event.key == K_RIGHT:
  90.                 speed[0] = 4
  91.         if event.type == KEYUP:
  92.             if event.key == K_UP:
  93.                 inc = 2
  94.             if event.key == K_DOWN and speed[1] > 0:
  95.                 player.rect = pygame.Rect(player.rect.x, player.rect.y - 8,16,16)
  96.             if event.key == K_LEFT and speed[0] < 0:
  97.                 speed[0] = 0  
  98.             if event.key == K_RIGHT and speed[0] > 0:
  99.                 speed[0] = 0  
  100.         if event.type == pygame.QUIT:
  101.             running = False
  102.             sys.exit()
  103.            
  104. def loadMap(num):          
  105.     y = 0
  106.     for row in level[num]:
  107.         if row == ".":
  108.             walls.append([])
  109.             y += 1
  110.         if row == "W":
  111.             walls[y].append(1)
  112.         if row == " ":
  113.             walls[y].append(0)
  114.         if row == "S":
  115.             walls[y].append(2)
  116.             player.rect.x, player.rect.y = (len(walls) - 1) * 16, y * 16
  117.            
  118. loadMap(0)
  119. running = True
  120. while running:
  121.     ballbuff = pygame.Rect(player.rect.x, player.rect.y, 16, player.rect.bottom - player.rect.top)
  122.     getEvents()
  123.        
  124.     player.move(speed[0], speed[1])
  125.     if speed[1] < jHeight:
  126.         if speed[1] < 0:
  127.             speed[1] += inc
  128.         else:
  129.             speed[1] += 1
  130.     if jumping:
  131.         if (speed[1] >= 0) and (jump <= jumpNum - 1):
  132.             jumping = False
  133.     for i in range(0, len(walls)):
  134.         for e in range(0, len(walls[i])):
  135.             if walls[i][e] == 1:
  136.                 pygame.draw.rect(screen, (0, 0, 255), [e * 16, i * 16, 16, 16])
  137.             if walls[i][e] == 0:
  138.                 pygame.draw.rect(screen, (0, 0, 0), [e * 16, i * 16, 16, 16])
  139.     pygame.draw.rect(screen, (0,0,0), ballbuff) #used to be screen.blit...so change back for images
  140.     pygame.draw.rect(screen, (255, 200, 0), player)
  141.     clock.tick(30)
  142.     pygame.display.flip()
Add Comment
Please, Sign In to add comment