Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Sukuea. Shi nai shikakkei
- import sys, pygame, math
- from pygame.locals import *
- running = True
- fps = 30
- size = (608, 408)
- mid = ((size[0] + 1) / 2, (size[1] + 1) / 2)
- class Camera:
- def __init__(self, dim, foc=mid):
- self.rect = pygame.Rect((0, 0), dim)
- self.focus = pygame.Rect((0, 0), foc)
- def move(self, pos):
- if not self.focus.collidepoint(pos):
- if pos[0] >= self.focus.right:
- self.focus.right = pos[0]
- elif pos[0] < self.focus.left:
- self.focus.left = pos[0]
- if pos[1] >= self.focus.bottom:
- self.focus.bottom = pos[1]
- elif pos[1] < self.focus.top:
- self.focus.top = pos[1]
- self.rect.center = self.focus.center
- class Input:
- UP = 0
- LEFT = 1
- DOWN = 2
- RIGHT = 3
- GRAB = 4
- QUIT = 5
- 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 == 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)
- 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 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
- class sprite(base):
- def __init__(self, visible, dim=(16, 16)):
- base.__init__(self, dim, visible)
- self.vel = Point()
- self.mass, self.dir = 1, 0
- def update(self, objects):
- xr, yr, gr = False, False, False
- if self.vel.x != 0:
- self.slicePos.x += self.vel.x
- self.rect.centerx = int(self.slicePos.x)
- xr = self.handleCollision(objects, True)
- if self.vel.y != 0:
- self.slicePos.y += self.vel.y
- self.rect.centery = int(self.slicePos.y)
- gr = self.handleCollision(objects, False)
- yr = gr and self.vel.y < 0
- if yr:
- gr = False
- return (xr, yr, gr)
- def handleCollision(self, objects, xaxis):
- for item in objects:
- if self.rect.colliderect(item.rect):
- if xaxis:
- if self.vel.x > 0:
- self.rect.right = item.rect.left
- else:
- self.rect.left = item.rect.right
- self.slicePos.x = self.rect.centerx
- else:
- if self.vel.y > 0:
- self.rect.bottom = item.rect.top
- self.vel.y = 0
- else:
- self.rect.top = item.rect.bottom
- self.vel.y = -0.3
- self.slicePos.y = self.rect.centery
- return True
- return False
- def applyPush(self, dvx, dvy):
- self.vel.x += dvx / self.mass
- self.vel.y += dvy / self.mass
- def draw(self, surface, cam):
- base.draw(self, surface, cam)
- class Player(sprite):
- def __init__(self, visible, dim=(16, 16)):
- sprite.__init__(self, dim, visible)
- self.wall, self.ceiling = False, False
- self.grab, self.hang = False, False
- self.jump = True
- 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}
- def update(self, objects):
- if not self.hang:
- (self.wall, self.ceiling, gr) = sprite.update(self, objects)
- self.jump = (self.jump and self.vel.y < 0.5) or gr
- if not gr:
- self.vel.y += 0.1
- if self.vel.y > 6:
- self.vel.y = 6
- 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] and self.jump:
- self.jump = False
- self.vel.y += -4
- if self.wall and self.hang:
- self.hang = False
- elif inp[0] == Input.LEFT:
- if inp[1]:
- self.vel.x = -2
- elif self.vel.x == -2:
- self.vel.x = 0
- elif inp[0] == Input.DOWN:
- pass
- elif inp[0] == Input.RIGHT:
- if inp[1]:
- self.vel.x = 2
- elif self.vel.x == 2:
- self.vel.x = 0
- elif inp[0] == Input.GRAB:
- if inp[1]:
- self.grab = True
- if self.ceiling or self.wall:
- self.vel.y = 0
- self.hang = True
- self.jump = self.wall
- else:
- self.grab = False
- self.hang = False
- self.jump = False
- elif inp[0] == Input.QUIT:
- global running
- running = False
- else:
- print('Invalid input was passed to handleInput - "', inp, '"')
- pygame.init()
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('Sukuea. Shi nai shikakkei')
- pygame.mouse.set_visible(0)
- t = Player(rectangle(colour=(255, 0, 0)))
- c = Camera(size)
- t.warpSlice(mid)
- t.draw(screen, c)
- pygame.display.flip()
- 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)))]
- bound[0].rect.height = 160
- bound[0].warpSlice((mid[0] + 80, mid[1]))
- bound[0].vis.dim = (16, 160)
- bound[1].rect.height = 160
- bound[1].warpSlice((mid[0] - 80, mid[1]))
- bound[1].vis.dim = (16, 160)
- bound[2].rect.width = 800
- bound[2].warpSlice((mid[0], mid[1] + 80))
- bound[2].vis.dim = (800, 16)
- bound[3].rect.width = 80
- bound[3].warpSlice((mid[0], mid[1] - 80))
- bound[3].vis.dim = (80, 16)
- for o in bound:
- o.draw(screen, c)
- clock = pygame.time.Clock()
- running = True
- while running:
- t.handleInput(getInput())
- t.update(bound)
- c.move(t.rect.center)
- screen.fill((0, 0, 0))
- t.draw(screen, c)
- for o in bound:
- o.draw(screen, c)
- pygame.display.flip()
- clock.tick(fps)
Add Comment
Please, Sign In to add comment