From 6263f266f2800c531b03a27cf7de96d3ecba961f Mon Sep 17 00:00:00 2001 From: jsoud <146140752+jsoud@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:45:21 -0800 Subject: [PATCH 1/2] Update Caterpillar.py Updated Caterpillar.py so that it keeps track of the player's high score. --- Caterpillar_Game/Caterpillar.py | 83 +++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/Caterpillar_Game/Caterpillar.py b/Caterpillar_Game/Caterpillar.py index 4afc8538..2055aecf 100644 --- a/Caterpillar_Game/Caterpillar.py +++ b/Caterpillar_Game/Caterpillar.py @@ -10,7 +10,7 @@ caterpillar.hideturtle() leaf = t.Turtle() -leaf_shape = ((0,0),(14,2),(18,6),(20,20),(6,18),(2,14)) +leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14)) t.register_shape('leaf', leaf_shape) leaf.shape('leaf') leaf.color('green') @@ -27,50 +27,65 @@ score_turtle = t.Turtle() score_turtle.hideturtle() score_turtle.speed(0) +score = 0 +high_score = None +with open("highscore_tracker.txt") as file: + high_score = int(file.read()) def outside_window(): - left_wall = -t.window_width()/2 - right_Wall = t.window_width()/2 - top_wall = t.window_height()/2 - bottom_wall = -t.window_height()/2 - (x,y) = caterpillar.pos() + left_wall = -t.window_width() / 2 + right_Wall = t.window_width() / 2 + top_wall = t.window_height() / 2 + bottom_wall = -t.window_height() / 2 + (x, y) = caterpillar.pos() outside = x < left_wall or x > right_Wall or y > top_wall or y < bottom_wall return outside + def game_over(): + global high_score + if score > high_score: + high_score = score + with open("highscore_tracker.txt", mode="w") as file: + file.write(str(high_score)) caterpillar.color('yellow') leaf.color('yellow') t.penup() t.hideturtle() - t.write('GAME OVER !', align='center', font=('Arial', 30, 'normal') ) - t.onkey(start_game,'space') + t.write('GAME OVER !', align='center', font=('Arial', 30, 'normal')) + t.onkey(start_game, 'space') def display_score(current_score): score_turtle.clear() score_turtle.penup() - x = (t.window_width()/2) - 70 - y = (t.window_height()/2) - 70 - score_turtle.setpos(x,y) - score_turtle.write(str(current_score), align='right', font=('Arial', 40, 'bold')) + score_x = (t.window_width() / 2) - 300 + score_y = (t.window_height() / 2) - 70 + score_turtle.setpos(score_x, score_y) + score_turtle.write("Score: " + str(current_score), align='left', font=('Arial', 40, 'bold')) + highscore_x = (t.window_width() / 2) - 950 + high_score_y = (t.window_height() / 2) - 70 + score_turtle.setpos(highscore_x, high_score_y) + score_turtle.write("High Score: " + str(high_score), align='left', font=('Arial', 40, 'bold')) def place_leaf(): leaf.hideturtle() - leaf.setx(rd.randint(-200,200)) - leaf.sety(rd.randint(-200,200)) + leaf.setx(rd.randint(-200, 200)) + leaf.sety(rd.randint(-200, 200)) leaf.showturtle() + def start_game(): global game_started if game_started: return game_started = True - - score = 0 + + global score text_turtle.clear() caterpillar_speed = 2 caterpillar_length = 3 - caterpillar.shapesize(1,caterpillar_length,1) + caterpillar.shapesize(1, caterpillar_length, 1) caterpillar.showturtle() display_score(score) place_leaf() @@ -80,7 +95,7 @@ def start_game(): if caterpillar.distance(leaf) < 20: place_leaf() caterpillar_length = caterpillar_length + 1 - caterpillar.shapesize(1,caterpillar_length,1) + caterpillar.shapesize(1, caterpillar_length, 1) caterpillar_speed = caterpillar_speed + 1 score = score + 10 display_score(score) @@ -88,26 +103,32 @@ def start_game(): game_over() break + def move_up(): - caterpillar.setheading(90) + caterpillar.setheading(90) + def move_down(): - caterpillar.setheading(270) + caterpillar.setheading(270) + def move_left(): - caterpillar.setheading(180) + caterpillar.setheading(180) + def move_right(): - caterpillar.setheading(0) - + caterpillar.setheading(0) + + def restart_game(): - start_game() - -t.onkey(start_game,'space') -t.onkey(restart_game,'Up') -t.onkey(move_up,'Up') -t.onkey(move_right,'Right') -t.onkey(move_down,'Down') -t.onkey(move_left,'Left') + start_game() + + +t.onkey(start_game, 'space') +t.onkey(restart_game, 'Up') +t.onkey(move_up, 'Up') +t.onkey(move_right, 'Right') +t.onkey(move_down, 'Down') +t.onkey(move_left, 'Left') t.listen() t.mainloop() From 383ef702bf103c18079ab71e222f286a9b763be5 Mon Sep 17 00:00:00 2001 From: jsoud <146140752+jsoud@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:46:41 -0800 Subject: [PATCH 2/2] Add files via upload Keeps track of the player's high score. --- Caterpillar_Game/highscore_tracker.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Caterpillar_Game/highscore_tracker.txt diff --git a/Caterpillar_Game/highscore_tracker.txt b/Caterpillar_Game/highscore_tracker.txt new file mode 100644 index 00000000..c2270834 --- /dev/null +++ b/Caterpillar_Game/highscore_tracker.txt @@ -0,0 +1 @@ +0 \ No newline at end of file