def main(): # d = distance from origin to screen centre d = 1. camera_pos = np.array([[0, 0, 0, 0]]) look_point = np.array([[0, 0, 1., 0]]) up_vector = np.array([[0, 1, 0, 0]]) # camera_pos = np.array([[0, 0, -1, 0]]) # look_point = np.array([[.2, .2, 1.1, 0]]) # up_vector = np.array([[0, 1, 0, 0]]) camera_pos = np.array([[1., .3, 0, 0]]) look_point = np.array([[1., .3, 1., 0]]) up_vector = np.array([[0., 1., 0, 0]]) # test values # camera_pos = np.array([[1, -2, 0, 0]]) # look_point = np.array([[1, -2, 1, 0]]) # up_vector = np.array([[3, 3, 1, 0]]) m_matrix = gen_m_matrix(camera_pos, look_point, up_vector, d) print m_matrix # light_pos = np.array([0., 0., -1.]) light_pos = np.array([5., 4., -4.]) # light_pos = np.array([1., .3, 1.]) light_pos_t = m_matrix.dot(np.append(light_pos, [0]))[:3] camera_pos_t = m_matrix.dot(camera_pos.T)[:3] screen.draw_bounding_box() obj = obj_file.load('SV1-utah/wt_teapot.obj')['teapot.005'] # obj = obj_file.load('SV1-utah/cube.obj')['Cube'] obj.vertices = [m_matrix.dot(np.append(v, [0]))[:3] for v in obj.vertices] obj.normals = [m_matrix.dot(np.append(v, [0]))[:3] for v in obj.normals] f_nearest = lambda face: min(obj.vertices[v][2] for v, _ in face) obj.faces = sorted(obj.faces, key=f_nearest) for face in obj.faces: try: n_face = [(obj.vertices[v], obj.normals[vn]) for v, vn in face] rasterize_triangle(n_face, m_matrix=m_matrix, light=light_pos_t, view_pt=camera_pos_t) except IndexError: print "cannot find vertex ", pprint(face) # draw_triangle([a for a, _ in face]) # triangle.draw([(1,1),(300,400),(500,400)]) # triangle.draw([(10+5, 10+10),(10+5,50+10),(10+50,10+10)]) # triangle.draw([(50, 50),(50+50,50),(50,50+50)]) # break screen.write('SV1-utah/teapot.png')
def main(): """ Starting point for the whole game. Called automatically when script is run. """ interaction.do_load_default_game() while True: screen.clear_screen() screen.draw_screen(interaction.last_message) interaction.last_message = "" user_input = interaction.get_user_command() should_continue = interaction.do_command(user_input) if should_continue == False: # LT break screen.write_new_line() screen.write('Thank you for playing. Goodbye.')
def do_save_game(): """ Asks the user which game slot to save a game in. Call's the dungeon's save_game() function with player and werewolf data. Updates last_message. :return: """ global last_message # DO NOT REMOVE screen.write("Enter a digit 0-9 to save game into that slot.\nEnter any other character to cancel.\nGame slot: ") slot_number = input() if len(slot_number) != 1 and not slot_number.isdigit(): last_message = "Must enter 0-9 to save a game." else: filename = f'gameSlot{slot_number}.txt' success = dungeon.save_game(None, None, None, None, None, None) # TODO: Replace all the None on this line with the correct arguments. if success: last_message = f'Saved game {slot_number}.' else: last_message = f"Could not save '{filename}'. Data is corrupt or writing files is not allowed."
def do_load_game(): """ Asks the user which game slot to load a game from. Call's the dungeon's load_game() function and, if it was successful, updates the player and werewolf data. Updates last_message.s """ global last_message # DO NOT REMOVE screen.write("Enter a digit 0-9 to load a saved game.\nEnter any other character to cancel.\nGame slot: ") slot_number = input() if len(slot_number) != 1 and not slot_number.isdigit(): last_message = "Must enter 0-9 to load a saved game." else: filename = f'gameSlot{slot_number}.txt' player_data = dungeon.load_game(filename) if player_data: do_update_after_load_game(player_data) last_message = f'Loaded game {slot_number}.' else: last_message = f"Could not load '{filename}'. File is corrupt or does not exist."
def get_user_command(): """ Asks the user for a command. Repeats asking until user enters valid command. Returns command. :return: str, single character valid command """ while True: user_input = input('Enter a command: ') if user_input in [ KEYBOARD_UP, KEYBOARD_DOWN, KEYBOARD_LEFT, KEYBOARD_RIGHT, KEYBOARD_LOOK, KEYBOARD_TAKE, KEYBOARD_USE, KEYBOARD_QUIT, KEYBOARD_LOAD_GAME, KEYBOARD_SAVE_GAME, KEYBOARD_SHOW_HELP, ]: break screen.write('Bad input. Try again. ') return user_input
def display_percent_bar(screen, cloum, lable, percent): screen.cursor(0, cloum) width = screen.width() #clean screen screen.rectangle(0, cloum, width, 0) #format bar = width - (len(lable) + 2) * 6 if percent < 0: percent = 0 elif percent > 100: percent = 100 fill = int(percent * bar / 100) screen.write(lable) screen.cursor(len(lable)*6+bar,cloum) if percent == 100: screen.write('**') else: screen.write('%02d'%percent) screen.rectangle(len(lable)*6, cloum, fill, 0x7E)
def Emit(fsm): screen = fsm.something[0] screen.write(fsm.input_symbol)
def do_show_help(): """ Displays the list of keyboard commands on the screen. Updates last_message. """ global last_message # DO NOT REMOVE screen.clear_screen() screen.write('KEYBOARD COMMANDS\n') screen.write(f' {KEYBOARD_UP} Move up.\n') screen.write(f' {KEYBOARD_DOWN} Move down.\n') screen.write(f' {KEYBOARD_LEFT} Move left.\n') screen.write(f' {KEYBOARD_RIGHT} Move right.\n') screen.write(f' {KEYBOARD_LOOK} Look at the square immediately in front of you.\n') screen.write(f' {KEYBOARD_TAKE} Take object immediately in front of you.\n') screen.write(f' {KEYBOARD_USE} Use an item from your inventory.\n') screen.write(f' {KEYBOARD_QUIT} Quit the game.\n') screen.write(f' {KEYBOARD_LOAD_GAME} Load game from file.\n') screen.write(f' {KEYBOARD_SAVE_GAME} Save game to file.\n') screen.write(f' {KEYBOARD_SHOW_HELP} Show this help screen.\n') last_message = 'Scroll up to see a list of keyboard commands.'
def do_use(from_x, from_y, looking_direction): """ Use item as if you were at location (from_x, from_y) with the given direction. Updates last_message. :param from_x: int, horizontal index in the map :param from_y: int, vertical index in the map :param looking_direction: str, single character representing which way you're looking """ global last_message # DO NOT REMOVE screen.write("What would you like to use? ") item = input() if item == dungeon.MAP_SQUARE_SLINGSHOT and\ player.inventory_has(dungeon.MAP_SQUARE_SLINGSHOT) and\ not player.inventory_has(dungeon.MAP_SQUARE_PEBBLE): last_message = "Can't use the slingshot without ammunition." elif not player.inventory_has(item): last_message = "You don't have any." else: last_message = None object_x = None object_y = None # TODO: Set the values of object_x and object_y so they match the location of the square that you would be looking at given the parameter values. See description. map_square = None # TODO: Replace the None with code that gets the map square at location (object_x, object_y). # TODO: If the user uses a plank on a chasm: # - use the plank from the player's inventory # - update the map to show that the plank has been set (where the chasm was) # - last_message should be "You lay the plank of wood over the chasm. It just barely touches both sides." # TODO: If the user uses a rope on a chasm: # - use the rope from the player's inventory # - update the map to show that the rope has been tied (where the chasm was) # - last_message should be "Standing on the tips of your toes, you reach up and tie the rope to a beam above you." # TODO: If the user uses a pebble on a chasm: # - use the pebble from the player's inventory # - last_message should be "You drop a pebble into the chasm, counting the seconds until it hits the bottom. You hear nothing." # TODO: If the user uses a key on a lock: # - use the key from the player's inventory # - clear the square on the map where the lock was # - last_message should be "You turn the key. Hard. Just as the lock opens you feel the key snap in half." if item == dungeon.MAP_SQUARE_SLINGSHOT: if False: # TODO: Replace False with condition that checks that the player is looking at the werewolf and that the werewolf is close enough for the slingshot to hit it. # TODO: Use a pebble from the player's inventory. damage_points = random.randint(1, player.SLINGSHOT_MAX_DAMAGE) werewolf.do_hit(damage_points) last_message = "You hit the werewolf! " if werewolf.is_alive(): # TODO: Increase werewolf's stun count by 2. last_message += "The beast is temporarily stunned. The werewolf took " last_message += str(damage_points) last_message += " point" if damage_points == 1 else " points" last_message += " of damage." else: last_message += "You have killed the werewolf." else: pebble_destination_x, pebble_destination_y = player.get_farthest_actionable_location(player.SLINGSHOT_DISTANCE, True) distance_shot = utilities.manhattan_distance(player.x, player.y, pebble_destination_x, pebble_destination_y) if distance_shot > 0: # TODO: Use a pebble from the player's inventory. # TODO: Update the location on the map where the pebble landed with a pebble. It was previously empty. last_message = "The pebble you shot lands " last_message += str(distance_shot) last_message += " square" if distance_shot == 1 else " squares" last_message += " away." else: last_message = "There is nothing to shoot your slingshot at." if last_message is None: last_message = "You can't use that here."