Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -- This code is: The Snake Game (v 0.11) on Linux Console!
- -- Copyright (C) 2011 Utroz - Access: http://Gcoders.wordpress.com
- --
- -- This program is free software: you can redistribute it and/or modify
- -- it under the terms of the GNU General Public License as published by
- -- the Free Software Foundation, either version 3 of the License, or
- -- (at your option) any later version.
- --
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warranty of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- You should have received a copy of the GNU General Public License
- -- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- -------
- /* The Snake Game (v 0.11) on Linux Console!
- * Copyright ©2011 - @uthor #Utroz(RsC)#.
- * File: main.c
- * Blog: http://Gcoders.wordpress.com/ (Access it)!
- */
- extern game_start();
- int main(void)
- {
- game_start();
- return(0);
- }
- -------
- /* The Snake Game (v 0.11) on Linux Console!
- * Copyright ©2011 - @uthor #Utroz(RsC)#.
- * File: global.h
- * Blog: http://Gcoders.wordpress.com/ (Access it)!
- */
- /* Key Cordenates */
- #define KEY_UP 'w'
- #define KEY_DOWN 's'
- #define KEY_LEFT 'a'
- #define KEY_RIGHT 'd'
- /* Map Cordenates */
- #define POS_X 0
- #define POS_Y 1
- /* Map Size */
- #define ROW 8
- #define COLUMN 12
- /* Map IDS */
- #define M 32 //Map ID
- #define S 42 //Snake ID
- #define A 64 //Apple ID
- /* Graphics */
- #define V 124 //Vertical Graphic
- #define H 126 //Horizontal Graphic
- /* Directions */
- #define WE 0 // West
- #define EA 1 // East
- #define NO 2 // North
- #define SO 3 // South
- /* Snake Status */
- #define LIVE 0; // Live
- #define DEAD 1; // Dead
- /* ID's Position
- Row: [0] / Column: [1] */
- int snake_pos[99][2];
- int apple_pos[2];
- #define DEFAULT_X 3
- #define DEFAULT_Y 5
- /* Snake ID's */
- #define HEAD 0
- /* Data Config */
- struct {
- /* Snake Body Count */
- int count; // (Start on: 0)
- /* Snake Status */
- int snake_status, body_found;
- /* Log Snake */
- char log[40];
- /* Snake Direction */
- int snake_direction;
- /* Data User */
- int game_level, points_user;
- } config;
- /* Game Level : Delay */
- #define N1 1.4
- #define N2 1.2
- #define N3 1.0
- /* Apple Points */
- #define POINT 10
- -------
- /* The Snake Game (v 0.11) on Linux Console!
- * Copyright ©2011 - @uthor #Utroz(RsC)#.
- * File: game.c
- * Blog: http://Gcoders.wordpress.com/ (Access it)!
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "global.h"
- extern int game_modules(void);
- extern void msg_log(char *args);
- void move_modules(void);
- int snake_move(void);
- void set_pos(void);
- void remove_pos(void);
- void remake_body(void);
- void create_body(void);
- int check_status(void);
- int check_body(void);
- int check_wall(void);
- int check_apple(void);
- int create_apple(void);
- int snake_check = 0;
- /* Game Map */
- char map[ROW][COLUMN] = { //Column |
- { H, H, H, H, H, H, H, H, H, H, H, H }, //Row -
- { V, M, M, M, M, M, M, M, M, M, M, V },
- { V, M, M, M, M, M, M, M, M, M, M, V },
- { V, M, M, M, M, M, M, M, M, M, M, V },
- { V, M, M, M, M, M, M, M, M, M, M, V },
- { V, M, M, M, M, M, M, M, M, M, M, V },
- { V, M, M, M, M, M, M, M, M, M, M, V },
- { H, H, H, H, H, H, H, H, H, H, H, H },
- };
- /* Snake Move Modules */
- void move_modules()
- {
- /* Remove all Snake IDS on map */
- remove_pos();
- /* If snake head are in body don't walk */
- if(snake_check == 0) remake_body();
- /* Return snake condition */
- snake_check = snake_move();
- /* If snake head are in wall don't walk! */
- if(check_status())
- set_pos();
- }
- /* Snake Move */
- snake_move(void)
- {
- int i;
- /* Increment snake position. */
- switch(config.snake_direction)
- {
- case WE: // West.
- snake_pos[HEAD][POS_Y]--;
- if(check_body() == 1)
- {
- snake_pos[HEAD][POS_Y]++;
- return 1;
- }
- break;
- case EA: // East.
- snake_pos[HEAD][POS_Y]++;
- if(check_body() == 1)
- {
- snake_pos[HEAD][POS_Y]--;
- return 1;
- }
- break;
- case NO: // North.
- snake_pos[HEAD][POS_X]--;
- if(check_body() == 1)
- {
- snake_pos[HEAD][POS_X]++;
- return 1;
- }
- break;
- case SO: // South.
- snake_pos[HEAD][POS_X]++;
- if(check_body() == 1)
- {
- snake_pos[HEAD][POS_X]--;
- return 1;
- }
- break;
- case '1': exit(0);
- }
- return 0;
- }
- /* Set Pos on map. */
- void set_pos(void)
- {
- int i;
- for(i = config.count; i >= 0; i--)
- map[snake_pos[i][POS_X]][snake_pos[i][POS_Y]] = S; // Snake ID
- }
- /* Remove pos on map */
- void remove_pos(void)
- {
- int i;
- for(i = config.count; i >= 0; i--)
- map[snake_pos[i][POS_X]][snake_pos[i][POS_Y]] = M; // Map ID
- }
- /* Remake all body parts */
- void remake_body(void)
- {
- int i;
- for(i = config.count; i > 0; i--)
- {
- snake_pos[i][POS_X] = snake_pos[i-1][POS_X];
- snake_pos[i][POS_Y] = snake_pos[i-1][POS_Y];
- }
- }
- /* Create a Body */
- void create_body(void)
- {
- ++config.count;
- snake_pos[config.count][POS_X] = snake_pos[config.count-1][POS_X];
- snake_pos[config.count][POS_Y] = snake_pos[config.count-1][POS_Y];
- }
- /* Check Snake Status */
- check_status(void)
- {
- if(check_apple() == 1)
- {
- config.points_user += POINT;
- strcpy(config.log, "Apple eat!");
- create_apple();
- create_body();
- return 1;
- }
- if(check_wall() == 1)
- {
- strcpy(config.log, "You are dead!");
- config.snake_status = DEAD;
- return 0;
- }
- return 1;
- }
- check_body(void)
- {
- int i;
- for(i = config.count; i > 0; i--)
- {
- if (snake_pos[HEAD][POS_X] == snake_pos[i][POS_X])
- if(snake_pos[HEAD][POS_Y] == snake_pos[i][POS_Y])
- {
- config.body_found = 1;
- strcpy(config.log, "Body Found!");
- return 1;
- }
- }
- return 0;
- }
- check_wall(void)
- {
- // Check if snake are in wall.
- return map[snake_pos[HEAD][POS_X]][snake_pos[HEAD][POS_Y]] != M ? 1 : 0;
- }
- check_apple(void)
- {
- // Check if snake apple eat.
- return map[snake_pos[HEAD][POS_X]][snake_pos[HEAD][POS_Y]] == A ? 1 : 0;
- }
- /* Create Apple on Map */
- create_apple(void)
- {
- srand(time(NULL));
- apple_pos[POS_X] = rand() % (ROW-2) + 1;
- apple_pos[POS_Y] = rand() % (COLUMN-2) + 1;
- // Check if apple position is equals to snake.
- int i;
- for(i = 0; i <= config.count; i++)
- {
- if(apple_pos[POS_X] == snake_pos[i][POS_X] && apple_pos[POS_Y] == snake_pos[i][POS_Y]) return create_apple();
- }
- map[apple_pos[POS_X]][apple_pos[POS_Y]] = A; // Set Apple ID on map.
- return (0);
- }
- -------
- /* The Snake Game (v 0.11) on Linux Console!
- * Copyright ©2011 - @uthor #Utroz(RsC)#.
- * File: functions.c
- * Blog: http://Gcoders.wordpress.com/ (Access it)!
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "global.h"
- extern void goto_apple(void);
- extern char map[ROW][COLUMN];
- extern void move_modules(void);
- extern void set_key(void);
- void game_start(void);
- void player_data(void);
- int game_modules(void);
- void msg_log(char *args);
- void clean_body(void);
- void delay_time(int seconds);
- void clean_console(void);
- void show_game(void);
- void clean_console(void);
- void remove_garbage(void);
- void game_exit(void);
- const char game_name[] = ("The Snake Game (v0.11) on Linux Console!"),
- author[] = (":: By Utroz(RSC)::");
- /* Game Start */
- void game_start(void)
- {
- clean_console();
- player_data();
- printf("\n\t\t%s\n\t\t\t%s\n", game_name, author);
- delay_time(2);
- while(1)
- {
- /* Check if snake is dead */
- if(config.snake_status == 1) game_exit();
- game_modules();
- printf("\n");
- }
- }
- /* Reset Player Data */
- void player_data(void)
- {
- config.snake_status = LIVE;
- snake_pos[HEAD][POS_X] = DEFAULT_X;
- snake_pos[HEAD][POS_Y] = DEFAULT_Y;
- create_apple();
- clean_body();
- config.count = 0;
- config.points_user = 0;
- config.snake_direction = WE;
- config.game_level = N3;
- }
- /* Run all modules of game */
- game_modules(void)
- {
- clean_console();
- goto_apple();
- move_modules();
- show_game();
- //set_key();
- delay_time(config.game_level); // Nivel.
- return (0);
- }
- /* Show Logs in Game Screen */
- void msg_log(char *args)
- {
- printf("\t\t\t:: Last Log: %s ::\n", args);
- strcpy(config.log, "-");
- }
- /* Clean all body parts in the map */
- void clean_body(void)
- {
- int i;
- for(i = 1; i <= config.count; i++)
- {
- snake_pos[i][POS_X] = 0;
- snake_pos[i][POS_Y] = 0;
- }
- }
- /* Delay time in snake movement */
- void delay_time(int seconds)
- {
- // sleep(seconds);
- usleep(seconds * 1000000);
- }
- /* Clean the game image in console */
- void clean_console(void)
- {
- printf("\e[H\e[2J");
- }
- /* Return the direction in console */
- char *direct_msg(void)
- {
- if(config.snake_direction == 0) return "West";
- else if(config.snake_direction == 1) return "East";
- else if(config.snake_direction == 2) return "North";
- else return "South";
- return NULL;
- }
- /* Show Screen of Game. */
- void show_game(void)
- {
- int level;
- register int i, j;
- char direction[2];
- printf("\n\t\t%s\n\t\t\t%s\n", game_name, author);
- //if(config.snake_direction == 0) direction
- if(config.game_level == N3) level = 3;
- else if(config.game_level == N2) level = 2;
- else level = 1;
- printf("\n\t\t\tLevel: %d | Points: %d\n\n", level, config.points_user);
- for(i = 0; i < ROW; i++)
- {
- printf("\t\t%c%c%c%c%c", M, M, M, M, M);
- for(j = 0; j < COLUMN; j++) printf(" %c",map[i][j]);
- printf("\n");
- }
- msg_log(config.log);
- printf("\t\tApple Count: %d / Snake Direction: %s\n", config.count, direct_msg());
- printf("\n\t\tPlease access: http://GCoders.wordpress.com\n");
- }
- /* Remove Garbage by the map */
- void remove_garbage(void)
- {
- register int i, j;
- for(i = 1; i < ROW-1; i++)
- {
- for(j = 1; j < COLUMN-1; j++)
- {
- if(map[i][j] != M) map[i][j] = M;
- }
- }
- }
- /* Snake Dead: game exit */
- void game_exit(void)
- {
- char choose;
- printf("\t\t\t:::::::::::::::::::\n");
- printf("\t\t\t:: You are dead! ::\n");
- printf("\t\t\t:::::::::::::::::::\n");
- delay_time(1);
- while(1)
- {
- printf("\n\t\tYou wanna play other time? (y/n):");
- scanf("%c", &choose);
- fflush(stdin);
- switch(choose)
- {
- case 'y':
- remove_garbage();
- player_data();
- game_start();
- break;
- case 'n':
- printf("\n\t\tThanks for play the game!\n");
- delay_time(1);
- clean_console();
- exit(1);
- break;
- default:
- printf("\n\t\tPlease input a valid option! (y/n)\n");
- break;
- }
- }
- }
- -------
- /* The Snake Game (v 0.11) on Linux Console!
- * Copyright ©2011 - @uthor #Utroz(RsC)#.
- * File: control.c
- * Blog: http://Gcoders.wordpress.com/ (Access it)!
- */
- #include <stdio.h>
- #include "global.h"
- /* Choose a snake direction */
- void set_key(void)
- {
- char key, ch;
- ch = getchar();
- key = ch;
- fflush(stdin);
- switch(key)
- {
- case KEY_UP:
- config.snake_direction = NO;
- break;
- case KEY_DOWN:
- config.snake_direction = SO;
- break;
- case KEY_LEFT:
- config.snake_direction = WE;
- break;
- case KEY_RIGHT:
- config.snake_direction = EA;
- break;
- }
- if(key != KEY_UP && key != KEY_DOWN && key != KEY_LEFT && key != KEY_RIGHT) set_key();
- key = 0;
- }
- -------
- /* The Snake Game (v 0.11) on Linux Console!
- * Copyright ©2011 - @uthor #Utroz(RsC)#.
- * File: ia.c
- * Blog: http://Gcoders.wordpress.com/ (Access it)!
- */
- #include "global.h"
- #include <stdlib.h>
- extern char map[ROW][COLUMN];
- /* Snake Head Pos */
- int h_pos_x, h_pos_y;
- void head_pos(void)
- {
- h_pos_x = snake_pos[HEAD][POS_X];
- h_pos_y = snake_pos[HEAD][POS_Y];
- }
- /* Make a function to don't found body! */
- int goto_apple(void)
- {
- head_pos();
- if(config.body_found == 1)
- {
- srand(time(NULL));
- /* Select a random direction */
- config.snake_direction = rand() % 4;
- config.body_found = 0;
- return 0;
- }
- /* Snake pos is below line of Apple Pos */
- if(h_pos_x > apple_pos[POS_X])
- {
- if(map[(h_pos_x) -1][h_pos_y] != M && map[(h_pos_x) -1][h_pos_y] != A)
- if(h_pos_y > apple_pos[POS_Y])
- config.snake_direction = WE;
- else
- config.snake_direction = EA;
- else config.snake_direction = NO;
- }
- /* Snake pos is above line of Apple Pos */
- else if (h_pos_x < apple_pos[POS_X])
- {
- if(map[(h_pos_x) +1][h_pos_y] != M && map[(h_pos_x) +1][h_pos_y] != A)
- if(h_pos_y > apple_pos[POS_Y])
- config.snake_direction = WE;
- else
- config.snake_direction = EA;
- else config.snake_direction = SO;
- }
- /* Snake pos is equals line of Apple Pos */
- else if (h_pos_x == apple_pos[POS_X])
- {
- /* Apple pos are to left from Snake. */
- if(h_pos_y > apple_pos[POS_Y])
- {
- if(map[h_pos_x][(h_pos_y) -1] != M && map[h_pos_x][(h_pos_y) -1] != A)
- if(map[(h_pos_x) -1][h_pos_y] == V || map[(h_pos_x) -1][h_pos_y] == H)
- config.snake_direction = SO;
- else
- config.snake_direction = NO;
- else config.snake_direction = WE;
- }
- /* Apple pos are to right from Snake. */
- else if(h_pos_y < apple_pos[POS_Y])
- {
- if(map[h_pos_x][(h_pos_y) +1] != M && map[h_pos_x][(h_pos_y) +1] != A)
- if(map[(h_pos_x) -1][h_pos_y] == V || map[(h_pos_x) -1][h_pos_y] == H)
- config.snake_direction = SO;
- else
- config.snake_direction = NO;
- else config.snake_direction = EA;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement