Skip to content

Added High Score Tracker to Caterpillar Game #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 52 additions & 31 deletions Caterpillar_Game/Caterpillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()
Expand All @@ -80,34 +95,40 @@ 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)
if outside_window():
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()
1 change: 1 addition & 0 deletions Caterpillar_Game/highscore_tracker.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0