Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created July 21, 2011 23:29
Show Gist options
  • Save pachacamac/1098494 to your computer and use it in GitHub Desktop.
Save pachacamac/1098494 to your computer and use it in GitHub Desktop.
A snake game in a hand full of Ruby
#!/usr/bin/env ruby
d,l,s,c,f,S=15,3,[[5,6],[5,5],[5,4]],[1,0],[rand(15),rand(15)],STDIN
system'stty -icanon -echoke'
loop do
k=S.getc if k=select([S],[],[],0.2)&&S.getc=="\e"&&S.getc==?[
c={?A=>[0,-1],?B=>[0,1],?C=>[1,0],?D=>[-1,0]}[k]||c
s=[[s[0][0]+c[0],s[0][1]+c[1]]]+s[0..-2]
exit if s[0][0]+1>d||s[0][1]+1>d||s[0][0]+1==0||s[0][1]+1==0||s[1..-1].index(s[0])
s+=[[s[0][0]+c[0],s[0][1]+c[1]]] if s[0]==f
f=[rand(d),rand(d)] if s.index f
(d+3).times{|y|(d+3).times{|x|print y==0?"\e[2J\e[1;1H": s.index([x-1,y-2])? ?#: f==[x-1,y-2]? ?@: x==d+2? "\n": y==1||y==d+2||x==0||x==d+1? ?X: ' '}}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment