Created
January 27, 2011 15:48
-
-
Save aarongreenlee/798664 to your computer and use it in GitHub Desktop.
Have fun in a text adventure game inside the Matrix.
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
# Text Adventure Game | |
# "Inside the Matrix" | |
# http://aarongreenlee.com/ | |
SLEEPTIME = 1 | |
INTRO_STRINGS = ['The Matrix has you.','Follow the White Rabbit.'] | |
# Helpers | |
def print_delay(s) | |
sleep(SLEEPTIME) | |
print s + " " | |
end | |
def puts_delay(s) | |
sleep(SLEEPTIME) | |
puts s + " " | |
end | |
# The entire universe of the game is contained within the matrix. | |
# There is no exit. There is only matrix. | |
class Matrix | |
attr_accessor :player | |
def initialize(player_name) | |
@player = Player.new(player_name) | |
@rooms = [] | |
end | |
def add_room(reference, name, description, connections) | |
@rooms << Room.new(reference, name, description, connections) | |
end | |
def start(location) | |
@player.location = location | |
puts "You were sleeping but now you are awake." | |
print_delay("\nYou look around to see where you are\n") | |
3.times do | |
print_delay(".") | |
end | |
show_current_description | |
sleep(SLEEPTIME * 2) | |
end | |
def show_current_description | |
print_delay(find_room_in_matrix(@player.location).full_description) | |
end | |
def find_room_in_matrix(reference) | |
@rooms.detect { |room| room.reference == reference } | |
end | |
def find_room_in_direction(direction) | |
find_room_in_matrix(@player.location).connections[direction] | |
end | |
def room_has_rabbit(reference) | |
find_room_in_matrix(reference).has_rabbit | |
end | |
def go(direction) | |
system('cls') | |
puts "You go " + direction.to_s + " and find yourself in" | |
3.times do | |
print_delay(".") | |
end | |
@player.location = find_room_in_direction(direction) | |
show_current_description | |
end | |
class Player | |
attr_accessor :name, :location | |
def initialize(name) | |
@name = name | |
end | |
end | |
class Room | |
attr_accessor :reference, :name, :description, :connections, :has_rabbit | |
def initialize(reference, name, description, connections) | |
@reference = reference | |
@name = name | |
@description = description | |
@connections = connections | |
@has_rabbit = false | |
end | |
def full_description | |
@name + "\n\nYou are in " + @description | |
end | |
end | |
end | |
class Game | |
def initialize | |
system('cls') | |
print "What is your name? " | |
@relm = Matrix.new(gets.chomp) | |
INTRO_STRINGS.unshift("Wake up, #{@relm.player.name}.") | |
construct_rooms | |
# Print our cool intro | |
intro | |
# Play the game | |
@relm.start(:neopad) | |
white_rabbit_found = false | |
until white_rabbit_found | |
white_rabbit_found = @relm.room_has_rabbit(@relm.player.location) | |
if white_rabbit_found | |
puts "\nThe White Rabbit is here..." | |
puts "\nYou can now leave the Matrix." | |
sleep(SLEEPTIME) | |
Process.exit | |
else | |
puts "\nBut the White Rabbit is not here...\n" | |
end | |
# Navigate | |
navigate | |
end | |
end | |
def navigate | |
puts "\nYou are faced with a decision to go somewhere." | |
puts "What direction do you want to go?" | |
get_directions | |
end | |
def get_directions | |
place = @relm.find_room_in_matrix(@relm.player.location) | |
puts "\nYou can go in the following directions: " | |
choices = [] | |
direction_map = {} | |
place.connections.each { |direction,value| | |
print '...' | |
case direction | |
when :north | |
print "North\n" | |
choices << 'n' | |
direction_map['n'] = :north | |
when :south | |
print "South\n" | |
choices << 's' | |
direction_map['s'] = :south | |
when :east | |
print "East\n" | |
choices << 'e' | |
direction_map['e'] = :east | |
when :west | |
print "West\n" | |
choices << 'w' | |
direction_map['w'] = :west | |
end | |
} | |
destination = '' | |
until choices.include? destination | |
print "\n\nWhere would you like to go (#{choices.join(',')}) ? " | |
destination = gets.chomp | |
end | |
@relm.go(direction_map[destination]) | |
end | |
private | |
def intro | |
system("cls") | |
print "\n" | |
INTRO_STRINGS.each{ |word| | |
word.split(' ').each { | |
|w| | |
print_delay(w) | |
} | |
sleep(SLEEPTIME*2) | |
print "\n" | |
} | |
puts "Knock, knock." | |
sleep(SLEEPTIME) | |
system("cls") | |
end | |
def construct_rooms | |
rabbit_rooms = [:nebuchadnezzar,:FBI,:work,:oracle,:city] | |
@relm.add_room(:neopad, | |
'Neo\'s apartment', | |
'a classic computer hacker pad. Messy with lots of computers.', | |
{:south => :club} | |
) | |
@relm.add_room(:club, | |
'a Techno Bondage Club', | |
'a club with techno music, black leather and more shinny leather. This is a crazy place.', | |
{ | |
:north => :neopad, | |
:east => :work, | |
:west => :oracle, | |
:south => :nebuchadnezzar | |
} | |
) | |
@relm.add_room(:nebuchadnezzar, | |
'the Nebuchadnezzar', | |
'a ship with a captain named Morpheus and a first mate named Trinity.', | |
{ | |
:north => :club, | |
:east => :FBI, | |
:west => :city | |
} | |
) | |
@relm.add_room(:FBI, | |
'the FBI Headquarters!!!!', | |
'an office building. Agents dwell here. Be careful!', | |
{ | |
:north => :work, | |
:west => :nebuchadnezzar | |
} | |
) | |
@relm.add_room(:work, | |
'at an office building with a cube farm.', | |
'a place where many lives are wasted here. Be careful!', | |
{ | |
:south => :FBI, | |
:east => :club | |
} | |
) | |
@relm.add_room(:oracle, | |
'inside the Oracle\'s ghetto apartment.', | |
'that smells like cookies in here. Don\'t worry about the vase.', | |
{ | |
:south => :city, | |
:west => :club | |
} | |
) | |
@relm.add_room(:city, | |
'on the city streets.', | |
'a city with people are everywhere. Everything is green. You are distracted by a woman in a red dress', | |
{ | |
:north => :oracle, | |
:east => :nebuchadnezzar | |
} | |
) | |
# Randomly select a room to place the white rabbit in. | |
r = rand(rabbit_rooms.length) | |
rabbit_room = @relm.find_room_in_matrix(rabbit_rooms[r]) | |
rabbit_room.has_rabbit = true | |
end | |
end | |
######################### | |
# BOOTSTRAP THIS Matrix # | |
######################### | |
g = Game.new() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment