2016-12-26 34 views
1

我有一個問題,我的遊戲,可以說在遊戲中的分數是3,然後我嘗試寫一個高於10的分數。它不會寫我一直在嘗試的文件修復,但它沒有工作..請幫助。以下只是一段代碼Pygame評分 - 開放和寫作文件

# 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) 
+0

究竟是你面臨的問題?難道10分以上的分數沒有寫出來嗎? – Octo

+1

請參閱[mcve]。你應該減少代碼,使其只包含與展示你所問​​的行爲有關的部分。這不僅不會浪費我們的時間,而且您甚至可以自己弄清楚自己的方式沒有不相關的細節。順便說一下,打開文件時,你確實應該使用'with'構造。 – Hurkyl

+0

是高於10的得分不會被寫入 – StrozeR

回答

1

新Anwser: 對不起,我是不活動一下,但無論如何... 問題就在這條線:

if str(score) > str(highscore): 

或者,換句話說,你是比較字符串。而不是str,做int,你會沒事的。在這種情況下,你無法做到比字符串更大的原因很複雜。但如果你想要一個解釋看到這個:String Comparison Technique Used by Python

+0

謝謝你的時間,這節省了我大量的時間毫無意義地盯着代碼行 – StrozeR

+0

@StrozeR如果我上面的anwser或任何答案已解決您的問題,請考慮通過點擊複選標記來接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做 – Octo

0

下面的代碼應該完全fin,儘管我不太清楚這個問題對你來說是什麼。

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

這只是從我的頭頂。
編輯:也有很多不必要的代碼。閱讀https://stackoverflow.com/help/mcve,他們的關鍵詞是最小

+0

該代碼仍然無法讓我再次解釋.. 如果得分低於字符串「10」可以說分數是6然後如果用戶/ player比6的分數更勝一籌,並且創建比python高10的字符串的新高分只是簡單地忽略它而不寫它。它仍然使用之前低於10的分數。 – StrozeR