mixster

Untitled

Apr 12th, 2010
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.87 KB | None | 0 0
  1. #!user/bin/env python
  2.  
  3.  
  4. import sys, pygame, os
  5. from pygame.locals import *
  6.  
  7.  
  8. #--- Our Variables
  9. black = (0,0,0)
  10. res = (16, 16)
  11. size = (20 * res[0], 17 * res[1])
  12. speed = [0,0]
  13. pSize = (res[0] - 1, res[1])
  14. sp = (4, 10)
  15.  
  16. #--- Init pygame
  17. os.environ["SDL_VIDEO_CENTERED"] = "1"
  18. pygame.init
  19.  
  20. #--- Display
  21. pygame.display.set_caption("Rectangle Man: Story of a Square")
  22. screen = pygame.display.set_mode(size)
  23.  
  24. class Player(object):
  25.    
  26.     def __init__(self):
  27.         self.rect = pygame.Rect(0, 0, pSize[0], pSize[1])
  28.  
  29.     def move(self, dx, dy):
  30.         if dx != 0:
  31.             self.move_single_axis(dx, 0)
  32.         if dy != 0:
  33.             self.move_single_axis(0, dy)
  34.    
  35.     def move_single_axis(self, dx, dy):
  36.         self.rect.x += dx
  37.         self.rect.y += dy
  38.        
  39.         tx, ty = int(player.rect.x / res[0]), int(player.rect.y / res[1])
  40.         rects = [[tx, ty],[tx + 1, ty],[tx, ty + 1],[tx + 1, ty + 1]]
  41.  
  42.         for h in rects:
  43.             if walls[int(h[0])][int(h[1])].tNum == 1:
  44.                 wam = walls[int(h[0])][int(h[1])].rect
  45.                 if self.rect.colliderect(wam):
  46.                     if dx > 0:
  47.                         self.rect.right = wam.left
  48.                     if dx < 0:
  49.                         self.rect.left = wam.right
  50.                     if dy > 0:
  51.                         self.rect.bottom = wam.top
  52.                         global jump
  53.                         jump = 0
  54.                         speed[1] = 0
  55.                         #return
  56.                     if dy < 0:
  57.                         self.rect.top = wam.bottom
  58.                         speed[1] = 0
  59.                     return
  60.  
  61. class Wall(object):
  62.    
  63.     def __init__(self, pos):
  64.         self.rect = pygame.Rect(pos[0], pos[1], pos[2], pos[3])
  65.         self.tNum = pos[4]
  66.        
  67. #--- Variables
  68. clock = pygame.time.Clock()
  69. #ball = pygame.image.load("ball.jpg").convert()
  70. jumping, uCr = False, False
  71. jump, jumpNum, inc = 0, 2, 1
  72.            
  73. walls = []
  74. player = Player()
  75. camera = pygame.Rect(0, 0, screen.get_width(), screen.get_height())
  76.  
  77. def translate(rect):
  78.     global camera
  79.     return pygame.Rect(rect.x - camera.x, rect.y - camera.y, rect.w, rect.h)
  80.  
  81. def getEvents():  
  82.     global camera
  83.     for event in pygame.event.get():    
  84.         if event.type == KEYDOWN:
  85.             if event.key == K_UP:
  86.                 global jumping, jump
  87.                 if jumping == False:
  88.                     jumping = True
  89.                     jump += 1
  90.                     speed[1] = -sp[1]
  91.                     global inc
  92.                     inc = 1
  93.             if event.key == K_DOWN:
  94.                 player.rect = pygame.Rect(player.rect.x, player.rect.y + 8, pSize[0], pSize[1]/2)
  95.                 pygame.draw.rect(screen, (0,0,0), ballbuff)
  96.             if event.key == K_LEFT:
  97.                 speed[0] = -sp[0]
  98.             if event.key == K_RIGHT:
  99.                 speed[0] = sp[0]
  100.         if event.type == KEYUP:
  101.             if event.key == K_UP:
  102.                 inc = 2
  103.             if event.key == K_DOWN and speed[1] > 0:
  104.                 q = player.rect.x
  105.                 player.move(0, -res[1]/2)
  106.                 if player.rect.x == q:
  107.                     player.move(0, res[1]/2)
  108.                     player.rect = pygame.Rect(player.rect.x, player.rect.y - 8, pSize[0], pSize[1])
  109.             if event.key == K_LEFT and speed[0] < 0:
  110.                 speed[0] = 0  
  111.             if event.key == K_RIGHT and speed[0] > 0:
  112.                 speed[0] = 0  
  113.         if event.type == pygame.QUIT:
  114.             running = False
  115.             sys.exit()
  116.                      
  117. def parseLevel(filename):
  118.     fileObj = open("level.txt")
  119.     st = fileObj.read()
  120.     fileObj.close()
  121.     x = st.find(".", 0)
  122.     y = st.find("(", x)
  123.     h = 0
  124.     m = []
  125.     for i in range(0, int(st[0:x])):
  126.         walls.append([])
  127.         for e in range(0, int(st[x + 1:y])):
  128.             h = st.find("(", h) + 1
  129.             h2 = st.find(")", h)
  130.             for la in range(0, 5):
  131.                 go = st.find(",", h, h2)
  132.                 m.append(int(st[h:go]))
  133.                 h = go + 1
  134.             if m[4] == 2:
  135.                 m[4] = 0
  136.                 player.rect = pygame.Rect(m[0], m[1], m[2], m[3])
  137.             walls[i].append(Wall((m[0], m[1], m[2], m[3], m[4])))
  138.             m = []
  139.            
  140. parseLevel("level.txt")          
  141. running = True
  142. while running:
  143.     ballbuff = pygame.Rect(player.rect.x, player.rect.y, res[0], player.rect.bottom - player.rect.top)
  144.     getEvents()
  145.        
  146.     player.move(speed[0], speed[1])
  147.     camera.x = player.rect.x - (camera.width / 2)
  148.     if camera.x < 0:
  149.         camera.x = 0
  150.     if camera.x + camera.width > len(walls) * res[0]:
  151.         camera.x = (len(walls) * res[0]) - camera.width
  152.  
  153.     camera.y = player.rect.y - (camera.height / 2)
  154.     if camera.y < 0:
  155.         camera.y = 0
  156.     if camera.y + camera.height > len(walls[0]) * res[1]:
  157.         camera.y = (len(walls[0]) * res[1]) - camera.height        
  158.     if speed[1] < sp[1]:
  159.         if speed[1] < 0:
  160.             speed[1] += inc
  161.         else:
  162.             speed[1] += 1
  163.     if jumping:
  164.         if (speed[1] >= 0) and (jump <= jumpNum - 1):
  165.             jumping = False
  166.     for i in range(0, len(walls)):
  167.         for e in range(0, len(walls[i])):
  168.             rect = translate(walls[i][e].rect)
  169.             col = {0 : (0,0,0), 1: (0,0,255), 2 : (0,0,0)}[walls[i][e].tNum]
  170.             pygame.draw.rect(screen, col, rect)
  171.                 #pygame.draw.rect(screen, (0, 0, 255), [e * 16, i * 16, 16, 16])
  172.             #if walls[i][e].tNum == 0:
  173.             #    pygame.draw.rect(screen, (0, 0, 0), [e * 16, i * 16, 16, 16])
  174.     rect = translate(ballbuff)
  175.     pygame.draw.rect(screen, (0,0,0), rect) #used to be screen.blit...so change back for images
  176.     rect = translate(player.rect)
  177.     pygame.draw.rect(screen, (255, 200, 0), rect)
  178.     clock.tick(30)
  179.     pygame.display.flip()
Add Comment
Please, Sign In to add comment