341 lines
11 KiB
C
341 lines
11 KiB
C
#include <stdio.h>
|
|
#include <curses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "world.h"
|
|
#include "game.h"
|
|
|
|
|
|
void draw_dino (int x, int y, short COLOR){
|
|
set_color_cell(' ', x, y, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-1, y, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-2, y, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-3, y, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-4, y, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-2, y-1, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-4, y-1, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-4, y-2, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-2, y-2, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-6, y-3, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-5, y-3, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-4, y-3, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-3, y-3, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-2, y-3, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-1, y-3, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-7, y-4, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-6, y-4, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-5, y-4, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-4, y-4, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-3, y-4, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-2, y-4, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-1, y-4, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x, y-4, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-8, y-5, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-7, y-5, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-4, y-5, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-3, y-5, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-2, y-5, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-1, y-5, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x, y-5, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+2, y-5, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-8, y-6, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-3, y-6, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-2, y-6, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-1, y-6, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x, y-6, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+1, y-6, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+2, y-6, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-8, y-7, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-2, y-7, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x-1, y-7, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x, y-7, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-1, y-8, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x, y-8, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-1, y-9, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x, y-9, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+1, y-9, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+2, y-9, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+3, y-9, COLOR_WHITE, COLOR);
|
|
|
|
set_color_cell(' ', x-1, y-10, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x, y-10, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+1, y-10, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+2, y-10, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+3, y-10, COLOR_WHITE, COLOR);
|
|
|
|
|
|
set_color_cell(' ', x, y-11, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+1, y-11, COLOR_WHITE, COLOR);
|
|
set_color_cell(' ', x+2, y-11, COLOR_WHITE, COLOR);
|
|
|
|
}
|
|
|
|
void* init_game(){
|
|
// Allocate memory for the state
|
|
struct game* st = calloc(1,(sizeof(struct game)));
|
|
// Initialize state
|
|
st->dinox = 10;
|
|
st->dinoy = LINES-1;
|
|
st -> level = 1;
|
|
for (int i = 0; i< CACTUS_COUNT; i++){
|
|
st->type[i]=rand()%4;
|
|
st->cactusy[i] = LINES-1;
|
|
st->cactusx[i] = -1;
|
|
st->first_cactusx[i] = -1;
|
|
}
|
|
st->first_cactusx[0]=COLS;
|
|
st->color = 0;
|
|
st->jumps = 0;
|
|
return st;
|
|
}
|
|
|
|
// Step is called in a loop once in interval.
|
|
// It should modify the state and draw it.
|
|
int game_event(struct event* event,void* game){
|
|
|
|
struct game* state = game;
|
|
char msg[200];
|
|
sprintf(msg,"%d",event->type);
|
|
set_message(msg,10,0);
|
|
if ( event->type == EVENT_ESC){
|
|
// Non zero means finish the loop and stop the game.
|
|
return 1;
|
|
}
|
|
|
|
|
|
if (state->level == 1){
|
|
for (int i = 0; i<CACTUS_COUNT; i++){
|
|
if (state->first_cactusx[i]==COLS){
|
|
continue;
|
|
}
|
|
if (state->cactusx[i-1]<COLS-50 && state->cactusx[i-1]>0 && i!=0){
|
|
state->first_cactusx[i] = COLS;
|
|
}
|
|
}
|
|
|
|
|
|
for (int i = 0; i<CACTUS_COUNT; i++){
|
|
if (state->first_cactusx[i]==COLS && state->cactusx[i]<0){
|
|
state->cactusx[i]=state->first_cactusx[i]-1;
|
|
}
|
|
|
|
}
|
|
}
|
|
else if (state->level == 2){
|
|
|
|
for (int i = 0; i<CACTUS_COUNT; i++){
|
|
if (state->first_cactusx[i]==COLS){
|
|
continue;
|
|
}
|
|
if (state->cactusx[i-1]<COLS-50 && state->cactusx[i-1]>0 && i!=0){
|
|
state->first_cactusx[i] = COLS;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i<CACTUS_COUNT; i++){
|
|
if (state->first_cactusx[i]==COLS && state->cactusx[i]<0){
|
|
state->cactusx[i]=state->first_cactusx[i]-1;
|
|
}
|
|
}
|
|
|
|
}
|
|
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 ||
|
|
state->dinoy == state->cactusy[i]-3 || state->dinoy == state->cactusy[i]-4 || state->dinoy == state->cactusy[i]-5)){
|
|
|
|
clear_screen();
|
|
|
|
set_message("OOOOPS, YOU LOOSE", COLS/2-20, LINES/2);
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
|
|
for (int i=0; i<CACTUS_COUNT; i++){
|
|
if (state->dinox==state->cactusx[i] ){
|
|
if (state->level==1){
|
|
state->score = state->score +10;
|
|
}
|
|
|
|
else if(state->level == 2){
|
|
state->score = state->score +15;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if ( event->type == EVENT_ESC){
|
|
// Non zero means finish the loop and stop the game.
|
|
return 1;
|
|
}
|
|
|
|
else if (event->type == EVENT_KEY){
|
|
|
|
if ( event->key == KEY_UP && state->dinoy-12>0 && state->jumps == 0){
|
|
state->jumps = 12;
|
|
}
|
|
}
|
|
|
|
else if(event->type == EVENT_TIMEOUT){
|
|
//for jumping
|
|
if (state->jumps>0){
|
|
state->jumps--;
|
|
|
|
if (state->dinoy>0){
|
|
state->dinoy--;
|
|
}
|
|
}
|
|
|
|
//for falling down
|
|
if (state->jumps == 0 && state->dinoy < LINES-1){
|
|
state->dinoy++;
|
|
}
|
|
|
|
for (int i = 0; i< CACTUS_COUNT; i++){
|
|
if (state->cactusx[i]>0){
|
|
if (state->level ==1){
|
|
state->cactusx[i]--;
|
|
}
|
|
|
|
else if (state->level == 2){
|
|
state->cactusx[i]=state->cactusx[i]-2;
|
|
}
|
|
|
|
}
|
|
|
|
if (state->level == 1 && i==CACTUS_COUNT-1 && state->cactusx[i]==2){
|
|
state->level = 2;
|
|
for (int j=0; j<CACTUS_COUNT; j++){
|
|
state->cactusx[j] = -1;
|
|
state->first_cactusx[j] = -1;
|
|
}
|
|
state->first_cactusx[0] = COLS;
|
|
}
|
|
|
|
if (state->level==2 && i==CACTUS_COUNT-1 && state->cactusx[i]==2){
|
|
state->level = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//drawing main game content
|
|
clear_screen();
|
|
for (int i = 0; i< CACTUS_COUNT; i++){
|
|
if (state->cactusx[i]-2>0 && state->cactusx[i]<COLS-3){
|
|
|
|
if(state->type[i] == 0){
|
|
|
|
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]-2, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-3, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-4, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]+1, state->cactusy[i]-2, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]+2, state->cactusy[i]-2, 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);
|
|
}
|
|
|
|
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]-1, 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]-3, 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]+1, state->cactusy[i]-3, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]-1, state->cactusy[i]-1, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]-1, state->cactusy[i]-4, COLOR_WHITE, COLOR_GREEN);
|
|
}
|
|
|
|
else if (state->type[i]==2){
|
|
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]-2, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-3, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]-1, state->cactusy[i]-2, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]-2, state->cactusy[i]-2, 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]-4, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-5, COLOR_WHITE, COLOR_GREEN);
|
|
}
|
|
|
|
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]-2, 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]-3, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-4, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]+1, state->cactusy[i]-3, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]+2, state->cactusy[i]-4, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]+2, state->cactusy[i]-4, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i]+2, state->cactusy[i]-5, COLOR_WHITE, COLOR_GREEN);
|
|
set_color_cell( ' ', state->cactusx[i], state->cactusy[i]-5, COLOR_WHITE, COLOR_GREEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (state->color ==0){
|
|
draw_dino(state->dinox, state->dinoy, COLOR_RED);
|
|
}
|
|
|
|
if (state->color==1){
|
|
draw_dino(state->dinox, state->dinoy, COLOR_CYAN);
|
|
}
|
|
|
|
if (state->color == 1){
|
|
state->color =0;
|
|
}
|
|
|
|
else{
|
|
state->color = 1;
|
|
}
|
|
|
|
if (state->level==2){
|
|
set_message("LEVEL 2", 20, 20);
|
|
}
|
|
|
|
else if (state->level == 1 ){
|
|
set_message("LEVEL 1", 20, 20);
|
|
}
|
|
|
|
char int_str[20];
|
|
|
|
sprintf(int_str, " YOUR SCORE %d", state->score);
|
|
set_message(int_str, 60,20);
|
|
|
|
if (state->level == 0){ //end game
|
|
clear_screen();
|
|
|
|
draw_dino(65, 40, COLOR_BLUE);
|
|
|
|
draw_dino(50, 40, COLOR_YELLOW);
|
|
|
|
draw_dino(80,40, COLOR_MAGENTA);
|
|
|
|
draw_dino(95,40, COLOR_GREEN);
|
|
|
|
draw_dino(110,40, COLOR_WHITE);
|
|
|
|
draw_dino(125, 40, COLOR_RED);
|
|
|
|
set_message("CONGRATULATIONS! YOU WON!", COLS/2-20, LINES/2);
|
|
}
|
|
return 0;
|
|
}
|