-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighscore.py
148 lines (135 loc) · 4.7 KB
/
highscore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pygame_sdl2
pygame_sdl2.import_as_pygame()
import pygame
import sys
import game
from images import *
from settings import *
def message_display(screen, txt, color, pos, size):
myfont = pygame.font.SysFont("DejaVuSans", size)
label = myfont.render(txt, 1, color)
screen.blit(label, pos)
pygame.display.flip()
def read_from_file_and_find_highscore(file_name):
file = open(file_name, 'r')
lines=file.readlines()
file.close()
all_score = []
for line in lines:
sep = line.index(' - ')
name = line[:sep]
score = int(line[sep+3:])
all_score.append((score, name))
all_score.sort(reverse=True) # sort from largest to smallest
high_score = all_score[0][0]
high_name = all_score[0][1]
return high_name, high_score
def write_to_file(file_name, your_name, points):
score_file = open(file_name, 'a')
score_file.write(your_name+" - "+str(points))
score_file.write("\n")
score_file.close()
def show_top10(screen, file_name, ingame):
file = open(file_name, 'r')
lines=file.readlines()
all_score = []
for line in lines:
sep = line.index(' - ')
name = line[:sep]
score = int(line[sep+3:])
all_score.append((score, name))
file.close()
all_score.sort(reverse=True) # sort from largest to smallest
best = all_score[:10]
screen.blit(background, (0, 0))
screen.blit(framehs, (60, 40))
message_display(screen, "High Score", black, (120, 150), 90)
if ingame:
screen.blit(button, (20, 1100))
screen.blit(button, (380, 1100))
message_display(screen, "Play Again", white, (50, 1120), 50)
message_display(screen, "Main Menu", white, (405, 1120), 50)
else:
message_display(screen, "tap to continue", white, (100, 1120), 70)
for i, entry in enumerate(best):
message_display(screen, entry[1] + " " + str(entry[0]), black, (120, 55*i+280), 55)
while True:
for event in pygame.event.get():
if event.type == pygame.FINGERDOWN:
touch_pos = [event.x * WIDTH, event.y * HEIGHT]
if ingame:
if 1200 > touch_pos[1] > 1100:
if 340 > touch_pos[0] > 20:
screen.blit(buttonp, (20, 1100))
message_display(screen, "Play Again", white, (50, 1120), 50)
return
if 700 > touch_pos[0] > 380:
screen.blit(buttonp, (380, 1100))
message_display(screen, "Main Menu", white, (405, 1120), 50)
#pygame.quit()
game.g.show_start_screen()
else:
game.start_game()
game.g.clock.tick(FPS)
def enterbox(screen, txt, txt2, txt3):
font = pygame.font.SysFont("DejaVuSans", 30)
def blink(screen):
for color in [black, white]:
pygame.draw.rect(screen, color, (300, 251, 1, 28))
pygame.display.flip()
pygame.time.wait(300)
def show_name(screen, name):
pygame.draw.rect(screen, white, (120, 250, 400, 30), 0)
txt_surf = font.render(name, True, black)
txt_rect = txt_surf.get_rect(center=(300, 265))
screen.blit(txt_surf, txt_rect)
pygame.display.flip()
screen.blit(framehss,(60, 40))
screen.blit(button, (200, 350))
message_display(screen, "Submit", white, (270, 370), 50)
message_display(screen, txt, black, (120, 100), 35)
message_display(screen, txt2, black, (120, 145), 35)
message_display(screen, txt3, black, (120, 190), 35)
name = ""
show_name(screen, name)
pygame_sdl2.key.start_text_input()
# the input-loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.TEXTINPUT:
input = str(event.text)
name = name + input
elif event.type == KEYDOWN:
if event.key == 8:
name = name[:-1]
elif event.type == FINGERDOWN:
touch_pos = [event.x * WIDTH, event.y * HEIGHT]
if 450 > touch_pos[1] > 350:
if 520 > touch_pos[0] > 200:
pygame_sdl2.key.start_text_input()
if 500 > touch_pos[1] > 300:
if 520 > touch_pos[0] > 120:
screen.blit(buttonp, (200, 350))
message_display(screen, "Submit", white, (270, 370), 50)
pygame.display.flip()
pygame_sdl2.key.stop_text_input()
return name
if name == "":
blink(screen)
show_name(screen, name)
def highscore(screen, file_name, your_points):
high_name, high_score = read_from_file_and_find_highscore(file_name)
if your_points > high_score:
your_name = enterbox(screen, "YOU HAVE BEATEN", "THE HIGHSCORE - ", "What is your name?")
elif your_points <= high_score:
st1 = "Highscore is " + str(high_score)
st2 = "made by " + high_name
st3 = "What is your name?"
your_name = enterbox(screen, st1, st2, st3)
if your_name == None or len(your_name) == 0:
return # do not update the file unless a name is given
write_to_file(file_name, your_name, your_points)
show_top10(screen, file_name, True)
return