mixster

ManFlardin fixed by mixster

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