我的程序运行正常但我无法更新分数。这是我的项目;我是最后一部分,不能自己解决。我粘贴了整个代码以供参考。
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。
答案 0 :(得分:0)
有两种方法可以访问想到的“betmoney”变量。首先,只需将其传递给您计划将其作为参数访问的函数。这通常是首选方式。其次,只需将“betmoney”声明为全局变量,就可以从任何地方访问它。