Reputation: 128
I am making a High_Name_Score (players name & high score) list to display during the Game Over Screen (show_go_screen). I am able to save and load list, sort it by score and blit it to screen but I have two issues:
1) it displays to screen all on one line and I would like to have each player / score on new line
2) it shows the parenthesis and quotations, e.g. HIGH SCORE ['3580 AABBCC', '3508 CCBBAA'] and I would like it to be cleaner
I've search online with no luck yet of telling me how to do either
import pygame
import random
import sys
import json
from pygame.locals import *
def (main):
try:
with open('High_Name_Score.txt', 'r') as file:
High_Name_Score = json.load(file)
High_Name_Score.sort()
except FileNotFoundError:
High_Name_Score = []
High_Score = 0
PURPLE = (139, 0, 139)
arial = pygame.font.match_font('arial')
pygame.init()
CLOCK = pygame.time.Clock()
DS = pygame.display.set_mode((W, H))
pygame.display.set_caption("Racing Fury")
def events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def draw_text(surf, text, size, x, y, fonts, color):
font = pygame.font.Font(fonts, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
def high_name():
High_Name = ""
Enter_Name = True
while Enter_Name:
DS.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYUP:
if event.key == K_a:
High_Name += 'A'
if event.key == K_b:
High_Name += 'B'
if event.key == K_c:
High_Name += 'C'
if event.key == K_RETURN:
High_Name_Score.append(str(High_Score) + " " + High_Name)
with open('High_Name_Score.txt', 'w') as file:
json.dump(High_Name_Score, file)
main()
draw_text(DS, "HIGH SCORE: " + str(High_Name), 18, W / 2, 700, arial, PURPLE)
pygame.display.flip()
def show_go_screen():
DS.fill(WHITE)
Bckgrnd = [Summer, Winter, Desert, Space]
Bckgrnd_Img = random.choice(Bckgrnd)
if Bckgrnd_Img == Space:
color = WHITE
else:
color = BLACK
DS.blit(Bckgrnd_Img, (0, 0))
for i in range(3):
draw_text(DS, "RACING FURY!", 64, W / 2 + (i + 1), H / 4 - 60 + (i + 1), bodoni, color)
draw_text(DS, "RACING FURY!", 64, W / 2, (H / 4 - 60), bodoni, PURPLE)
draw_text(DS, "Summer Track (S)", 18, W / 2, (HH - 70), arial, color)
draw_text(DS, "Winter Track (W)", 18, W / 2, (HH + 0), arial, color)
draw_text(DS, "Desert Track (D)", 18, W / 2, (HH + 70), arial, color)
for i in High_Name_Score:
draw_text(DS, "HIGH SCORE: " + str(High_Name_Score), 18, W / 2, (H / 4 - 160), arial, PURPLE)
pygame.display.flip()
waiting = True
while waiting:
global TRACK
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
keystate = pygame.key.get_pressed()
if keystate[pygame.K_s]:
TRACK = 1
waiting = False
This is I think all the portion of the code that is required. Needless to say when the finish line is crossed the High_Score is calculated and then the high_name() is called to enter player name. All works well and player name & score is saved, and put in list, it is just when I blit it to screen I get the parenthesis & quotations which I don't want to see and it all shows on one line
Any suggestions would be much appreciated
Upvotes: 0
Views: 176
Reputation: 128
Well it's a bit long but the following code will save your top 3 scores in order of highest to lowest and always amend if anyone gets new high score in the top 3 (dropping # 3 off completely)
def high_name():
High_Name = ""
Enter_Name = True
try:
with open('HS1.txt', 'r') as f1:
HS1 = json.load(f1)
with open('HS2.txt', 'r') as f2:
HS2 = json.load(f2)
with open('HS3.txt', 'r') as f3:
HS3 = json.load(f3)
with open('HN1.txt', 'r') as f4:
HN1 = json.load(f4)
with open('HN2.txt', 'r') as f5:
HN2 = json.load(f5)
with open('HN3.txt', 'r') as f6:
HN3 = json.load(f6)
except FileNotFoundError:
HS1 = 0
HS2 = 0
HS3 = 0
HN1 = ""
HN2 = ""
HN3 = ""
while Enter_Name:
DS.fill(WHITE)
if Winner == 1:
draw_text(DS, "Congrats Player1, Enter Your Name", 18, W / 2, 650, arial, PURPLE)
elif Winner == 2:
draw_text(DS, "Congrats Player2, Enter Your Name", 18, W / 2, 650, arial, PURPLE)
if len(High_Name) >= 7:
High_Name = High_Name[:-1]
draw_text(DS, "Only 6 Characters", 18, W / 2, 750, arial, PURPLE)
pygame.display.flip()
pygame.time.delay(2000)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key >= K_a and event.key <= K_z:
High_Name += chr(event.key)
elif event.key >= K_0 and event.key <= K_9:
High_Name += chr(event.key)
elif event.key == K_BACKSPACE:
High_Name = High_Name[:-1]
elif event.key == K_RETURN:
High_Name = High_Name.capitalize()
if High_Score >= HS1:
HS3 = HS2
HN3 = HN2
HS2 = HS1
HN2 = HN1
HS1 = High_Score
HN1 = High_Name
elif High_Score >= HS2:
HS3 = HS2
HN3 = HN2
HS2 = High_Score
HN2 = High_Name
elif High_Score >= HS3:
HS3 = High_Score
HN3 = High_Name
with open('HS1.txt', 'w') as f1:
json.dump(HS1, f1)
with open('HS2.txt', 'w') as f2:
json.dump(HS2, f2)
with open('HS3.txt', 'w') as f3:
json.dump(HS3, f3)
with open('HN1.txt', 'w') as f4:
json.dump(HN1, f4)
with open('HN2.txt', 'w') as f5:
json.dump(HN2, f5)
with open('HN3.txt', 'w') as f6:
json.dump(HN3, f6)
main()
draw_text(DS, "HIGH SCORE: " + str(High_Name), 18, W / 2, 700, arial, PURPLE)
pygame.display.flip()
I then have the scores displaying on the Game Over screen:
def show_go_screen():
DS.fill(WHITE)
Bckgrnd = [Summer, Winter, Desert, Space]
Bckgrnd_Img = random.choice(Bckgrnd)
if Bckgrnd_Img == Space:
color = WHITE
else:
color = BLACK
DS.blit(Bckgrnd_Img, (0, 0))
for i in range(3):
draw_text(DS, "RACING FURY!", 64, W / 2 + (i + 1), H / 4 - 60 + (i + 1), bodoni, color)
draw_text(DS, "RACING FURY!", 64, W / 2, (H / 4 - 60), bodoni, PURPLE)
draw_text(DS, "Summer Track (S)", 18, W / 2, (HH - 70), arial, color)
draw_text(DS, "Winter Track (W)", 18, W / 2, (HH + 0), arial, color)
draw_text(DS, "Desert Track (D)", 18, W / 2, (HH + 70), arial, color)
for s in range(3):
draw_text(DS, "HIGH SCORES", 24, W / 2 + (s + 1), H / 4 - 180 + (s + 1), arial, color)
draw_text(DS, "HIGH SCORES", 24, W / 2, (H / 4 - 180), arial, PURPLE)
draw_text(DS, str(HN1) + ": " + str(HS1), 18, W / 2, (H / 4 - 140), arial, PURPLE)
draw_text(DS, str(HN2) + ": " + str(HS2), 18, W / 2, (H / 4 - 120), arial, PURPLE)
draw_text(DS, str(HN3) + ": " + str(HS3), 18, W / 2, (H / 4 - 100), arial, PURPLE)
pygame.display.flip()
waiting = True
while waiting:
global TRACK
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
keystate = pygame.key.get_pressed()
if keystate[pygame.K_s]:
TRACK = 1
waiting = False
And of course you would have to get some arrays like High_Score from other parts of your game.
If anyone can do this in much less code, please let me know!!!
Upvotes: 0