Created
August 14, 2021 07:29
-
-
Save mhassanist/7edab353a715795f3811c09d58a1af89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Please follow the TODOs in the code to complete the project | |
''' | |
import turtle #use the turtle library | |
from tkinter import messagebox #tkinter liraray to show a message box | |
#Set the size of the main window. | |
turtle.setup(600,600) | |
#Window size is 600, so we have 300 pixels right and 300 pixels left in the coordinate system. | |
#(0,0) at the center. | |
#create window variable to reference the screen and set some properties | |
window = turtle.Screen() | |
window.title("Get The Food") | |
window.bgcolor("dark salmon") | |
window.tracer(0) | |
#Create the player turtle. | |
player = turtle.Turtle() | |
player.speed(0) | |
player.shape("turtle") | |
player.penup() | |
player.shapesize(0.80, 0.80) | |
player.goto(-250, 0) #Start from the left. | |
#Generate random food items | |
#The below code shows 1 green triangle food item at position (-50,50). | |
position_point_1_x = -50 | |
position_point_1_y = 50 | |
food = turtle.Turtle() | |
food.speed(0) | |
food.shape("triangle") | |
food.color("green") | |
food.penup() | |
food.left(90) | |
food.shapesize(0.80, 0.80) | |
food.goto(position_point_1_x, position_point_1_y) | |
#TODO (1) Modify the code above to display 3 different food items | |
#with different shapes and colors at random positions within the screen | |
#TODO (4): | |
#Write the code within the function body that checks if the position of the player | |
#is close enough to the food. If so, hide the food. | |
def check_touch(): | |
print('TODO') | |
#TODO (5): | |
#Check if all the food generated in step1 got eaten | |
#Show the below alert to indicate winning. | |
#messagebox.showinfo("Hey There", "You Won!!!!!!!!!!!!!!!!!") | |
#function to move the turtle forward | |
def forward(): | |
player.color('light green') | |
player.forward(10) | |
check_touch() | |
#function to rotate the turtle right | |
def turn_right(): | |
player.color('light green') | |
player.right(10) | |
check_touch() | |
#TODO (2): | |
#Define 2 functions for moving right and moving backword | |
#Make sure you call the check_touch() function at the end. | |
#Listn to the window key stroked and take actions | |
#based on the clicked key | |
window.onkeypress(forward, "Up") #When Up arrow clicked, call forward function | |
window.onkeypress(turn_right, "Right") #When Right arrow clicked, call turn_right function | |
#TODO (3): | |
#add the code lines that handles the left and down keys | |
## Go up for TODO 4 and 5 | |
window.listen() #start listening to key strokes | |
# Main game loop | |
while True: | |
window.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment