⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 project.c

📁 snake game in avr snake game in avr
💻 C
字号:
/*
 * FILE: project.c
 *
 * Written by Peter Sutton - October 2004
 */

#include "board.h"
#include "snake.h"
#include "display.h"
#include "serialio.h"
#include "terminalio.h"
#include "timer.h"
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

/*
** Function prototypes - these are defined below main()
*/
void new_game(void);
void splash_screen(void);
void handle_game_over(void);

/*
 * main -- Main program.
 */
int main(void) {
	char c;
    int8_t move_status;

	/* Because we receive an escape sequence character by character, we need 
	** to count how many characters into a sequence we are. If we get an 
	** unexpected character, we're back to the beginning
	*/
	int8_t chars_into_escape_sequence = 0;
	
	/*
	** Initialise the serial IO. (We disable echo - we don't want the keypresses
	** being sent back out.)
	*/
	init_serial_stdio(19200, 0);
	
	/*
	** Turn on interrupts (serial IO and the timer need this)
	*/
	sei();
	
	/*
	** Display splash screen 
	*/
	splash_screen();
	
	/*
	** Perform necessary initialisations for a new game.
	*/
	new_game();
		
	/*
	** Event loop - wait for a certain amount of time to pass 
    	** (depending on how we've initialised the timer) or wait
	** for a character to arrive from standard input.
	*/
	while(1) { /* forever */
		if(is_period_over()) {
           		 /* Attempt to move snake */
           		 move_status = move_snake();
            		/* Display the current score */
            		output_score();
            
            /* ADD CODE HERE TO adjust the clock period (i.e. speed up 
            ** game) based on the current score (e.g. if the score has
            ** passed some value(s)).
            */
		
            if(move_status < 0) {
                /* Snake ran into itself or edge - game over */
                handle_game_over();
            } else if (move_status == ATE_FOOD) {
                /* If the snake ate something - add some more food */
                add_food_items(1);
            }
        }
		
		if(input_available()) {
			c = fgetc(stdin);			
			if(chars_into_escape_sequence == 0 && c == '\x1b') {
				/*
				** Received ESCAPE character - we're one character into
				** an escape sequence
				*/
				chars_into_escape_sequence = 1;
			} else if(chars_into_escape_sequence == 1 && c == '[') {
				/* 
				** We're two characters into an escape sequence
				*/
				chars_into_escape_sequence = 2;
			} else if (chars_into_escape_sequence == 2 && c >= 'A' && c <= 'D') {
				/* 
				** Have received a cursor key escape sequence - process it if
				** we have a current piece - otherwise ignore it
				*/
				if(c == 'A') {
					/* Up cursor key pressed */
                    			set_snake_dirn(UP);
				} else if(c == 'B') {
                   			 /* Down cursor key pressed */
                    			set_snake_dirn(DOWN);
               			} else if(c == 'C') {
                    			/* Right cursor key pressed */
                   			 set_snake_dirn(RIGHT);
                		} else if(c == 'D') {
                    			/* Left cursor key pressed */
                   			 set_snake_dirn(LEFT);
               		}
			chars_into_escape_sequence = 0; 
			} else if(chars_into_escape_sequence) {
				/*
				** We started an escape sequence but didn't get a character
				** we recognised - discard it and assume that we're not
				** in an escape sequence
				*/
				chars_into_escape_sequence = 0;
			} else {            
				/*
				** Some other character received. Handle it (or ignore it).
				*/
								
				/*
				** YOUR CODE HERE TO CHECK FOR OTHER KEY PRESSES
				** AND TAKE APPROPRIATE ACTION. You may need to 
				** add code in other locations also.
				*/
			}
		}
	}
}

void new_game(void) {
	/* 
	** Initialise the screen (clears the screen)
    	** and the board
	*/	
	init_screen();
    	init_board();
	
	/*
	** Set up our timer - we want the initial timer period to
    	** be 1 second (1000 milliseconds).
	*/
	init_timer(1000);
}

void splash_screen(void) {
	clear_terminal();

	/* YOUR CODE HERE */
	printf("Snake Game\n");
	printf("Name\n");
	init_timer(8000);
	/* 
	** Display some suitable message to the user that includes your name(s).
	** You may need to use terminalio functions to position the cursor appropriately.
	** When done, set the timer target for a suitable number of seconds and 
	** wait for the target to be reached. See the use of the timer in the 
	** main() and new_game() functions above.
	*/
}

void handle_game_over(void) {
	game_over_message();
	pause_timer();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -