Advertisement
here2share

### UfoZapper.py

Nov 29th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # UfoZapper.py
  2. import key_codes, appuifw, e32, graphics, random
  3.  
  4. MAX_UFO_SIZE = 50
  5. MAX_UFOS = 7
  6. UFO_TIME = 100.0
  7.  
  8. PAD_W = 40
  9. PAD_H = 15
  10. PAD_COLOR = (12, 116, 204)
  11. PAD_SPEED = 7
  12. LASER_COLOR = (255, 0, 204)
  13. TIME = 1000
  14.  
  15. def handle_redraw(rect):
  16.         global shoot, sleep, ufos, timer
  17.         buf.clear((0, 0, 0))
  18.         buf.rectangle((pad_x, H - PAD_H, pad_x + PAD_W, H),\
  19.                 fill = PAD_COLOR)
  20.  
  21.         if shoot:
  22.                 x = pad_x + PAD_W / 2
  23.                 buf.line((x, H - PAD_H, x, 0),\
  24.                         width = 4, outline = LASER_COLOR)
  25.                 shoot = False
  26.                 sleep = 0.1
  27.                 check_hits(x)
  28.         else:
  29.                 sleep = 0.01
  30.        
  31.         for x, y, s, t, hit in ufos:
  32.                 f = 1.0 - (timer - t) / UFO_TIME
  33.                 if hit:
  34.                         c = (255, 0, 0)
  35.                 else:
  36.                         c = (0, int(f * 255), 0)
  37.                 buf.ellipse((x, y, x + s, y + s), fill = c)
  38.  
  39.         buf.text((10, 40), u"%d" % score,
  40.                 fill = LASER_COLOR, font = "title")
  41.        
  42.         buf.text((W - 70, 40), u"%d" % (TIME - timer),
  43.                 fill = LASER_COLOR, font = "title")
  44.        
  45.         canvas.blit(buf)
  46.  
  47. def check_hits(laser_x):
  48.         global ufos, score
  49.         i = 0
  50.         ok_ufos = []
  51.         for x, y, s, t, hit in ufos:
  52.                 if laser_x > x and laser_x < x + s:
  53.                         ok_ufos.append((x, y, s, t, True))
  54.                         score += MAX_UFO_SIZE*5 - (s - 1)
  55.                 else:
  56.                         ok_ufos.append((x, y, s, t, False))
  57.                         score -= 10
  58.         ufos = ok_ufos
  59.  
  60. def update_ufos():
  61.         global ufos, timer
  62.         ok_ufos = []
  63.         for x, y, s, t, hit in ufos:
  64.                 if not hit and timer < t + UFO_TIME:
  65.                         ok_ufos.append((x, y, s, t, False))
  66.         ufos = ok_ufos
  67.        
  68.         if len(ufos) < MAX_UFOS:
  69.                 s = random.randint(10, MAX_UFO_SIZE)
  70.                 x = random.randint(0, W - s)
  71.                 y = random.randint(0, H - PAD_H * 3)
  72.                 t = random.randint(0, UFO_TIME)
  73.                 ufos.append((x, y, s, timer + t, False))
  74.  
  75.  
  76. def handle_event(event):
  77.         global direction, shoot
  78.         if event['keycode'] == key_codes.EKeyLeftArrow:
  79.                 direction = -PAD_SPEED
  80.         elif event['keycode'] == key_codes.EKeyRightArrow:
  81.                 direction = PAD_SPEED
  82.         elif event['keycode'] == key_codes.EKeySelect or event['keycode'] == key_codes.EKeyUpArrow:
  83.                 shoot = True
  84.  
  85. def quit():
  86.         global timer
  87.         timer = TIME
  88.  
  89. ufos = []
  90. shoot = False
  91. direction = pad_x = score = timer = 0
  92. appuifw.app.exit_key_handler = quit
  93. appuifw.app.screen = 'large'
  94. canvas = appuifw.Canvas(event_callback = handle_event,\
  95.                         redraw_callback = handle_redraw)
  96. W, H = canvas.size
  97. H=(H/4*3)+5
  98. buf = graphics.Image.new((W, H))
  99. appuifw.app.body = canvas
  100.  
  101. while timer < TIME:
  102.         pad_x += direction
  103.         pad_x = min(pad_x, W - PAD_W)
  104.         pad_x = max(pad_x, 0)
  105.  
  106.         update_ufos()
  107.         handle_redraw((W, H))
  108.         e32.ao_sleep(sleep)
  109.         timer += 1
  110.  
  111. print "Score: %d!" % score
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement