我有一個問題,我的遊戲,可以說在遊戲中的分數是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)
究竟是你面臨的問題?難道10分以上的分數沒有寫出來嗎? – Octo
請參閱[mcve]。你應該減少代碼,使其只包含與展示你所問的行爲有關的部分。這不僅不會浪費我們的時間,而且您甚至可以自己弄清楚自己的方式沒有不相關的細節。順便說一下,打開文件時,你確實應該使用'with'構造。 – Hurkyl
是高於10的得分不會被寫入 – StrozeR