在我的游戏中,我创建了一个评分系统,但我很难找到一种方法来存储在gameloop
再次运行时不会重置的高分。这是代码:
import pygame
import time
import random
import timeit
pygame.init()
display_width = 1600
display_height = 850
speed = [1, -1]
black = (0,0,0)
white = (255,255,255)
red = (255, 0, 0)
grey = (177,177,177)
blue = (0,0,255)
green = (0,255,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Kill hitler before he gets trump')
clock = pygame.time.Clock()
crosshairImg = pygame.image.load('crosshair.PNG')
hitlerImg = pygame.image.load('hitler.png')
donaldImg = pygame.image.load('donald.png')
def crosshair(x, y):
gameDisplay.blit(crosshairImg, (x,y))
def enemy(x, y):
gameDisplay.blit(hitlerImg, (x,y))
def friendly(x, y):
gameDisplay.blit(donaldImg, (x,y))
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',45)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def text_objects(text, font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()
def status(total, enemy_kills):
if total == 0:
status = 0
else:
status = round((enemy_kills/total)*100, 2)
font = pygame.font.SysFont(None, 25)
text = font.render('Status:'+str(status)+'%', True, blue)
gameDisplay.blit(text,(0,30))
def score(count):
font = pygame.font.SysFont(None,25)
text = font.render('Confirmed Enemy Kills:'+str(count), True, blue)
gameDisplay.blit(text,(0,0))
def timer(count):
count = round(count, 1)
font = pygame.font.SysFont(None,25)
text = font.render('time left:'+str(count), True, blue)
gameDisplay.blit(text,(0,45))
def friend_kill(count):
font = pygame.font.SysFont(None,25)
text = font.render('Confirmed Friendly Kills:'+str(count), True, blue)
gameDisplay.blit(text,(0,15))
我认为以下部分代码需要编辑。我向你展示了以前的内容,希望能让下一部分更容易阅读。
def gameloop():
thing_startx = (display_width/2)
thing_starty = (display_height/2)
thing_xspeed = 0
thing_yspeed = 0
enemy_startx = random.randrange(0, (display_width-20))
enemy_starty = random.randrange(0, (display_height-20))
enemy_xspeed = 0
enemy_yspeed = 0
friend_startx = random.randrange(0, (display_width-40))
friend_starty = random.randrange(0, (display_height-40))
friend_width = 40
friend_height = 40
friend_xspeed = 0
friend_yspeed = 0
ammo = 5
kills = 0
total_kills = 0
friendly_kills = 0
gameExit = False
start = time.time()
elapsed = 0
while not gameExit and elapsed < 30:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEMOTION:
thing_startx = event.pos[0]-37
thing_starty = event.pos[1]-37
thing_startx += thing_xspeed
thing_starty += thing_yspeed
gameDisplay.fill (grey)
score(kills)
status(total_kills, kills)
friend_kill(friendly_kills)
timer((30-elapsed))
enemy(enemy_startx, enemy_starty)
friendly(friend_startx, friend_starty)
crosshair(thing_startx, thing_starty)
pygame.display.update()
pygame.mouse.set_visible(0)
if event.type == pygame.MOUSEBUTTONDOWN:
if (event.pos[0] > enemy_startx and event.pos[0] < enemy_startx+45) and event.pos[1] > enemy_starty and event.pos[1] < enemy_starty+60:
enemy_startx = random.randrange(0, (display_width-45))
enemy_starty = random.randrange(0, (display_height-60))
friend_startx = random.randrange(0,(display_width-40))
friend_starty = random.randrange(0,(display_height-40))
kills += 1
total_kills += 1
if event.type == pygame.MOUSEBUTTONDOWN:
if (event.pos[0] > friend_startx and event.pos[0] < friend_startx+60) and event.pos[1] > friend_starty and event.pos[1] < friend_starty+80:
friend_startx = random.randrange(0,(display_width-40))
friend_starty = random.randrange(0,(display_height-40))
enemy_startx = random.randrange(0, (display_width-45))
enemy_starty = random.randrange(0, (display_height-60))
friendly_kills += 1
total_kills += 1
message_display('you killed a friendly')
time.sleep(1)
if (kills>=5 and kills<10) or (kills>=15 and kills<20):
if enemy_startx+45<display_width and enemy_xspeed not in (1, -1):
enemy_xspeed += 1
if friend_startx+60<display_width and friend_xspeed not in (1,-1):
friend_xspeed += 1
if enemy_startx>0 and enemy_xspeed not in (-1, 0):
enemy_xspeed = 1
if friend_startx>0 and friend_xspeed not in (-1, 0):
friend_xspeed = 1
elif kills>=10 and kills<20:
if enemy_starty+60<display_height and enemy_yspeed not in (1, -1):
enemy_yspeed += 1
if friend_starty+80<display_width and friend_yspeed not in (1,-1):
friend_yspeed += 1
if enemy_starty>0 and enemy_yspeed not in (-1, 0):
enemy_yspeed = 1
if friend_starty>0 and friend_yspeed not in (-1, 0):
friend_yspeed = 1
elif kills>=20:
if enemy_startx+45<display_height and enemy_xspeed not in (2, -2):
enemy_xspeed = 2
if friend_startx+60<display_height and friend_xspeed not in (2, -2):
friend_xspeed = 2
if enemy_starty>0 and enemy_yspeed not in (2, -2):
enemy_yspeed = -2
if friend_starty>0 and friend_yspeed not in (2, -2):
friend_yspeed = -2
else:
enemy_xspeed = 0
enemy_yspeed = 0
friend_xspeed = 0
friend_yspeed = 0
if enemy_startx+45>display_width or enemy_startx<0:
enemy_xspeed *= -1
if enemy_starty+60>display_height or enemy_starty<0:
enemy_yspeed *= -1
if friend_startx+40>display_width or friend_startx<0:
friend_xspeed *= -1
if friend_starty+40>display_height or friend_starty<0:
friend_yspeed *= -1
enemy_starty += enemy_yspeed
enemy_startx += enemy_xspeed
friend_startx += friend_xspeed
friend_starty += friend_yspeed
pygame.display.update()
clock.tick(100)
elapsed = time.time() - start
msg = 'you got',kills,'enemy kills amd',friendly_kills,'enemy kills'
message_display('you got '+str(kills)+' enemy kills and '+str(friendly_kills)+' friendly kills')
time.sleep(2)
gameloop()
gameloop()
答案 0 :(得分:1)
修改:之前的回答无法回答问题。
在文件顶部导入pickle模块:
import pickle
在game_loop()函数的开头写下以下代码:
try:
with open("var.pickle", "rb") as file:
high = pickle.load(file)
except (OSError, IOError) as e:
high = 0
with open("var.pickle", "wb") as file:
pickle.dump( high, file)
最后在行
之前message_display('you got '+str(kills)+' enemy kills and '+str(friendly_kills)+' friendly kills')
time.sleep(2)
添加以下内容:
if high < kills:
with open("var.pickle", "wb") as file:
pickle.dump(high, open("var.pickle", "wb"))
message_display("New High-score!")
如果存在高分,或者得分为0,则会得到高分。一旦你完成游戏循环,如果分数高于高分,你将高分改为新分数。值。
答案 1 :(得分:0)
将高分存储在名为highscore.txt的单独.txt文件中。 .txt文件是可以存储字符串但也可以用作不被删除的数据的文件。确保您的.txt文件与游戏文件位于同一目录中。要启动它,请写下&#39; 0&#39; 0在没有撇号的第一行。不需要下载或额外导入。创建.txt文件并在游戏循环中执行此操作:
File = open(highscore.txt, 'r') #Out of game Loop
HighScore = int(File.read())
if kills > HighScore: #In game loop
File.truncate()
File.close()
File = open(highscore.txt, 'w')
File.write(kills)
File.close()
File = open(highscore.txt, 'r')