Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tdl
- import math
- import random
- # Constants are defined here
- WINDOW_WIDTH = 80
- WINDOW_HEIGHT = 40
- FPS_LIMIT = 20
- MAIN_CONSOLE_WIDTH = 58
- MAIN_CONSOLE_HEIGHT = 40
- SIDE_CONSOLE_WIDTH = WINDOW_WIDTH - MAIN_CONSOLE_WIDTH
- SIDE_CONSOLE_HEIGHT = MAIN_CONSOLE_HEIGHT
- MAX_MESSAGES = MAIN_CONSOLE_HEIGHT - 4
- LOGIN_WIDTH = 30
- LOGIN_HEIGHT = 9
- TEMP_LIMIT = 40
- # Initialize main game window and root console
- tdl.set_font('terminal16x16.png', greyscale=True, altLayout=False)
- root_console = tdl.init(WINDOW_WIDTH, WINDOW_HEIGHT, title="HackNet")
- tdl.set_fps(FPS_LIMIT)
- # Create console windows
- main_console = tdl.Console(MAIN_CONSOLE_WIDTH, MAIN_CONSOLE_HEIGHT)
- side_console = tdl.Console(SIDE_CONSOLE_WIDTH, SIDE_CONSOLE_HEIGHT)
- # Global variables are defined here
- name = "John Smith"
- cash = 0
- ip_route = ["127.0.0.1"]
- username = "none"
- command_index = len(username) + len(ip_route[0]) + 3
- current_command = []
- current_username = ""
- current_password = ""
- messages = []
- temp_message = ""
- temp_time = 0
- class User:
- """Base class for all users found in computers"""
- def __init__(self, name, password):
- self.name = name
- self.password = password
- class Computer:
- """Base class for all computer objects"""
- def __init__(self, name, ip, users=[], files=[]):
- self.name = name
- self.ip = ip
- self.users = users
- self.files = files
- def present_login(self):
- """Presents the login screen to the player"""
- # Draw the login square in the middle of the main game window
- w = LOGIN_WIDTH
- h = LOGIN_HEIGHT
- x = (MAIN_CONSOLE_WIDTH - w) // 2
- y = (MAIN_CONSOLE_HEIGHT - h) // 2
- draw_frame(main_console, x, y, w, h)
- # Write computer name and ip at the top
- top = self.name + " - " + self.ip
- main_console.draw_str((w - len(top) // 2 - 1), y+1, top)
- # Write main matter
- main_console.draw_str(x+6, y+4, "USER")
- main_console.draw_str(x+2, y+7, "PASSWORD")
- # Draw user input boxes
- draw_frame(main_console, x+11, y+3, 15, 2)
- draw_frame(main_console, x+11, y+6, 15, 2)
- # Draw temporary message
- main_console.draw_str(x+8, y+10, temp_message, fg=(255,0,0))
- # Draw username input
- if current_username == "":
- for i in range(len(current_command)):
- main_console.draw_str(x+12+i, y+4, current_command[i], fg=(0,255,0))
- else:
- main_console.draw_str(x+12, y+4, current_username, fg=(0,255,0))
- # Draw password input
- if current_password == "":
- for i in range(len(current_command)):
- main_console.draw_str(x+12+i, y+7, current_command[i], fg=(0,255,0))
- else:
- main_console.draw_str(x+12, y+7, current_password, fg=(0,255,0))
- #state = 'command_line'
- state = 'login'
- user = User("jsmith", "123")
- users = [user]
- computer = Computer("My Computer", "127.0.0.1", users)
- active_computer = computer
- computers = []
- computers.append(computer)
- user = User("guest", "")
- admin = User("admin", "password")
- users = [user, admin]
- computer = Computer("Friend's Computer", "127.0.0.2", users)
- computers.append(computer)
- def draw_frame(console, start_x, start_y, w, h, fg=(255,255,255)):
- """Prettier version of the built-in draw_frame"""
- # Draw the four courners
- console.draw_char(start_x, start_y, '218', fg=fg)
- console.draw_char(start_x, start_y + h, '192', fg=fg)
- console.draw_char(start_x + w, start_y, '191', fg=fg)
- console.draw_char(start_x + w, start_y + h, '217', fg=fg)
- # Draw the left and right borders
- for i in range(h - 1):
- console.draw_char(start_x, i+start_y+1, '179', fg=fg)
- console.draw_char(start_x + w, i+start_y+1, '179', fg=fg)
- # Draw the top and bottom borders
- for i in range(w - 1):
- console.draw_char(i+start_x+1, start_y, '196', fg=fg)
- console.draw_char(i+start_x+1, start_y + h, '196', fg=fg)
- def draw_main():
- # Draw bottom input line
- main_console.draw_str(1, MAIN_CONSOLE_HEIGHT-2, username + "@" + ip_route[-1] + "> ")
- # Draw input
- for x in range(len(current_command)):
- main_console.draw_str(command_index+x, MAIN_CONSOLE_HEIGHT-2, current_command[x], fg=(0,255,0))
- # Draw messages
- for y in range(len(messages)):
- main_console.draw_str(1, y+1, messages[y], fg=(0,255,0))
- def draw_side():
- side_console.draw_char(1, 1, '160')
- side_console.draw_char(2, 1, '161')
- side_console.draw_char(3, 1, '162')
- side_console.draw_char(1, 2, '163')
- side_console.draw_char(2, 2, '164')
- side_console.draw_char(3, 2, '165')
- side_console.draw_char(1, 3, '166')
- side_console.draw_char(2, 3, '167')
- side_console.draw_char(3, 3, '168')
- side_console.draw_str(5, 1, "Name: " + name, fg=(0,255,0))
- side_console.draw_str(5, 2, "Cash: $" + str(cash), fg=(0,255,0))
- side_console.draw_str(5, 3, "IP: " + ip_route[-1], fg=(0,255,0))
- def handle_keys():
- global current_command
- keypress = False
- for event in tdl.event.get():
- if event.type == 'KEYDOWN':
- user_input = event
- keypress = True
- if user_input.keychar != 'TEXT':
- if (user_input.keychar == 'BACKSPACE'):
- if (len(current_command) > 0): del(current_command[-1])
- break
- if (len(current_command) == MAIN_CONSOLE_WIDTH - command_index - 1 and state == 'command_line'): break
- if (len(current_command) == 14 and state == 'login'): break
- if user_input.keychar == 'SPACE': current_command.append(' ')
- elif user_input.keychar == 'ENTER':
- if (state == 'command_line'): handle_input()
- elif (state == 'login'): handle_login()
- else: current_command.append(user_input.keychar)
- if not keypress:
- return
- def handle_input():
- """Fires whenever the user presses the enter key while entering a command"""
- global state
- # If the user has entered nothing we just send back an ">"
- if (len(current_command) == 0):
- message(">")
- # Otherwise we check the command for a match
- else:
- command = ''.join(current_command)
- if (command == "clear"):
- messages.clear()
- elif (command == "disconnect"):
- del(ip_route[-1])
- state = 'login'
- elif (command[:8] == "connect "):
- connect(command[8:])
- else:
- message(">ERROR: " + command + " is not a recognized command")
- # Clears the current command entered
- current_command.clear()
- def handle_login():
- """Handles storing the state and input on a login screen"""
- global current_username, current_password, temp_message, temp_time, state, username
- # If we have no username our input becomes the username
- if current_username == "":
- current_username = ''.join(current_command)
- current_command.clear()
- # Otherwise it becomes the password
- else:
- current_password = ''.join(current_command)
- current_command.clear()
- # Compare user and password matches for all users on the active computer
- user = None
- for user in active_computer.users:
- print("Current username: " + current_username)
- print("Current password: " + current_password)
- print(user.name + " " + user.password)
- if user.name == current_username and user.password == current_password:
- username = user.name
- state = 'command_line'
- message("Welcome " + username + "!")
- break
- else:
- w = LOGIN_WIDTH
- h = LOGIN_HEIGHT
- x = (MAIN_CONSOLE_WIDTH - w) // 2
- y = (MAIN_CONSOLE_HEIGHT - h) // 2
- temp_message = "Incorrect login!"
- temp_time = 0
- # Finally clear stored username and password
- current_password = ""
- current_username = ""
- def message(mess):
- """Inserts a message into the global list of messages"""
- # Makes sure message is not too long
- while(len(mess) > MAIN_CONSOLE_WIDTH - 2):
- mess = mess[:-1]
- # Insert the new message at the top of the list
- messages.append(mess)
- # If too many messages are active we cull the oldest
- if (len(messages) > MAX_MESSAGES): del(messages[0])
- def connect(ip):
- """Fires when the user connects to another computer"""
- global state, ip_route
- for computer in computers:
- if computer.ip == ip and computer != active_computer:
- ip_route.append(ip)
- username = "none"
- state = 'login'
- messages.clear()
- break
- else:
- message("Error: Could not find address")
- def render():
- """This function draws all objects and blits them onto the screen"""
- # Clear the console
- main_console.clear(fg=(0,0,0), bg=(0,0,0))
- # Draw frames for consoles
- draw_frame(main_console, 0, 0, MAIN_CONSOLE_WIDTH-1, MAIN_CONSOLE_HEIGHT-1, fg=(0,255,0))
- draw_frame(side_console, 0, 0, SIDE_CONSOLE_WIDTH-1, SIDE_CONSOLE_HEIGHT-1, fg=(0,255,0))
- # Draw side console information
- draw_side()
- # Draw main console if we're at the regular screen
- if (state == 'command_line'):
- draw_main()
- if (state == 'login'):
- active_computer.present_login()
- # Blit onto the root console and flush updates
- root_console.blit(main_console)
- root_console.blit(side_console, x=MAIN_CONSOLE_WIDTH, y=0)
- tdl.flush()
- # Main Loop
- while not tdl.event.is_window_closed():
- # Update where commands are to start being written
- command_index = len(username) + len(ip_route[0]) + 3
- # Update the current computer being used by the player
- for computer in computers:
- if (ip_route[-1] == computer.ip):
- active_computer = computer
- # Handle user input
- handle_keys()
- # Increase temporary message time
- temp_time += 1
- if (temp_time == TEMP_LIMIT):
- temp_time = 0
- temp_message = ""
- # Render the screen
- render()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement