2014-12-07 64 views
0

我在一個簡單的無盡的隧道賽車類型的遊戲中寫一個高分的文件有一個小問題。python失敗的文件編寫

如果在我開始遊戲之前寫了高分,文件中沒有任何內容。但文件中的一個號碼後,它完全忽略了:

if score > hs: 
    hs = score 
    hsfile = open("hs.txt", "w") 
    hsfile.write(str(hs)) 
    hsfile.close() 

要打開的文件,並檢查,如果事情是寫我的代碼:

hf = open("hs.txt", "r") 

h = hf.readline() 

if len(h) > 0: 
    hs = h 
else: 
    hs = 0 

重述:

如果在hs.txt中沒有寫入任何內容,則將高分添加到txt文件中,然後寫入該文件。

如果寫入內容(在第一次播放和重新啓動應用程序之後),分數將保留爲文件中的內容。幫幫我?如果需要的話

全碼:

import pygame, sys, time, random 

pygame.init() 

disw = 640 
dish = 480 

white = (255,255,255) 
black = ( 0, 0, 0) 
brown = (193,125, 58) 
green = ( 0,220, 0) 

gameExit = False 

score = 0 

speed = -2 

spawned = False 

hf = open("hs.txt", "r") 

h = hf.readline() 

if len(h) > 0: 
    hs = h 
else: 
    hs = 0 

if gameExit == True: 
    h.close() 

rockx = 640 

rocky = random.randint(0, dish) 

rockw = random.randint(10,150) 

rockh = random.randint(10,150) 

ship = pygame.image.load("ship.png") 

bg = pygame.image.load("bg.png") 

gameOver = False 

def rock(thingx,thingy,thingw,thingh,color): 
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) 

def rockhit(): 
    global score, spawned, speed, rockx, rocky, rockw, rockh 
    rockx = 640 
    rocky = random.randint(0, dish) 
    rockw = random.randint(10,150) 
    rockh = random.randint(10,150) 
    score += 1 
    spawned = False 
    speed -= 1 

class Ship(): 
    def __init__(self, x, y, w, h): 
     self.x = x 
     self.y = y 
     self.w = w 
     self.h = h 

gameDisplay = pygame.display.set_mode((disw,dish)) 
pygame.display.set_caption("Tunnel") 
Clock = pygame.time.Clock() 

def main(): 
    global gameOver, score, spawned, speed, rockx, rocky, rockw, rockh, hs, gameExit 
    shipdimensions = Ship(200, 10, 60, 40) 
    xc = 0 
    yc = 0 
    def startGame(): 
     gameDisplay.blit(bg, (0, 0)) 
     gameDisplay.blit(ship, (shipdimensions.x, shipdimensions.y)) 

    while not gameExit: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_w: 
        yc = -6 
       if event.key == pygame.K_s: 
        yc = 6 
       if event.key == pygame.K_SPACE: 
        if gameOver == True: 
         startGame() 
         gameOver = False 
         speed = -2 

      elif event.type == pygame.KEYUP: 
       if event.key == pygame.K_w or event.key == pygame.K_s: 
        yc = 0 

     shipdimensions.y += yc 
     rockx += speed 

     gameDisplay.fill(black) 
     gameDisplay.blit(bg, (0, 0)) 
     gameDisplay.blit(ship, (shipdimensions.x, shipdimensions.y)) 

     font = pygame.font.SysFont(None, 25) 
     text = font.render("Score: " + str(score), True, green) 
     gameDisplay.blit(text, (1,1)) 

     font = pygame.font.SysFont(None, 25) 
     text = font.render("High Score: " + str(hs), True, green) 
     gameDisplay.blit(text, (1,20)) 

     if gameOver == True: 
      font = pygame.font.SysFont(None, 25) 
      text = font.render("Game Over, press 'Space' to restart.", True, green) 
      gameDisplay.blit(text, (100,200)) 

     if shipdimensions.y < rocky + rockh: 
      if shipdimensions.y + shipdimensions.h < rocky: 
       pass  
      elif shipdimensions.x > rockx and shipdimensions.x < rockx + rockw or shipdimensions.x + shipdimensions.w > rockx and shipdimensions.x + shipdimensions.w < rockx + rockw:    
       rockx = 640 
       rocky = random.randint(0, dish) 
       rockw = random.randint(10,150) 
       rockh = random.randint(10,150) 
       speed = 0 
       gameOver = True 
       score = 0 

     if gameOver == False and spawned == False: 
      rock(rockx,rocky,rockw,rockh,brown) 

     if shipdimensions.y <= 0: 
      shipdimensions.y = 0 

     if shipdimensions.y + shipdimensions.h >= dish: 
      shipdimensions.y = dish - shipdimensions.h 

     if rockx <= 0: 
      rockhit() 

     if score > hs: 
      hs = score 
      hsfile = open("hs.txt", "w") 
      hsfile.write(str(hs)) 
      hsfile.close() 


     pygame.display.update() 
     Clock.tick(60) 

if '__main__' == __name__: 
    main() 
+0

請注意,此代碼永遠不會執行:'如果gameExit == True:h.close()',因爲您在程序的早期將'gameExit'設置爲'False'。而要真正關閉你的文件,你需要'hf.close()'。是的,如果你打開一個帶有「r」的文件,你需要關閉它,然後用''w''重新打開它。 – 2014-12-07 08:28:09

+0

gameExit = False當我退出遊戲時,它在關閉之前運行。它現在適用於別人的修復。 – HKVariant 2014-12-07 11:47:15

回答

2

嘗試在讀取它時將hs轉換爲int(否則它將是一個字符串)?

if len(h) > 0: 
    hs = int(h) 
else: 
    hs = 0 

例如你會得到奇怪的比較字符串和整數..即:

>>> 12 > "10" 
False 

在python解釋器。 (Python應該可能會拋出一個異常,我想不出你想要比較一個int和一個字符串的時間)。

+0

謝謝,它的工作,有一個小錯誤我想我可以用它自己解決。 – HKVariant 2014-12-07 11:41:29

+0

是的,修正了它寫出每個數字的錯誤,謝謝你的回答!簡直不敢相信。 – HKVariant 2014-12-07 11:45:34

0

您可能打開同一文件進行寫入之前關閉HF。

+0

沒有工作!我認爲這與頂部的if和else聲明有關,但我不知道如何解決它。 – HKVariant 2014-12-07 05:48:35

+0

您是否試過在讀寫模式下打開文件?使用「R +」而不是「R」或「W」,所以你只需要處理打開文件一次 – 2014-12-07 05:56:01

+0

同樣的錯誤做到這一點,我很困惑這是爲什麼 – HKVariant 2014-12-07 05:58:51