Advertisement
here2share

# snake.py ZZZ

May 25th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # snake.py ZZZ Not yet a generic, but very near to it. May iron this all out soon.
  2.  
  3. import math
  4. import random
  5. import time
  6. import appuifw
  7. import e32
  8. from key_codes import *
  9. from graphics import *
  10.  
  11. img=None # temp reference
  12. def draw_square(loc,color):
  13.     img.rectangle((loc[0]*step, 20+loc[1]*step, loc[0]*step+step, 20+loc[1]*step+step), fill=color)
  14. #
  15. def redraw(rect):
  16.     if img:
  17.         canvas.blit(img)
  18. def set_exit():
  19.     global exitflag
  20.     exitflag=1
  21. appuifw.app.exit_key_handler=set_exit
  22.  
  23. appuifw.app.orientation='portrait'
  24. appuifw.app.screen='large'
  25. appuifw.app.directional_pad=False
  26. canvas=appuifw.Canvas(redraw_callback=redraw)
  27. appuifw.app.body=canvas
  28. img=Image.new(canvas.size)
  29.  
  30. step=10
  31. _ww,_hh=(360/step,540/step)
  32.  
  33. jxa,jya=70,350 ### <<< jpad xy axis point
  34. jxb,jyb=jxa+220,jya+220
  35. jx1,jy1=jxa+70,jya+70
  36. jx2,jy2=jxa+150,jya+150
  37.  
  38. mv=0
  39. def mU(event):
  40.     global mv
  41.     if mv <> 1:
  42.         mv=0
  43. def mD(event):
  44.     global mv
  45.     if mv <> 0:
  46.         mv=1
  47. def mL(event):
  48.     global mv
  49.     if mv <> 3:
  50.         mv=2
  51. def mR(event):
  52.     global mv
  53.     if mv <> 2:
  54.         mv=3
  55.  
  56. canvas.bind(EButton1Down,mU,((jx1,jya),(jx2,jy1)))     # up
  57. canvas.bind(EButton1Down,mD,((jx1,jy2),(jx2,jyb)))     # down
  58. canvas.bind(EButton1Down,mL,((jxa,jy1),(jx1,jy2)))     # left
  59. canvas.bind(EButton1Down,mR,((jx2,jy1),(jxb,jy2)))     # right
  60.  
  61. def jpad():
  62.     grey=(220,220,220)
  63.     img.rectangle((jxa,jy1,jxb,jy2),fill=grey) # w
  64.     img.rectangle((jx1,jya,jx2,jyb),fill=grey) # h
  65.     redraw(())
  66. def jpad_key(): ### bonus feature!
  67.     if   direction == 0:
  68.         xy_=(jx1,jya,jx2,jy1) # U
  69.     elif direction == 1:
  70.         xy_=(jx1,jy2,jx2,jyb) # D
  71.     elif direction == 2:
  72.         xy_=(jxa,jy1,jx1,jy2) # L
  73.     elif direction == 3:
  74.         xy_=(jx2,jy1,jxb,jy2) # R
  75.     else: return
  76.     img.rectangle(xy_,fill=0xffff00)
  77.     redraw(())
  78.  
  79. score=0
  80. bodycolor=(32,180,32)
  81. headcolor=(0,128,0)
  82. fieldcolor=(192,192,128)
  83. foodcolor=(255,0,0)
  84. padding=4
  85. deltas=((0,-1),(0,1),(-1,0),(1,0)) # up down left right
  86. defaultsnake=12
  87. fillarray=[]
  88. y_max=(jya-padding)/step
  89. loc=w,h=defaultloc=[_ww/2,y_max/2]
  90.  
  91. for i in range(defaultsnake,0,-1): # body (excluding the snake head)
  92.     fillarray.append([w,h+i])
  93. arraydefault=fillarray[:]
  94. foodloc=[5,5]
  95.  
  96. score=0
  97. defaultspeed=speed=1200.00
  98. playing=1
  99. while playing:
  100.     mv=0
  101.     w,h=loc=defaultloc
  102.     foodloc=[5,5]
  103.     sp=10000.00
  104.     snakelocs=[]
  105.     snakelength=defaultsnake
  106.     fillarray=arraydefault[:]
  107.     while 1:
  108.         if loc == foodloc:
  109.             score+=1
  110.             a,b=(   random.randint(2,_ww/2)+loc[0]+_ww/4,
  111.                     random.randint(2,y_max/2)+loc[1]+y_max/4)
  112.             if a > _ww: a-=_ww
  113.             if b > y_max-4: b-=y_max-4 # minus padding?
  114.             while 1:
  115.                 if a > _ww-2:
  116.                     a=2
  117.                     if b > y_max-2: b=2
  118.                 if [a,b] not in list(fillarray+loc):
  119.                     break
  120.                 a+=1
  121.                 b+=3
  122.             foodloc=[a,b]
  123.         else:
  124.             fillarray.pop(0)
  125.         fillarray.append(loc)
  126.         img.clear((0,0,0)) # black background
  127.         jpad()
  128.         img.rectangle((0,20,360,y_max*step),fill=fieldcolor)
  129.         img.rectangle((0,0,360,20),fill=(0,0,0))
  130.         img.text((2,14),u"Score: %d      Speed: %d"%(score,1200-speed),(0,192,0))
  131.         draw_square(foodloc,foodcolor)
  132.         for bloc in fillarray:
  133.             draw_square(bloc,bodycolor)
  134.         draw_square(loc,headcolor)
  135.         redraw(())
  136.         try:
  137.             e32.ao_sleep(speed/sp)
  138.             speed-=0.9
  139.         except: pass
  140.         direction=mv
  141.         h+=deltas[direction][1]
  142.         w+=deltas[direction][0]
  143.         loc=[w,h]
  144.         if loc in fillarray or loc[0]>=_ww or loc[0]<0 or loc[1]>=y_max-2 or loc[1]<0: #ZZZ
  145.             print
  146.             print 'end of game'
  147.             break
  148.     playing=appuifw.query(u'Score: %d  Speed: %d\nPlay Again?'%(score,1200-speed),'query')
  149.     score=0
  150.     speed=defaultspeed
  151. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement