Skip to content

Instantly share code, notes, and snippets.

@Auer11

Auer11/reaction.py

Last active Mar 29, 2018
Embed
What would you like to do?
reaction.py
from scene import *
import random
import ui
import shelve
import pickle
#from game_menu import MenuScene
A = Action
g1=Texture('g1.PNG')
g2=Texture('g2.PNG')
g3=Texture( 'g3.PNG')
g4=Texture('g4.PNG')
g5=Texture('g5.PNG')
tb=Texture('toolbar.PNG')
#all_tiles=[Texture(n)for n in [g1,g2,g3,g4,g5]]
all_tiles=[g1,g2,g3,g4,g5]
tiles=all_tiles
current_tile=g4
mapsize=5
themap=[[] for _ in range(mapsize)]
class Arrow (SpriteNode):
def __init__(self, **kwargs):
SpriteNode.__init__(self, 'arrow.PNG', **kwargs)
#class Button (object):
# def __init__(self,texture):
# SpriteNode.__init__(self,texture)
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(tb)
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(current_tile)
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#self.present_modal_scene(MenuScene)
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.frame:
#THE BELOW LINE WORKS PERFECTLY AS A STANDIN
#tile.color='#ff0000'
#BUT THIS ONE HERE BREAKS EVERYTHING SOMEHOW
tile.texture=current_tile
run(MyScene(), PORTRAIT)
@Auer11

This comment has been minimized.

Copy link
Owner Author

@Auer11 Auer11 commented Mar 29, 2018

treetrunk
arrow
g1
g2
g3
g4
g5
leaves
p1
toolbar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment