示例#1
0
文件: main.c 项目: nicball/playground
void gameloop(int nrow, int ncol) {
    timeout(1000 / SAMPLING_RATE);
    const clock_t checkpoint = (clock_t) (CLOCKS_PER_SEC/GAME_SPEED);
    clock_t last_update = clock();
    struct snake * snake = new_snake(ncol/2, nrow/2);
    struct point food_pos = generate_food(nrow, ncol, snake);
    redraw(snake, food_pos);
    for (;;) {
        struct point tail_pos = snake->tail->pos;
        int ch;
        if ((ch = getch()) != ERR) {
            switch (ch) {
            case KEY_UP:
            case KEY_DOWN:
            case KEY_LEFT:
            case KEY_RIGHT:
                if (to_dir(ch) == reverse_dir(snake->heading)) {
                    break;
                }
                else {
                    snake->heading = to_dir(ch);
                    step_snake(snake);
                    tail_pos = snake->tail->pos;
                    redraw(snake, food_pos);
                }
                break;
            default:
                break;
            }
        }
        if (clock() - last_update >= checkpoint) {
            step_snake(snake);
            tail_pos = snake->tail->pos;
            redraw(snake, food_pos);
            last_update = clock();
        }
        if (point_equal(snake->head->pos, food_pos)) {
            grow_snake(snake, tail_pos);
            food_pos = generate_food(nrow, ncol, snake);
            redraw(snake, food_pos);
        }
        if (out_of_border(snake->head->pos, nrow, ncol)
                || eat_self(snake)) {
            display_lose(nrow, ncol);
            return;
        }
        if (snake->length == (nrow-2)*(ncol-2)) {
            display_win(nrow, ncol);
            return;
        }
    }
}
示例#2
0
void GameSnake::run()
{
	snake.set_pos(Point2D(5, 5));
	map.set_size(20, 20);
	snake.set_size(5);
	this->generate_food();
	this->speed = 300;
	while(snake.is_live())
	{
		this->update_view();
		this->control_snake();
		if ( *snake.get_m_snake().begin() == this->pos_food)
		{
			generate_food();
			snake.eat();
		}
		else
			snake.move();
		this->draw();
		Sleep(this->speed);

	}
	this->update_view();
	cout <<"\n\n\n\n"<<endl;
	cout << " GAME OVER" << endl;
	cout << " Your Score: "<< snake.get_m_snake().size() << endl;
	cout <<endl;
}
示例#3
0
文件: main.c 项目: nicball/playground
struct point generate_food(int nrow, int ncol, struct snake * s) {
    struct point p;
    p.x = rand() % (ncol-2) + 1;
    p.y = rand() % (nrow-2) + 1;
    for (struct bone * b = s->tail;
            b != NULL;
            b = b->next) {
        if (point_equal(b->pos, p)) {
            return generate_food(nrow, ncol, s);
        }
    }
    return p;
}
示例#4
0
int main() {
    self[0] = 0b00000100;
    self[1] = 0b00000100;

    self[2] = 0b00000011;
    self[3] = 0b00000100;

    self[4] = 0b00000010;
    self[5] = 0b00000100;
   /* 
    self[6] = 0b00000001;
    self[7] = 0b00000100;
    
    self[8] = 0b00000000;
    self[9] = 0b00000100;
    
    self[10] = 0b00000000;
    self[11] = 0b00000101;

    self[12] = 0b00000000;
    self[13] = 0b00000110;
*/

    generate_food();
    while(1) {
        check_control();
        move();
        update_fb();

        draw_food();
        draw();
        delay();
        undraw_food();
        draw();
        delay();
    }

    return 0;
}
示例#5
0
void move() {
//ldam [self]                   ; 2
//sta [new_head_x]             ; 2
//ldam [self+1]                 ; 2
//sta [new_head_y]             ; 2
//ldam [curent_direction]       ; 2
//ldbi DIRECTION_LEFT           ; 2
//jne L1                        ; 2
//ldam [new_head_x]             ; 2
//ldbi 1                        ; 2
//sub a                         ; 1
//sta [new_head_x]             ; 2
//jmp L10                       ; 2
//L1:                           ; 0
//ldam [curent_direction]       ; 2
//ldbi DIRECTION_RIGHT          ; 2
//jne L2                        ; 2
//ldam [new_head_x]             ; 2
//ldbi 1                        ; 2
//add a                         ; 1
//sta [new_head_x]             ; 2
//jmp L10                       ; 2
//L2:                           ; 0
//ldam [curent_direction]       ; 2
//ldbi DIRECTION_UP             ; 2
//jne L3                        ; 2
//ldam [new_head_y]             ; 2
//ldbi 1                        ; 2
//sub a                         ; 1
//sta [new_head_y]             ; 2
//jmp L10                       ; 2
//L3:                           ; 0
//ldam [curent_direction]       ; 2
//ldbi DIRECTION_DOWN           ; 2
//jne L4                        ; 2
//ldam [new_head_y]             ; 2
//ldbi 1                        ; 2
//add a                         ; 1
//sta [new_head_y]             ; 2
//jmp L10                       ; 2
//L4:                           ; 0
//ret                           ; 1
//L10:                   recheck; 0
//ldam [food_x]                 ; 2
//ldbm [new_head_x]             ; 2
//jne  L5                       ; 2
//ldam [food_y]                 ; 2
//ldbm [new_head_y]             ; 2
//jne  L5                       ; 2
//ldai 1                        ; 2
//sta [need_enlarge]           ; 2
//call [generate_food]          ; 2
//call [random]                 ; 2
//jmp  L10                      ; 2
//L5:                           ; 0
//ldam [new_head_x]             ; 2
//ldbm [new_head_y]             ; 2
//call is_valid                 ; 2
//ldbi 0                        ; 2
//jeq fail                      ; 2
//ldam [need_enlarge]           ; 2
//jeq L6                        ; 2
//stbm [need_enlarge]           ; 2
//ldam [current_length]         ; 2
//ldbi 2                        ; 2
//add a                         ; 1
//sta [current_length]         ; 2
//L6:                           ; 0
    new_head_x = self[0];
    new_head_y = self[1];

    if(current_direction == DIRECTION_LEFT) {
        new_head_x -= 1;
    } else if(current_direction == DIRECTION_RIGHT) {
        new_head_x += 1;
    } else if(current_direction == DIRECTION_UP) {
        new_head_y -= 1;
    } else if(current_direction == DIRECTION_DOWN) {
        new_head_y += 1;
    } else {
        return;
    }


recheck:


        printf("ch %d %d f %d %d\n", new_head_x, new_head_y, food_x, food_y);
    if(new_head_x == food_x) {
        if(new_head_y == food_y) {
            need_enlarge = 1;
            generate_food();
            random();
            goto recheck;
        }
    }
    
    if(!is_valid(new_head_x, new_head_y)) {
        goto fail;
    }

    if(need_enlarge) {
        need_enlarge = 0;
        current_length += 2;
    }

    
    iterator = current_length - 2; /* from */
    iterator2 = current_length; /* to */
    while(1) {
        if(iterator > current_length)
            break;

        self[iterator2] = self[iterator];
        iterator--;
        iterator2--;

        self[iterator2] = self[iterator];
        iterator--;
        iterator2--;

    }

    self[0] = new_head_x;
    self[1] = new_head_y;

    return;

fail:
                printf("Fail!\n");
                while(1) {}


}