Pythonista day 5: How to make a map

Here is the code for today, where we are going to make a whole map, not just one row. As you can see there is a list that contains lists, one for every “row” on the screen. The screen is divided in rows of 64 pixels each and columns that are also 64 pixels, just like the tiles size.

Here is the list with the map

The script iterates the map and put a brick or a coin when finds 1 or 2

This is the result

This is the screen as it appears
This is how the list rapresent the items in the map

For the moment 1 correspond to the terrain and 2 correnspond to a coin.

from scene import *
import numpy

class Game(Scene):
	def setup(self):
		self.add_ground()
		self.add_background()
		self.add_earth()
		self.add_player()
	
	def add_ground(self):
		''' the root node. the window object, the main surface '''
		self.ground=Node(parent=self)
				
	def add_background(self):
		''' the color of the background ''' 
		self.background_color = '#20106e'
	
	def add_earth(self):
		''' draw the tiles for under the player '''
		x = 32
		y = 800
		tiles = [
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0],
			[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
			[1,0,0,0,2,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0],
			[1,1,1,1,1,0,1,1,0,1,1,1,0,1,1,1,1,1,1,1],
			]
		for row in tiles:
				for i in row:
						if i == 1: 
							tile = SpriteNode('plf:Ground_Grass', position=(x, y))
							self.ground.add_child(tile)
						if i == 2:
							tile = SpriteNode('plf:Item_CoinGold', position=(x, y))
						
							self.ground.add_child(tile)
						x += 64
				x = 32
				y -= 64
						
		
			
	def add_player(self):
		self.player = SpriteNode('plf:AlienBeige_front')
		self.player.anchor_point=(0.5, 0)
		self.player.position = (self.size.w//2, 64)
		self.add_child(self.player)
		
	def touch_began(self, touch):
		x, y = touch.location
		px, py = self.player.position
		self.move_action = Action.move_to(x, 64, 0.7, TIMING_SINODIAL)
		self.player.run_action(self.move_action, 'move_action_key')
		
	def touch_ended(self, touch):
		self.player.remove_action("move_action_key")
		
		
	

run(Game(), LANDSCAPE, show_fps=True)

This is the video


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.