# -*- coding: utf-8 -*- """ Created on Wed Feb 13 11:56:29 2019 Create a game where a character is on a 2D plane with x and y co-ordinates. Accept wasd input to move him in the 4 directions and update his position. Print out a special message when he lands on the goal square, for example (9,3) and end the game. xExtension 1: Count the number of moves the user takes. xExtension 2: Make the game end after 30 moves. Challenge: Give clues such as hot/cold to the destination after each step. Could use a numerical scale of hot and coldness. If you do MATH you better know how to calculate it properly. xChallenge 2:Randomise the goal and start square each time. - you are free to skip this when testing xChallenge 3: What about hotter or colder? Give hints that compare to the previous location Challenge 4:At this point you might as well use ASCII characters and draw a board, use a symbol for squares and a symbol for the character. You can assume the board will only be 10x10, you don't need to go too crazy You do not need arrays to do this task. @author: achuang """ def main(): import random import math player_y = random.randrange(1,10) player_x = random.randrange(1,10) prev_player_y = 0 prev_player_x = 0 move = 0 tries = 0 goal_x = random.randrange(1,10) goal_y = random.randrange(1,10) print("goal coordinates are: x" + str(goal_x) + " y" + str(goal_y)) print("player coordinates are: x" + str(player_x) + " y" + str(player_y)) while(int(tries) <= 20): skip = 0 print("which direction would you like to move? use wasd") move = input() if(move == "w" and player_y != 10): prev_player_y = player_y player_y = player_y + 1 tries = tries + 1 print("x" + str(player_x) + " y" + str(player_y)) print("you have used " + str(tries) + "/20 tries.") elif(move == "a" and player_x != 1): prev_player_x = player_x player_x = player_x - 1 tries = tries + 1 print("x" + str(player_x) + " y" + str(player_y)) print("you have used " + str(tries) + "/20 tries.") elif(move == "s" and player_y != 1): prev_player_y = player_y player_y = player_y - 1 tries = tries + 1 print("x" + str(player_x) + " y" + str(player_y)) print("you have used " + str(tries) + "/20 tries.") elif(move == "d" and player_x != 10): prev_player_x = player_x player_x = player_x + 1 tries = tries + 1 print("x" + str(player_x) + " y" + str(player_y)) print("you have used " + str(tries) + "/20 tries.") else: print("invalid input, try again.") skip = 1 if(player_x == goal_x and player_y == goal_y and skip == 0): print("you have reached the goal!") break if(skip == 0): player_x_dif = player_x - goal_x prev_player_x_dif = prev_player_x - goal_x player_x_dif = abs(player_x_dif) prev_player_x_dif = abs(prev_player_x_dif) player_y_dif = player_y - goal_y prev_player_y_dif = prev_player_y - goal_y player_y_dif = abs(player_y_dif) prev_player_y_dif = abs(prev_player_y_dif) current_dist = (player_x_dif * player_x_dif + player_y_dif * player_y_dif) prev_dist = (prev_player_x_dif * prev_player_x_dif + prev_player_y_dif * prev_player_y_dif) current_dist = (math.sqrt(current_dist)) prev_dist = (math.sqrt(prev_dist)) if(current_dist < prev_dist): print("warmer") elif(current_dist > prev_dist): print("colder")