The snake game

Sharvi Jain
Data Mining the City
1 min readOct 3, 2018

Adjacent blocks in the grid merge when you navigate with the four arrow keys on the keyboard.

def setup():
size(700, 700)
background(255)
global x, y, a, b, selectedRow, selectedColumn, path
x, y, a, b = (20, 20, 50, 75)
selectedRow, selectedColumn = (0, 0)
path = [(0,0)]

def draw():
background(255)
noStroke()

for row in xrange(0,9):
for column in xrange(0,9):
fill(1)
rect(b*column+x, b*row+y, a, a)

#highlight selected cell
fill(0)
rect(b*selectedColumn+x, b*selectedRow+y, a, a)
#create a shape (polyline) by iterating through (x,y) vertexes in our list called "path"
s = createShape()
s.beginShape()
s.stroke(0)
s.noFill()
s.strokeCap(PROJECT)
s.strokeWeight(a)

for block in path:
# rect(b*block[0]+x, b*block[1]+y, a, a)
s.vertex(b*block[0]+x+a/2, b*block[1]+y+a/2)

s.endShape()
shape(s, 0, 0)
#track which arrow keys are pressed then adds that (x,y) to our list called "path"
def keyPressed():
global x, y, a, b, selectedRow, selectedColumn, path

if keyCode == LEFT:
selectedColumn -= 1
path.append((selectedColumn,selectedRow))

if keyCode == RIGHT:
selectedColumn += 1
path.append((selectedColumn,selectedRow))

if keyCode == UP:
selectedRow -= 1
path.append((selectedColumn,selectedRow))

if keyCode == DOWN:
selectedRow += 1
path.append((selectedColumn,selectedRow))

--

--