Description: A computer game uses a graphical sprite which can be moved around o
ID: 3683122 • Letter: D
Question
Description: A computer game uses a graphical sprite which can be moved around on the screen, one pixel at a time. The screen is of size 10 by 10pixels. Each pixel is associated with a set oftwo numbers the distance along the vertical from the top-left corner (called y) and the distance along the horizontal from the top-left corner (called x) The top-left pixel is associated with the numbers 0,0 for x,y and the bottom-right pixel is associated with 9,9. The diagram below shows how this works Your program must repetitively ask user to press a key on the keyboard to move the sprite by one pixel along either the horizontal or the vertical direction. The game ends once the sprite moves outside the screen. This problem does not ask you to do any visual display of the moving sprite it is enough to print the updated location of the sprite after each move. At the start of the program (i.e. before any user input) the sprite is located on a randomly chosen pixel on the screen (this is called the initial location of the sprite). A number within the range 0,9 (inclusive of both endpoints) can be chosen randomly by program with a call to the randint function available in the random package. Therefore first do import random then call random randint (0,9) which returns a number randomly chosen from the range 0,9. You will need to place the call twice to get randomly chosen numbers for both x and y of the sprite location.Explanation / Answer
import random
x = random.randint(0,9)
y = random.randint(0,9)
print("The initial location of sprite is (",x,",",y," ")
while(1):
option = input("Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:")
if(option == "u"):
y = y-1
elif(option == "d"):
y = y+1
elif(option == "l"):
x = x-1
elif(option == "r"):
x = x+1
else:
print("Your input is not one of the keys u,d,l or r so please try again ")
print("The current location of sprite is (",x,",",y," ")
if(x < 0 or x > 9 or y < 0 or y > 10):
print("Game over since the sprite went outside the screen limits")
break
--output----------------
The initial location of sprite is ( 3 , 9
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:u
The current location of sprite is ( 3 , 8
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:d
The current location of sprite is ( 3 , 9
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:x
Your input is not one of the keys u,d,l or r so please try again
The current location of sprite is ( 3 , 9
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:x
Your input is not one of the keys u,d,l or r so please try again
The current location of sprite is ( 3 , 9
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:l
The current location of sprite is ( 2 , 9
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:l
The current location of sprite is ( 1 , 9
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:l
The current location of sprite is ( 0 , 9
Press one key out of u(for up), d (for down), l (for left), r (for right) followed by enter:l
The current location of sprite is ( -1 , 9
Game over since the sprite went outside the screen limits