Advertisement
phjoe

Ball

Dec 9th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # joe 10/12/2014
  2. import appuifw as A
  3. import graphics as G
  4. import math
  5. import random
  6. rand=random.randint
  7. run=1
  8. def stop():
  9.  global run
  10.  run=0
  11.  
  12. A.app.screen='full'
  13. A.app.body=c=A.Canvas()
  14. A.app.exit_key_handler=stop
  15. w,h=c.size
  16. img=G.Image.new((w,h))
  17.  
  18. dt,R=1.0,10
  19. vx=[2,2,2,2]
  20. vy=[2,2,2,2]
  21. px=[rand(20,160),rand(20,160),rand(20,160),rand(20,160)]
  22. py=[rand(20,180),rand(20,180),rand(20,180),rand(20,180)]
  23. color=[0xff0000,0x00ff00,0x0000ff,0xffff00]
  24. nxt=1
  25. while run:
  26.  img.clear(0)
  27.  for i in range(len(px)):
  28.   j=nxt+1
  29.   if (px[i]+dt*vx[i] > w-R) or (px[i]+dt*vx[i] < R):
  30.    vx[i]=-vx[i]
  31.   if (py[i]+dt*vy[i] > h-R) or (py[i]+dt*vy[i] < R):
  32.    vy[i]=-vy[i]
  33.   dx=(px[i]-px[j])+(vx[i]-vx[j])
  34.   dy=(py[i]-py[j])+(vy[i]-vy[j])
  35.   dist=math.sqrt(dx*dx+dy*dy)
  36.   if dist <= (3*R)/2: #2*R
  37.    tempx=vx[i]
  38.    tempy=vy[i]
  39.    vx[i]=vx[j]
  40.    vy[i]=vy[j]
  41.    vx[j]=tempx
  42.    vy[j]=tempy
  43.   px[i]=px[i]+vx[i]*dt
  44.   py[i]=py[i]+vy[i]*dt
  45.   img.ellipse((px[i]-R,py[i]-R,px[i]+R,py[i]+R),fill=color[i])
  46.  c.blit(img)
  47.  A.e32.ao_sleep(0.01)
  48.  A.e32.reset_inactivity()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement