mixster

mixster

Apr 5th, 2010
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.22 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, 240)
  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 = int(player.rect.x / 16)
  36.         ty = int(player.rect.y / 16)
  37.         rects = [[tx, ty],[tx + 1, ty],[tx, ty + 1],[tx + 1, ty + 1]]
  38.  
  39.         for h in rects:
  40.             if walls[int(h[1])][int(h[0])] == 1:
  41.                 wam = pygame.Rect(h[0] * 16, h[1] * 16, 16, 16)
  42.                 if self.rect.colliderect(wam):
  43.                     if dx > 0:
  44.                         self.rect.right = wam.left
  45.                     if dx < 0:
  46.                         self.rect.left = wam.right
  47.                     if dy > 0:
  48.                         self.rect.bottom = wam.top
  49.                         global jump
  50.                         jump = 0
  51.                         speed[1] = 0
  52.                     if dy < 0:
  53.                         self.rect.top = wam.bottom
  54.                         speed[1] = 0
  55.  
  56. class Wall(object):
  57.    
  58.     def __init__(self, pos, tile):
  59.         tNum = tile
  60.        
  61. #--- Variables
  62. clock = pygame.time.Clock()
  63. #ball = pygame.image.load("ball.jpg").convert()
  64. #buff = pygame.image.load("buff.jpg").convert()
  65. rects = [pygame.Rect(0,0, 1, size[1]), pygame.Rect(0, size[1] - 20, size[0], size[1]), pygame.Rect(size[0] - 1, 0, size[0], size[1])]
  66. jumping = False
  67. jump = 0
  68. jumpNum = 2
  69. inc = 1
  70. jHeight = 10
  71.  
  72. level = ["""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."""
  73. ]
  74.            
  75. walls = []
  76. walls.append([])
  77. player = Player()
  78.  
  79. def getEvents():  
  80.     for event in pygame.event.get():    
  81.         if event.type == KEYDOWN:
  82.             if event.key == K_UP:
  83.                 global jumping, jump
  84.                 if jumping == False:
  85.                     jumping = True
  86.                     jump += 1
  87.                     speed[1] = -jHeight
  88.                     global inc
  89.                     inc = 1
  90.             if event.key == K_DOWN:
  91.                 player.rect = pygame.Rect(player.rect.x, player.rect.y + 8,16,8)
  92.                 pygame.draw.rect(screen, (0,0,0), ballbuff)
  93.             if event.key == K_LEFT:
  94.                 speed[0] = -4
  95.             if event.key == K_RIGHT:
  96.                 speed[0] = 4
  97.         if event.type == KEYUP:
  98.             if event.key == K_UP:
  99.                 inc = 2
  100.             if event.key == K_DOWN and speed[1] > 0:
  101.                 player.rect = pygame.Rect(player.rect.x, player.rect.y - 8,16,16)
  102.             if event.key == K_LEFT and speed[0] < 0:
  103.                 speed[0] = 0  
  104.             if event.key == K_RIGHT and speed[0] > 0:
  105.                 speed[0] = 0  
  106.         if event.type == pygame.QUIT:
  107.             running = False
  108.             sys.exit()
  109.            
  110. def loadMap(num):          
  111.     y = 0
  112.     for row in level[num]:
  113.         if row == ".":
  114.             walls.append([])
  115.             y += 1
  116.         if row == "W":
  117.             walls[y].append(1)
  118.         if row == " ":
  119.             walls[y].append(0)
  120.         if row == "S":
  121.             walls[y].append(2)
  122.             player.rect.x = (len(walls) - 1) * 16
  123.             player.rect.y = y * 16
  124.            
  125. loadMap(0)
  126. running = True
  127. while running:
  128.     ballbuff = pygame.Rect(player.rect.x, player.rect.y, 16, player.rect.bottom - player.rect.top)
  129.     getEvents()
  130.        
  131.     player.move(speed[0], speed[1])
  132.     if speed[1] < jHeight:
  133.         if speed[1] < 0:
  134.             speed[1] += inc
  135.         else:
  136.             speed[1] += 1
  137.     if jumping:
  138.         if (speed[1] >= 0) and (jump <= jumpNum - 1):
  139.             jumping = False
  140.     for i in range(0, len(rects)):
  141.         pygame.draw.rect(screen, (0, 255, 0), rects[i])
  142.     for i in range(0, len(walls)):
  143.         for e in range(0, len(walls[i])):
  144.             if walls[i][e] == 1:
  145.                 pygame.draw.rect(screen, (0, 0, 255), [e * 16, i * 16, 16, 16])
  146.             if walls[i][e] == 0:
  147.                 pygame.draw.rect(screen, (0, 0, 0), [e * 16, i * 16, 16, 16])
  148.    
  149.     tx = player.rect.x / 16
  150.     ty = player.rect.y / 16
  151.     pygame.draw.rect(screen, (0,255,0), [tx * 16, ty * 16, 16, 16])
  152.     pygame.draw.rect(screen, (0,255,0), [(tx + 1) * 16, ty * 16, 16, 16])
  153.     pygame.draw.rect(screen, (0,255,0), [tx * 16, (ty + 1) * 16, 16, 16])
  154.     pygame.draw.rect(screen, (0,255,0), [(tx + 1) * 16, (ty + 1) * 16, 16, 16])
  155.     pygame.draw.rect(screen, (0,0,0), ballbuff) #used to be screen.blit...so change back for images
  156.     pygame.draw.rect(screen, (255, 200, 0), player)
  157.     clock.tick(30)
  158.     pygame.display.flip()
Add Comment
Please, Sign In to add comment