Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Direction {
- dir_none,
- dir_left,
- dir_right,
- dir_up,
- dir_down,
- };
- enum CollisionType {
- coll_simple,
- coll_horiz,
- coll_vert,
- };
- struct log {
- int x,
- y,
- dir;
- };
- struct apple {
- int x,
- y;
- };
- struct player {
- int x,
- y,
- dir,
- energy,
- points,
- weird;
- };
- struct entity {
- int x,
- y,
- dir;
- };
- struct wall {
- int left,
- right,
- top,
- bottom;
- };
- void check_walls(void *v, wall w);
- int collision(player *p, void *v, int ctype, int length);
- void do_keys(player *p);
- void do_weird_thing(player *p, apple *a, log *logs);
- void draw_apple(apple *a);
- void draw_logs(log *logs);
- void draw_player(player *p);
- void game_loop();
- void game_over();
- void init_ncurses();
- void move_entity(void *v);
- void print_surround(void *v, char c);
- int rand_pos(int offset, int range);
- /*
- * Survive
- * original by: Wilson Perez <wperez274@gmail.com>
- * improved by: Braden Best <bradentbest@gmail.com>
- */
- #include <ncurses.h>
- #include <stdlib.h>
- #include <time.h>
- #include "survive.h"
- void
- check_walls(void *v, wall w)
- {
- entity *e = (entity *)v;
- if ( e->x < w.left )
- {
- e->x = w.left;
- e->dir = dir_right;
- }
- if ( e->x > w.right )
- {
- e->x = w.right;
- e->dir = dir_left;
- }
- if ( e->y < w.top )
- {
- e->y = w.top;
- e->dir = dir_down;
- }
- if(e->y > w.bottom){
- e->y = w.bottom;
- e->dir = dir_up;
- }
- }
- int
- collision(player *p, void *v, CollisionType ctype, int length)
- {
- entity *e = (entity *)v;
- switch(ctype){
- case coll_simple:
- return p->x == e->x && p->y == e->y;
- case coll_horiz:
- return p->x >= e->x && p->x <= e->x + length && p->y == e->y;
- case coll_vert:
- return p->y >= e->y && p->y <= e->y + length && p->x == e->x;
- }
- return false;
- }
- void
- do_keys( player *p )
- {
- int ch = getch();
- switch (ch)
- {
- case 'a': p->dir = dir_left; break;
- case 'd': p->dir = dir_right; break;
- case 'w': p->dir =
- dir_up; break;
- case 'z': p->dir =
- dir_down; break;
- case 'p':
- mvprintw(0, 0, "PAUSED");
- refresh();
- while (1)
- {
- ch = getch();
- if (ch == 'p')
- {
- break;
- }
- }
- break;
- case 'q': game_over();
- break;
- }
- }
- void
- do_weird_thing(player *p, apple *a, log *logs)
- {
- p->weird++;
- if (p->weird >= rand_pos(20, 20)){
- logs[0].dir = rand_pos(1, 3);
- logs[1].dir = rand_pos(1, 3);
- a->x = rand_pos(1, 29);
- a->y = rand_pos(1, 12);
- p->energy -= 2;
- p->weird = 0;
- }
- }
- void
- draw_apple(apple *a)
- {
- attrset(COLOR_PAIR(1));
- mvprintw(a->y, a->x, "@");
- attrset(COLOR_PAIR(0));
- }
- void
- draw_logs(log *logs)
- {
- int i;
- attrset(COLOR_PAIR(6));
- mvprintw(logs[0].y, logs[0].x, " ");
- for (i = 0; i < 5; i++)
- mvprintw(logs[1].y + i, logs[1].x, " ");
- attrset(COLOR_PAIR(0));
- }
- void
- draw_player(player *p)
- {
- attrset(COLOR_PAIR(1));
- mvprintw(p->y, p->x, "O");
- attrset(COLOR_PAIR(3));
- mvprintw(0, 0, "Health: %i", p->energy);
- mvprintw(0, 15, "Score: %i", p->points);
- attrset(COLOR_PAIR(0));
- }
- void
- game_loop()
- {
- log logs[2] = {
- { rand_pos(0, 22), rand_pos(0, 3), dir_none },
- { rand_pos(5, 22), rand_pos(2, 5), dir_none }
- };
- player p = { 4, 12, dir_none, 50, 0, 0 };
- apple a = {0};
- while (1)
- {
- do_keys(&p);
- attron(A_BOLD);
- erase();
- do_weird_thing(&p, &a, logs);
- move_entity(&p);
- move_entity(&logs[0]);
- move_entity(&logs[1]);
- check_walls(&logs[0], (const wall){ 0, 30, 1, 13 });
- check_walls(&logs[1], (const wall){ 0, 30, 1, 9 });
- check_walls(&p, (const wall){ 0, 29, 0, 13 });
- if (collision(&p, &a, coll_simple, 0))
- {
- print_surround(&p, 'o');
- p.energy += 2;
- p.points += 10;
- a.x = rand_pos(1, 29);
- a.y = rand_pos(1, 11);
- }
- if ( collision(&p, &logs[0], coll_horiz, 12) || collision(&p, &logs[1], coll_vert, 4) )
- {
- print_surround(&p, 'x');
- p.energy -= 10;
- }
- if ( p.energy<1 )
- {
- game_over();
- }
- draw_apple(&a);
- draw_player(&p);
- draw_logs(logs);
- }
- }
- void
- game_over()
- {
- endwin();
- printf("Game over\n");
- exit(0);
- }
- void
- init_ncurses()
- {
- initscr();
- noecho();
- curs_set(0);
- keypad(stdscr, TRUE);
- timeout(175);
- start_color();
- bkgd(COLOR_PAIR(1));
- init_pair(1,COLOR_BLUE, COLOR_WHITE);
- init_pair(2,COLOR_BLACK, COLOR_WHITE);
- init_pair(3,COLOR_CYAN, COLOR_BLACK);
- init_pair(4,COLOR_GREEN, COLOR_GREEN);
- init_pair(5,COLOR_RED, COLOR_WHITE);
- init_pair(6,COLOR_BLACK, COLOR_RED);
- }
- void
- move_entity(void *v)
- {
- entity *e = (entity *)v;
- switch ( e->dir ){
- case dir_left:
- e->x--; break;
- case dir_right:
- e->x++; break;
- case dir_up:
- e->y--; break;
- case dir_down:
- e->y++; break;
- }
- }
- void
- print_surround(void *v, char c)
- {
- entity *e = (entity *)v;
- mvaddch( e->y + -1, e->x + 0, c );
- mvaddch( e->y + 1, e->x + 0, c );
- mvaddch( e->y + 0, e->x + -1, c );
- mvaddch( e->y + 0, e->x + 1, c );
- }
- int
- rand_pos( int offset, int range )
- {
- return offset + rand() % range;
- }
- int
- main()
- {
- srand(time(0));
- init_ncurses();
- game_loop();
- game_over();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement