-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.h
111 lines (100 loc) · 3.05 KB
/
game.h
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
#include <math.h>
#include <stdlib.h>
int randRange(int a, int b){
return a+rand()%(b-a);
}
struct Player{
int x;
int y;
int x_border;
int y_border;
int jumpx;
int lifes;
int invulerable;
};
int jumpPoly(int x){
int x0=30;
if(x>=x0) return -2;
else if(x>x0/2) return -1;
else if(x<x0/2-2) return 1;
return 0;
int xm=6;
double fx=-x*(x-x0)*(4*xm/pow(xm,2));
mvprintw(10, 1, "jmply=%lf", fx);
return (int)floor(fx);
}
void genChunk(Matrix chunk){
//fresh chunk
initMatrix(chunk, '+');
//generate
int height=randRange((int)floor(WIN_height/2), WIN_height);
int length=OBS_maxlength-(rand()%OBS_lengthvar); //largo del obstaculo
for(int x=0; x<length && x<chunk.nCols; x++)
chunk.ptr[height][x]='=';
if(rand()%5==0)
chunk.ptr[height-1-rand()%2][rand()%length]='*';
}
void printPlayer(struct Player player){
for(int i=0; i<player.y_border; i++)
mvprintw(player.y-i, player.x, "%%");
}
int canDrop(Matrix world, struct Player *player){
if(player->y+1>=world.nRens) return 0;
for(int n=0; n<player->x_border && n<world.nCols; n++){
// mvprintw(player->y+1, player->x+n, "@");
if(world.ptr[player->y+1][player->x+n]!='+') return 0;
}
if(player->y+2>=world.nRens) return 0;
return 1;
}
int canJump(Matrix world, struct Player *player){
if(player->y-1>=world.nRens || player->y-1<0) return 0;
for(int n=0; n<player->x_border && n<world.nCols; n++){
// mvprintw(player->y-1, player->x+n, "@");
if(world.ptr[player->y-player->y_border][player->x+n]!='+') return 0;
}
return 1;
}
int playerColision(Matrix world, struct Player *player){
for(int m=0; m<player->y_border && player->y-m<world.nRens; m++) //verifica los bordes del personaje
if(world.ptr[player->y-m][player->x+player->x_border]=='=' || world.ptr[player->y-m][player->x+player->x_border]=='*')
return 1;
return 0;
}
void movePlayer(WINDOW *win, struct Player *player, Matrix world){
nodelay(win, TRUE);
char c = getch();
//nodelay(win, TRUE);
switch(c) {
case ' ':
if(player->jumpx==0) player->jumpx=1;
mvprintw(4, 1, ":UP");
break;
case 's':
mvprintw(4, 1, ":PAUSA");
pauseMenu(win);
break;
default:
mvprintw(4, 1, ":");
break;
}
if(player->jumpx!=0){//si esta brincando opera
int jumpy=jumpPoly(player->jumpx);
player->jumpx++;
mvprintw(6, 1, ":BRINCANDO");
if(jumpy==-2 || !canJump(world, player)) //si ya acabo de brincar o no puede reinicializa
player->jumpx=0;
else if(jumpy>0){
mvprintw(7, 1, ":SUBIENDOO");
player->y-=jumpy; //subiendo
}
else if(jumpy<0 && canDrop(world, player)){
mvprintw(7, 1, ":BAJANDOO");
player->y-=jumpy; //bajando
}
}
else if(canDrop(world, player)){
mvprintw(7, 1, ":CAYENDOO");
player->y++;
}
}