Загрузить файлы в «a3»
This commit is contained in:
		
							parent
							
								
									40d250a832
								
							
						
					
					
						commit
						9653efcd07
					
				
							
								
								
									
										45
									
								
								a3/main.c
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								a3/main.c
									
									
									
									
									
								
							@ -25,8 +25,11 @@ void init_snake(struct event* world, struct state* st){
 | 
			
		||||
    int cx = st->width / 2;
 | 
			
		||||
    int cy = st->height / 2;
 | 
			
		||||
    st->snake = NULL;
 | 
			
		||||
    // Place snake horizontally, centered, away from borders
 | 
			
		||||
    int start_x = cx - 2;
 | 
			
		||||
    int y = cy;
 | 
			
		||||
    for (int i = 0; i < 5; i++) {
 | 
			
		||||
        st->snake = add_snake(st->snake, cx, cy + i); // vertical, head at top
 | 
			
		||||
        st->snake = add_snake(st->snake, start_x + i, y);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int placed = 0;
 | 
			
		||||
@ -40,7 +43,7 @@ void init_snake(struct event* world, struct state* st){
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Initial game vector
 | 
			
		||||
    // Initial game vector: right
 | 
			
		||||
    st->sx = 1;
 | 
			
		||||
    st->sy = 0;
 | 
			
		||||
}
 | 
			
		||||
@ -48,31 +51,45 @@ void init_snake(struct event* world, struct state* st){
 | 
			
		||||
// Step is called in a loop once in interval.
 | 
			
		||||
// It should modify the state and draw it.
 | 
			
		||||
int world_event(struct event* w,void* game){
 | 
			
		||||
    // Get state pointer
 | 
			
		||||
    static int game_over = 0;
 | 
			
		||||
    struct state* st = game;
 | 
			
		||||
    if (w->type == EVENT_START){
 | 
			
		||||
        // Called on beginning
 | 
			
		||||
        game_over = 0;
 | 
			
		||||
        init_snake(w,st);
 | 
			
		||||
    }
 | 
			
		||||
    if (game_over) {
 | 
			
		||||
        // Ignore all events except EVENT_START and EVENT_ESC
 | 
			
		||||
        if (w->type != EVENT_ESC && w->type != EVENT_START) {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Called on key press
 | 
			
		||||
    else if (w->type == EVENT_KEY){
 | 
			
		||||
        int key = w->key;
 | 
			
		||||
        // Modifies vector of snake movement
 | 
			
		||||
        if (key == KEY_RIGHT){
 | 
			
		||||
           st->sx = 1;
 | 
			
		||||
           st->sy = 0;
 | 
			
		||||
            if (!(st->sx == -1 && st->sy == 0)) { // Prevent reverse
 | 
			
		||||
                st->sx = 1;
 | 
			
		||||
                st->sy = 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (key == KEY_LEFT){
 | 
			
		||||
            st->sx = -1;
 | 
			
		||||
            st->sy = 0;
 | 
			
		||||
            if (!(st->sx == 1 && st->sy == 0)) { // Prevent reverse
 | 
			
		||||
                st->sx = -1;
 | 
			
		||||
                st->sy = 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (key == KEY_DOWN){
 | 
			
		||||
            st->sx = 0;
 | 
			
		||||
            st->sy = 1;
 | 
			
		||||
            if (!(st->sx == 0 && st->sy == -1)) { // Prevent reverse
 | 
			
		||||
                st->sx = 0;
 | 
			
		||||
                st->sy = 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (key == KEY_UP){
 | 
			
		||||
            st->sx = 0;
 | 
			
		||||
            st->sy = -1;
 | 
			
		||||
            if (!(st->sx == 0 && st->sy == 1)) { // Prevent reverse
 | 
			
		||||
                st->sx = 0;
 | 
			
		||||
                st->sy = -1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // Called on esc key
 | 
			
		||||
@ -88,9 +105,6 @@ int world_event(struct event* w,void* game){
 | 
			
		||||
        st->height = w->height;
 | 
			
		||||
        // Change game state
 | 
			
		||||
        int r = step_state(st);
 | 
			
		||||
        char ms[200];
 | 
			
		||||
        sprintf(ms,"r %d\n",r);
 | 
			
		||||
        set_message(ms,9,9);
 | 
			
		||||
        // Draw snake
 | 
			
		||||
        struct snake* sn = st->snake;
 | 
			
		||||
        while (sn != NULL){
 | 
			
		||||
@ -107,6 +121,7 @@ int world_event(struct event* w,void* game){
 | 
			
		||||
        if (r){
 | 
			
		||||
            char message[] = "Koniec";
 | 
			
		||||
            set_message(message,10,10);
 | 
			
		||||
            game_over = 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
@ -2,7 +2,7 @@
 | 
			
		||||
#define snake_h_INCLUDED
 | 
			
		||||
 | 
			
		||||
// Number of food items on the plane
 | 
			
		||||
#define FOOD_COUNT 5
 | 
			
		||||
#define FOOD_COUNT 20
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * One part of the snake;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user