我知道我刚才刚刚问了另一个问题,但是我有点卡住了。我的游戏是关于您在三个空间之间移动以躲避速度不同的正方形。 “速度”文本标签(我不知道该怎么称呼)更新正常(并且当块到达屏幕底部时更新),但是高分文本标签(高分从泡菜中加载) 。如果您想知道泡菜文件中是否已经加载了高分。它不是空的。
import pygame
import random
import math
import sys
import cPickle as pickle
#import os.path
pygame.init()
pygame.font.init()
score = 0
myfont = pygame.font.SysFont("Comic Sans MS", 30)
screenWidth = 700
screenHeight = 800
red = (255,0,0)
blue = (0,0,255)
yellow = (255,255,0)
cyan = (0,255,255)
purple = (255, 0, 255)
x = screenWidth / 2
y = (screenHeight / 4) * 3
width = 50
height = 50
highScore = 0
corn1 = pygame.image.load("new1.png")
corn2 = pygame.image.load("new2.png")
corn3 = pygame.image.load("new3.png")
corn4 = pygame.image.load("new4.png")
mac = [corn1, corn2, corn3, corn4]
class player():
def __init__(self, x, y):
self.x = x
self.y = y
self.walkCount = 0
def draw(self, win):
if self.walkCount + 1 >= 60:
self.walkCount = 0
win.blit(mac[self.walkCount//15], (self.x,self.y))
self.walkCount += 1
win = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Corn Dash")
bg = pygame.image.load("background.png").convert()
gameover = pygame.image.load("gameover.png").convert()
#corn = pygame.image.load("player.png")
bgx = (screenWidth / 6) * 2
bgy = 0
#mac = [pygame.image.load("1.png"), pygame.image.load("2.png"), pygame.image.load("3.png"), pygame.image.load("4.png")]
clock = pygame.time.Clock()
class enemy():
def __init__(self,c,y,width,height, color, vel):
self.c = c
self.y = y
self.width = width
self.height = height
self.vel = vel
self.color = color
def draw(self, win):
global score
if gameOver == False:
if self.c == 1:
self.x = 250
#250
elif self.c == 2:
self.x = 350
#350
else:
self.x = 450
#450
self.y += self.vel
if self.y >= 800:
score += 1
self.y = random.randint(-500,0)
self.c = random.randint(1,3)
pygame.draw.rect(win, (self.color), (self.x,self.y,self.width,self.height))
else:
self.y = 0
evil = enemy(random.randint(1,3),0,50,50, blue, 5)
evil2 = enemy(random.randint(1,3),-400,50,50, blue, 5)
evil3 = enemy(random.randint(1,3),random.randint(-500,-100),50,50, blue, 5)
evil4 = enemy(random.randint(1,3),-1000,50,50, yellow, 15)
evil5 = enemy(random.randint(1,3),100,50,50, purple, 2)
running = True
gameOver = False
corn = player(screenWidth/2,(screenHeight / 4) * 3)
def saveScore():
highScore = score
pickle_out = open("save.pickle","wb")
pickle.dump(highScore, pickle_out)
pickle_out.close()
print("Score saved:" + str(highScore))
def loadScore():
pickle_in = open("save.pickle","rb")
highScore = pickle.load(pickle_in)
pickle_in.close()
print("Score loaded:" + str(highScore))
loadScore()
while running:
# -----The game over scene.-----
while gameOver:
scoreLabel = myfont.render(("Score:"+str(score)), False, (0,0,0))
highScoreLabel = myfont.render(("High Score:"+str(highScore)), False, (0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
# pygame.quit only uninitializes the pygame modules and
# doesn't quit the program.
pygame.quit()
# This will quit the whole program. You need to import sys.
sys.exit()
elif event.type == pygame.KEYUP: # event.type not pygame.event.type
if event.key == pygame.K_SPACE:
# Change it to False to break out of the loop.
gameOver = False
# Reset the game. You could reset the position of the
# `evil` object or instantiate a new one.
x = 350
evil.c = random.randint(1,3)
evil.y = 0
evil2.c = random.randint(1,3)
evil2.y = random.randint(-300,0)
evil3.c = random.randint(1,3)
evil3.y = random.randint(-600,20)
evil4.c = random.randint(1,3)
evil4.y = random.randint(-3000,-2500)
evil5.c = random.randint(1,3)
evil5.y = random.randint(0,300)
score = 0
elif event.key == pygame.K_s:
saveScore()
print("Score saved:"+str(highScore))
highScoreLabel = myfont.render(("High Score:"+str(highScore)), False, (0,0,0))
win.blit(gameover, (0,0))
win.blit(scoreLabel,(0,0))
win.blit(highScoreLabel, (0,30))
pygame.display.update()
clock.tick(60) # You need to call tick in this loop as well.
# ------------------------------
# -----The main scene.-----
highScoreLabel1 = myfont.render(("High Score:"+str(highScore)), False, (255,255,255))
scoreLabel1 = myfont.render(("Score:"+str(score)), False, (255,255,255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT and corn.x < 450:
corn.x += 100
elif event.key == pygame.K_LEFT and corn.x > 250:
corn.x -= 100
win.fill((0,0,0))
win.blit(bg, (bgx, bgy))
evil.draw(win)
evil2.draw(win)
evil3.draw(win)
evil4.draw(win)
evil5.draw(win)
win.blit(scoreLabel1,(0,0))
win.blit(highScoreLabel1, (0,30))
dist = math.hypot(evil.x - corn.x, evil.y - corn.y)
dist2 = math.hypot(evil2.x - corn.x, evil2.y - corn.y)
dist3 = math.hypot(evil3.x - corn.x, evil3.y - corn.y)
dist4 = math.hypot(evil4.x - corn.x, evil4.y - corn.y)
dist5 = math.hypot(evil5.x - corn.x, evil5.y - corn.y)
if dist <= 50 or dist2 <= 50 or dist3 <= 50 or dist4 <= 50 or dist5 <= 50:
print("Game Over!")
gameOver = True
corn.draw(win)
pygame.display.update()
clock.tick(60)
答案 0 :(得分:0)
这是一个可变范围的问题。在将score
和highScore
都定义为全局变量(在任何函数之外)的同时,您随后在highScore
和saveScore()
中为loadScore()
分配了一个值。执行分配时,Python会将变量解释为函数的本地变量。 saveScore()
的最终结果仍然有效。实际上,您可以为score
分配一个值,并查看它是否正确保存。但是,当您使用loadScore()
时,将从文件中读取值,然后将其分配给highScore
,但仅在函数的本地范围中:一旦函数完成,价值丢失了。
两种解决方法。
最简单的方法是在global highScore
函数内部简单地添加一个loadScore()
语句,这样就不会产生歧义。可以,但是不建议这样做。
第二种也是最合适的方法是重新构建变量范围,并进行如下更改:
saveScore()
接受参数,这是您要保存的值loadScore()
在return
语句中返回加载的值score
和highScore
的跟踪,并在适当时保存/加载另外,为获得更好,更完整的解释,请查看Global, Local and nonlocal Variables上的本教程。希望这会有所帮助。