-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.cpp
67 lines (63 loc) · 1.88 KB
/
game.cpp
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
#include <iostream>
#include "stdlib.h"
#include <map>
#include "game.h"
#include "pers.h"
int game::keyEvent(){
if( !initialized){
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
int bytesWaiting;
//int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
};
void game::frameSleep(const int& ms){
clock_t end;
end = clock() + ms * CLOCKS_PER_SEC/1000;
while( clock() < end){
// wait
}
};
std::pair<int,int> game::from_key_to_move(int key){
if ( key == 68 ) {//flèche gauche
return std::make_pair(-1,0);
}
if ( key == 67 ) {//flèche droite
return std::make_pair(1,0);
}
if ( key == 65 ) {//flèche basse
return std::make_pair(0,-1);
}
if ( key == 66 ) {//flèche haute
return std::make_pair(0,1);
}
return std::make_pair(0,0);
};
void game::startGame( board &b ){
int key; //variable contenant la frappe
/*std::pair<int, int> dim = b.get_dim();
int nx = dim.first;
int ny = dim.second;*/
//o.create();
//b.add_object( a );
Hero h(40,5);
while( true ){
frameSleep(lap);
if( keyEvent() ){
int a = getchar();
int b = getchar();
int key = getchar(); //récupération de la frappe (3ème caractère pour différencier les flèches)
h.move( from_key_to_move(key).first, from_key_to_move(key).second ); //maj du mouvement
}
b.backgroundClear();
b.remove_perso( h );
b.add_perso( h ); //ajout du perso au plateau
b.printFrame( h ); //affichage du plateau
};
};