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 = "Testing 1,2,3"
- levelFile = "level.txt"
- tilesetFile = "tileset.bmp"
- screenSize = (20, 17)
- FPS = 30
- class vector(object):
- def __init__(self, list1, list2):
- self.diff = (list2[0] - list1[0], list2[1] - list2[1])
- def distance(self):
- self.a = self.diff[0]
- self.b = self.diff[1]
- return math.sqrt(self.a**2, self.b**2)
- def unit(self):
- dis = self.distance()
- self.aunit = self.a / distance
- self.bunit = self.b / distance
- return self.aunit, self.bunit
- def add(self, first, second):
- self.sum = (first[0] + second[0], first[1] + second[1])
- return self.sum
- def multiply(self, v, num):
- self.product = (v[0] * num, v[1] * num)
- return self.product
- 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.currentJump = 0
- self.totJumpNum = 2
- self.vectors = []
- self.jumping = False
- self.walking = False
- self.controlled = True
- self.lastValue = [0,0]
- self.gravity = 0
- self.unjump = False
- self.inputt = {"jump": [K_UP], "right": [K_RIGHT], "left": [K_LEFT], "crouch": [K_DOWN]}
- def move(self, dx, dy):
- if self.speed[0] != 0:
- self.move_single_axis(self.speed[0], 0)
- if self.speed[1] != 0:
- 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 dx < 0:
- self.rect.left = collideBox.right
- if dy > 0:
- self.rect.bottom = collideBox.top
- self.jumping = False
- self.speed[1] = 0
- self.gravity = 0;
- if dy < 0:
- self.rect.top = collideBox.bottom
- self.jumping = False
- 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 jump(self):
- if self.jumping == False:
- self.jumping = True
- self.currentJump += 1
- self.jumpVal = -self.topSpeed[1]
- self.unjump = False
- def addVector(self, first, second):
- return [first[0] + second[0], first[1] + second[1]]
- def walk(self, dir):
- if self.lastValue[0] < dir * self.topSpeed[0]:
- self.lastValue[0] = self.topSpeed[0] * dir#+= dir
- self.vectors.append(self.lastValue)
- def input(self, type, keys):
- if type == KEYDOWN:
- if keys in self.inputt["jump"]:
- self.jump()
- #if keys in self.inputt["crouch"]:
- if keys in self.inputt["left"]:
- if self.walking == False:
- self.walking = True
- self.direc = -1
- if keys in self.inputt["right"]:
- if self.walking == False:
- self.walking = True
- self.direc = 1
- if type == KEYUP:
- if keys in self.inputt["jump"]:
- self.unjump = True
- #if keys in self.inputt["crouch"] and self.speed[1] > 0:
- if keys in self.inputt["left"] and self.speed[0] < 0:
- self.walking = False
- if keys in self.inputt["right"] and self.speed[0] > 0:
- self.walking = False
- def tick(self, inputs):
- #Get input
- self.vectors.append([0,0])
- if len(inputs) != 0:
- for inputy in inputs:
- self.input(inputy[0], inputy[1])
- # end get input
- #moving
- if self.walking:
- if self.lastValue[0] != self.direc * self.topSpeed[0]:
- #self.lastValue[0] += self.direc
- self.lastValue[0] = self.topSpeed[0] * self.direc
- else:
- if self.lastValue[0] <> 0:
- #self.lastValue[0] -= self.direc
- self.lastValue[0] = 0
- self.vectors.append(self.lastValue)
- # end moving
- #jumping
- if self.jumping:
- self.vectors.append([0,self.jumpVal])
- if self.unjump:
- self.gravity += 2
- else:
- self.gravity += 1
- self.vectors.append([0,self.gravity])
- # end jumping
- #applying input
- if len(self.vectors) > 1:
- for i in range(0, len(self.vectors)):
- self.vectors[0] = self.addVector(self.vectors[0], self.vectors[i])
- self.speed = self.vectors[0]
- self.vectors = []
- self.move(self.speed[0], self.speed[1])
- # end applying input
- #if self.jumping:
- # if (self.speed[1] >= 0) and (self.currentJump <= self.totJumpNum - 1):
- # self.jumping = 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], 8 * 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()
- pygame.quit ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement