mixster

mixster

May 11th, 2010
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.89 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Sukuea. Shi nai shikakkei - Chizu edita!
  4.  
  5. import sys, pygame
  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. maprects = []
  13.  
  14. class Input:
  15.     UP     = 0
  16.     LEFT   = 1
  17.     DOWN   = 2
  18.     RIGHT  = 3
  19.     QUIT   = 5
  20.     MLEFT  = 6
  21.     MRIGHT = 7
  22.  
  23. class Camera:
  24.     def __init__(self, dim, foc=mid):
  25.         self.rect = pygame.Rect((0, 0), dim)
  26.         self.bindings = {pygame.K_UP: Input.UP, pygame.K_LEFT: Input.LEFT, pygame.K_DOWN: Input.DOWN, pygame.K_RIGHT: Input.RIGHT, pygame.K_q: Input.QUIT, 1: Input.MLEFT, 3: Input.MRIGHT}
  27.         self.vel = [0, 0]
  28.  
  29.     def update(self):
  30.         self.rect.move_ip(self.vel[0], self.vel[1])
  31.  
  32.     def parseInput(self, inputs):
  33.         res = []
  34.         for inp in inputs:
  35.             if self.bindings.has_key(inp[0]):
  36.                 res.append((self.bindings[inp[0]], inp[1]))
  37.         return res
  38.  
  39.     def handleInput(self, inputs):
  40.         for inp in self.parseInput(inputs):
  41.             if inp[0] == Input.UP:
  42.                 if inp[1]:
  43.                     self.vel[1] = -4
  44.                 else:
  45.                     self.vel[1] = 0
  46.             elif inp[0] == Input.DOWN:
  47.                 if inp[1]:
  48.                     self.vel[1] = 4
  49.                 else:
  50.                     self.vel[1] = 0
  51.             elif inp[0] == Input.LEFT:
  52.                 if inp[1]:
  53.                     self.vel[0] = -4
  54.                 else:
  55.                     self.vel[0] = 0
  56.             elif inp[0] == Input.RIGHT:
  57.                 if inp[1]:
  58.                     self.vel[0] = 4
  59.                 else:
  60.                     self.vel[0] = 0
  61.             elif inp[0] == Input.MLEFT:
  62.                 p = (inp[1][0][0] + self.rect.left, inp[1][0][1] + self.rect.top)
  63.                 if inp[1][1]:
  64.                     maprects.append(base(rectangle(colour=(0, 255, 0))))
  65.                     maprects[-1].rect.topleft = p
  66.                 else:
  67.                     maprects[-1].expand(p[0] - maprects[-1].rect.left, p[1] - maprects[-1].rect.top)
  68.             elif inp[0] == Input.MRIGHT:
  69.                 if inp[1][1]:
  70.                     p = (inp[1][0][0] + self.rect.left, inp[1][0][1] + self.rect.top)
  71.                     for r in reversed(maprects):
  72.                         if r.rect.collidepoint(p):
  73.                             maprects.remove(r)
  74.                             break
  75.             elif inp[0] == Input.QUIT:
  76.                 global running
  77.                 running = False
  78.             else:
  79.                 print('Invalid input was passed to handleInput - "', inp, '"')
  80.  
  81. def getInput():
  82.     res = []
  83.     for event in pygame.event.get():
  84.         if event.type == KEYDOWN:
  85.             res.append((event.key, True))
  86.  
  87.         if event.type == KEYUP:
  88.             res.append((event.key, False))
  89.  
  90.         if event.type == MOUSEBUTTONDOWN:
  91.             res.append((event.button, (event.pos, True)))
  92.  
  93.         if event.type == MOUSEBUTTONUP:
  94.             res.append((event.button, (event.pos, False)))
  95.  
  96.         if event.type == pygame.QUIT:
  97.             res.append((Input.QUIT, True))
  98.     return res
  99.  
  100. class Point:
  101.     def __init__(self, val=(0, 0)):
  102.         self.x, self.y = val[0], val[1]
  103.  
  104. class Pos:
  105.     def __init__(self, val=(0, 0, 0)):
  106.         self.x, self.y, self.z = val[0], val[1], val[2]
  107.  
  108. class rectangle:
  109.     def __init__(self, dimensions=(16, 16), colour=(0, 0, 0), width=0):
  110.         self.dim = dimensions
  111.         self.col = colour
  112.         self.wid = width
  113.  
  114.     def draw(self, surface, pos):
  115.         pygame.draw.rect(surface, self.col, pygame.Rect(pos, self.dim), self.wid)
  116.  
  117.     def expand(self, width, height):
  118.         self.dim = (width, height)
  119.  
  120. class base:
  121.     def __init__(self, visible, dim=(16, 16)):
  122.         self.rect = pygame.Rect((0, 0), dim)
  123.         self.rect.center = (0, 0)
  124.         self.slicePos = Point()
  125.         self.pos = Pos()
  126.         self.vis = visible
  127.  
  128.     def draw(self, surface, cam):
  129.         self.vis.draw(surface, (self.rect.left - cam.rect.left, self.rect.top - cam.rect.top))
  130.  
  131.     def expand(self, width, height):
  132.         self.rect.width = width
  133.         self.rect.height = height
  134.         self.vis.expand(width, height)
  135.  
  136.  
  137.     def update(self):
  138.         pass
  139.  
  140.     def warpWorld(self, newPos):
  141.         self.pos.x, self.pos.y, self.pos.z = newPos[0], newPos[1], newPos[2]
  142.  
  143.     def warpSlice(self, newPos):
  144.         self.slicePos.x, self.slicePos.y = newPos[0], newPos[1]
  145.         self.rect.center = newPos
  146.  
  147. pygame.init()
  148. screen = pygame.display.set_mode(size)
  149. pygame.display.set_caption('Sukuea. Shi nai shikakkei - chizu edita')
  150.  
  151. c = Camera(size)
  152. clock = pygame.time.Clock()
  153. t = base(rectangle(colour=(255, 0, 0)))
  154.  
  155. while running:
  156.     c.handleInput(getInput())
  157.     c.update()
  158.  
  159.     screen.fill((0, 0, 0))
  160.     for r in maprects:
  161.         r.draw(screen, c)
  162.     t.draw(screen, c)
  163.     pygame.display.flip()
  164.     clock.tick(fps)
Add Comment
Please, Sign In to add comment