STAY INFORMED
following content serves as a personal note and may lack complete accuracy or
certainty.
Minimal-Mistakes instruction
Useful vscode Shortcut Keys
Unix Commands
npm Commands
Vim Commands
Git Note
Useful Figma Shortcut Keys
Snake Game Project_5
difficulty level
It is a bit boring if there is no any difficulty changed, so I decided to make the snake faster depending on the score.
I will make a variable for game speed and modify the clock.tick() function.
game_speed = 10
.
.
.
clock.tick(game_speed)
and write a short code like
if food.score % 5 == 0:
game_speed += 5
If I write this code in the main function, it will be called per a millisecond until score is divided by 5, so I need one variable.
is_increase = False
if abs(snake.body[0][0] - food.x) < scale and abs(snake.body[0][1] - food.y) < scale:
food.new_location()
snake.grow()
food.score += 1
if food.score % 5 == 0:
is_increase = True
if is_increase == True and food.score % 5 == 0:
game_speed += 5
is_increase = False
best score
I will use fstream method to get the best score. file is always string, so you need to be careful when you compare, get, or assign it.
Add code to record best score in game_over().
if int(b_score) < food.score:
with open('best-score.txt', 'w') as file:
file.write(str(food.score))
and get best score from the file. There is no file very first time obviously, so I write code for writing file in except.
try:
with open('best-score.txt', 'r') as file:
best_score = int(file.read())
except:
with open('best-score.txt', 'w') as file:
file.write("0")
and display the best score
def show_best_score(b_score):
font = pygame.font.SysFont("Copperplate Gothic Bold", 30)
text = font.render("Best Score: " + str(b_score), True, (255, 255, 255))
display.blit(text, (10, 30))