Advertisement
here2share

# bouncing_sq_with_gc.py

Jan 5th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # bouncing_sq_with_gc.py ### seems to work fast as the threading demo version
  2.  
  3. from appuifw import *
  4. import e32
  5. import gc
  6. from graphics import *
  7.  
  8. running = 1
  9. def quit():
  10.     global running
  11.     running = 0
  12. app.exit_key_handler = quit
  13.  
  14. def draw(rect):
  15.     if img:
  16.         canvas.blit(img)
  17.  
  18. img = None
  19. app.screen = 'large'
  20. app.orientation = 'portrait'
  21. app.directional_pad = False
  22. app.body = canvas = Canvas(redraw_callback=draw)
  23. img = Image.new(canvas.size)
  24. res_x, res_y = canvas.size
  25.  
  26. dy = 1
  27. dx = 1
  28. x1 = 0
  29. y1 = 0
  30. x2 = 0
  31. y2 = 0
  32.  
  33. from threading import Thread
  34.  
  35. def plot():
  36.     global x1,y1,dx,dy,x2,y2
  37.     x1 = x1 + dx
  38.     y1 = y1 + dy
  39.     x2 = x1 + 100
  40.     y2 = y1 + 100
  41.     if (x1 < -50):
  42.         dx = -1 * dx
  43.     elif (x1 > res_x - 50):
  44.         dx = -1 * dx
  45.     if (y1 < -50):
  46.         dy = -1 * dy
  47.     elif (y1 > res_y - 50):
  48.         dy = -1 * dy
  49.  
  50. while running:
  51.  
  52.     gc.collect()
  53.     plot()
  54.     img.clear(0x000000)
  55.     img.rectangle((x1, y1, x2, y2), fill=0xff0000)
  56.     draw(())
  57.     e32.ao_yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement