-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT-REX.c
More file actions
169 lines (136 loc) · 3.12 KB
/
Copy pathT-REX.c
File metadata and controls
169 lines (136 loc) · 3.12 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
int game_over = 0, score = 0, iscactus = 0, isDay = 0;
int Dino_x = 20, Dino_y = 20, height = 35, width = 80;
int velocity, state, ground = 20;
int gravity = 1, jump = 0;
int i, j, cactus_x[3], cactus_y[3];
int random_cactus[3], delay;
int sun_x = 70, sun_y = 5;
void input();
void hitBox();
void grid_print(int height, int width);
void update_frame();
void Atmosphere();
int main(void)
{
srand(time(NULL));
for(i = 0; i < 3; i++)
{
random_cactus[i] = rand() % 50 + 30;
cactus_x[i] = width + random_cactus[i] * i;
cactus_y[i] = ground;
}
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO); // disable buffering + echo
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // non-blocking
while(game_over == 0)
{
system("clear");
printf("Game Running -->\n");
printf("Score: %i\n", score);
input();
update_frame();
Atmosphere();
hitBox();
grid_print(height, width);
usleep(delay); // Delay in ms
}
printf("\nYour score: %i", score);
printf("\n\nGame Over MATE!!!\n");
return 0;
}
void grid_print(int height, int width)
{
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
iscactus = 0;
//cactus
for(int k = 0; k < 3; k++)
{
if (i == cactus_y[k] && j == cactus_x[k])
{
iscactus = 1;
break;
}
}
//for Dinosaur body
if (i == Dino_y && j == Dino_x) printf("D");
//cactus
else if(iscactus == 1) printf("|");
//for grid buondry print
else if (i == height-1) printf("_");
else if (j == width - 1) printf("|");
else if (i == 0) printf("_");
else if (j == 0) printf("|");
//sun and moon
else if(i == sun_y && j == sun_x)
{
if(isDay == 0) printf("O");
else printf("C");
}
//whole Sky body
else if (i < ground && isDay == 0) printf(" ");
else if (i < ground && isDay == 1) printf(".");
//Whole groung body
else printf("_");
}
printf("\n");
}
}
void update_frame()
{
if(jump && Dino_y == ground)
{
velocity = -3; //handle jump trigger
jump = 0;
}
//gravity case
velocity += gravity;
Dino_y += velocity;
//stop when reach ground
if(Dino_y >= ground)
{
Dino_y = ground;
velocity = 0;
}
for(i = 1; i <= 3; i++)
{
cactus_x[i]--;
if(cactus_x[i] < 1)
{
random_cactus[i] = rand() % 30;
cactus_x[i] = random_cactus[i] + width;
}
}
score += 1;
delay = 50000 - (score * 100);
if (delay < 40000) delay = 40000;
}
void input()
{
char key;
if (read(STDIN_FILENO, &key, 1) > 0)
if (key == ' ') jump = 1;
}
void hitBox()
{
for(i = 0; i < 3; i++)
{
if(Dino_x <= cactus_x[i] && cactus_x[i] == Dino_x + 1 && Dino_y + 1 >= cactus_y[i] && cactus_y[i] >= Dino_y) game_over = 1;
}
}
void Atmosphere()
{
if(score % 2000 < 1000) isDay = 0;
else isDay = 1;
}