a3
This commit is contained in:
		
							parent
							
								
									fb7f6cdc18
								
							
						
					
					
						commit
						380c0a5779
					
				
							
								
								
									
										134
									
								
								a3/main.c
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								a3/main.c
									
									
									
									
									
								
							| @ -1,108 +1,68 @@ | ||||
| #include <stdlib.h> | ||||
| #include <time.h> | ||||
| #include <unistd.h> | ||||
| #include <ncurses.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <unistd.h> | ||||
| #include "snake.h" | ||||
| #include "world.h" | ||||
| 
 | ||||
| #define HIGHSCORE_FILE "highscore.txt" | ||||
| void render(const struct state* state){ | ||||
|     system("clear"); | ||||
| 
 | ||||
| int read_highscore(){ | ||||
|     FILE* file = fopen(HIGHSCORE_FILE, "r"); | ||||
|     if (!file) return 0; | ||||
|     int score; | ||||
|     fscanf(file, "%d", &score); | ||||
|     fclose(file); | ||||
|     return score; | ||||
|     for(int y = 0; y < state->height; y++){ | ||||
|         for(int x = 0; x < state->width; x++){ | ||||
|             if(is_snake(state->snake, x, y)){ | ||||
|                 printf("O"); | ||||
|             }else if(is_food_at(state, x, y)){ | ||||
|                 printf("@"); | ||||
|             }else{ | ||||
|                 printf(" "); | ||||
|             } | ||||
|         } | ||||
|         printf("\n"); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void write_highscore(int score){ | ||||
|     FILE* file = fopen(HIGHSCORE_FILE, "w"); | ||||
|     if (file){ | ||||
|         fprintf(file, "%d\n", score); | ||||
|         fclose(file); | ||||
| int get_key(){ | ||||
|     int ch; | ||||
|     if (scanf("%c", &ch) != 1){ | ||||
|         return -1; | ||||
|     } | ||||
|     return ch; | ||||
| } | ||||
| 
 | ||||
| int main(){ | ||||
|     srand(time(NULL)); | ||||
|     initscr(); | ||||
|     start_color(); | ||||
|     init_pair(1, COLOR_GREEN, COLOR_BLACK); | ||||
|     init_pair(2, COLOR_RED, COLOR_BLACK); | ||||
|     cbreak(); | ||||
|     noecho(); | ||||
|     curs_set(0); | ||||
|     keypad(stdscr, TRUE); | ||||
|     nodelay(stdscr, TRUE); | ||||
| 
 | ||||
|     struct state game; | ||||
|     game.width = 40; | ||||
|     game.height = 20; | ||||
|     game.sx = 1; | ||||
|     game.sy = 0; | ||||
|     game.snake = add_snake(NULL, game.width / 2, game.height / 2); | ||||
|     place_food(&game); | ||||
| 
 | ||||
|     int score = 0; | ||||
|     int highscore = read_highscore(); | ||||
|     int running = 1; | ||||
| 
 | ||||
|     while(running){ | ||||
|         int ch = getch(); | ||||
|         switch(ch){ | ||||
|             case KEY_UP: | ||||
|                 if (game.sy != 1) { game.sx = 0; game.sy = -1; } | ||||
|                 break; | ||||
|             case KEY_DOWN: | ||||
|                 if (game.sy != -1) { game.sx = 0; game.sy = 1; } | ||||
|                 break; | ||||
|             case KEY_LEFT: | ||||
|                 if (game.sx != 1) { game.sx = -1; game.sy = 0; } | ||||
|                 break; | ||||
|             case KEY_RIGHT: | ||||
|                 if (game.sx != -1) { game.sx = 1; game.sy = 0; } | ||||
|                 break; | ||||
|             case 'q': | ||||
|                 running = 0; | ||||
|                 continue; | ||||
|         } | ||||
| 
 | ||||
|         int status = step_state(&game); | ||||
| 
 | ||||
|         if(status == END_CONTINUE || status == END_FOOD){ | ||||
|             struct snake* head = game.snake; | ||||
|             int length = 0; | ||||
|             while (head) { | ||||
|                 length++; | ||||
|                 head = head->next; | ||||
|             } | ||||
|             score = length - 1; | ||||
|         } | ||||
| 
 | ||||
|     init_game(&game); | ||||
|     int ch; | ||||
|     while(1){ | ||||
|         render(&game); | ||||
|         mvprintw(game.height, 0, "Score: %d   High Score: %d   (q to quit)", score, highscore); | ||||
|         refresh(); | ||||
|         ch = get_key(); | ||||
| 
 | ||||
|         if(status != END_CONTINUE){ | ||||
|             if (score > highscore) { | ||||
|                 write_highscore(score); | ||||
|                 mvprintw(game.height + 1, 0, "New High Score! %d", score); | ||||
|             }else{ | ||||
|                 mvprintw(game.height + 1, 0, "Game Over! Score: %d", score); | ||||
|             } | ||||
| 
 | ||||
|             mvprintw(game.height + 2, 0, "Press any key to exit..."); | ||||
|             nodelay(stdscr, FALSE); | ||||
|             getch(); | ||||
|         if(ch == 'q'){ | ||||
|             break; | ||||
|         } | ||||
| 
 | ||||
|         usleep(150000); | ||||
|         if(ch == 'w'){ | ||||
|             game.sy = -1; | ||||
|             game.sx = 0; | ||||
|         }else if(ch == 's'){ | ||||
|             game.sy = 1; | ||||
|             game.sx = 0; | ||||
|         }else if(ch == 'a'){ | ||||
|             game.sx = -1; | ||||
|             game.sy = 0; | ||||
|         }else if(ch == 'd'){ | ||||
|             game.sx = 1; | ||||
|             game.sy = 0; | ||||
|         } | ||||
| 
 | ||||
|         int result = step_state(&game); | ||||
|         if (result != END_CONTINUE) { | ||||
|             break; | ||||
|         } | ||||
| 
 | ||||
|         usleep(100000); | ||||
|     } | ||||
| 
 | ||||
|     endwin(); | ||||
|     free_snake(game.snake); | ||||
|     printf("Game Over\n"); | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user