Skip to content

Instantly share code, notes, and snippets.

@jsbain

jsbain/mapscene.py

Last active Aug 18, 2020
Embed
What would you like to do?
mapscene.py
from scene import *
import sound
import random
import math
A = Action
mapsprites=['plc:Brown_Block','plc:Dirt_Block','plc:Grass_Block','plc:Plain_Block',]
def choose_random_node():
tx=random.choice(mapsprites)
sn=SpriteNode(tx)
return sn
class MapNode(Node):
def __init__(self, size):
'''create a map, randomly filled with sprites
size is tuple containg row,col number of tiles'''
self.map=[[choose_random_node() for i in range(size[1])] for j in range(size[0])]
r=0
for row in self.map:
r+=1
c=0
for col in row:
c+=1
col.position=(c*col.size.width, r*(row[0].size.height-40))
col.z_position=-r
self.add_child(col)
self.player=SpriteNode('plc:Character_Boy')
self.size=self.bbox
self.player.position=40*10,40*10
self.add_child(self.player)
self.scale=1.2
class MyScene (Scene):
def setup(self):
self.mapnode=MapNode([30,40])
self.add_child(self.mapnode)
self.size=self.bbox
self.zoom_in=SpriteNode('iob:arrow_expand_32')
self.zoom_in.position=64,64
self.zoom_in.scale=2
self.zoom_out=SpriteNode('iob:arrow_shrink_32')
self.zoom_out.position=2*64,64
self.zoom_out.scale=2
self.add_child(self.zoom_in)
self.add_child(self.zoom_out)
ln=LabelNode('tap edges to walk. drag map to move map')
ln.anchor_point=.5,1
ln.position=get_screen_size()[0]/2, get_screen_size()[1]
self.add_child(ln)
def did_change_size(self):
pass
def update(self):
pass
def touch_began(self, touch):
'''simple screen edge joystick
tap near edge to move character one square
'''
self.dragging=False
player=self.mapnode.player
dp=player.size
dt=.1
if touch.location in self.zoom_in.bbox:
self.mapnode.scale=self.mapnode.scale*1.2
return
if touch.location in self.zoom_out.bbox:
self.mapnode.scale=self.mapnode.scale/1.2
return
if touch.location.x>0.9*self.size.width:
player.run_action(Action.move_by(dp.x,0,dt,TIMING_EASE_IN))
sound.play_effect('rpg:Footstep08')
elif touch.location.x<0.1*self.size.width:
player.run_action(Action.move_by(-dp.x,0, dt,TIMING_EASE_IN))
sound.play_effect('rpg:Footstep07')
elif touch.location.y>0.9*self.size.height:
player.run_action(Action.move_by(0,dp.y/2,dt,TIMING_EASE_IN))
sound.play_effect('rpg:Footstep02')
elif touch.location.y<0.1*self.size.height:
player.run_action(Action.move_by(0,-dp.y/2,dt,TIMING_EASE_IN))
sound.play_effect('rpg:Footstep04')
else:
self.dragging=True
def touch_moved(self, touch):
'''simple scrollview type action'''
if self.dragging:
dp=touch.location-touch.prev_location
#self.mapnode.run_action(Action.move_by(dp.x,dp.y,0))
self.mapnode.position+=dp
self.touch_time=self.t
def touch_ended(self, touch):
self.dragging=False
if __name__ == '__main__':
s=MyScene()
run(s, show_fps=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.