Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # circlestouch.py
- from appuifw import *
- import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,sensor
- running=1
- def quit():
- global running
- rgb_pt=9
- running=0
- app.exit_key_handler=quit
- def handle_redraw(rect):
- pass
- img=None
- app.screen='large'
- app.orientation='portrait'
- app.directional_pad=False
- app.body=canvas=Canvas(redraw_callback=handle_redraw)
- img=graphics.Image.new(canvas.size)
- res_x, res_y=canvas.size
- class z_Object(object):
- x=0
- y=0
- speed=0,0
- rad=0
- r_=z_Object()
- g_=z_Object()
- b_=z_Object()
- objA=z_Object()
- objB=z_Object()
- distance=0
- r_.x,r_.y=(100,100)
- g_.x,g_.y=(200,200)
- b_.x,b_.y=(300,300)
- r_.speed=(-3,5)
- g_.speed=(30,16)
- b_.speed=(-7,-9)
- r_.cz=(255,0,0)
- g_.cz=(0,255,0)
- b_.cz=(0,0,255)
- r_.rad=30
- g_.rad=30
- b_.rad=30
- r_.mass=1
- g_.mass=1
- b_.mass=1
- rgb_pt=0
- objects=[(r_),(g_),(b_)]
- dx,dy=0,0
- Xs=Ys=0.0000
- skip=0
- number_of_objs=3
- stat_msg=""
- def dist():
- dx=abs(objB.x-objA.x)
- dy=abs(objB.y-objA.y)
- distance=int(math.sqrt(dx*dx+dy*dy))
- return distance
- def isColliding():
- if dist() < (objA.rad+objB.rad):
- return True
- return False
- def z_xy(pos):
- if pos[1]>564:
- z_notouch(pos)
- elif rgb_pt==1:
- objects.remove(r_)
- r_.x,r_.y=pos[0],pos[1]
- objects.append(r_)
- elif rgb_pt==2:
- objects.remove(g_)
- g_.x,g_.y=pos[0],pos[1]
- objects.append(g_)
- elif rgb_pt==3:
- objects.remove(b_)
- b_.x,b_.y=pos[0],pos[1]
- objects.append(b_)
- def z_dot(pos):
- global rgb_pt,hSpeed
- if img.getpixel(pos)[0]==r_.cz:
- hSpeed=r_.speed
- r_.speed=0,0
- rgb_pt=1
- elif img.getpixel(pos)[0]==g_.cz:
- hSpeed=g_.speed
- g_.speed=0,0
- rgb_pt=2
- elif img.getpixel(pos)[0]==b_.cz:
- hSpeed=b_.speed
- b_.speed=0,0
- rgb_pt=3
- def z_notouch(pos):
- global r_,g_,b_,rgb_pt
- if rgb_pt==1:
- r_.speed=hSpeed
- elif rgb_pt==2:
- g_.speed=hSpeed
- elif rgb_pt==3:
- b_.speed=hSpeed
- rgb_pt=0
- def atColor(objAB):
- global stat_msg
- if objAB.cz==r_.cz:
- stat_msg+="R "
- elif objAB.cz==g_.cz:
- stat_msg+="G "
- elif objAB.cz==b_.cz:
- stat_msg+="B "
- def atCircle(distance):
- global stat_msg
- incr=0.05
- radA=objA.rad
- radB=objB.rad
- rad=radA+radB
- Xo=objA.speed[0]
- Yo=objA.speed[1]
- objA.x=objA.x-Xo
- objA.y=objA.y-Yo
- distance=dist()
- while distance > rad+1:
- objA.x=objA.x+(Xo*incr)
- objA.y=objA.y+(Yo*incr)
- dx=abs(objB.x-objA.x)
- dy=abs(objB.y-objA.y)
- distance=dist()
- objA.x=int(objA.x)
- objA.y=int(objA.y)
- skip=1
- draw()
- def draw():
- img.clear(0x000000)
- for objA in objects:
- img.ellipse(((objA.x-objA.rad,objA.y-objA.rad),(objA.x+objA.rad,objA.y+objA.rad)),objA.cz,objA.cz)
- img.text( (20,30),'X:'+unicode(r_.x)+' Y:'+unicode(r_.y),(240,0,0))
- img.text((130,30),'X:'+unicode(g_.x)+' Y:'+unicode(g_.y),(0,240,0))
- img.text((240,30),'X:'+unicode(b_.x)+' Y:'+unicode(b_.y),(0,0,240))
- img.text((20,50),'* '+unicode(stat_msg),(255,255,255))
- canvas.blit(img)
- z_draw(img)
- def z_draw(rect):
- pass
- canvas.bind(key_codes.EButton1Up,z_notouch)
- canvas.bind(key_codes.EButton1Down,z_dot)
- canvas.bind(key_codes.EDrag,z_xy)
- while running:
- if skip==0:
- stat_msg=" ---"
- draw()
- skip=0
- for objA in objects: # attempt to move all 3 before blitting
- objA.x+=objA.speed[0]
- objA.y+=objA.speed[1]
- if objA.x<30:
- objA.x=30
- objA.speed=objA.speed[0]*-1,objA.speed[1]
- elif objA.x>330:
- objA.x=330
- objA.speed=objA.speed[0]*-1,objA.speed[1]
- if objA.y<80:
- objA.y=80
- objA.speed=objA.speed[0],objA.speed[1]*-1
- elif objA.y>540:
- objA.y=540
- objA.speed=objA.speed[0],objA.speed[1]*-1
- for objB in objects:
- if objA <> objB:
- if isColliding():
- stat_msg=""
- atColor(objA)
- atColor(objB)
- atCircle(distance)
- e32.reset_inactivity()
- e32.ao_yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement