1

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)
  • What exactly is the issue you are facing? Is it that scores above 10 don't get written? – Octo Dec 26 '16 at 19:30
  • 1
    See [mcve]. You should trim down the code so that it contains just the parts relevant to demonstrating the behavior you're asking about. Not only does that not waste our time, but you might even be able to figure it out yourself without the irrelevant details in the way. Incidentally, you really should use a `with` construct when opening files. –  Dec 26 '16 at 19:30
  • yes the score that is higher than 10 doesnt not get written –  Dec 26 '16 at 19:31

2 Answers2

0

The following code should work completely fin, although I am not quite sure what the issue is for you.

with open("score.txt", "w") as f:
    f.write(str(score))

That is just from the top of my head.
Edit: there is also lots of unnecessary code. Read https://stackoverflow.com/help/mcve, they key word being minimal.

Community
  • 1
  • 1
Octo
  • 695
  • 6
  • 20
  • The code is still not working let me explain again.. If the score is lower than the string "10" lets say the score is 6 and then if the user/player beats the score of 6 and sets up a new highscore of a string higher than 10 than python simply just ignores it and doesnt write it. it still uses the previous lower than 10 score. –  Dec 26 '16 at 19:40
0

New Anwser: Sorry I was inactive for a bit but anyway... The problem lies on this line:

if str(score) > str(highscore):

Or, in other words, you are comparing strings. instead of str, do int and you will be fine. The reason why you cant do greater than in terms of strings in this situation is complicated. but if you want an explanation see this: String Comparison Technique Used by Python

Community
  • 1
  • 1
Octo
  • 695
  • 6
  • 20
  • Thank you for your time this saved me a lot of hours of pointless staring at lines of code –  Dec 26 '16 at 19:53
  • @StrozeR if my anwser above or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this – Octo Dec 26 '16 at 19:57