Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- */ https://www.facebook.com/fysl.fyslm
- */ ked ans | eVilscript | bresco_dz |mosta_team|hacker-fire | hacker_1420| elite-trojen &aLl algerian |
- */ oroginal by : plewh
- ~~ all resv to plewn :D i just share LOOl :p
- #include <ncurses.h>
- #include <stdlib.h>
- #include <time.h>
- #define SLEEPTIME 125000
- #define SCREENWIDTH 60
- #define SCREENHEIGHT 20
- #define MAXSNAKELENGTH 200
- struct coords
- {
- int x;
- int y;
- };
- struct snake
- {
- int alive;
- int length;
- struct coords segment[MAXSNAKELENGTH];
- };
- void initObjects(struct snake* playa, struct coords* froot)
- {
- /* Sets initial state of snake object and fruit object
- */
- // INIT SNAKE
- int j;
- playa->alive = 1;
- playa->length = 5;
- for(j = 0; j < MAXSNAKELENGTH; ++j)
- {
- if(j < playa->length)
- {
- playa->segment[j].x = 30;
- playa->segment[j].y = (j + 10);
- }
- else
- {
- playa->segment[j].x = 0;
- playa->segment[j].y = 0;
- }
- }
- // INIT FRUIT
- froot->x = 30;
- froot->y = 5;
- }
- void drawHUD(int screenWidth, int screenHeight, int score)
- {
- /* Draws the pretty border around the play field and the score.
- * Maybe stats later (can snake have stats?)
- */
- int j;
- for(j = 0; j < screenWidth; ++j)
- {
- mvaddch(1, j, '-');
- mvaddch(screenHeight, j, '-');
- }
- for(j = 1; j <= screenHeight; ++j)
- {
- mvaddch(j, 0, '|');
- mvaddch(j, screenWidth, '|');
- }
- mvprintw(0, 0, "SCORE %d", score);
- }
- int getInput(int direction)
- {
- /* Pressing keys will buffer the input. getch() will take the character in the
- * buffer and place it in keyTemp. Because nodelay() is true, if no key is
- * pressed buffer will be NULL. If buffer is NULL or input is not one of the
- * macros in the switch, or player attempts to move the snake in the opposite
- * direction of travel (back into itself), snake continues in current direction.
- */
- int bufferedKey = getch();
- int temp = 0;
- switch(bufferedKey)
- {
- case KEY_UP:
- temp = 0;
- break;
- case KEY_RIGHT:
- temp = 1;
- break;
- case KEY_DOWN:
- temp = 2;
- break;
- case KEY_LEFT:
- temp = 3;
- break;
- default:
- temp = direction;
- break;
- }
- if(temp == 0 && direction == 2)
- return direction;
- if(temp == 1 && direction == 3)
- return direction;
- if(temp == 2 && direction == 0)
- return direction;
- if(temp == 3 && direction == 1)
- return direction;
- return temp;
- }
- void moveSnake(struct snake* player, int direction)
- {
- /* Not much to comment on, code speaks for itself really.
- */
- int j;
- for(j = player->length; j > 0; --j)
- {
- player->segment[j] = player->segment[j - 1];
- }
- // Figure out where to move head
- switch(direction)
- {
- // UP
- case 0:
- player->segment[0].y--;
- break;
- // RIGHT
- case 1:
- player->segment[0].x++;
- break;
- // DOWN
- case 2:
- player->segment[0].y++;
- break;
- // LEFT
- case 3:
- player->segment[0].x--;
- break;
- }
- }
- int fruitEaten(struct snake* player, struct coords* fruit)
- {
- /* Return true if head and fruit share a space.
- */
- if(player->segment[0].y == fruit->y && player->segment[0].x == fruit->x)
- return 1;
- return 0;
- }
- void moveFruit(struct coords* fruit)
- {
- /* Called if fruit gets eaten. Uses rand() from stdlib. Modulo sets MAX_RAND.
- * Cases where fruit appears where it shouldn't are caught with the loop and
- * re-rolled until they are acceptable.
- */
- fruit->x = rand() % SCREENWIDTH;
- fruit->y = rand() % SCREENHEIGHT;
- while(fruit->x < 1 || fruit->y < 2)
- {
- fruit->x = rand() % SCREENWIDTH;
- fruit->y = rand() % SCREENHEIGHT;
- }
- }
- int playerDead(struct snake* player)
- {
- /* If player hits a wall or hits any part of itself, return true.
- */
- /*** WALLS ***/
- if(player->segment[0].y == SCREENHEIGHT || player->segment[0].y == 1)
- return 1;
- if(player->segment[0].x == SCREENWIDTH || player->segment[0].x == 0)
- return 1;
- /*** SELF ***/
- int j;
- for(j = 1; j < player->length; ++j)
- {
- if(player->segment[0].x == player->segment[j].x && player->segment[0].y == player->segment[j].y)
- return 1;
- }
- return 0;
- }
- void drawObjects(struct snake* player, struct coords* fruit)
- {
- /* Draws dynamic objects (snake and fruit) on screen.
- */
- /*** SNAKE ***/
- int j;
- for(j = 0; j < player->length; j++)
- {
- if(j == 0) // Draw head
- mvaddch(player->segment[j].y, player->segment[j].x, 'S');
- else // Draw body
- mvaddch(player->segment[j].y, player->segment[j].x, 'O');
- }
- /*** FRUIT ***/
- mvaddch(fruit->y, fruit->x, 'F');
- }
- int main(void)
- {
- /*** Create initial variables ***/
- int score = 0;
- int direction = 0;
- struct snake player;
- struct coords fruit;
- /*** Perform inits ***/
- srand(time(NULL)); // Seed timer with time since system boot
- initObjects(&player, &fruit);
- initscr(); // Created stdscr, a blank canvas that covers the screen.
- /*** ncurses behaviour toggles ***/
- cbreak(); // don't buffer key presses, instead make them available ASAP
- curs_set(0); // don't draw cursor
- nodelay(stdscr, TRUE); // don't wait for input when getch() is called.
- // Return ERR if nothing in buffer. Allows game
- // to loop with no input :D
- keypad(stdscr, TRUE); // replace keyboard input with ncurses macros for
- // arrow keys
- /*** GAME LOOP ***/
- while(player.alive)
- {
- clear();
- drawHUD(SCREENWIDTH, SCREENHEIGHT, score);
- direction = getInput(direction);
- moveSnake(&player, direction);
- if(fruitEaten(&player, &fruit) == 1)
- {
- moveFruit(&fruit);
- ++score;
- ++player.length;
- }
- if(playerDead(&player) == 1)
- player.alive = 0;
- drawObjects(&player, &fruit);
- refresh();
- usleep(SLEEPTIME); // prevents game running too fast
- }
- endwin(); // tell ncurses to clean up after itself.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment