I have a problem with my game, lets say the score in game is 3 and then i try to write a score that is higher than 10. it doesnt write the file i've been trying to fix but it doesnt work.. please help. the following is just a piece of the code
# Variables within loop
loop = True
over = False
# car
carx = int(display_w/2 - 20)
cary = 500
carwidth = 40
carheight = 70
cxchange = 0
# obstacle
obx = carx - carwidth
oby = -200
obwidth = random.randint(200, 450)
obheight = random.randint(100, 200)
obychange = 0
obstacle_speed = 7
# score appending
readhighscore = open("score.txt", "r")
highscore = readhighscore.read()
while loop:
if over == True:
message_to_screen("Game over!", black, -200, "large")
message_to_screen("Final Score: " + str(score), black, -130, "small")
fire_explosion(carx + int(carwidth / 2), cary)
if str(score) > str(highscore):
writehighscore = open("score.txt", "w")
writehighscore.write(str(score))
pygame.display.update()
while over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
cxchange += 4
elif event.key == pygame.K_LEFT:
cxchange -= 4
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
cxchange = 0
elif event.key == pygame.K_LEFT:
cxchange = 0
# Movement
carx += cxchange
oby += obychange
obychange = obstacle_speed
# If Statements
# end of map and if the car successfully dodges the obstacle
if carx >= display_w - carwidth:
carx = display_w - carwidth
elif carx <= 0:
carx = 0
if oby > display_h:
if score > 16:
obwidth = random.randint(300, 450)
else:
obwidth = random.randint(200, 450)
obx = carx - random.randint(0, 100)
oby = random.randint(-1000, -500)
score += 3
# obstacle collision with the car
if oby+obheight >= cary and oby <= cary+carheight:
if carx+carwidth >= obx and carx <= obx + obwidth:
over = True
obychange = 0
# score concept
print(highscore)
# Drawing
# background color
gameDisplay.fill(white)
# car
gameDisplay.blit(car1, [carx, cary])
# obstacle
drawobjects(obx, oby, obwidth, obheight, blue)
# score
drawScore(score)
pygame.display.update()
clock.tick(FPS)