mixster

mixster

Apr 29th, 2010
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.31 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Sukuea. Shi nai shikakkei
  4.  
  5. import sys, pygame, math
  6. from pygame.locals import *
  7.  
  8. running = True
  9. fps = 30
  10. size = (608, 408)
  11. mid = ((size[0] + 1) / 2, (size[1] + 1) / 2)
  12.  
  13. class Camera:
  14.     def __init__(self):
  15.         self.rect = pygame.Rect((0, 0), size)
  16.         self.focus = pygame.Rect((0, 0), mid)
  17.  
  18.     def move(self, pos):
  19. #        self.rect.center = pos
  20.         if not self.focus.collidepoint(pos):
  21.             if pos[0] >= self.focus.right:
  22.                 self.focus.right = pos[0]
  23.             elif pos[0] < self.focus.left:
  24.                 self.focus.left = pos[0]
  25.             if pos[1] >= self.focus.bottom:
  26.                 self.focus.bottom = pos[1]
  27.             elif pos[1] < self.focus.top:
  28.                 self.focus.top = pos[1]
  29.             self.rect.center = self.focus.center
  30.  
  31.  
  32. class Input:
  33.     UP    = 0
  34.     LEFT  = 1
  35.     DOWN  = 2
  36.     RIGHT = 3
  37.     GRAB  = 4
  38.     QUIT  = 5
  39.  
  40. def getInput():
  41.     res = []
  42.     for event in pygame.event.get():
  43.         if event.type == KEYDOWN:
  44.             #if bindings.has_key(event.key):
  45.             res.append((event.key, True))
  46.  
  47.         if event.type == KEYUP:
  48.             #if bindings.has_key(event.key):
  49.             res.append((event.key, False))
  50.  
  51.         if event.type == pygame.QUIT:
  52.             res.append((Input.QUIT, True))
  53.     return res
  54.  
  55. class Point:
  56.     def __init__(self, val=(0, 0)):
  57.         self.x, self.y = val[0], val[1]
  58.  
  59. class Pos:
  60.     def __init__(self, val=(0, 0, 0)):
  61.         self.x, self.y, self.z = val[0], val[1], val[2]
  62.  
  63. class rectangle:
  64.     def __init__(self):
  65.         self.dim = (16, 16)
  66.         self.col = (255, 0, 0)
  67.         self.wid = 0
  68.  
  69.     def draw(self, surface, pos):
  70.         pygame.draw.rect(surface, self.col, pygame.Rect(pos, self.dim), self.wid)
  71.  
  72.  
  73. class base:
  74.     def __init__(self):
  75.         self.rect = pygame.Rect(-8, -8, 16, 16)
  76.         self.slicePos = Point()
  77.         self.pos = Pos()
  78.         self.vis = rectangle()
  79.  
  80.     def draw(self, surface, cam):
  81.         self.vis.draw(surface, (self.rect.left - cam.rect.left, self.rect.top - cam.rect.top))
  82.  
  83.     def update(self):
  84.         pass
  85.  
  86.     def warpWorld(self, newPos):
  87.         self.pos.x, self.pos.y, self.pos.z = newPos[0], newPos[1], newPos[2]
  88.  
  89.     def warpSlice(self, newPos):
  90.         self.slicePos.x, self.slicePos.y = newPos[0], newPos[1]
  91.         self.rect.center = newPos
  92.  
  93. class sprite(base):
  94.     def __init__(self):
  95.         base.__init__(self)
  96.         self.vel = Point()
  97.         self.mass, self.dir = 1, 0
  98.  
  99.     def update(self, objects):
  100.         xr, yr, gr = False, False, False
  101.         if self.vel.x != 0:
  102.             self.slicePos.x += self.vel.x
  103.             self.rect.centerx = int(self.slicePos.x)
  104.             xr = self.handleCollision(objects, True)
  105.  
  106.         if self.vel.y != 0:
  107.             self.slicePos.y += self.vel.y
  108.             self.rect.centery = int(self.slicePos.y)
  109.             gr = self.handleCollision(objects, False)
  110.             yr = gr and self.vel.y < 0
  111.             if yr:
  112.                 gr = False
  113.  
  114.         return (xr, yr, gr)
  115.  
  116.     def handleCollision(self, objects, xaxis):
  117.         for item in objects:
  118.             if self.rect.colliderect(item.rect):
  119.                 if xaxis:
  120.                     if self.vel.x > 0:
  121.                         self.rect.right = item.rect.left
  122.                     else:
  123.                         self.rect.left = item.rect.right
  124.                     self.slicePos.x = self.rect.centerx
  125.  
  126.                 else:
  127.                     if self.vel.y > 0:
  128.                         self.rect.bottom = item.rect.top
  129.                         self.vel.y = 0
  130.                     else:
  131.                         self.rect.top = item.rect.bottom
  132.                         self.vel.y = -0.3
  133.  
  134.                     self.slicePos.y = self.rect.centery
  135.  
  136.                 return True
  137.  
  138.         return False
  139.  
  140.     def applyPush(self, dvx, dvy):
  141.         self.vel.x += dvx / self.mass
  142.         self.vel.y += dvy / self.mass
  143.  
  144.     def draw(self, surface, cam):
  145.         base.draw(self, surface, cam)
  146.  
  147.  
  148.  
  149. class Player(sprite):
  150.     def __init__(self):
  151.         sprite.__init__(self)
  152.         self.grab, self.wall, self.ceiling, self.hang = False, False, False, False
  153.         self.jump = True
  154.         self.bindings = {pygame.K_UP: Input.UP, pygame.K_LEFT: Input.LEFT, pygame.K_DOWN: Input.GRAB, pygame.K_RIGHT: Input.RIGHT, pygame.K_SPACE: Input.GRAB, pygame.K_q: Input.QUIT}
  155.  
  156.     def update(self, objects):
  157.         if not self.hang:
  158.             (self.wall, self.ceiling, gr) = sprite.update(self, objects)
  159.             self.jump = (self.jump and self.vel.y < 0.5) or gr
  160.  
  161.             if not gr:
  162.                 self.vel.y += 0.1
  163.  
  164.             if self.vel.y > 6:
  165.                 self.vel.y = 6
  166.  
  167.     def parseInput(self, inputs):
  168.         res = []
  169.         for inp in inputs:
  170.             if self.bindings.has_key(inp[0]):
  171.                 res.append((self.bindings[inp[0]], inp[1]))
  172.         return res
  173.  
  174.     def handleInput(self, inputs):
  175.         for inp in self.parseInput(inputs):
  176.             if inp[0] == Input.UP:
  177.                 if inp[1] and self.jump:
  178.                     self.jump = False
  179.                     self.vel.y += -4
  180.                     if self.wall and self.hang:
  181.                         self.hang = False
  182.             elif inp[0] == Input.LEFT:
  183.                 if inp[1]:
  184.                     self.vel.x = -2
  185.                 elif self.vel.x == -2:
  186.                     self.vel.x = 0
  187.             elif inp[0] == Input.DOWN:
  188.                 pass
  189.             elif inp[0] == Input.RIGHT:
  190.                 if inp[1]:
  191.                     self.vel.x = 2
  192.                 elif self.vel.x == 2:
  193.                     self.vel.x = 0
  194.             elif inp[0] == Input.GRAB:
  195.                 if inp[1]:
  196.                     self.grab = True
  197.                     if self.ceiling or self.wall:
  198.                         self.vel.y = 0
  199.                         self.hang = True
  200.                         self.jump = self.wall
  201.                 else:
  202.                     self.grab = False
  203.                     self.hang = False
  204.                     self.jump = False
  205.                    
  206.             elif inp[0] == Input.QUIT:
  207.                 global running
  208.                 running = False
  209.             else:
  210.                 print('Invalid input was passed to handleInput - "', inp, '"')
  211.  
  212.  
  213. pygame.init()
  214. screen = pygame.display.set_mode(size)
  215. pygame.display.set_caption('Sukuea. Shi nai shikakkei')
  216. pygame.mouse.set_visible(0)
  217.  
  218. t = Player()
  219. t.vis.col = (0, 255, 0)
  220. c = Camera()
  221. t.warpSlice(mid)
  222. t.draw(screen, c)
  223. pygame.display.flip()
  224.  
  225. bound = [base(), base(), base(), base()]
  226. bound[0].rect.height = 160
  227. bound[0].warpSlice((mid[0] + 80, mid[1]))
  228. bound[0].vis.dim = (16, 160)
  229.  
  230. bound[1].rect.height = 160
  231. bound[1].warpSlice((mid[0] - 80, mid[1]))
  232. bound[1].vis.dim = (16, 160)
  233.  
  234. bound[2].rect.width = 800
  235. bound[2].warpSlice((mid[0], mid[1] + 80))
  236. bound[2].vis.dim = (800, 16)
  237.  
  238. bound[3].rect.width = 80
  239. bound[3].warpSlice((mid[0], mid[1] - 80))
  240. bound[3].vis.dim = (80, 16)
  241.  
  242. for o in bound:
  243.     o.draw(screen, c)
  244.  
  245. clock = pygame.time.Clock()
  246. running = True
  247.  
  248. while running:
  249.     t.handleInput(getInput())
  250.     t.update(bound)
  251.     c.move(t.rect.center)
  252.  
  253.     screen.fill((0, 0, 0))
  254.     t.draw(screen, c)
  255.     for o in bound:
  256.         o.draw(screen, c)
  257.  
  258.     pygame.display.flip()
  259.     clock.tick(fps)
Add Comment
Please, Sign In to add comment