Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # snake.py ZZZ Not yet a generic, but very near to it. May iron this all out soon.
- import math
- import random
- import time
- import appuifw
- import e32
- from key_codes import *
- from graphics import *
- img=None # temp reference
- def draw_square(loc,color):
- img.rectangle((loc[0]*step, 20+loc[1]*step, loc[0]*step+step, 20+loc[1]*step+step), fill=color)
- #
- def redraw(rect):
- if img:
- canvas.blit(img)
- def set_exit():
- global exitflag
- exitflag=1
- appuifw.app.exit_key_handler=set_exit
- appuifw.app.orientation='portrait'
- appuifw.app.screen='large'
- appuifw.app.directional_pad=False
- canvas=appuifw.Canvas(redraw_callback=redraw)
- appuifw.app.body=canvas
- img=Image.new(canvas.size)
- step=10
- _ww,_hh=(360/step,540/step)
- jxa,jya=70,350 ### <<< jpad xy axis point
- jxb,jyb=jxa+220,jya+220
- jx1,jy1=jxa+70,jya+70
- jx2,jy2=jxa+150,jya+150
- mv=0
- def mU(event):
- global mv
- if mv <> 1:
- mv=0
- def mD(event):
- global mv
- if mv <> 0:
- mv=1
- def mL(event):
- global mv
- if mv <> 3:
- mv=2
- def mR(event):
- global mv
- if mv <> 2:
- mv=3
- canvas.bind(EButton1Down,mU,((jx1,jya),(jx2,jy1))) # up
- canvas.bind(EButton1Down,mD,((jx1,jy2),(jx2,jyb))) # down
- canvas.bind(EButton1Down,mL,((jxa,jy1),(jx1,jy2))) # left
- canvas.bind(EButton1Down,mR,((jx2,jy1),(jxb,jy2))) # right
- def jpad():
- grey=(220,220,220)
- img.rectangle((jxa,jy1,jxb,jy2),fill=grey) # w
- img.rectangle((jx1,jya,jx2,jyb),fill=grey) # h
- redraw(())
- def jpad_key(): ### bonus feature!
- if direction == 0:
- xy_=(jx1,jya,jx2,jy1) # U
- elif direction == 1:
- xy_=(jx1,jy2,jx2,jyb) # D
- elif direction == 2:
- xy_=(jxa,jy1,jx1,jy2) # L
- elif direction == 3:
- xy_=(jx2,jy1,jxb,jy2) # R
- else: return
- img.rectangle(xy_,fill=0xffff00)
- redraw(())
- score=0
- bodycolor=(32,180,32)
- headcolor=(0,128,0)
- fieldcolor=(192,192,128)
- foodcolor=(255,0,0)
- padding=4
- deltas=((0,-1),(0,1),(-1,0),(1,0)) # up down left right
- defaultsnake=12
- fillarray=[]
- y_max=(jya-padding)/step
- loc=w,h=defaultloc=[_ww/2,y_max/2]
- for i in range(defaultsnake,0,-1): # body (excluding the snake head)
- fillarray.append([w,h+i])
- arraydefault=fillarray[:]
- foodloc=[5,5]
- score=0
- defaultspeed=speed=1200.00
- playing=1
- while playing:
- mv=0
- w,h=loc=defaultloc
- foodloc=[5,5]
- sp=10000.00
- snakelocs=[]
- snakelength=defaultsnake
- fillarray=arraydefault[:]
- while 1:
- if loc == foodloc:
- score+=1
- a,b=( random.randint(2,_ww/2)+loc[0]+_ww/4,
- random.randint(2,y_max/2)+loc[1]+y_max/4)
- if a > _ww: a-=_ww
- if b > y_max-4: b-=y_max-4 # minus padding?
- while 1:
- if a > _ww-2:
- a=2
- if b > y_max-2: b=2
- if [a,b] not in list(fillarray+loc):
- break
- a+=1
- b+=3
- foodloc=[a,b]
- else:
- fillarray.pop(0)
- fillarray.append(loc)
- img.clear((0,0,0)) # black background
- jpad()
- img.rectangle((0,20,360,y_max*step),fill=fieldcolor)
- img.rectangle((0,0,360,20),fill=(0,0,0))
- img.text((2,14),u"Score: %d Speed: %d"%(score,1200-speed),(0,192,0))
- draw_square(foodloc,foodcolor)
- for bloc in fillarray:
- draw_square(bloc,bodycolor)
- draw_square(loc,headcolor)
- redraw(())
- try:
- e32.ao_sleep(speed/sp)
- speed-=0.9
- except: pass
- direction=mv
- h+=deltas[direction][1]
- w+=deltas[direction][0]
- loc=[w,h]
- if loc in fillarray or loc[0]>=_ww or loc[0]<0 or loc[1]>=y_max-2 or loc[1]<0: #ZZZ
- print
- print 'end of game'
- break
- playing=appuifw.query(u'Score: %d Speed: %d\nPlay Again?'%(score,1200-speed),'query')
- score=0
- speed=defaultspeed
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement