from Togra import * from TograUtil import * from random import * import sys scene = init() LEFT = Vector(1,0,0) RIGHT = Vector(-1,0,0) UP = Vector(0,1,0) DOWN = Vector(0,-1,0) BUMP = Vector(0,0,-10) BOARD_WIDTH = 11 BOARD_HEIGHT = 11 def gen_nibble(board): a = randrange(0, BOARD_WIDTH) b = randrange(0, BOARD_HEIGHT) if board[a][b] == 0: return (a, b) return gen_nibble(board) class Snake: def __init__(t, scene): t.box = box(0,0,0,9.5,9.5,9.5) t.scene = scene scene.view_from = Vector(50, 0, -150) scene.view_to = Vector(50, 0, 0) scene.view_up = Vector(0, 1, 0) t.pl = fromToPath(Vector(0, 0, 0), Vector(40, 0, 0), 2000) t.next = Vector(50, 0, 0) t.time = 2500 t.currentG = 0 offset = 0 for i in range(4): abox = Container(1) abox.append(t.box) abox.setColour(t.currentG, 1, t.currentG, 1) t.currentG += 0.02 abox.setPath(t.pl, offset, t.update, t, abox) scene.baseContainer.append(abox) offset -= 500 t.cur_a = 40 t.nextKey = [LEFT] t.ack = 1 t.pulse = 0 t.addPos = 500 t.move = 0 t.occupied = [] t.o_list = [] for i in range(BOARD_WIDTH): w = [] for i in range(BOARD_HEIGHT): w.append(0) t.occupied.append(w) for i in range(4): t.occupied[5+i][5] = 1 t.o_list.append((5+3-i, 5)) #end of o_list = front of queue pos = gen_nibble(t.occupied) t.occupied[pos[0]][pos[1]] = 2 print pos nibble = sphere(-10 + BOARD_WIDTH * 10, 5 - BOARD_HEIGHT*5, 0,\ 2, 10, 10) t.nibble = Container() t.nibble.setTransform(Transform(\ translate=Vector(-10*pos[0], 10 * pos[1], 0))) t.nibble.append(nibble) t.nibble.setColour(1, 0.6, 0.4, 1) scene.baseContainer.append(t.nibble) scene.baseContainer.append(box(-5,0,0,2,BOARD_HEIGHT*10,2)) scene.baseContainer.append(box(BOARD_WIDTH*10-5,0,0, 2,BOARD_HEIGHT*10,2)) scene.baseContainer.append(box(BOARD_WIDTH*5-5,BOARD_HEIGHT*5, 0, BOARD_WIDTH*10,2,2)) scene.baseContainer.append(box(BOARD_WIDTH*5-5,-BOARD_HEIGHT*5, 0, BOARD_WIDTH*10,2,2)) def go(t): scene.run() def update(t, abox, *extra): current = t.nextKey[0] realnext = None while len(t.nextKey) > 1: next = t.nextKey[1] if next + current != 3 or next == current: current = next del t.nextKey[0] break del t.nextKey[1] inter = Vector(current[0] * 5, current[1] * 5, 0) + BUMP + \ t.next t.next = Vector(current[0] * 10, current[1] * 10, 0) + t.next cpos = t.o_list[-1] npos = (int(cpos[0] - current[0]), int(cpos[1] + current[1])) if npos[0] >= BOARD_WIDTH or npos[0] < 0 or npos[1] >= \ BOARD_HEIGHT or npos[1] < 0: sys.exit(0) elif t.occupied[npos[0]][npos[1]] == 1: sys.exit(0) if t.occupied[npos[0]][npos[1]] == 2: t.pulse = 1 t.move = 1 elif t.move == 1: t.move = 0 pos = gen_nibble(t.occupied) t.occupied[pos[0]][pos[1]] = 2 t.nibble.setTransform(Transform(translate= Vector(-10 * pos[0], 10 * pos[1], 0))) t.time += 500 t.ack = 1 if t.pulse: t.pulse = 0 t.pl.append(inter, t.time - 250) abox = Container(1) abox.append(t.box) abox.setPath(t.pl, t.addPos) abox.setColour(t.currentG, 1, t.currentG, 1) t.currentG += 0.02 t.addPos += 500 scene.baseContainer.append(abox) t.grow = 0 else: opos = t.o_list[0] t.occupied[opos[0]][opos[1]] = 0 del t.o_list[0] t.occupied[npos[0]][npos[1]] = 1 t.o_list.append(npos) t.pl.append(t.next, t.time) t.pl.setMaxIndex(t.time) return paused = 0 def myKb(*args): global s global paused if args[0] == ord('q'): scene.exit() elif args[0] == ord('a'): s.nextKey.append(LEFT) elif args[0] == ord('w'): s.nextKey.append(UP) elif args[0] == ord('d'): s.nextKey.append(RIGHT) elif args[0] == ord('s'): s.nextKey.append(DOWN) elif args[0] == ord(' '): if paused: paused = 0 s.scene.cont() else: paused = 1 s.scene.pause() scene.keyFunction = myKb s = Snake(scene) s.go()