Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys, pygame, os
- from pygame.locals import *
- import math, random, time
- class Tile(object):
- def __init__(self, tilesheet, dfx, dfy, ix=0, iy=0):
- self.tilesheet = tilesheet
- self.source = (dfx, dfy)
- self.index = (ix, iy)
- def draw(self, dest, offset):
- self.tilesheet.drawTile(dest, self, offset)
- class Tilesheet(object):
- def __init__(self, filename, tileWidth=32, tileHeight=32, transparentColor=None):
- self.source = pygame.image.load(filename)
- self.source.set_colorkey(transparentColor)
- self.tileSize = (tileWidth, tileHeight)
- self.tiles = (self.source.get_width() // tileWidth, self.source.get_height() // tileHeight)
- def isValid(self, x, y):
- return (x >= 0) and (x < self.tiles[0]) and (y >= 0) and (y < self.tiles[1])
- def drawTile(self, dest, tile, offset):
- dest.blit(self.source, offset, (tile.source, self.tileSize))
- def intToTile(self, n):
- return Tile(self, (n % self.tiles[1]) * self.tileSize[0], (n // self.tiles[1]) * self.tileSize[1])
- def tileToInt(self, t):
- return ((t.source[1] / self.tileSize[1]) * self.tiles[1]) + (t.source[0] / self.tileSize[0])
- class TileMap(object):
- def __init__(self, width, height, tileWidth, tileHeight):
- self.source = [[[] for col in range(width)] for row in range(height)]
- self.size = (width, height)
- self.tileSize = (tileWidth, tileHeight)
- def randomMap(self, tilesheet, thickness):
- for r in range(self.size[1]):
- for c in range(self.size[0]):
- self.source[r][c] = []
- for z in range(thickness):
- for r in range(self.size[1]):
- for c in range(self.size[0]):
- self.source[r][c].append(Tile(tilesheet, random.randint(0, tilesheet.tiles[0]) * 32, random.randint(0, tilesheet.tiles[1]) * 32, c, r))
- def draw(self, dest, area):
- for r in range(area[1][1]):
- for c in range(area[1][0]):
- for t in self.source[area[0][1] + r][area[0][0] + c]:
- t.draw(dest, (c * self.tileSize[0], r * self.tileSize[1]))
- class Camera(object):
- def __init__(self, tilemap, width, height, px=0, py=0):
- self.source = tilemap
- self.size = (width, height)
- self.dimensions = (width // self.source.tileSize[0], height // self.source.tileSize[1])
- self.pos = [px, py]
- def move(self, dx, dy):
- self.pos[0] += dx
- self.pos[1] += dy
- def draw(self, dest):
- self.source.draw(dest, (self.pos, self.dimensions))
- running = True
- def getEvents(cam):
- for event in pygame.event.get():
- if event.type == KEYDOWN:
- if event.key == pygame.K_UP:
- cam.move(0, -1)
- if event.key == pygame.K_DOWN:
- cam.move(0, 1)
- if event.key == pygame.K_LEFT:
- cam.move(-1, 0)
- if event.key == pygame.K_RIGHT:
- cam.move(1, 0)
- if event.type == pygame.QUIT:
- global running
- running = False
- pygame.init()
- clock = pygame.time.Clock()
- random.seed()
- screen = pygame.display.set_mode((800, 608))
- screen.fill((255, 255, 255))
- pygame.display.set_caption('RPyG')
- pygame.mouse.set_visible(1)
- ts = Tilesheet('tiles.bmp', 32, 32, (255, 255, 255))
- tm = TileMap(100, 100, 32, 32)
- tm.randomMap(ts, 3)
- c = Camera(tm, 800, 608)
- while running:
- getEvents(c)
- screen.fill((255, 255, 255))
- c.draw(screen)
- pygame.display.flip()
- clock.tick(30)
- print 'Terminating'
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement