Update 'final/game.c'

This commit is contained in:
Anzhelika Nikolaieva 2023-05-29 08:40:13 +00:00
parent a371e8038d
commit 67cb43cef1

View File

@ -78,6 +78,7 @@ set_color_cell(' ', x+1, y-11, COLOR_WHITE, COLOR);
set_color_cell(' ', x+2, y-11, COLOR_WHITE, COLOR); set_color_cell(' ', x+2, y-11, COLOR_WHITE, COLOR);
} }
void* init_game(){ void* init_game(){
// Allocate memory for the state // Allocate memory for the state
struct game* st = calloc(1,(sizeof(struct game))); struct game* st = calloc(1,(sizeof(struct game)));
@ -96,6 +97,7 @@ st->color = 0;
st->jumps = 0; st->jumps = 0;
return st; return st;
} }
// Step is called in a loop once in interval. // Step is called in a loop once in interval.
// It should modify the state and draw it. // It should modify the state and draw it.
int game_event(struct event* event,void* game){ int game_event(struct event* event,void* game){
@ -112,86 +114,87 @@ if ( event->type == EVENT_ESC){
if (state->level == 1){ if (state->level == 1){
for (int i = 0; i<CACTUS_COUNT; i++){ for (int i = 0; i<CACTUS_COUNT; i++){
if (state->first_cactusx[i]==COLS){ if (state->first_cactusx[i]==COLS){
continue; continue;
} }
if (state->cactusx[i-1]<COLS-50 && state->cactusx[i-1]>0 && i!=0){ if (state->cactusx[i-1]<COLS-50 && state->cactusx[i-1]>0 && i!=0){
state->first_cactusx[i] = COLS; state->first_cactusx[i] = COLS;
} }
} }
for (int i = 0; i<CACTUS_COUNT; i++){ for (int i = 0; i<CACTUS_COUNT; i++){
if (state->first_cactusx[i]==COLS && state->cactusx[i]<0){ if (state->first_cactusx[i]==COLS && state->cactusx[i]<0){
state->cactusx[i]=state->first_cactusx[i]-1; state->cactusx[i]=state->first_cactusx[i]-1;
} }
} }
} }
else if (state->level == 2){ else if (state->level == 2){
for (int i = 0; i<CACTUS_COUNT; i++){ for (int i = 0; i<CACTUS_COUNT; i++){
if (state->first_cactusx[i]==COLS){ if (state->first_cactusx[i]==COLS){
continue; continue;
} }
if (state->cactusx[i-1]<COLS-50 && state->cactusx[i-1]>0 && i!=0){ if (state->cactusx[i-1]<COLS-50 && state->cactusx[i-1]>0 && i!=0){
state->first_cactusx[i] = COLS; state->first_cactusx[i] = COLS;
} }
} }
for (int i = 0; i<CACTUS_COUNT; i++){ for (int i = 0; i<CACTUS_COUNT; i++){
if (state->first_cactusx[i]==COLS && state->cactusx[i]<0){ if (state->first_cactusx[i]==COLS && state->cactusx[i]<0){
state->cactusx[i]=state->first_cactusx[i]-1; state->cactusx[i]=state->first_cactusx[i]-1;
}
} }
}
} }
for (int i = 0; i< CACTUS_COUNT; i++){ for (int i = 0; i< CACTUS_COUNT; i++){
if (state->dinox == state->cactusx[i] && (state->dinoy == state->cactusy[i] || state->dinoy == state->cactusy[i]-1 || state->dinoy == state->cactusy[i]-2 || if (state->dinox == state->cactusx[i] && (state->dinoy == state->cactusy[i] || state->dinoy == state->cactusy[i]-1 || state->dinoy == state->cactusy[i]-2 ||
state->dinoy == state->cactusy[i]-3 || state->dinoy == state->cactusy[i]-4 || state->dinoy == state->cactusy[i]-5)){ state->dinoy == state->cactusy[i]-3 || state->dinoy == state->cactusy[i]-4 || state->dinoy == state->cactusy[i]-5)){
clear_screen(); clear_screen();
set_message("OOOOPS, YOU LOOSE", COLS/2-20, LINES/2); set_message("OOOOPS, YOU LOOSE", COLS/2-20, LINES/2);
return 0; return 0;
}
} }
}
for (int i=0; i<CACTUS_COUNT; i++){ for (int i=0; i<CACTUS_COUNT; i++){
if (state->dinox==state->cactusx[i] ){ if (state->dinox==state->cactusx[i] ){
if (state->level==1){ if (state->level==1){
state->score = state->score +10; state->score = state->score +10;
} }
else if(state->level == 2){ else if(state->level == 2){
state->score = state->score +15; state->score = state->score +15;
}
}
} }
}
}
if ( event->type == EVENT_ESC){ if ( event->type == EVENT_ESC){
// Non zero means finish the loop and stop the game. // Non zero means finish the loop and stop the game.
return 1; return 1;
} }
else if (event->type == EVENT_KEY){ else if (event->type == EVENT_KEY){
if ( event->key == KEY_UP && state->dinoy-12>0 && state->jumps == 0){ if ( event->key == KEY_UP && state->dinoy-12>0 && state->jumps == 0){
state->jumps = 12; state->jumps = 12;
}
} }
}
else if(event->type == EVENT_TIMEOUT){ else if(event->type == EVENT_TIMEOUT){
//for jumping //for jumping
if (state->jumps>0){ if (state->jumps>0){
state->jumps--; state->jumps--;
if (state->dinoy>0){
state->dinoy--; if (state->dinoy>0){
} state->dinoy--;
}
} }
//for falling down //for falling down
if (state->jumps == 0 && state->dinoy < LINES-1){ if (state->jumps == 0 && state->dinoy < LINES-1){
state->dinoy++; state->dinoy++;
@ -201,11 +204,14 @@ for (int i = 0; i< CACTUS_COUNT; i++){
if (state->cactusx[i]>0){ if (state->cactusx[i]>0){
if (state->level ==1){ if (state->level ==1){
state->cactusx[i]--; state->cactusx[i]--;
} }
else if (state->level == 2){ else if (state->level == 2){
state->cactusx[i]=state->cactusx[i]-2; state->cactusx[i]=state->cactusx[i]-2;
} }
}
}
if (state->level == 1 && i==CACTUS_COUNT-1 && state->cactusx[i]==2){ if (state->level == 1 && i==CACTUS_COUNT-1 && state->cactusx[i]==2){
state->level = 2; state->level = 2;
for (int j=0; j<CACTUS_COUNT; j++){ for (int j=0; j<CACTUS_COUNT; j++){
@ -214,12 +220,13 @@ for (int j=0; j<CACTUS_COUNT; j++){
} }
state->first_cactusx[0] = COLS; state->first_cactusx[0] = COLS;
} }
if (state->level==2 && i==CACTUS_COUNT-1 && state->cactusx[i]==2){ if (state->level==2 && i==CACTUS_COUNT-1 && state->cactusx[i]==2){
state->level = 0; state->level = 0;
} }
}
}
} }
@ -240,6 +247,7 @@ set_color_cell( ' ', state->cactusx[i]+2, state->cactusy[i]-2, COLOR_WHITE, COLO
set_color_cell( ' ', state->cactusx[i]+2, state->cactusy[i]-3, COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i]+2, state->cactusy[i]-3, COLOR_WHITE, COLOR_GREEN);
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-5, COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-5, COLOR_WHITE, COLOR_GREEN);
} }
else if (state->type[i]==1){ else if (state->type[i]==1){
set_color_cell( ' ', state->cactusx[i], state->cactusy[i], COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i], state->cactusy[i], COLOR_WHITE, COLOR_GREEN);
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-1, COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-1, COLOR_WHITE, COLOR_GREEN);
@ -263,6 +271,7 @@ set_color_cell( ' ', state->cactusx[i]-2, state->cactusy[i]-3, COLOR_WHITE, COLO
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-4, COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-4, COLOR_WHITE, COLOR_GREEN);
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-5, COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-5, COLOR_WHITE, COLOR_GREEN);
} }
else if(state->type[i] ==3){ else if(state->type[i] ==3){
set_color_cell( ' ', state->cactusx[i], state->cactusy[i], COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i], state->cactusy[i], COLOR_WHITE, COLOR_GREEN);
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-2, COLOR_WHITE, COLOR_GREEN); set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-2, COLOR_WHITE, COLOR_GREEN);
@ -278,7 +287,9 @@ set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-5, COLOR_WHITE, COLOR_
} }
} }
} }
if (state->color ==0){ if (state->color ==0){
draw_dino(state->dinox, state->dinoy, COLOR_RED); draw_dino(state->dinox, state->dinoy, COLOR_RED);
} }
@ -286,22 +297,28 @@ draw_dino(state->dinox, state->dinoy, COLOR_RED);
if (state->color==1){ if (state->color==1){
draw_dino(state->dinox, state->dinoy, COLOR_CYAN); draw_dino(state->dinox, state->dinoy, COLOR_CYAN);
} }
if (state->color == 1){ if (state->color == 1){
state->color =0; state->color =0;
} }
else{ else{
state->color = 1; state->color = 1;
} }
if (state->level==2){ if (state->level==2){
set_message("LEVEL 2", 20, 20); set_message("LEVEL 2", 20, 20);
} }
else if (state->level == 1 ){ else if (state->level == 1 ){
set_message("LEVEL 1", 20, 20); set_message("LEVEL 1", 20, 20);
} }
char int_str[20]; char int_str[20];
sprintf(int_str, " YOUR SCORE %d", state->score); sprintf(int_str, " YOUR SCORE %d", state->score);
set_message(int_str, 60,20); set_message(int_str, 60,20);
if (state->level == 0){ //end game if (state->level == 0){ //end game
clear_screen(); clear_screen();