Advertisement
Guest User

Untitled

a guest
Jan 31st, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.17 KB | None | 0 0
  1. import tdl
  2. import math
  3. import random
  4.  
  5. # Constants are defined here
  6. WINDOW_WIDTH = 80
  7. WINDOW_HEIGHT = 40
  8. FPS_LIMIT = 20
  9.  
  10. MAIN_CONSOLE_WIDTH = 58
  11. MAIN_CONSOLE_HEIGHT = 40
  12.  
  13. SIDE_CONSOLE_WIDTH = WINDOW_WIDTH - MAIN_CONSOLE_WIDTH
  14. SIDE_CONSOLE_HEIGHT = MAIN_CONSOLE_HEIGHT
  15.  
  16. MAX_MESSAGES = MAIN_CONSOLE_HEIGHT - 4
  17.  
  18. LOGIN_WIDTH = 30
  19. LOGIN_HEIGHT = 9
  20.  
  21. TEMP_LIMIT = 40
  22.  
  23. # Initialize main game window and root console
  24. tdl.set_font('terminal16x16.png', greyscale=True, altLayout=False)
  25. root_console = tdl.init(WINDOW_WIDTH, WINDOW_HEIGHT, title="HackNet")
  26. tdl.set_fps(FPS_LIMIT)
  27.  
  28. # Create console windows
  29. main_console = tdl.Console(MAIN_CONSOLE_WIDTH, MAIN_CONSOLE_HEIGHT)
  30. side_console = tdl.Console(SIDE_CONSOLE_WIDTH, SIDE_CONSOLE_HEIGHT)
  31.  
  32. # Global variables are defined here
  33. name = "John Smith"
  34. cash = 0
  35. ip_route = ["127.0.0.1"]
  36. username = "none"
  37. command_index = len(username) + len(ip_route[0]) + 3
  38. current_command = []
  39. current_username = ""
  40. current_password = ""
  41. messages = []
  42.  
  43. temp_message = ""
  44. temp_time = 0
  45.  
  46.  
  47. class User:
  48.     """Base class for all users found in computers"""
  49.     def __init__(self, name, password):
  50.         self.name = name
  51.         self.password = password
  52.  
  53. class Computer:
  54.     """Base class for all computer objects"""
  55.     def __init__(self, name, ip, users=[], files=[]):
  56.         self.name = name
  57.         self.ip = ip
  58.         self.users = users
  59.         self.files = files
  60.  
  61.     def present_login(self):
  62.         """Presents the login screen to the player"""
  63.         # Draw the login square in the middle of the main game window
  64.         w = LOGIN_WIDTH
  65.         h = LOGIN_HEIGHT
  66.         x = (MAIN_CONSOLE_WIDTH - w) // 2
  67.         y = (MAIN_CONSOLE_HEIGHT - h) // 2
  68.         draw_frame(main_console, x, y, w, h)
  69.  
  70.         # Write computer name and ip at the top
  71.         top = self.name + " - " + self.ip
  72.         main_console.draw_str((w - len(top) // 2 - 1), y+1, top)
  73.  
  74.         # Write main matter
  75.         main_console.draw_str(x+6, y+4, "USER")
  76.         main_console.draw_str(x+2, y+7, "PASSWORD")
  77.  
  78.         # Draw user input boxes
  79.         draw_frame(main_console, x+11, y+3, 15, 2)
  80.         draw_frame(main_console, x+11, y+6, 15, 2)
  81.  
  82.         # Draw temporary message
  83.         main_console.draw_str(x+8, y+10, temp_message, fg=(255,0,0))
  84.  
  85.         # Draw username input
  86.         if current_username == "":
  87.             for i in range(len(current_command)):
  88.                 main_console.draw_str(x+12+i, y+4, current_command[i], fg=(0,255,0))
  89.         else:
  90.             main_console.draw_str(x+12, y+4, current_username, fg=(0,255,0))
  91.             # Draw password input
  92.             if current_password == "":
  93.                 for i in range(len(current_command)):
  94.                     main_console.draw_str(x+12+i, y+7, current_command[i], fg=(0,255,0))
  95.             else:
  96.                 main_console.draw_str(x+12, y+7, current_password, fg=(0,255,0))
  97.  
  98.  
  99. #state = 'command_line'
  100. state = 'login'
  101.  
  102. user = User("jsmith", "123")
  103. users = [user]
  104. computer = Computer("My Computer", "127.0.0.1", users)
  105. active_computer = computer
  106. computers = []
  107. computers.append(computer)
  108.  
  109. user = User("guest", "")
  110. admin = User("admin", "password")
  111. users = [user, admin]
  112. computer = Computer("Friend's Computer", "127.0.0.2", users)
  113. computers.append(computer)
  114.  
  115.  
  116. def draw_frame(console, start_x, start_y, w, h, fg=(255,255,255)):
  117.     """Prettier version of the built-in draw_frame"""
  118.     # Draw the four courners
  119.     console.draw_char(start_x, start_y, '218', fg=fg)
  120.     console.draw_char(start_x, start_y + h, '192', fg=fg)
  121.     console.draw_char(start_x + w, start_y, '191', fg=fg)
  122.     console.draw_char(start_x + w, start_y + h, '217', fg=fg)
  123.  
  124.     # Draw the left and right borders
  125.     for i in range(h - 1):
  126.         console.draw_char(start_x, i+start_y+1, '179', fg=fg)
  127.         console.draw_char(start_x + w, i+start_y+1, '179', fg=fg)
  128.  
  129.     # Draw the top and bottom borders
  130.     for i in range(w - 1):
  131.         console.draw_char(i+start_x+1, start_y, '196', fg=fg)
  132.         console.draw_char(i+start_x+1, start_y + h, '196', fg=fg)
  133.  
  134. def draw_main():
  135.     # Draw bottom input line
  136.     main_console.draw_str(1, MAIN_CONSOLE_HEIGHT-2, username + "@" + ip_route[-1] + "> ")
  137.  
  138.     # Draw input
  139.     for x in range(len(current_command)):
  140.         main_console.draw_str(command_index+x, MAIN_CONSOLE_HEIGHT-2, current_command[x], fg=(0,255,0))
  141.  
  142.     # Draw messages
  143.     for y in range(len(messages)):
  144.         main_console.draw_str(1, y+1, messages[y], fg=(0,255,0))
  145.  
  146. def draw_side():
  147.     side_console.draw_char(1, 1, '160')
  148.     side_console.draw_char(2, 1, '161')
  149.     side_console.draw_char(3, 1, '162')
  150.     side_console.draw_char(1, 2, '163')
  151.     side_console.draw_char(2, 2, '164')
  152.     side_console.draw_char(3, 2, '165')
  153.     side_console.draw_char(1, 3, '166')
  154.     side_console.draw_char(2, 3, '167')
  155.     side_console.draw_char(3, 3, '168')
  156.  
  157.     side_console.draw_str(5, 1, "Name: " + name, fg=(0,255,0))
  158.     side_console.draw_str(5, 2, "Cash: $" + str(cash), fg=(0,255,0))
  159.     side_console.draw_str(5, 3, "IP: " + ip_route[-1], fg=(0,255,0))
  160.  
  161. def handle_keys():
  162.     global current_command
  163.  
  164.     keypress = False
  165.     for event in tdl.event.get():
  166.         if event.type == 'KEYDOWN':
  167.             user_input = event
  168.             keypress = True
  169.             if user_input.keychar != 'TEXT':
  170.                 if (user_input.keychar == 'BACKSPACE'):
  171.                     if (len(current_command) > 0): del(current_command[-1])
  172.                     break
  173.                 if (len(current_command) == MAIN_CONSOLE_WIDTH - command_index - 1 and state == 'command_line'): break
  174.                 if (len(current_command) == 14 and state == 'login'): break
  175.                 if user_input.keychar == 'SPACE': current_command.append(' ')
  176.                 elif user_input.keychar == 'ENTER':
  177.                     if (state == 'command_line'): handle_input()
  178.                     elif (state == 'login'): handle_login()
  179.                 else: current_command.append(user_input.keychar)
  180.         if not keypress:
  181.             return
  182.  
  183. def handle_input():
  184.     """Fires whenever the user presses the enter key while entering a command"""
  185.     global state
  186.  
  187.     # If the user has entered nothing we just send back an ">"
  188.     if (len(current_command) == 0):
  189.         message(">")
  190.  
  191.     # Otherwise we check the command for a match
  192.     else:
  193.         command = ''.join(current_command)
  194.         if (command == "clear"):
  195.             messages.clear()
  196.         elif (command == "disconnect"):
  197.             del(ip_route[-1])
  198.             state = 'login'
  199.         elif (command[:8] == "connect "):
  200.             connect(command[8:])
  201.         else:
  202.             message(">ERROR: " + command + " is not a recognized command")
  203.    
  204.     # Clears the current command entered
  205.     current_command.clear()
  206.  
  207. def handle_login():
  208.     """Handles storing the state and input on a login screen"""
  209.     global current_username, current_password, temp_message, temp_time, state, username
  210.  
  211.     # If we have no username our input becomes the username
  212.     if current_username == "":
  213.         current_username = ''.join(current_command)
  214.         current_command.clear()
  215.     # Otherwise it becomes the password
  216.     else:
  217.         current_password = ''.join(current_command)
  218.         current_command.clear()
  219.  
  220.         # Compare user and password matches for all users on the active computer
  221.         user = None
  222.         for user in active_computer.users:
  223.             print("Current username: " + current_username)
  224.             print("Current password: " + current_password)
  225.             print(user.name + " " + user.password)
  226.             if user.name == current_username and user.password == current_password:
  227.                 username = user.name
  228.                 state = 'command_line'
  229.                 message("Welcome " + username + "!")
  230.                 break
  231.         else:
  232.             w = LOGIN_WIDTH
  233.             h = LOGIN_HEIGHT
  234.             x = (MAIN_CONSOLE_WIDTH - w) // 2
  235.             y = (MAIN_CONSOLE_HEIGHT - h) // 2
  236.             temp_message = "Incorrect login!"
  237.             temp_time = 0
  238.  
  239.         # Finally clear stored username and password
  240.         current_password = ""
  241.         current_username = ""
  242.  
  243. def message(mess):
  244.     """Inserts a message into the global list of messages"""
  245.     # Makes sure message is not too long
  246.     while(len(mess) > MAIN_CONSOLE_WIDTH - 2):
  247.         mess = mess[:-1]
  248.  
  249.     # Insert the new message at the top of the list
  250.     messages.append(mess)
  251.  
  252.     # If too many messages are active we cull the oldest
  253.     if (len(messages) > MAX_MESSAGES): del(messages[0])
  254.  
  255. def connect(ip):
  256.     """Fires when the user connects to another computer"""
  257.     global state, ip_route
  258.  
  259.     for computer in computers:
  260.         if computer.ip == ip and computer != active_computer:
  261.             ip_route.append(ip)
  262.             username = "none"
  263.             state = 'login'
  264.             messages.clear()
  265.             break
  266.     else:
  267.         message("Error: Could not find address")
  268.  
  269. def render():
  270.     """This function draws all objects and blits them onto the screen"""
  271.     # Clear the console
  272.     main_console.clear(fg=(0,0,0), bg=(0,0,0))
  273.  
  274.     # Draw frames for consoles
  275.     draw_frame(main_console, 0, 0, MAIN_CONSOLE_WIDTH-1, MAIN_CONSOLE_HEIGHT-1, fg=(0,255,0))
  276.     draw_frame(side_console, 0, 0, SIDE_CONSOLE_WIDTH-1, SIDE_CONSOLE_HEIGHT-1, fg=(0,255,0))
  277.  
  278.     # Draw side console information
  279.     draw_side()
  280.  
  281.     # Draw main console if we're at the regular screen
  282.     if (state == 'command_line'):
  283.         draw_main()
  284.     if (state == 'login'):
  285.         active_computer.present_login()
  286.  
  287.  
  288.     # Blit onto the root console and flush updates
  289.     root_console.blit(main_console)
  290.     root_console.blit(side_console, x=MAIN_CONSOLE_WIDTH, y=0)
  291.     tdl.flush()
  292.  
  293. # Main Loop
  294. while not tdl.event.is_window_closed():
  295.     # Update where commands are to start being written
  296.     command_index = len(username) + len(ip_route[0]) + 3
  297.  
  298.     # Update the current computer being used by the player
  299.     for computer in computers:
  300.         if (ip_route[-1] == computer.ip):
  301.             active_computer = computer
  302.  
  303.     # Handle user input
  304.     handle_keys()
  305.  
  306.     # Increase temporary message time
  307.     temp_time += 1
  308.     if (temp_time == TEMP_LIMIT):
  309.         temp_time = 0
  310.         temp_message = ""
  311.  
  312.     # Render the screen
  313.     render()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement