2017-03-06 20 views
0

我的程序工作正常,但我無法更新分數。這是爲了我的項目;我在最後一部分,不能自己弄清楚。我粘貼了整個代碼以供參考。如何更新我的分數?

import pygame 
import random 

# Define some colors 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = (0, 255, 0) 
RED = (255, 0, 0) 
YELLOW = (255, 255, 0) 
betmoney = 100 
pygame.init() 
pygame.mixer.init() 
clock = pygame.time.Clock() 

def menu(): 
    done = True 
    while done: 

     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_RETURN: 
        tracks() 
       elif event.key == pygame.K_F1: 
        about() 
      elif event.type == pygame.QUIT: 
       pygame.quit() 


     game_S.blit(bg, [0,0]) 
     for i in range (30): 
      x = random.randrange(0, 700) 
      y = random.randrange(0, 700) 
      pygame.draw.circle(game_S, GREEN, [x,y], 5) 
      pygame.display.update() 

     game_S.blit(text, [170, 450]) 
     pygame.display.flip() 
     game_S.blit(betm, [0,0]) 
     pygame.display.flip() 
     clock.tick(10) 

def win(): 
    while True: 
     game_S.blit(winner, [0,0]) 
     game_S.blit(text, [170, 450]) 
     game_S.blit(betm, [0, 0]) 
     pygame.display.flip() 
     pygame.display.update() 


     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_RETURN: 
        menu() 

      elif event.type == pygame.QUIT: 
       pygame.quit() 

    clock.tick(10) 

def loser(): 
    while True: 
     game_S.blit(lose, [0,0]) 
     game_S.blit(text, [170, 450]) 
     pygame.display.flip() 
     pygame.display.update() 

     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_RETURN: 
        menu() 

      elif event.type == pygame.QUIT: 
       pygame.quit() 

    clock.tick(10) 

def tracks(): 
    pygame.time.wait(1000) 
    game_S.blit(track3, [0,0]) 
    pygame.display.update() 
    pygame.time.wait(1000) 
    game_S.blit(track2, [0,0]) 
    pygame.display.update() 
    pygame.time.wait(1000) 
    game_S.blit(track1, [0,0]) 
    pygame.display.update() 
    pygame.time.wait(1000) 
    game_S.blit(trackgo, [0,0]) 
    pygame.display.update() 
    pygame.time.wait(1000) 
    game_S.fill(WHITE) 
    game_S.blit(track, [0,0]) 
    pygame.display.update() 
    game_S.blit(bet, [0, 230]) 
    game_S.blit(en1, [0, 20]) 
    game_S.blit(en2, [0, 80]) 
    game_S.blit(en3, [0, 160]) 
    game_S.blit(en4, [0, 295]) 
    game_S.blit(en5, [0, 360]) 
    game_S.blit(en6, [0, 440]) 
    pygame.display.update 
    pygame.display.flip() 
    r1 = random.randrange(1,10) 
    r2 = random.randrange(1,10) 
    r3 = random.randrange(1,10) 
    r4 = random.randrange(1,10) 
    r5 = random.randrange(1,10) 
    r6 = random.randrange(1,10) 
    racemode(r1,r2,r3,r4,r5,r6,0) 

def racemode(a,b,c,d,e,f,wow): 
    move = True 

    while move: 
     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_SPACE and wow <= 600: 
        game_S.blit(bet, [wow, 230]) 
        wow +=5 
        pygame.display.update() 
        clock.tick(10) 
        continue 
      elif event.type == pygame.QUIT: 
       pygame.quit() 

     if a<= 600: 
      game_S.blit(en1, [a,20]) 
      a += random.randrange(10,15) 
      pygame.display.update() 
      clock.tick(30) 

     if b<= 600: 
      game_S.blit(en2, [b,80]) 
      b +=random.randrange(10,15) 
      pygame.display.update() 
      clock.tick(30) 

     if c<= 600: 
      game_S.blit(en3, [c,160]) 
      c += random.randrange(10,15) 
      pygame.display.update() 
      clock.tick(30) 

     if d<= 600: 
      game_S.blit(en4, [d,295]) 
      d+= random.randrange(10,15) 
      pygame.display.update() 
      clock.tick(30) 

     if e<= 600: 
      game_S.blit(en5, [e,360]) 
      e+= random.randrange(10,15) 
      pygame.display.update() 
      clock.tick(30) 

     if f<=600: 
      game_S.blit(en6, [f,440]) 
      f+=random.randrange(10,15) 
      pygame.display.update() 
      clock.tick(30) 

     if wow >= 600 and a <600 and b <600 and c<600 and d<600 and e<600 and f<600: 
      win() 
     elif wow < 600 and a>= 600 or b>=600 or c>= 600 or d >= 600 or e >= 600 or f >=600: 
      loser() 



def about(): 
    tan = True 
    game_S.fill(WHITE) 
    game_S.blit(ab, [0,0]) 
    pygame.display.update() 
    texts = pygame.font.SysFont('Arial', 35, True, False) 
    fonts = pygame.font.SysFont('Calibri', 30, True, False) 
    tput1 = texts.render("ABOUT THE BETTING SYSTEM", True, BLACK) 
    tput2 = fonts.render("-> You are given $100 as starting money", True, RED) 
    tput3 = fonts.render("-> You can get +100 if you win" , True, RED) 
    tput3b = fonts.render("-> You will lose 100 if you lose", True, RED) 
    tput4 = fonts.render("-> You beat the game when you reach 1000", True, RED) 
    tput5 = fonts.render("-> GAME OVER if you got $0 money", True, RED) 
    back = font.render(" PRESS F1 to go Back", True, BLACK) 
    game_S.blit(tput1, [120,80]) 
    pygame.display.update() 
    game_S.blit(tput2, [100,150]) 
    pygame.display.update() 
    game_S.blit(tput3, [100,200]) 
    pygame.display.update() 
    game_S.blit(tput3b, [100,250]) 
    pygame.display.update() 
    game_S.blit(tput4, [100,300]) 
    pygame.display.update() 
    game_S.blit(tput5, [100,350]) 
    pygame.display.update() 

    while tan: 
     game_S.blit(back, [170,450]) 
     pygame.display.update() 

     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_F1: 
        menu() 

      elif event.type == pygame.QUIT: 
       pygame.quit() 

    clock.tick(10) 

# Set the width and height of the screen [width, height] 
size = (700, 500) 
game_S = pygame.display.set_mode(size) 
pygame.display.set_caption("BET ON YOUR TURTLE") 
font = pygame.font.SysFont('Calibri', 34, True, False) 
text = font.render("PRESS ENTER TO CONTINUE", True, RED) 
betm = font.render(str(betmoney), 1, RED) 







#---IMAGES---- 
bg = pygame.image.load("kim.png") 
ab = pygame.image.load("waw.png") 
bet = pygame.image.load("witwew.png").convert_alpha() 
en1 = pygame.image.load("enemy1.png") 
en2 = pygame.image.load("enemy2.png") 
en3 = pygame.image.load("enemy3.png") 
en4 = pygame.image.load("enemy4.png") 
en5 = pygame.image.load("enemy5.png") 
en6 = pygame.image.load("enemy6.png") 
track = pygame.image.load("TRACK.png") 
track3 = pygame.image.load("TRACK3.png") 
track2 = pygame.image.load("TRACK2.png") 
track1 = pygame.image.load("TRACK1.png") 
trackgo = pygame.image.load("TRACKGO.png") 
winner = pygame.image.load("winner.png") 
lose = pygame.image.load("lose.png") 
#--IMAGES (end)-- 

pygame.mixer.music.load("icecream_8bit.mp3") 
pygame.mixer.music.play(-1) 

menu() 

你可以看到,我把betmoney爲局部變量值100,不知道如何添加100或100扣除

+7

請參閱[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – khelwood

+0

雖然我不使用Python,但是它的絕對大小表明某些調試會是有必要創建一個小問題的例子。你會嘗試縮小它嗎? – halfer

回答

0

有兩種方式去訪問「betmoney」變量想到這些。首先,簡單地將它傳遞給你打算作爲參數訪問它的函數。這通常是首選的方式。其次,只需聲明「betmoney」是一個全局變量,它可以從任何地方訪問。