Skip to content

Instantly share code, notes, and snippets.

@robbrit
Forked from anonymous/gist:2703820
Created May 15, 2012 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbrit/2703854 to your computer and use it in GitHub Desktop.
Save robbrit/2703854 to your computer and use it in GitHub Desktop.
$ ->
c = $("canvas").get(0)
ctx = c.getContext "2d"
#ctx.font="20px sans-serif"
#mainLoop
#running
initial_sleep_length=250
sleep_length=initial_sleep_length
img_data_for_player=ctx.createImageData c.width,c.height
tile_size=25
player=snake c.width/2,c.height/2,img_data_for_player,tile_size
#feed=[]
player.up()
window.onkeydown=readKey
debug_out = $("#debug_area")
readKey = (event) ->
key_code = event.keyCode
switch key_code
when 27, 96 then (stop() if running else start())
when 33, 93 then faster()
when 34, 91 then slower()
when 37, 97 then player.left()
when 38, 119 then player.up()
when 39, 100 then player.right()
when 40, 115 then player.down()
else debug_out.innerHTML="Unbinded key code: #{key_code}\n"
draw = ->
player.paint()
null
paintImgData = (img_data) ->
ctx.putImageData img_data,0,0,0,0,c.width,c.height
putFeed()
null
randomTile = (resolution) ->
Math.floor Math.random * resolution/tile_size
putFeed = ->
if not feed[0]?
feed[0]="waiting"
setTimeout(
->
feed=[randomTile c.width,randomTile c.height]
Math.floor Math.random()*1000+200)
console.log "timeout set"
else if feed[0] is "waiting" #console.log "elseif: "+feed
else
console.log "else"
ctx.fillStyle = "#ffff00"
ctx.beginPath
ctx.arc feed[0]*tile_size-tile_size/2,feed[1]*tile_size-tile_size/2,tile_size/2,0,2*Math.PI
ctx.fill
ctx.closePath
null
snake = (x,y,img_data,@tile_size) ->
#variables
max_snake_length=3
#array stores x and y per each snake`s segment hence times two
max_array_length=max_snake_length*2
#move_x, move_y
body_coordinates=new Array parseInt x/tile_size, parseInt y/tile_size
grid_resolution_x=img_data.width/tile_size
grid_resolution_y=img_data.height/tile_size
#every_other_frame=false
#functions
addCoordinates = (x,y) ->
if body_coordinates.length >= max_array_length
body_coordinates.splice 0,2
else body_coordinates.push x,y
null
currentCoordinates = ->
body_coordinates.slice -2
this.up = ->
move_x=0
move_y=-1
null
this.down = ->
move_x=0
move_y=1
null
this.right = ->
move_x=1
move_y=0
null
this.left = ->
move_x = -1
move_y = 0
null
isPartOfSnake = (a,b) ->
if body_coordinates[i] is a and body_coordinates[i+1] is b then return true for body_coordinates in [0..body_coordinates.length] by 2
false
move = ->
###
currentCoordinates return an array
in form of (current_x,current_y)
###
current_location = currentCoordinates()
if (!(isPartOfSnake(current_location[0]+move_x,current_location[1]+move_y) || current_location[0]+move_x>=grid_resolution_x || current_location[0]+move_x < 0 || current_location[1]+move_y>=grid_resolution_y || current_location[1]+move_y < 0))
addCoordinates(current_location[0]+move_x,current_location[1]+move_y)
else
stop()
alert "Game Over"
null
this.paint = ->
#we might someday move the player only the every other frame
#if (every_other_frame) {
move()
###every_other_frame=false;
} else {
every_other_frame=true;
}###
#TODO can be optimized a lot
#clear img_data of previous paint
for channel, i in img_data.data
channel = if (i+1)%4 then 0 else 255
###for (i=0; i<img_data.width*img_data.height*4;i++) {
img_data.data[i]=((i+1)%4) ? 0 : 255;
}###
#instead let's just be inefficient and draw as needed
#NOTE each coordiante pair is stored as two values hence incriment by 2
paintTile body_coordinates[i]*tile_size,body_coordinates[i+1]*tile_size,(body_coordinates[i]+1)*tile_size,(body_coordinates[i+1]+1)*tile_size
paintImgData img_data
paintTile = (x1,y1,x2,y2) ->
original_x1=x1
for y1 in [y1..y2]
for x1 in [x1..x2]
#paint it green
img_data.data[(x1+y1*img_data.width)*4+1]=255
#make it visible
img_data.data[(x1+y1*img_data.width)*4+3]=255
#we must restore x1 value before we can do new inner loop
x1=original_x1
stop = ->
mainLoop=window.clearInterval mainLoop
running = false
start = ->
if not running
mainLoop=self.setInterval "draw()", sleep_length
running = true
refresh_timer = ->
stop()
start()
updateStatusSpeed()
faster = ->
if sleep_length > 50
sleep_length-=25
refresh_timer()
slower = ->
if sleep_length < 400
sleep_length+=25
refresh_timer()
updateStatusSpeed = ->
$("#speed_value").innerHTML="#{(initial_sleep_length/sleep_length).toFixed 2}x"
$("#speed_entry").style.visibility="visible"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment