Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <termios.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h> //usleep
- //#include <curses.h> //for kbhit and getch
- #define ANSI_COLOR_BLACK "\x1b[30m"
- #define ANSI_COLOR_RED "\x1b[31m"
- #define ANSI_COLOR_GREEN "\x1b[32m"
- #define ANSI_COLOR_BLUE "\x1b[34m"
- #define ANSI_COLOR_MAGENTA "\x1b[35m"
- #define ANSI_COLOR_CYAN "\x1b[36m"
- #define ANSI_COLOR_RESET "\x1b[0m"
- #define clear_console() printf("\033[H\033[J")
- #define input_without_enter() system ("/bin/stty raw")//get input without pressing enter
- #define input_usual() system ("/bin/stty cooked");//usual input mode
- #define true 1
- #define false 0
- #define LEFT 0
- #define RIGHT 1
- #define FORWARD 2
- #define NONE 2
- // '#' - frog, 'r' - right car, 'l' - left car ,'*' - empty
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/select.h>
- #include <termios.h>
- const char numbers_arr[10][5][3]= {
- { //0
- {'o','o','o'},
- {'o','*','o'},
- {'o','*','o'},
- {'o','*','o'},
- {'o','o','o'}
- },
- { //1
- {'*','*','o'},
- {'*','o','o'},
- {'*','*','o'},
- {'*','*','o'},
- {'*','*','o'}
- },
- { //2
- {'o','o','o'},
- {'*','*','o'},
- {'*','o','*'},
- {'o','*','*'},
- {'o','o','o'}
- },
- { //3
- {'o','o','o'},
- {'*','*','o'},
- {'o','o','o'},
- {'*','*','o'},
- {'o','o','o'}
- },
- { //4
- {'o','*','o'},
- {'o','*','o'},
- {'o','o','o'},
- {'*','*','o'},
- {'*','*','o'}
- },
- { //5
- {'o','o','o'},
- {'o','*','*'},
- {'o','o','o'},
- {'*','*','o'},
- {'o','o','o'}
- },
- { //6
- {'o','o','o'},
- {'o','*','*'},
- {'o','o','o'},
- {'o','*','o'},
- {'o','o','o'}
- },
- { //7
- {'o','o','o'},
- {'*','*','o'},
- {'*','o','*'},
- {'*','o','*'},
- {'*','o','*'}
- },
- { //8
- {'o','o','o'},
- {'o','*','o'},
- {'o','o','o'},
- {'o','*','o'},
- {'o','o','o'}
- },
- { //9
- {'o','o','o'},
- {'o','*','o'},
- {'o','o','o'},
- {'*','*','o'},
- {'o','o','o'}
- }
- };
- struct termios orig_termios;
- void reset_terminal_mode()
- {
- tcsetattr(0, TCSANOW, &orig_termios);
- }
- void set_conio_terminal_mode()
- {
- struct termios new_termios;
- /* take two copies - one for now, one for later */
- tcgetattr(0, &orig_termios);
- memcpy(&new_termios, &orig_termios, sizeof(new_termios));
- /* register cleanup handler, and set the new terminal mode */
- atexit(reset_terminal_mode);
- cfmakeraw(&new_termios);
- tcsetattr(0, TCSANOW, &new_termios);
- }
- int kbhit()
- {
- struct timeval tv = { 0L, 0L };
- fd_set fds;
- FD_ZERO(&fds);
- FD_SET(0, &fds);
- return select(1, &fds, NULL, NULL, &tv);
- }
- int getch()
- {
- int r;
- unsigned char c;
- if ((r = read(0, &c, sizeof(c))) < 0) {
- return r;
- } else {
- return c;
- }
- }
- struct frog_pos {
- int row;//from 0 to 7
- int col;//from 0 to 15
- };
- struct frog_pos frog_pos;
- int is_shifting=false;
- int game_arr[8][16];
- int score=0;
- void set_num(int number,int pos) {//pos = {1,2,3,4}
- int start_col=(pos-1)*(4);
- if(pos>2)start_col+=1;
- for(int row=1; row<6; row++) {
- for(int col=start_col; col<start_col+3; col++) {//every number's width is 4 pixels
- game_arr[row][col]=numbers_arr[number][row-1][col-start_col];
- }
- }
- }
- void generate_cars(int row) { //generate car (direction, start_pos(!),length(!))
- //none?
- int direction=rand() % 2;
- int length=1+(rand() % 2);
- int start_pos=rand() % (16-length+1);//-length
- for(int i=0;i<length;i++){
- if(direction==LEFT) {//<--
- game_arr[row][start_pos+i]='l';
- }
- else if (direction==RIGHT) {//-->
- game_arr[row][start_pos+i]='r';
- }
- }
- }
- void init_game() {
- for(int row=0; row<8; row++) {//init game_arr
- for(int col=0; col<16; col++) {
- game_arr[row][col]= '*';
- }
- if(row!=0&&row!=7) {
- generate_cars(row);
- }
- }
- game_arr[7][8]= '#';//frog starting position
- frog_pos.row=7;
- frog_pos.col=8;
- score=0;
- }
- void output_arr() {
- reset_terminal_mode();
- clear_console();
- for(int row=0; row<8; row++) {
- for(int col=0; col<16; col++) {
- if(game_arr[row][col]=='#') {
- printf("%s%c%s ", ANSI_COLOR_GREEN,'#',ANSI_COLOR_RESET);
- }
- /*else if(game_arr[row][col]=='l'||game_arr[row][col]=='r') {
- printf("%s%c%s ", ANSI_COLOR_RED,'o',ANSI_COLOR_RESET);
- }*/
- else if(game_arr[row][col]=='o') {
- printf("%s%c%s ", ANSI_COLOR_RED,'#',ANSI_COLOR_RESET);
- }
- else if(game_arr[row][col]=='l') {
- printf("%s%c%s ", ANSI_COLOR_RED,'#',ANSI_COLOR_RESET);
- }
- else if(game_arr[row][col]=='r') {
- printf("%s%c%s ", ANSI_COLOR_RED,'#',ANSI_COLOR_RESET);
- }
- else {
- printf("%s%c%s ", ANSI_COLOR_BLUE,'*',ANSI_COLOR_RESET);
- // printf("%c ",time_arr[row][col]);
- }
- }
- printf("\n");
- }
- set_conio_terminal_mode();
- }
- void display_score(){
- set_num((score/1000)%10,1);
- set_num((score/100)%10,2);
- set_num((score/10)%10,3);
- set_num(score%10,4);
- output_arr();
- char wait =getchar();
- wait=wait;
- }
- void clear_arr() {
- for(int row=0; row<8; row++) {//init game_arr
- for(int col=0; col<16; col++) {
- game_arr[row][col]= '*';
- }
- }
- }
- void reset_arr() {
- usleep(200*1000);
- clear_arr();
- display_score();
- clear_arr();
- init_game();
- }
- void reset_win_arr() {//todo add win msg
- clear_arr();
- display_score();
- clear_arr();
- init_game();
- }
- void car_move_n_check_lose(int direction,int row,int col) {
- int move_col;
- if(direction==LEFT) {
- if(col==0) {
- move_col=15;
- }
- else {
- move_col=col-1;
- }
- }
- if(direction==RIGHT) {
- if(col==15) {
- move_col=0;
- }
- else {
- move_col=col+1;
- }
- }
- if(game_arr[row][move_col]=='#') { //lose
- reset_arr();
- }
- else {
- char temp=game_arr[row][col];
- game_arr[row][col]=game_arr[row][move_col];
- game_arr[row][move_col]=temp;
- }
- }
- void update_arr() {
- for(int row=0; row<8; row++) {
- for(int col=0; col<16; col++) {
- if(game_arr[row][col]=='r'){break;}//time economy
- if(game_arr[row][col]=='l') { //<--
- car_move_n_check_lose(LEFT,row,col);
- }
- }
- for(int col=15; col>=0; col--) {
- if(game_arr[row][col]=='l'){break;}//time economy
- if(game_arr[row][col]=='r') { //<--
- car_move_n_check_lose(RIGHT,row,col);
- }
- }
- }
- }
- void shift_arr() { //shift array down and then generate new cars
- //is_shifting=true;
- output_arr();
- for(int i=0; i<7; i++) { //shift array 7 times down
- for(int row=7; row>=0; row--) {//init game_arr
- for(int col=0; col<16; col++) {
- if(row==0) { //generate last empty row
- game_arr[row][col]='*';
- }
- else {
- game_arr[row][col]=game_arr[row-1][col];
- }
- }
- if(row==0&&i!=6) { //generate new cars
- generate_cars(row);
- }
- }
- usleep(200*1000);//microseconds (1000 times smaller than milliseconds )
- output_arr();
- }
- //is_shifting=false;
- }
- int frog_is_lose(int direction) {
- switch(direction) {
- case LEFT:
- if(game_arr[frog_pos.row][frog_pos.col-1]=='r'||game_arr[frog_pos.row][frog_pos.col-1]=='l') {
- return true;
- }
- return false;
- case RIGHT:
- if(game_arr[frog_pos.row][frog_pos.col+1]=='r'||game_arr[frog_pos.row][frog_pos.col+1]=='l') {
- return true;
- }
- return false;
- case FORWARD:
- if(game_arr[frog_pos.row-1][frog_pos.col]=='r'||game_arr[frog_pos.row-1][frog_pos.col]=='l') {
- return true;
- }
- return false;
- }
- return false;
- }
- void move_forward() { //move along row
- //shift arr after moving forward
- if(frog_pos.row>0) {
- if(frog_is_lose(FORWARD)==true) { //lose
- reset_arr();
- }
- else {
- char temp=game_arr[frog_pos.row][frog_pos.col];
- game_arr[frog_pos.row][frog_pos.col]=game_arr[frog_pos.row-1][frog_pos.col];
- game_arr[frog_pos.row-1][frog_pos.col]=temp;
- frog_pos.row--;
- score++;
- if(score==9999){//WIN MSG
- reset_win_arr();
- }
- }
- }
- if(frog_pos.row==0) { //shifting array to new location
- shift_arr();
- frog_pos.row=7;
- }
- }
- void move_left() {//move along col (--)
- if(frog_pos.col>0) {
- if(frog_is_lose(LEFT)==true) { //lose
- reset_arr();
- }
- else {
- char temp=game_arr[frog_pos.row][frog_pos.col];
- game_arr[frog_pos.row][frog_pos.col]=game_arr[frog_pos.row][frog_pos.col-1];
- game_arr[frog_pos.row][frog_pos.col-1]=temp;
- frog_pos.col--;
- }
- }
- }
- void move_right() {//move along col (++)
- if(frog_pos.col<15) {
- if(frog_is_lose(RIGHT)==true) { //lose
- reset_arr();
- }
- else {
- char temp=game_arr[frog_pos.row][frog_pos.col];
- game_arr[frog_pos.row][frog_pos.col]=game_arr[frog_pos.row][frog_pos.col+1];
- game_arr[frog_pos.row][frog_pos.col+1]=temp;
- frog_pos.col++;
- }
- }
- }
- /*void get_pressed_key() {
- input_without_enter();
- char key = getchar();
- input_usual();
- switch(key) {
- case 'w':
- move_forward();
- break;
- case 'a':
- move_left();
- break;
- case 'd':
- move_right();
- break;
- }
- output_arr();
- }*/
- void get_pressed_key() {
- char key = getch();
- switch(key) {
- case 'w':
- move_forward();
- break;
- case 'a':
- move_left();
- break;
- case 'd':
- move_right();
- break;
- }
- output_arr();
- }
- int main(int argc, char **argv)
- {
- srand(time(NULL)); //init rand
- set_conio_terminal_mode();
- init_game();
- output_arr();
- while(1) {
- while (!kbhit()) {//no click
- update_arr();
- usleep(200*1000);
- output_arr();
- }
- get_pressed_key();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement