mixster

mixster

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