Beispiel #1
0
void update_game()
{
	// move snake
	int rRed = snake_move(&redSnake, enTile_RedHeadUp);
	int rGreen = snake_move(&greenSnake, enTile_GreenHeadUp);
	//int rGreen = snake_mov(&greenSnake);
	// generate food every ten rounds
	if (g_Round % 10 == 0) {
		int x = rand()%g_max_x;
		int y = rand()%g_max_y;
		put_tile(enTile_Food, x, y);
	}
	// check game statues
	if (rRed == 2 && rGreen == 2) {
		mvaddstr(g_max_x/2, g_max_y/2, "Draw!");
		gbGameOn = false; 
		getch();
	} else if (rRed == 2) {
		mvaddstr(g_max_x/2, g_max_y/2, "Red Lose!");
		gbGameOn = false; 
		getch();
	} else if (rGreen == 2) {
		mvaddstr(g_max_x/2, g_max_y/2, "Green Lose!");
		gbGameOn = false; 
		getch();
	}
	g_Round++;
}
Beispiel #2
0
int main(void)
{
    init_data();
    snake_move();
    //init_data();
    //mouse_do();
    return 0;
}
Beispiel #3
0
static void change_directions(int direction)
{
	snake *temp = NULL;
	temp = ps.s->previous;
	while (temp->previous != ps.s->previous) {
		temp->direction = temp->previous->direction;
		temp = temp->previous;
	}
	ps.s->direction = direction;
	snake_move();
}
Beispiel #4
0
int main()
{
 help_info();
 snake_init();
 show_food();
 drow_wall();
 game_info();
 while(judge()){
  snake_move();
 }
 return 0;
}
Beispiel #5
0
int main (void)
{
    snake_t snake;
    int tick = 0;

    system_init ();

    snake.dir = DIR_N;
    snake.pos.x = TINYGL_WIDTH / 2;
    snake.pos.y = TINYGL_HEIGHT - 1;

    tinygl_init (LOOP_RATE);

    navswitch_init ();

    pacer_init (LOOP_RATE);

    tinygl_draw_point (snake.pos, 1);

    /* Paced loop.  */
    while (1)
    {
        /* Wait for next tick.  */
        pacer_wait ();

        navswitch_update ();

        tick = tick + 1;
        if (tick > LOOP_RATE / SNAKE_SPEED)
        {   
            tick = 0;
            snake = snake_move (snake);
        }

        if (navswitch_push_event_p (NAVSWITCH_WEST))
        {
            snake = snake_turn_left (snake);
        }

        if (navswitch_push_event_p (NAVSWITCH_EAST))
        {
            snake = snake_turn_right (snake);
        }

  
        tinygl_update ();
    }

    return 0;
}
//handles the room elements
void room_handle_elements()
{
  for (char i=0; i < element_count; i++)
  {
    //if the element is hidden, skip past it
    if (roomElements[i].state > STATE_HIDDEN)
	{	   	   
       //test room elements for a collision with the elf
	   if (room_test_element(roomElements[i], poofy_get_x(), poofy_get_y())) roomElements[i] = poofy_hit(roomElements[i]);
	  
	   //determine the type of element and handle the behaviors
	   if (roomElements[i].type <= 49) roomElements[i] = snake_move(roomElements[i]);	 
	}
  }
}
Beispiel #7
0
void snake_show(int signum)
{
     snake_show_or_hide(BLANK);
     if(!snake_move())
     {
	  endwin();
	  exit(0);
     }

     if(is_touch_food(&food))
     {
	  snake_eat(&food);
	  food_create();
     }
     snake_show_or_hide(SNAKE_BODY);
}
Beispiel #8
0
GamePainter::GamePainter(QWidget *parent) : QWidget(parent)
{
    m_cellSize = QSize( 30, 30 );
    m_field    = new Field( 15, 15 );
    m_snake    = new Snake( QPoint( 7, 7) );
    m_IsPlay   = false;
    m_gameTimer = new QTimer;
    m_gameSpeed = 160;
    m_score     = 0;

    resize( m_cellSize.width() * ( m_field->getNumWCells() - 2 )
            , m_cellSize.height() * (m_field->getNumHCells() - 2 ) + 30 );


    QPixmap background("../snake_work_version/background.jpg" );
    background.scaled( this->size() );

    QPalette p;
    p.setBrush( this->backgroundRole(), QBrush( background ) );
    this->setPalette(p);

    QIcon windowIcon("../snake_work_version/icon1.png");
    window()->setWindowTitle("Snake");
    window()->setWindowIcon( windowIcon );

    connect( m_snake, SIGNAL(newStep(QPoint,QPoint)), m_field, SLOT(snake_move(QPoint,QPoint)) );
    connect( m_field, SIGNAL(snake_dead()), SLOT(snake_die()) );
    connect( m_field, SIGNAL(snake_ate()), SLOT(snake_eat()));
    connect( m_gameTimer, SIGNAL(timeout()), SLOT(game()) );

    m_settings  = new QSettings( "HOME", "Snake" );
    m_highScore = m_settings->value( HIGH_SCORE_KEY, 0 ).toInt() ;


    QLabel* high_score = new QLabel("<font face=\"mv boli\" color=#40E0D0><strong>High Score: </strong></font>", this );
    high_score->move( 20, -160  );
    high_score->resize( 300, 350 );

    QLabel* score = new QLabel("<font face=\"mv boli\" color=#40E0D0><strong>Score: </strong></font>", this );
    score->move( width() - 100, -160  );
    score->resize( 300, 350 );

    srand(time(0));
}
Beispiel #9
0
void snake_run(){
	_delay_ms(200);

	snake_init(snake);
	snake_print(snake);

	uint16_t delay = 15000 / (s_difficulty * 1.0);

	
	while (1){
		
			
			
		if(timer_read(TIMER_3) > delay){
			timer_reset(TIMER_3);
			
			if(joystick_x_value() > THRESHOLD && (direction != left)){
				direction = right;
			}
			else if(joystick_x_value() < -THRESHOLD && (direction != right)){
				direction = left;
			}
			else if(joystick_y_value() > THRESHOLD && (direction != down)){
				direction = up;
			}
			else if(joystick_y_value() < -THRESHOLD && (direction != up)){
				direction = down;
			}
			
			if(button_right_read()){
				snake_add_size(snake);

				
			}
			
			
			
			snake_move(direction, snake);
			
			
			//if we hit border
			if((snake[0][pos_x] > 15) || (snake[0][pos_x] < 0)){
				snake_game_over();
			}
			if((snake[0][pos_y] > 7) || (snake[0][pos_y] < 0)){
				snake_game_over();
			}
			
			//if snake head position == apple position
			if((snake[0][pos_x] == apple[0]) && (snake[0][pos_y] == apple[1]) && (apple[2])){
				//apple bool value = 0, add snake size
				apple[2] = 0;
				snake_points++;
				snake_add_size(snake);
			}
			if(!(apple[2])){
				snake_make_apple();
			}
			
			if(snake_self_collide(snake)){
				snake_game_over();
			}
			
			snake_print(snake);

		
			oled_write_screen();
		
		
		}
		
		//leave game
		if(button_left_read()){
			return;
		}
	}
	

}
Beispiel #10
0
void sig_alrm(int sigo)
{
	set_ticker(100);
	snake_move();
}
Beispiel #11
0
/* *****************************************************************************
 *
 * ************************************************************************** */
t_ret_code algo_Snake ( t_io_values *p_io,  t_io_Snake *p_snake ) {
/*
 * ) Set startup values (Random?)
 * )     <--------------------------------------------------------------------.
 * ) Check direction, check border limit    <-------------------------------. |
 * ) Replase last-bit into position which before head (...now it head...!)  | |
 * ) If exist Eat-bit, enlarge for length                                   | |
 * ) ...If no, do jump to ----------------------------------------------------'
 * ) random                                                                 |
 * ) if border limits is OK, direction to left/right/up/down ---------------'
 * ) ...If border is limited ->End
 * )
 */
	t_ret_code  rc = RC_FAILED;
	//static t_algo_step  algo_step;
	uint8_t      xx, yy;
	//uint32_t     rnd, rnd_tmp;
	//static char  snake_init = 0;
	static snake_node  player = {0};
	//snake_node* player = malloc(sizeof(snake_node));

	if ( p_io == NULL || p_snake == NULL ){
		rc=RC_PTR_FAIL;
		return rc;
	}

	rc = RC_OK;

	switch ( p_snake->algo_step ) {
		case S_INIT:
			/*p_snake->body = p_snake->ptr_field;
			p_snake->ptr_field     = &a_field[0][0];
			//p_snake->ptr_field_eat = &a_field_eat[0][0];
			p_snake->ptr_field_eat = &buf_out;
			p_snake->curr_x        = 0;
			p_snake->curr_y        = 0;

			// Clean field
			for ( xx=0; xx<8; xx++ ) {
				for ( yy=0; yy<16; yy++ ) { a_field_eat[xx][yy]=0; }
			}*/

			player.dir =  left; //init direction
			player.x = MAP_WIDTH/2;
			player.y = MAP_HEIGHT/2;
			player.next = NULL;

			int i, j; //this is so we don't get errors because the object map references nothing.
			for (i = 0; i < MAP_HEIGHT; i++) {
				for (j = 0; j < MAP_WIDTH; j++) {
					objMap[i][j] = nothing;
				}
			}

			p_snake->algo_step = S_START_RANDOM;
		break;

		case S_START_RANDOM:
			/*
			//srand ( 255 ); // Initiate seek random number
			//srand ( p_io->x );
			p_snake->rnd = rnd_fill_field( p_io->rnd );
			// Fill field as random
			//for ( xx=0; xx<8; xx++ )
			//{
			//	for ( yy=0; yy<16; yy++ ) {
			//		rnd = (uint8_t)rand(); // get random number
			//		if (rnd&0x01) rnd&=0x69; else rnd&=0x96;
			//		p_snake->rnd = rnd;
			//		rnd_tmp = rnd;
			//		//a_field_eat [xx][yy] = ( rnd_tmp < 8 ) ? 1 : 0;
			//		a_field_eat_16b[yy] = rnd_tmp;
			//	}
			//}
			*/

			//we need to seed our rand() and generate our first random object
			//srand(time(NULL));
			srand ( 255 ); // Initiate seek random number
			snake_generate_new_apple();

			p_snake->algo_step = S_BEGIN;
		break;

		case S_BEGIN:
	        snake_move(&player);
	        snake_draw();

	        ticks++;    //update ticks
	        resting=0;  //slow the game down

	        //this formula is hacked together. This number is the one which determines how slow the game is. The
	        //higher the number the longer we wait. So higher gamespeed means a lower wait.
	        //rest_callback(100-gamespeed * 30, rest1);

			//char val;
			static char prev_x, prev_y;

			//drv_led_8x8_clear( 0, 0, 1 );
			objMap[prev_x][prev_y]=0;
			objMap[player.x][player.y]=1;
	        xx=0;
			for ( xx=0; xx<8; xx++ )
			{
				yy=0;
				while ( yy<16 )
				//if ( _TIMER_READY == delay_timer_leds8x8('?') )
				{
					//val=objMap[xx][yy];
					drv_led_8x8_show_byte( objMap[xx][yy], xx, yy );
					//drv_led_8x8_show_byte( a_field_eat_16b[yy], xx, yy);
					//drv_led_8x8_pixel_set ( player.x, player.y, 1 );
					yy++;
				}
			}
			prev_x = player.x;
			prev_y = player.y;


			p_snake->algo_step = S_BEGIN;
			//p_snake->algo_step = S_START_RANDOM; //S_BEGIN;
			//p_snake->algo_step = S_MOVE;
		break;

		case S_MOVE:
			p_snake->algo_step = S_EAT_YES;
		break;

		case S_EAT_YES:
			p_snake->algo_step = S_EAT_NO;
		break;

		case S_EAT_NO:
			p_snake->algo_step = S_RANDOM;
		break;

		case S_RANDOM:
			p_snake->algo_step = S_CHANGE_DIRECTION;
		break;

		case S_CHANGE_DIRECTION:
			p_snake->algo_step = S_BORDER_END;
		break;

		case S_BORDER_END:
			p_snake->algo_step = S_BORDER_END;
		break;

		default :
			p_snake->algo_step = S_BORDER_END;
		break;
	}

	return rc;
}
Beispiel #12
0
int main(int argc, char *argv[]) {
	// 创建一个世界
    WINDOW *world;
	// 创建一条蛇
    Snake *snake;
	// 创建食物所在位置坐标
    Point food_point;
	// 初始移动方向为向右
    Direction direction = RIGHT;

    srand(time(NULL));
 
	// curses库初始化
    initscr();					/* 初始化屏幕							*/
    noecho();					/* 禁止输入字符getch读取回显			*/
    cbreak();					/* 关闭行缓冲							*/
    timeout(TICKRATE);			/* 每隔TICKRATE的时间检测一次窗口读操作 */
    keypad(stdscr, TRUE);		/* 开启键盘的键区,可以使用方向键		*/
    curs_set(0);				/* 禁止显示鼠标指针						*/
    
    refresh();					/* 刷新屏幕,准备重画					*/
 
    // 初始化世界
    world = create_world();
	// 以符号*界定世界窗口的边界
    box(world, '|' , '-');

	// 将世界窗口显示在终端上
    wrefresh(world);

    // 蛇初始化
    snake = snake_create(SNAKE_INITIAL_LENGTH);

	// 食物位置初始化
    food_point = new_food_position();
 
    int ch;
	// 因为没TICKRATE检测一次,并且设置了nodelay模式,getch不会
	// 阻塞,每隔TICKRATE下述循环将被执行一次
	while ((ch = getch()) != 'q')
    {
		// 每当检测到用户输入,重画世界
		wclear(world);
		box(world, '|' , '-');

		// 蛇移动,修改蛇的各个部分移动之后的坐标值
		snake_move(snake, direction, WORLD_WIDTH, WORLD_HEIGHT);

		Point *snake_head = (Point *)snake->head->value;
		// 蛇如果吃掉了食物,在新位置生成新的食物
		if (are_points_equal(food_point, *snake_head)) {
			food_point = new_food_position();
			snake_add_part_to_tail(snake);
		}

      // 绘制出蛇以及食物
      mvwaddch(world, food_point.y, food_point.x, '$');
      draw_snake(world, snake);

	  // 将当前绘制的窗口显示到终端
      wrefresh(world);

	  // 在nodelay模式下,getch读取不到字符则返回ERR
      if(ch != ERR) {
		// 修正移动方向
        direction = direction_for_key(ch);
      }
    }
 
    ll_free_list(snake);
    delwin(world);

    endwin();
 
    return 0;
}
Beispiel #13
0
int main(int argc, char *argv[])
{
    int ch;

    initscr();
    start_color();
    cbreak();
    keypad(stdscr, TRUE);
    noecho();
    curs_set(0);
    init_pair(1, COLOR_CYAN, COLOR_BLACK);

    attron(COLOR_PAIR(1));
    //画边
    mvaddch(1, 0, '+');
    mvaddch(1, COLS-1, '+');
    mvaddch(LINES-1, COLS-1, '+');
    mvaddch(LINES-1, 0, '+');
    mvhline(1, 1, '-', COLS-1-1);
    mvhline(LINES-1, 1, '-', COLS-1-1);
    mvvline(2, 0, '|', LINES-2-1);
    mvvline(2, COLS-1, '|', LINES-2-1);

    refresh();
    attroff(COLOR_PAIR(1));

    // 来条蛇
    SNAKE snake;
    init_snake(&snake);
    draw_whole_snake(&snake);
    
    // 初始豆儿
    int *food;
    food = random_food(&snake);
    //mvaddch(food[1], food[0], '*');
    draw_food(food);

    int d;
    int i;
    now_time=0;
    while (1)
    {
        if(ioctl(0,FIONREAD,&i)<0)
        {
            printf("ioctl failed, error=%d\n ",errno);
            break;
        }
        if(!i)  // 小蛇自动跑部分
        {
            usleep(time_rate);
            now_time = now_time + time_rate;
            if (!(now_time < speed))
            {
                now_time = 0;
                if (snake_move(&snake, food))
                    break;
            }
            continue;
        }
        now_time = 0;
        ch = getch();
        if (ch < 258 || ch > 261)
            continue;
        if ( ch+snake.d != KEY_LEFT+KEY_UP )
        {
            d = ch - snake.d;
            if ( d == 1 || d == -1)
                continue;
        }
        snake.d = ch;

        if (snake_move(&snake, food))
            break;

    }
    getch();
    endwin();
    return 0;
}
Beispiel #14
0
void design_cp2()
{
	cp=2;
	srand((unsigned)time(NULL));
	food.flag=1;
	mine.flag=1;
	direct=4;
	score=0;
	poigrass.cor[0].x=240;
	poigrass.cor[0].y=40;
	poigrass.cor[1].x=340;
	poigrass.cor[1].y=180;
	poigrass.cor[2].x=240;
    poigrass.cor[2].y=500;
	poigrass.cor[3].x=60;
	poigrass.cor[3].y=240;
	poigrass.cor[4].x=500;
	poigrass.cor[4].y=300;
    p0=(struct Snake*)malloc(LEN);
	head=(struct Snake*)malloc(LEN);
	tail=(struct Snake*)malloc(LEN);
	p1=(struct Snake*)malloc(LEN);
	head->x=2*20;
	head->y=5*20;
	tail->x=20;
	tail->y=5*20;
	p0=tail;
	map();
	tail->x=head->x-20;
	tail->y=head->y;
	head->next=tail;
	head->previous=NULL;
	tail->next=NULL;
	tail->previous=head;
	tim1=(unsigned)time(NULL);
    while(1)
	{
		while(!kbhit())
		{
		  set_poigrass();
		  set_poigrass1();
		  if(food.flag==1)
              set_food();
		  if(mine.flag==1)
	          set_mine();
		  show_food();
	      show_mine();
          snake_move();
		  ju_eat_food();
		  if(node==16)
		  {
			  initgraph(500,400);
			  setbkcolor(GREEN);
              cleardevice();
			  setcolor(RED);
			  setfont(40,0,"华文行楷");
			  outtextxy(70,160,"恭喜你,进入下一关!");
			  Sleep(3000);
			  design_cp3();
		  }
		  ju_meet_mine();
		  ju_eat_poigrass();
          while(p0->next!=NULL)
		  {
	         if((p0->next->x==head->x)&&(p0->next->y==head->y))
		        life=0;
	    	p0=p0->next; 
		  }
		  if(life==0)
			  gameover();
          if(head->x>=680)
		  {  
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x-20,tail->y,tail->x,tail->y+20);
		      gameover();
		  }
		  if(head->x<20)
		  {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x+20,tail->y,tail->x+40,tail->y+20);
		      gameover();
          }
		  if(head->y>=580)
		  {  
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y-20,tail->x+20,tail->y);
		      gameover();
		  }
		  if(head->y<20)
		  {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y+20,tail->x+20,tail->y+40);
		      gameover();
		  }
		  if(direct==4)
		     if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420))
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x-20,tail->y,tail->x,tail->y+20);
		      gameover();
			 }
		  if(direct==3)
		     if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420))
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x+20,tail->y,tail->x+40,tail->y+20);
		      gameover();
			 }
		  if(direct==1)
		     if(head->x==200&&head->y==380||head->x==420&&head->y==420)
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y+20,tail->x+20,tail->y+40);
		      gameover();
			 }
			 if(direct==2)
		     if(head->x==200&&head->y==160||head->x==420&&head->y==280)
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y-20,tail->x+20,tail->y);
		      gameover();
			 }
		  show_snake();
		  tim2=(unsigned)time(NULL);
		  if(tim2-tim1>=50)
			  no_poigrass();
		}
		change();
	}
}
Beispiel #15
0
void design_cp3()
{
	cp=3;
	srand((unsigned)time(NULL));
	food.flag=1;
	mine.flag=1;
	direct=4;
	poigrass.cor[0].x=240;
	poigrass.cor[0].y=40;
	poigrass.cor[1].x=340;
	poigrass.cor[1].y=180;
	poigrass.cor[2].x=240;
    poigrass.cor[2].y=500;
	poigrass.cor[3].x=60;
	poigrass.cor[3].y=240;
	poigrass.cor[4].x=500;
	poigrass.cor[4].y=300;
	poigrass.cor[5].x=600;
	poigrass.cor[5].y=120;
	poigrass.cor[6].x=520;
	poigrass.cor[6].y=540;
    p0=(struct Snake*)malloc(LEN);
	head=(struct Snake*)malloc(LEN);
	tail=(struct Snake*)malloc(LEN);
	p1=(struct Snake*)malloc(LEN);
	head->x=2*20;
	head->y=5*20;
	tail->x=20;
	tail->y=5*20;
	p0=tail;
	map();
	tail->x=head->x-20;
	tail->y=head->y;
	head->next=tail;
	head->previous=NULL;
	tail->next=NULL;
	tail->previous=head;
	tim1=(unsigned)time(NULL);
    while(1)
	{
		while(!kbhit())
		{
		  set_poigrass();
		  set_poigrass1();
		  if(food.flag==1)
              set_food();
		  if(mine.flag==1)
	          set_mine();
		  show_food();
	      show_mine();
          snake_move();
		  ju_eat_food();
		  ju_meet_mine();
		  ju_eat_poigrass();
          while(p0->next!=NULL)
		  {
	         if((p0->next->x==head->x)&&(p0->next->y==head->y))
		        life=0;
	    	p0=p0->next; 
		  }
		  if(life==0)
			  gameover();
          if(head->x>=680)
		  {  
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x-20,tail->y,tail->x,tail->y+20);
		      gameover();
		  }
		  if(head->x<20)
		  {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x+20,tail->y,tail->x+40,tail->y+20);
		      gameover();
          }
		  if(head->y>=580)
		  {  
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y-20,tail->x+20,tail->y);
		       gameover();
		  }
		  if(head->y<20)
		  {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y+20,tail->x+20,tail->y+40);
		      gameover();
		  }
		  if(direct==4)
		     if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420)||
				 head->y==200&&head->x==300)
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x-20,tail->y,tail->x,tail->y+20);
		      gameover();
			 }
		  if(direct==3)
		     if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420)||
				 head->y==200&&head->x==580)
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x+20,tail->y,tail->x+40,tail->y+20);
		      gameover();
			 }
		  if(direct==1)
		     if(head->x==200&&head->y==380||head->x==420&&head->y==420||
				 head->y==200&&(head->x>=300&&head->x<=580))
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y+20,tail->x+20,tail->y+40);
		      gameover();
			 }
			 if(direct==2)
		     if(head->x==200&&head->y==160||head->x==420&&head->y==280||
				 head->y==200&&(head->x>=300&&head->x<=580))
			 {
              setfillcolor(YELLOW);
	          setcolor(YELLOW);
	          bar(tail->x,tail->y-20,tail->x+20,tail->y);
		      gameover();
			  tim2=(unsigned)time(NULL);
		      if(tim2-tim1>=80)
			      no_poigrass();
			 }
		  show_snake();
		}
		change();
	}
}