-
-
Save anonymous/c6985292f2349e0272a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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