Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/user/bin/env python
- import sys
- import os
- import pygame
- from pygame.locals import *
- #--- Init pygame
- os.environ["SDL_VIDEO_CENTERED"] = "1"
- pygame.init
- #--- Variables
- res = (32, 32)
- PLAYER_NUM = 1
- TILE_SET_LENGTH = 9
- #TILE_SET_LENGTH = 20
- clock = pygame.time.Clock()
- #--- Setup
- gameName = "Rectangle Man: Story of a Square"
- levelFile = "level.txt"
- tilesetFile = "tileset.bmp"
- screenSize = (20, 17)
- FPS = 30
- class Entity(object):
- def __init__(self, Size):
- self.rect = pygame.Rect(Size[0], Size[1], Size[2], Size[3])
- self.permSize = (self.rect[2], self.rect[3])
- class Player(Entity):
- def __init__(self, Size, Speed):
- Entity.__init__(self, Size)
- self.topSpeed = Speed
- self.speed = [0, 0]
- self.uc = 16
- self.currentJump = 0
- self.totJumpNum = 2
- self.jIncrement = 1
- self.jumping = False
- self.controlled = True
- self.tPos = 0
- self.sliding = False
- self.hanging = False
- input = {}
- input["jump"] = [K_UP];
- def move(self):
- if self.speed[0] != 0:
- self.move_single_axis(self.speed[0], 0)
- if self.speed[1] != 0:
- if self.sliding:
- self.move_single_axis(0, self.speed[1] / 4)
- else:
- self.move_single_axis(0, self.speed[1])
- def collide(self, dx, dy, collideBox):
- if self.rect.colliderect(collideBox):
- if dx > 0:
- self.rect.right = collideBox.left
- if self.speed[1] > 0:
- self.sliding = True
- if dx < 0:
- self.rect.left = collideBox.right
- if self.speed[1] > 4:
- self.sliding = True
- if dy > 0:
- self.rect.bottom = collideBox.top
- self.currentJump = 0
- self.speed[1] = 0
- if self.rect.height == self.permSize[1] / 2 and self.speed[0] < 0:
- if self.rect.x + self.permSize[0] / 2 < collideBox.x:
- self.rect.height = self.permSize[0]
- self.rect = pygame.Rect(collideBox.x - self.permSize[0] / 2, collideBox.y, self.permSize[0] / 2, self.rect.height)
- self.hanging = True
- if self.rect.height == self.permSize[1] / 2 and self.speed[0] > 0:
- if self.rect.x > collideBox.x + collideBox.width / 2:
- self.rect.height = self.permSize[0]
- self.rect = pygame.Rect(collideBox.x + collideBox.width, collideBox.y, self.permSize[0] / 2, self.rect.height)
- self.hanging = True
- if dy < 0:
- self.rect.top = collideBox.bottom
- self.speed[1] = 0
- return
- def move_single_axis(self, dx, dy):
- self.rect.x += dx
- self.rect.y += dy
- tx, ty = int(self.rect.x / res[0]), int(self.rect.y / res[1])
- rects = [[tx, ty], [tx + 1, ty], [tx, ty + 1], [tx + 1, ty + 1]]
- for h in rects:
- if levels.map[int(h[0])][int(h[1])].tNum == 1:
- wam = levels.map[int(h[0])][int(h[1])].rect
- self.collide(dx, dy, wam)
- for players in player:
- if self <> players:
- self.collide(dx, dy, players.rect)
- def input(self, type, keys):
- if type == KEYDOWN:
- if keys == K_UP:#input["jump"]:
- if self.jumping == False:
- self.jumping = True
- self.currentJump += 1
- self.speed[1] = -self.topSpeed[1]
- self.increment = 1
- self.hanging = False
- if keys == K_DOWN:
- self.rect = pygame.Rect(self.rect.x, self.rect.y + self.permSize[1] / 2, self.permSize[0], self.permSize[1] / 2)
- if keys == K_LEFT:
- self.speed[0] = -self.topSpeed[0]
- if keys == K_RIGHT:
- self.speed[0] = self.topSpeed[0]
- #if keys == K_RCTRL:
- if type == KEYUP:
- if keys == K_UP:
- self.increment = 2
- if keys == K_DOWN and self.speed[1] > 0:
- if self.rect.height <> self.permSize[0]:
- q = self.rect.x
- self.move(0, -res[1] / 2)
- if self.rect.x == q:
- self.move(0, res[1] / 2)
- self.rect = pygame.Rect(self.rect.x, self.rect.y - self.permSize[1] / 2, self.permSize[0], self.permSize[1])
- if keys == K_LEFT and self.speed[0] < 0:
- self.speed[0] = 0
- if keys == K_RIGHT and self.speed[0] > 0:
- self.speed[0] = 0
- #if keys == K_RCTRL:
- # camera.target = player[0]
- def tick(self, inputs):
- if len(inputs) != 0:
- for inputy in inputs:
- self.input(inputy[0], inputy[1])
- self.move()
- if self.speed[1] <= self.topSpeed[1]:
- if self.speed[1] < 0:
- self.speed[1] += self.increment * levels.res[1]
- else:
- if self.sliding == True:
- self.speed[1] += 1 * levels.res[1] * 0.25
- else:
- self.speed[1] += 1 * levels.res[1]
- if self.jumping:
- if (self.speed[1] >= 0) and (self.currentJump <= self.totJumpNum - 1):
- self.jumping = False
- self.sliding = False
- class Wall(object):
- def __init__(self, pos):
- self.rect = pygame.Rect(pos[0], pos[1], pos[2], pos[3])
- self.tNum = pos[4]
- self.tile = pos[5]
- class Level(object):
- def __init__(self, (size, resolution, mapArray, playerStart)):
- self.size = size
- self.res = resolution
- self.map = mapArray
- self.start = playerStart
- class Camera(Entity):
- def __init__(self, Size, Target):
- Entity.__init__(self, Size)
- self.target = Target
- def tick(self):
- self.rect.x = self.target.rect.x - (self.rect.width / 2)
- if self.rect.x < 0:
- self.rect.x = 0
- if self.rect.x + self.rect.width > len(levels.map) * res[0]:
- self.rect.x = (len(levels.map) * res[0]) - self.rect.width
- self.rect.y = self.target.rect.y - (self.rect.height / 2)
- if self.rect.y < 0:
- self.rect.y = 0
- if self.rect.y + self.rect.height > len(levels.map[0]) * res[1]:
- self.rect.y = (len(levels.map[0]) * res[1]) - self.rect.height
- def translate(rect, Cam):
- return pygame.Rect(rect.x - Cam.rect.x, rect.y - Cam.rect.y, rect.w, rect.h)
- def getEvents():
- res = []
- for event in pygame.event.get():
- if event.type == KEYDOWN or event.type == KEYUP:
- res.append((event.type, event.key))
- if event.type == pygame.QUIT:
- gameRunning = False
- sys.exit()
- return res
- def parseLevel(filename):
- COMMA_ITER = 6
- pos, walls, tot = [], [], []
- fileObj = open(filename)
- s = fileObj.read()
- fileObj.close()
- mRes = s.find(".", 0)
- tot = [float(res[0]) / float(s[0: mRes]), float(res[1]) / float(s[0: mRes])]
- mx = s.find(".", mRes + 1)
- my = s.find("(", mx)
- strPart = 0
- for i in range(0, int(s[mRes + 1: mx])):
- walls.append([])
- for e in range(0, int(s[mx + 1: my])):
- strPart = s.find("(", strPart) + 1
- strPart2 = s.find(")", strPart)
- for la in range(0, COMMA_ITER):
- final = s.find(",", strPart, strPart2)
- pos.append(int(s[strPart: final]))
- strPart = final + 1
- if pos[4] == 2:
- pos[4] = 0
- star = (pos[0] * tot[0], pos[1] * tot[1])
- walls[i].append(Wall((pos[0] * tot[0], pos[1] * tot[1], pos[2] * tot[0], pos[3] * tot[1], pos[4], pos[5])))
- pos = []
- return ((mx, my), tot, walls, star)
- #--- Object Creation
- pygame.display.set_caption(gameName)
- screen = pygame.display.set_mode((screenSize[0] * res[0], screenSize[1] * res[1]))
- levels = Level(parseLevel(levelFile))
- player = []
- for h in range(0, PLAYER_NUM):
- player.append(Player((levels.start[0] + h * 16, levels.start[1], res[0] - 1, res[1]), (4 * levels.res[0], 10 * levels.res[1])))
- camera = Camera((0,0,screen.get_width(), screen.get_height()), player[0])
- tileset = pygame.transform.scale(pygame.image.load(tilesetFile), (res[0], res[1] * TILE_SET_LENGTH)).convert()
- #--- Main Loop
- gameRunning = True
- while gameRunning:
- inputer = getEvents()
- for players in player:
- if players.controlled == True:
- players.tick(inputer)
- else:
- players.tick([])
- inputer = False
- camera.tick()
- screen.fill((0, 0, 0))
- for i in range(0, len(levels.map)):
- for e in range(0, len(levels.map[i])):
- rect = translate(levels.map[i][e].rect, camera)
- til = pygame.surface.Surface((res[0],res[1]))
- til.set_colorkey((73,154,191))
- #til.set_colorkey((63,13,45))
- til.blit(tileset, (0, -levels.map[i][e].tile * res[1], res[0], res[1]))
- screen.blit(til, rect)
- for k in range(0, len(player)):
- rect = translate(player[k].rect, camera)
- pygame.draw.rect(screen, (255, 200, 0), rect)
- clock.tick(FPS)
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement