Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Sukuea. Shi nai shikakkei - Chizu edita!
- import sys, pygame
- from pygame.locals import *
- running = True
- fps = 30
- size = (608, 408)
- mid = ((size[0] + 1) / 2, (size[1] + 1) / 2)
- maprects = []
- class Input:
- UP = 0
- LEFT = 1
- DOWN = 2
- RIGHT = 3
- QUIT = 5
- MLEFT = 6
- MRIGHT = 7
- class Camera:
- def __init__(self, dim, foc=mid):
- self.rect = pygame.Rect((0, 0), dim)
- 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}
- self.vel = [0, 0]
- def update(self):
- self.rect.move_ip(self.vel[0], self.vel[1])
- def parseInput(self, inputs):
- res = []
- for inp in inputs:
- if self.bindings.has_key(inp[0]):
- res.append((self.bindings[inp[0]], inp[1]))
- return res
- def handleInput(self, inputs):
- for inp in self.parseInput(inputs):
- if inp[0] == Input.UP:
- if inp[1]:
- self.vel[1] = -4
- else:
- self.vel[1] = 0
- elif inp[0] == Input.DOWN:
- if inp[1]:
- self.vel[1] = 4
- else:
- self.vel[1] = 0
- elif inp[0] == Input.LEFT:
- if inp[1]:
- self.vel[0] = -4
- else:
- self.vel[0] = 0
- elif inp[0] == Input.RIGHT:
- if inp[1]:
- self.vel[0] = 4
- else:
- self.vel[0] = 0
- elif inp[0] == Input.MLEFT:
- p = (inp[1][0][0] + self.rect.left, inp[1][0][1] + self.rect.top)
- if inp[1][1]:
- maprects.append(base(rectangle(colour=(0, 255, 0))))
- maprects[-1].rect.topleft = p
- else:
- maprects[-1].expand(p[0] - maprects[-1].rect.left, p[1] - maprects[-1].rect.top)
- elif inp[0] == Input.MRIGHT:
- if inp[1][1]:
- p = (inp[1][0][0] + self.rect.left, inp[1][0][1] + self.rect.top)
- for r in reversed(maprects):
- if r.rect.collidepoint(p):
- maprects.remove(r)
- break
- elif inp[0] == Input.QUIT:
- global running
- running = False
- else:
- print('Invalid input was passed to handleInput - "', inp, '"')
- def getInput():
- res = []
- for event in pygame.event.get():
- if event.type == KEYDOWN:
- res.append((event.key, True))
- if event.type == KEYUP:
- res.append((event.key, False))
- if event.type == MOUSEBUTTONDOWN:
- res.append((event.button, (event.pos, True)))
- if event.type == MOUSEBUTTONUP:
- res.append((event.button, (event.pos, False)))
- if event.type == pygame.QUIT:
- res.append((Input.QUIT, True))
- return res
- class Point:
- def __init__(self, val=(0, 0)):
- self.x, self.y = val[0], val[1]
- class Pos:
- def __init__(self, val=(0, 0, 0)):
- self.x, self.y, self.z = val[0], val[1], val[2]
- class rectangle:
- def __init__(self, dimensions=(16, 16), colour=(0, 0, 0), width=0):
- self.dim = dimensions
- self.col = colour
- self.wid = width
- def draw(self, surface, pos):
- pygame.draw.rect(surface, self.col, pygame.Rect(pos, self.dim), self.wid)
- def expand(self, width, height):
- self.dim = (width, height)
- class base:
- def __init__(self, visible, dim=(16, 16)):
- self.rect = pygame.Rect((0, 0), dim)
- self.rect.center = (0, 0)
- self.slicePos = Point()
- self.pos = Pos()
- self.vis = visible
- def draw(self, surface, cam):
- self.vis.draw(surface, (self.rect.left - cam.rect.left, self.rect.top - cam.rect.top))
- def expand(self, width, height):
- self.rect.width = width
- self.rect.height = height
- self.vis.expand(width, height)
- def update(self):
- pass
- def warpWorld(self, newPos):
- self.pos.x, self.pos.y, self.pos.z = newPos[0], newPos[1], newPos[2]
- def warpSlice(self, newPos):
- self.slicePos.x, self.slicePos.y = newPos[0], newPos[1]
- self.rect.center = newPos
- pygame.init()
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('Sukuea. Shi nai shikakkei - chizu edita')
- c = Camera(size)
- clock = pygame.time.Clock()
- t = base(rectangle(colour=(255, 0, 0)))
- while running:
- c.handleInput(getInput())
- c.update()
- screen.fill((0, 0, 0))
- for r in maprects:
- r.draw(screen, c)
- t.draw(screen, c)
- pygame.display.flip()
- clock.tick(fps)
Add Comment
Please, Sign In to add comment