I'm not very experienced but i assume you would just append them to a list and make sure they all have the same sprite.z_position =? value then if you want to delete the layer just do
for item in layer:
item.remove_from_parent()
Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
I'm not very experienced but i assume you would just append them to a list and make sure they all have the same sprite.z_position =? value then if you want to delete the layer just do
for item in layer:
item.remove_from_parent()
This full code has a small section that throws no errors but for some reason doesn't work.
n0de.position=touch.location
^Works just fine... but the following
if n0de.position.x>touch.location.x:
n0de.position.x-=self.speed
elif n0de.position.x <touch.location.x:
n0de.position.x+=self.speed
if n0de.position.y >touch.location.y:
n0de.position.y-=self.speed
elif n0de.position.y<touch.location.y:
n0de.position.y+=self.speed
Doesn't do anything. Tho when I replace the position changing lines with print statements it works fine.
eg
if n0de.position.y >touch.location.y:
print('moves down')
I assume I'm missing something about updating Sprite positions?
Thanks.
The Gist
I also commented all the images, really any images can be used tho since i specify size.... i think
Thanks for the help!
I assumed that since tile.color='#whatever' works perfectly it must not be 'themap' 's problem
So i made the change and it doesnt give me the texture error, but now something crazy is happening. The texture color is overriding the background color and surrounding tiles.
tile.texture=current_tile
Turns my PRE-EDIT GAME MAP into this.
BROKEN TEXTURED MAP
But this simple code
tile.color='#ff0000'
I'm so confused why it doesn't just change the texture, yet it changes the color correctly???
I have what I believe to be a simple question..... how do i get the last line of code here to work.
tile.texture=current_tile
tile is a SpriteNode Object thing, and the current tile is just a png
I'm attempting to edit a tilemap of SpriteNodes.... I'm quite clueless as all of my programming i learned in pygame.
from scene import *
import random
import ui
A = Action
g1 ='g1.PNG'
g2 ='g2.PNG'
g3 = 'g3.PNG'
g4='g4.PNG'
g5='g5.PNG'
tb='toolbar.PNG'
all_tiles=[g1,g2,g3,g4,g5]
tiles=[g1,g2,g1,g3,g4,g5,g2,g1]
current_tile=g1
mapsize=100
themap=[[] for _ in range(mapsize)]
class Arrow (SpriteNode):
def __init__(self, **kwargs):
SpriteNode.__init__(self, 'arrow.PNG', **kwargs)
class MyScene (Scene):
def setup(self):
self.unit=20
unit =self.unit
self.items=[]
self.points=0
#states of buttons
self.edit_mode=False
self.recenter_gravity=0,0,0
self.background_color = '#000000'
sizex,sizey=self.size
self.ground = Node(parent=self)
self.gen_map()
#player
self.ship = SpriteNode('p1.PNG')
self.ship.size=(unit,unit)
self.ship.position = self.size / 2
self.add_child(self.ship)
#__________BUTTONS&UI_____________
self.toolbar=SpriteNode('toolbar.PNG')
self.toolbar.size=(unit*16,unit*2)
self.toolbar.position=(unit*8,unit)
self.add_child(self.toolbar)
self.edit_button=SpriteNode('emj:Wrench')
self.edit_button.size=(unit*2,unit*2)
self.edit_button.position=(unit,unit)
self.add_child(self.edit_button)
self.re_center=SpriteNode('emj:Anger_Symbol')
self.re_center.size=(unit*2,unit*2)
self.re_center.position=(unit*3,unit)
self.selected=SpriteNode(g1)
self.selected.size=(unit*2,unit*2)
self.selected.position=(unit*8,unit)
self.draws=SpriteNode('emj:Black_Nib')
self.draws.size=(unit*2,unit*2)
self.draws.position=(unit*6,unit)
self.tree = SpriteNode('leaves.PNG')
self.tree.size=(unit*3,unit*3)
#self.add_child(self.pb)
#text on screen
self.score= LabelNode('0')
self.score.position=10,sizey-10
self.add_child(self.score)
def update_map(self):
sizex,sizey=self.size
self.ground.position.x
for _ in themap:
for tile in _:
if tile.position.x+self.ground.position.x>=-self.unit and tile.position.y+self.ground.position.y>=-self.unit and tile.position.x+self.ground.position.x<= sizex +self.unit and tile.position.y+self.ground.position.y<= sizey+ self.unit:
tile.z_position=-1
self.ground.add_child(tile)
else:
tile.remove_from_parent()
def gen_map(self):
sizex,sizey=self.size
unit=self.unit
#laggyyyy if multi press!!!
for _ in themap:
for tile in _:
tile.remove_from_parent
#themap=[[] for _ in range(mapsize)]
#generate a random map
y=-unit/5
zpos=mapsize
ypos=0
for _ in range (mapsize):
x=-unit
y+=unit*2
for _ in range (mapsize):
x+=unit*2
tile = SpriteNode(random.choice(tiles), position=(x, y),)
tile.size=unit*2,unit*2
tile.z_position=zpos
themap[ypos].append(tile)
zpos-=1
ypos+=1
def place_tree(self,xy):
self.tree.position=xy-self.ground.position
self.tree.z_position=2
self.ground.add_child(self.tree)
def projectile(self,x,y):
ammo = Arrow(parent=self)
ammo.size=self.unit*2,self.unit*2
ammo.position = (self.ship.position)
#ammo.rotation=self.ship.rotation
actions = [A.move_to(x,y,1,6), A.remove()]
ammo.run_action(A.sequence(actions))
ammo.rotation=self.ship.rotation
self.items.append(ammo)
def check_ammo_collisions(self):
sizex,sizey=self.size
for ammo in self.items:
if ammo.position in self.pb.frame:
self.items.remove(ammo)
ammo.remove_from_parent()
self.points+=1
self.pb.position=random.randint(0,sizex),random.randint(0,sizey)
def update(self):
self.update_map()
self.check_ammo_collisions()
#self.score.text=str(self.points)
#save old posotion
pos=self.ground.position
posx,posy=pos
sizex,sizey=self.size
x, y, z = gravity()
#adjust for held angle of device
xx,yy,zz = self.recenter_gravity
x-=xx
y-=yy
z-=zz
self.ground.position-=(x * 10, y * 10)
#use old and new posotion for rotation
newpos=self.ground.position
newposx,newposy=newpos
deltax=posx-newposx
deltay=posy-newposy
self.ship.rotation = math.atan2(deltay,deltax)-4.7
def wrench(self,xy):
if xy in self.edit_button.bbox:
if self.edit_mode==True:
self.edit_mode=False
self.edit_button.color='red'
self.re_center.remove_from_parent()
self.draws.remove_from_parent()
self.selected.remove_from_parent()
elif self.edit_mode==False:
self.edit_mode=True
self.edit_button.color='#00ff00'
self.add_child(self.re_center)
self.add_child(self.draws)
self.add_child(self.selected)
def editmode(self,xy):
if xy in self.re_center.bbox:
self.recenter_gravity=gravity()
elif xy in self.draws.bbox:
pass
elif xy in self.selected.bbox:
pass
def playmode(self,xy):
pass
def touch_began(self, touch):
xy =touch.location
if xy in self.toolbar.bbox:
self.wrench(xy)
if self.edit_mode==True:
self.editmode(xy)
else:
self.playmode(xy)
else:
xy-=self.ground.position
#non toolbar interactions
if self.edit_mode==True:
for _ in themap:
for tile in _:
if xy in tile.bbox:
tile.texture=current_tile
run(MyScene(), PORTRAIT)