working game
This commit is contained in:
114
main.c
114
main.c
@@ -5,12 +5,11 @@
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
#include "./globals.h"
|
||||
#include "./utils/utils.h"
|
||||
#include "./snake/snake.h"
|
||||
|
||||
void mvSnakeParts(snakePart* head);
|
||||
|
||||
char board[BRD_SIZE_Y][BRD_SIZE_X];
|
||||
|
||||
const char SNAKE_VIS = '#';
|
||||
@@ -29,7 +28,7 @@ int main() {
|
||||
attr.c_cc[VMIN] = 0;
|
||||
attr.c_cc[VTIME] = 0;
|
||||
|
||||
tcsetattr(STDIN_FILENO, 0,&attr);
|
||||
tcsetattr(STDIN_FILENO, 0, &attr);
|
||||
|
||||
// Game board setup
|
||||
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
||||
@@ -49,12 +48,14 @@ int main() {
|
||||
board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+';
|
||||
|
||||
int points = 0;
|
||||
int gameSpeed = 0;
|
||||
int sleepInterval = 200 - gameSpeed;
|
||||
|
||||
// Snake head setup
|
||||
snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart));
|
||||
snakeHead->x = randomX(initClock);
|
||||
snakeHead->y = randomY(initClock);
|
||||
snakeHead->visChar = SNAKE_VIS;
|
||||
snakeHead->visChar = '&';
|
||||
snakeHead->dir = 'w';
|
||||
snakeHead->order = (order*) malloc(sizeof(order));
|
||||
snakeHead->order->dir = snakeHead->dir;
|
||||
@@ -84,18 +85,22 @@ int main() {
|
||||
fflush(stdout);
|
||||
char buf[1] = {0};
|
||||
|
||||
// Food collision
|
||||
if (snakeHead->x == food.x
|
||||
&& snakeHead->y == food.y) {
|
||||
points++;
|
||||
food.x = randomX(initClock);
|
||||
food.y = randomY(initClock);
|
||||
board[food.y][food.x] = food.visChar;
|
||||
if (points % 5 == 0)
|
||||
gameSpeed += 15;
|
||||
do {
|
||||
food.x = randomX(initClock);
|
||||
food.y = randomY(initClock);
|
||||
} while (checkCollision(snakeHead, food.x, food.y));
|
||||
addSnakePart(snakeHead);
|
||||
}
|
||||
|
||||
// Input handling
|
||||
if (read(STDIN_FILENO, buf, 1) != 0 && isalpha(buf[0])) {
|
||||
char input = buf[0];
|
||||
|
||||
input = tolower(input);
|
||||
|
||||
if (
|
||||
@@ -109,17 +114,36 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Update board
|
||||
board[food.y][food.x] = food.visChar;
|
||||
mvSnakeParts(snakeHead);
|
||||
|
||||
// Snake collision
|
||||
if (snakeHead->next != NULL) {
|
||||
snakePart* current = snakeHead->next;
|
||||
while (1) {
|
||||
if (current->x == snakeHead->x
|
||||
&& current->y == snakeHead->y)
|
||||
goto game_over;
|
||||
|
||||
if (current->next == NULL)
|
||||
break;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
system("clear");
|
||||
printBoard();
|
||||
printf("Points: %d\n", points);
|
||||
printf("Game Speed: %d\n", gameSpeed);
|
||||
|
||||
/*
|
||||
printf("x: %d\n", snakeHead->x);
|
||||
printf("y: %d\n\n", snakeHead->y);
|
||||
printf("food x: %d\n", food.x);
|
||||
printf("food y: %d\n", food.y);
|
||||
|
||||
/*
|
||||
{
|
||||
snakePart* current = snakeHead;
|
||||
int i = 0;
|
||||
@@ -155,71 +179,19 @@ int main() {
|
||||
}
|
||||
*/
|
||||
|
||||
sleep_ms(200);
|
||||
sleep_ms(sleepInterval - gameSpeed);
|
||||
}
|
||||
|
||||
// Termios reset
|
||||
game_over:
|
||||
|
||||
// Game over
|
||||
system("clear");
|
||||
|
||||
printBoard();
|
||||
printf("Final Points: %d\n", points);
|
||||
printf("Final Game Speed: %d\n", gameSpeed);
|
||||
|
||||
tcsetattr(STDIN_FILENO, 0, &ATTR_ORIG);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mvSnakeParts(snakePart* head) {
|
||||
snakePart* part = head;
|
||||
while (1) {
|
||||
|
||||
order* orderHead = part->order;
|
||||
|
||||
if (orderHead->next != NULL) {
|
||||
if (orderHead->next->delay == 0) {
|
||||
part->dir = orderHead->next->dir;
|
||||
removeOrder(orderHead);
|
||||
}
|
||||
}
|
||||
|
||||
if (orderHead->next != NULL) {
|
||||
order* current = orderHead->next;
|
||||
while (1) {
|
||||
if (current->delay > 0)
|
||||
current->delay--;
|
||||
if (current->next == NULL)
|
||||
break;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
int x = part->x;
|
||||
int y = part->y;
|
||||
|
||||
board[y][x] = ' ';
|
||||
|
||||
switch (part->dir) {
|
||||
case 'w':
|
||||
y = y - 1 < PL_BRD_YS ? PL_BRD_YE : y - 1;
|
||||
break;
|
||||
case 's':
|
||||
y = y + 1 > PL_BRD_YE ? PL_BRD_YS : y + 1;
|
||||
break;
|
||||
case 'a':
|
||||
x = x - 2 < PL_BRD_XS ? PL_BRD_XE - 1 : x - 2;
|
||||
break;
|
||||
case 'd':
|
||||
x = x + 2 > PL_BRD_XE ? PL_BRD_XS : x + 2;
|
||||
break;
|
||||
default:
|
||||
printf("ERROR in func mvSnakeParts\n");
|
||||
printf("dir: %c\n", part->dir);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
part->x = x;
|
||||
part->y = y;
|
||||
|
||||
board[y][x] = part->visChar;
|
||||
|
||||
if (part->next == NULL)
|
||||
return;
|
||||
|
||||
part = part->next;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user