Skip to content

Instantly share code, notes, and snippets.

/DodgeGame Secret

Created June 11, 2015 00:20
Show Gist options
  • Save anonymous/c6985292f2349e0272a4 to your computer and use it in GitHub Desktop.
Save anonymous/c6985292f2349e0272a4 to your computer and use it in GitHub Desktop.
class DodgeGame
def initialize
@position = 0
end
def displayWorld
puts "\r\n\n\n"
puts "#######################################\r"
print "#"
i = 0
while i < 32
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment