48 lines
1.5 KiB
C
48 lines
1.5 KiB
C
#include <curses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include "game.h"
|
|
#include "commands.h"
|
|
|
|
// Start is called one in the beginning
|
|
struct game* init_game(){
|
|
// Allocate memory for the state
|
|
struct game* st = calloc(1,(sizeof(struct game)));
|
|
// Initialize state
|
|
st->score = 0;
|
|
st->x = (rand() % (COLS - 5) + 2) / 2 * 2;
|
|
st->y = rand() % (LINES - 5) + 2;
|
|
// Store pointer to the state to the world variable
|
|
return st;
|
|
}
|
|
|
|
// It should modify the state and draw it.
|
|
int world_event(struct game* game){
|
|
//Wait for input to start the game
|
|
do{
|
|
game->direction = getch();
|
|
}while(game->direction != 'w' && game->direction != 'a' && game->direction != 's' && game->direction != 'd');
|
|
for(int i = -1; i < 2; i++){
|
|
for(int q = -2; q < 4; q += 2){
|
|
mvaddch(game->y + i, game->x + q, ' ');
|
|
}
|
|
}
|
|
//The loop of the game
|
|
while(1){
|
|
if(rand() % 10 == 0){
|
|
spawn_blocks(game);
|
|
move(game->y, game->x);
|
|
}
|
|
int save = game->direction;
|
|
flushinp();
|
|
game->direction = getch();
|
|
//If wrong input- ignore it and change to the old one
|
|
if(game->direction != 'w' && game->direction != 'a' && game->direction != 's' && game->direction != 'd' && game->direction != 'q') game->direction = save;
|
|
if(game->direction == 'q') break;
|
|
if(movement(game) == 0) break;
|
|
game->score += 1;
|
|
}
|
|
getch();
|
|
game_over(game);
|
|
} |