J.Fitz J.Fitz -4 years ago 136
Python Question

How would I add a displayable highscore variable?

In my game I have created a scoring system, but I am struggling to find a way to store a high score that doesn't reset when the

gameloop
is run again. Here is the code:

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))


I believe that the following part of the code needs editing. I showed you the previous to hopefully make the next part easier to read.

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()

Answer Source

Store the high score in a separate .txt file called highscore.txt. .txt files are files that can store strings but also be used as data that doesn't get deleted. Make sure your .txt file is in the same directory as your game file. To start it up, write '0' on the first line without apostrophes. Does not require downloads or extra imports. Make a .txt file and do this in the game loop:

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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download