Selaa lähdekoodia

refactor tileset

raylu 12 vuotta sitten
vanhempi
commit
948aec9635
3 muutettua tiedostoa jossa 64 lisäystä ja 58 poistoa
  1. 1 0
      .gitignore
  2. 56 0
      tileset.py
  3. 7 58
      troll

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.pyc

+ 56 - 0
tileset.py

@@ -0,0 +1,56 @@
+import struct
+from xml.etree import ElementTree
+
+import pygame
+
+class Tileset(object):
+	tilesize = 16
+	RIGHT = 0
+	DOWN = 1
+	LEFT = 2
+	UP = 3
+
+	def __init__(self, screen):
+		self.screen = screen
+		image = pygame.image.load('tileset.png').convert()
+		self.tileset = {
+			self.RIGHT: image,
+			self.UP: pygame.transform.rotate(image, 90),
+			self.LEFT: pygame.transform.rotate(image, 180),
+			self.DOWN: pygame.transform.rotate(image, 270),
+		}
+		self.tile_width, self.tile_height = image.get_size()
+		self.tile_width /= self.tilesize
+		self.tile_height /= self.tilesize
+
+	def _render(self, tile_x, tile_y, pos, direction=RIGHT):
+		tileset = self.tileset[direction]
+		if direction == self.RIGHT:
+			x, y = tile_x, tile_y
+		elif direction == self.UP:
+			x, y = tile_y, self.tile_width - tile_x - 1
+		elif direction == self.LEFT:
+			x, y = self.tile_width - tile_x - 1, self.tile_height - tile_y - 1
+		elif direction == self.DOWN:
+			x, y = self.tile_height -tile_y - 1, tile_x
+		x *= self.tilesize
+		y *= self.tilesize
+		self.screen.blit(tileset, pos, (x, y, self.tilesize, self.tilesize))
+
+	def render_person(self, pos, direction):
+		self._render(0, 13, pos, direction)
+
+	def load_level(self): # https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d/tmx.py#cl-241
+		level_data = ElementTree.parse('level1.tmx').getroot()
+		raw_data = level_data.findall('layer')[0].findall('data')[0].text.strip()
+		data = raw_data.decode('base64').decode('zlib')
+		# read as array of 4-byte ints
+		self.level_array = struct.unpack('<{}i'.format(len(data)/4), data)
+
+	def render_level(self):
+		for i, tile in enumerate(self.level_array):
+			x, y = divmod(i, 50)
+			x *= self.tilesize
+			y *= self.tilesize
+			tile_y, tile_x = divmod(tile - 1, self.tilesize) # those 1-indexing fuckers
+			self._render(tile_x, tile_y, (y, x))

+ 7 - 58
troll

@@ -1,57 +1,21 @@
 #!/usr/bin/env python
 
-from xml.etree import ElementTree
-import struct
 import sys
 
 import pygame
 
-class Tileset(object):
-	tilesize = 16
-	RIGHT = 0
-	DOWN = 1
-	LEFT = 2
-	UP = 3
-
-	def __init__(self, screen):
-		self.screen = screen
-		image = pygame.image.load('tileset.png').convert()
-		self.tileset = {
-			self.RIGHT: image,
-			self.UP: pygame.transform.rotate(image, 90),
-			self.LEFT: pygame.transform.rotate(image, 180),
-			self.DOWN: pygame.transform.rotate(image, 270),
-		}
-		self.tile_width, self.tile_height = image.get_size()
-		self.tile_width /= self.tilesize
-		self.tile_height /= self.tilesize
-
-	def render(self, tile_x, tile_y, pos, direction=RIGHT):
-		tileset = self.tileset[direction]
-		if direction == self.RIGHT:
-			x, y = tile_x, tile_y
-		elif direction == self.UP:
-			x, y = tile_y, self.tile_width - tile_x - 1
-		elif direction == self.LEFT:
-			x, y = self.tile_width - tile_x - 1, self.tile_height - tile_y - 1
-		elif direction == self.DOWN:
-			x, y = self.tile_height -tile_y - 1, tile_x
-		x *= self.tilesize
-		y *= self.tilesize
-		self.screen.blit(tileset, pos, (x, y, self.tilesize, self.tilesize))
-
-	def render_person(self, pos, direction):
-		self.render(0, 13, pos, direction)
-
-black = (0, 0, 0)
-render_x = 0
-render_y = 13*16
+from tileset import Tileset
 
 pygame.init()
 screen = pygame.display.set_mode((800, 800))
 tileset = Tileset(screen)
+tileset.load_level()
 clock = pygame.time.Clock()
 
+black = (0, 0, 0)
+render_x = 0
+render_y = 13*tileset.tilesize
+
 keys = {
 	pygame.K_LEFT: False,
 	pygame.K_RIGHT: False,
@@ -68,14 +32,6 @@ def handle_event(event):
 	elif event.type == pygame.QUIT:
 		sys.exit()
 
-def load_level(): # https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d/tmx.py#cl-241
-	level_data = ElementTree.parse('level1.tmx').getroot()
-	raw_data = level_data.findall('layer')[0].findall('data')[0].text.strip()
-	data = raw_data.decode('base64').decode('zlib')
-	level_array = struct.unpack('<{}i'.format(len(data)/4), data) # read as array of 4-byte ints
-	return level_array
-level_array = load_level()
-
 direction = Tileset.RIGHT
 movement = [0, 0]
 while True:
@@ -113,13 +69,6 @@ while True:
 	render_y += movement[1]
 
 	screen.fill(black)
-	# draw the map
-	for i, tile in enumerate(level_array):
-		x, y = divmod(i, 50)
-		x *= tileset.tilesize
-		y *= tileset.tilesize
-		tile_y, tile_x = divmod(tile - 1, tileset.tilesize) # those 1-indexing fuckers
-		tileset.render(tile_x, tile_y, (y, x))
-	# draw the player
+	tileset.render_level()
 	tileset.render_person((render_x, render_y), direction)
 	pygame.display.flip()