-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscores.c
151 lines (115 loc) · 2.79 KB
/
highscores.c
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
149
150
151
#ifndef HIGHSCORES_C
#define HIGHSCORES_C
#include <stdio.h>
#include <string.h>
#include "ctype.h"
#include "main.h"
#define TAM 5
#define TAM_NOME 15
typedef struct{
char nome[TAM_NOME];
int pontuacao;
} VPLAYER;
VPLAYER vplayer[(TAM + 1)];
char nome[TAM_NOME] = " ";
// funcao de teste para imprimir ranking
void le_ranking()
{
int x = 0;
FILE *fp = fopen("highscores.bin","rb");
rewind(fp);
while (x < TAM)
{
if (x < TAM)
fread(&vplayer[x], sizeof(VPLAYER), 1,fp);
x++;
}
fclose(fp);
}
// coloca a nova pontuacao no ranking
void arruma_posicoes(){
VPLAYER aux;
for (int i = 4; i > -1; i--)
{
if (vplayer[i].pontuacao < vplayer[i + 1].pontuacao)
{
aux.pontuacao = vplayer[i].pontuacao;
vplayer[i].pontuacao = vplayer[i+1].pontuacao;
vplayer[i+ 1].pontuacao = aux.pontuacao;
strcpy(aux.nome, vplayer[i].nome);
strcpy( vplayer[i].nome, vplayer[i+1].nome);
strcpy(vplayer[i+1].nome, aux.nome);
}
}
}
// salva ranking no highscores.bin
void escreve_ranking()
{
FILE *fp = fopen("highscores.bin","r+b");
rewind(fp);
fseek(fp, 0, SEEK_SET);
for (int i = 0; i < TAM ; i++)
fwrite(&vplayer[i], sizeof(VPLAYER), 1, fp);
fclose(fp);
}
// atualizar o ranking
void update_ranking()
{
le_ranking();
arruma_posicoes();
escreve_ranking();
}
int starthighscores()
{
if (!acessos){
strcpy(vplayer[0].nome, "Joao");
vplayer[0].pontuacao = 25;
strcpy(vplayer[1].nome, "Maria");
vplayer[1].pontuacao = 20;
strcpy(vplayer[2].nome, "Joana");
vplayer[2].pontuacao = 15;
strcpy(vplayer[3].nome, "Mario");
vplayer[3].pontuacao = 10;
strcpy(vplayer[4].nome, "Djario");
vplayer[4].pontuacao = 5;
strcpy(vplayer[5].nome, "Jogador");
vplayer[5].pontuacao = 0;
escreve_ranking();
}
else update_ranking();
acessos++;
return 0;
}
// testa se a pontuação entra para os highscores e pergunta o nome do jogador
void gameover() {
Rectangle textbox = {largura / 2 - 200, altura / 2 - 25, largura / 2, 50};
int letras = 0, key;
while (1) {
if (WindowShouldClose()) CloseWindow();
// desenhar
BeginDrawing();
ClearBackground(BLACK);
DrawText("Insira seu nome:", 50, 20, 20, RED);
DrawRectangleLinesEx(textbox, 3, RED);
DrawText(nome, 210, 135, 20, YELLOW);
EndDrawing();
// obter texto
key = GetKeyPressed();
if (isalpha((char) key) && (letras < TAM_NOME)) {
nome[letras] = (char) key;
letras++;
}
else if (key == KEY_BACKSPACE) {
nome[letras - 1] = '\0';
letras--;
}
else if (key == KEY_ENTER) {
strcpy(vplayer[5].nome, nome);
vplayer[5].pontuacao = game.pontuacao;
update_ranking();
game.executar_menu = true;
break;
}
}
}
#endif