Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2015 06:05
Show Gist options
  • Save anonymous/54060c0c7b3c47716758 to your computer and use it in GitHub Desktop.
Save anonymous/54060c0c7b3c47716758 to your computer and use it in GitHub Desktop.
def readLastKey
char = STDIN.read_nonblock(1).ord rescue nil
key = char
while char != nil
char = STDIN.read_nonblock(1).ord rescue nil
if char != nil
key = char
end
end
key
end
class DodgeGame
def initialize
@position = 0
end
def printWorld
puts "\r\n\n\n"
puts "#######################################\r"
print "#"
i = 0
while i < 33
if @position == i
print "(^u^)"
else
print " "
end
i = i + 1
end
puts "#\r"
puts "# #\r"
puts "# #\r"
puts "# #\r"
puts "# #\r"
puts "# #\r"
puts "# #\r"
puts "#######################################\r"
end
def moveChar
key = readLastKey
if key == 68 and @position > 0
@position = @position - 1
elsif key == 67 and @position < 31
@position = @position + 1
end
end
def gameLoop
i = 0
system('stty raw -echo')
while i < 300
sleep 0.1
moveChar
printWorld
i = i + 1
end
system('stty -raw echo')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment