예제 #1
0
void refresh_draw(int signum)
{
    signal(SIGALRM, SIG_IGN);
    baffle_move(signum);
    ball_move(signum);
    signal(SIGALRM, refresh_draw);
}
예제 #2
0
파일: game.c 프로젝트: reagent/pong
void game_tick(Game *game) {
    int ch = 0, move_result = BALL_MOVED;

    long elapsed = elapsed_since(&game->start_time);
    static long last_event = 0;

    if ((elapsed - last_event) >= (BALL_DELAY * MICROSECONDS_PER_SECOND)) {
        move_result = ball_move(game->ball, game->field, game->player);

        if (move_result == BALL_SCORE) {
          game->player->score += 1;
        } else if (move_result == BALL_MISS) {
          game->player->score = 0;
          ball_reset(game->ball, game->field);
        }

        last_event = elapsed;
    }

    if ((ch = getch()) != ERR) {
        if (ch == MOVE_UP) {
            player_move(game->field, game->player, -1);
        }

        if (ch == MOVE_DOWN) {
            player_move(game->field, game->player, 1);
        }
    }

    // field_refresh(game->field);

    field_redraw(game->field);

    wclear(game->field->game);

    field_draw_score(game->field, game->player);
    wrefresh(game->field->score);

    player_draw(game->player, game->field);
    ball_draw(game->ball, game->field);

    wrefresh(game->field->game);
}
예제 #3
0
파일: pong.c 프로젝트: dakerfp/libpong
int pong_game_tick(struct pong_game *g, int cmd[2]) {
    player_move(&g->players[0], cmd[0], g->grid.height);
    player_move(&g->players[1], cmd[1], g->grid.height);
    ball_move(&g->ball);
    ball_hits_player(&g->ball, &g->players[0]);
    ball_hits_player(&g->ball, &g->players[1]);
    switch (ball_hits_wall(&g->ball, g->grid)) {
    case -1:
        g->players[1].score++;
        g->ball.pos = g->ball.init;
        break;
    case 1:
        g->players[0].score++;
        g->ball.pos = g->ball.init;
        break;
    default:
        return 0;
    }
    if (g->players[0].score >= g->target_score)
        return -1;
    if (g->players[1].score >= g->target_score)
        return 1;
    return 0;
}
예제 #4
0
static void collision(int ball0, int ball1, int winWidth, int winHeight)
{
	int mode, i;
	float x[2],y[2],r[2];
	T_SPEED s[2], cs[2];
	double xx,yy,rr,cx,px,cy,py,xc,xp,yc,yp,tmp;

	r[0] = ball_getRadius(ballArray[ball0]);
	r[1] = ball_getRadius(ballArray[ball1]);
	ball_getPos(ballArray[ball0], &(x[0]), &(y[0]));
	ball_getPos(ballArray[ball1], &(x[1]), &(y[1]));

	s[0] = ball_getSpeed(ballArray[ball0]);
	s[1] = ball_getSpeed(ballArray[ball1]);

	xx = abs(x[0] - x[1]);
	yy = abs(y[0] - y[1]);
	rr = pow(xx*xx+yy*yy,0.5);

	if ((x[0]-x[1]>0 && y[0]-y[1]>0)
		|| (x[0]-x[1]<0 && y[0]-y[1]<0))
		mode = 1;
	else
		mode = -1;

	//transform to collision coordinate system
	for(i=0; i<2; i++)
	{
		cx = xx*s[i].xSpeed/rr;
		cy = mode*yy*s[i].ySpeed/rr;
		px = yy*s[i].xSpeed/rr;
		py = -mode*xx*s[i].ySpeed/rr;

		cs[i].xSpeed = cx + cy;
		cs[i].ySpeed = px + py;
	}

	//exchange collision speed
	tmp = cs[0].xSpeed;
	cs[0].xSpeed = cs[1].xSpeed;
	cs[1].xSpeed = tmp;

	//transform to normal coordinate system
	for(i=0; i<2; i++)
	{
		xc = xx*cs[i].xSpeed/rr;
		xp = yy*cs[i].ySpeed/rr;
		yc = mode*yy*cs[i].xSpeed/rr;
		yp = -mode*xx*cs[i].ySpeed/rr;

		s[i].xSpeed = xc + xp;
		s[i].ySpeed = yc + yp;
	}

	//set new speed
	ball_setSpeed(ballArray[ball0], s[0]);
	ball_setSpeed(ballArray[ball1], s[1]);

	//To avoid dead loop
	i = 0;
	while(rr<r[0]+r[1] && i<6)
	{
		ball_move(ballArray[ball0], winWidth, winHeight);
		ball_move(ballArray[ball1], winWidth, winHeight);

		r[0] = ball_getRadius(ballArray[ball0]);
		r[1] = ball_getRadius(ballArray[ball1]);
		ball_getPos(ballArray[ball0], &(x[0]), &(y[0]));
		ball_getPos(ballArray[ball1], &(x[1]), &(y[1]));
		
		xx = abs(x[0] - x[1]);
		yy = abs(y[0] - y[1]);
		rr = pow(xx*xx+yy*yy,0.5);

		i++;
	}
}
예제 #5
0
static void collision(int ball0, int ball1, int winWidth, int winHeight)
{
	int mode, i;
	float x[2],y[2],r[2];
	T_SPEED s[2], cs, ps;
	double xx,yy,rr,cs0,ps0,cs1,ps1,cx0,cx1,px0,px1,cy0,cy1,py0,py1,tmp;

	r[0] = ball_getRadius(ballArray[ball0]);
	r[1] = ball_getRadius(ballArray[ball1]);
	ball_getPos(ballArray[ball0], &(x[0]), &(y[0]));
	ball_getPos(ballArray[ball1], &(x[1]), &(y[1]));

	s[0] = ball_getSpeed(ballArray[ball0]);
	s[1] = ball_getSpeed(ballArray[ball1]);

	xx = abs(x[0] - x[1]);
	yy = abs(y[0] - y[1]);
	//printf("pow(%f,0.5)", xx*xx+yy*yy);
	rr = pow(xx*xx+yy*yy,0.5);

	if ((x[0]-x[1]>0 && y[0]-y[1]>0)
		|| (x[0]-x[1]<0 && y[0]-y[1]<0))
		mode = 0;
	else
		mode = 1;

//transform to collision coordinate system
	//caculate ball0
	cx0 = xx*s[0].xSpeed/rr;
	if (mode)
	{
		px0 = yy*s[0].xSpeed/rr;
		cy0 = -yy*s[0].ySpeed/rr;
	}
	else
	{
		px0 = -yy*s[0].xSpeed/rr;
		cy0 = yy*s[0].ySpeed/rr;
	}
	py0 = xx*s[0].ySpeed/rr;
	cs0 = cx0 + cy0;
	ps0 = px0 + py0;
	//caculate ball1
	cx1 = xx*s[1].xSpeed/rr;
	if (mode)
	{
		px1 = yy*s[1].xSpeed/rr;
		cy1 = -yy*s[1].ySpeed/rr;
	}
	else
	{
		px1 = -yy*s[1].xSpeed/rr;
		cy1 = yy*s[1].ySpeed/rr;
	}
	py1 = xx*s[1].ySpeed/rr;
	cs1 = cx1 + cy1;
	ps1 = px1 + py1;
	//exchange collision speed
	tmp = cs0;
	cs0 = cs1;
	cs1 = tmp;

//transform to normal coordinate system
	//caculate ball0
	if (mode)
	{
		cs.xSpeed = xx*cs0/rr;
		cs.ySpeed = -yy*cs0/rr;
		ps.xSpeed = yy*ps0/rr;
		ps.ySpeed = xx*ps0/rr;
	}
	else
	{
		cs.xSpeed = xx*cs0/rr;
		cs.ySpeed = yy*cs0/rr;
		ps.xSpeed = -yy*ps0/rr;
		ps.ySpeed = xx*ps0/rr;
	}
	s[0].xSpeed = cs.xSpeed + ps.xSpeed;
	s[0].ySpeed = cs.ySpeed + ps.ySpeed;
	ball_setSpeed(ballArray[ball0], s[0]);
	//caculate ball1
	if (mode)
	{
		cs.xSpeed = xx*cs1/rr;
		cs.ySpeed = -yy*cs1/rr;
		ps.xSpeed = yy*ps1/rr;
		ps.ySpeed = xx*ps1/rr;
	}
	else
	{
		cs.xSpeed = xx*cs1/rr;
		cs.ySpeed = yy*cs1/rr;
		ps.xSpeed = -yy*ps1/rr;
		ps.ySpeed = xx*ps1/rr;
	}
	s[1].xSpeed = cs.xSpeed + ps.xSpeed;
	s[1].ySpeed = cs.ySpeed + ps.ySpeed;
	ball_setSpeed(ballArray[ball1], s[1]);

	//To avoid dead loop
	i = 0;
	while(rr<r[0]+r[1] && i<10)
	{
		ball_move(ballArray[ball0], winWidth, winHeight);
		ball_move(ballArray[ball1], winWidth, winHeight);

		r[0] = ball_getRadius(ballArray[ball0]);
		r[1] = ball_getRadius(ballArray[ball1]);
		ball_getPos(ballArray[ball0], &(x[0]), &(y[0]));
		ball_getPos(ballArray[ball1], &(x[1]), &(y[1]));
		
		xx = abs(x[0] - x[1]);
		yy = abs(y[0] - y[1]);
		//printf("2.pow(%f,0.5)", xx*xx+yy*yy);
		rr = pow(xx*xx+yy*yy,0.5);
		//printf("Adjustin collision position,rr=%d,r[0]=%d,r[1]=%d,x[0]=%d,x[1]=%d,y[0]=%d,y[1]=%d.\r\n",(int)rr,r[0],r[1],x[0],x[1],y[0],y[1]);

		i++;
	}

	//printf("Collision[%d,%d]...\r\n",ball0,ball1);
}
예제 #6
0
//refresh ball status according to speed
void ball_refresh(T_BALL *pBall, cairo_t *pCR, int winWidth, int winHeight)
{
	ball_move(pBall, winWidth, winHeight);
	ball_show(pBall, pCR);
}
예제 #7
0
int             wait_event(SDL_Renderer *renderer)
{
  bool          quit;
  int           y_p1;
  int           y_p2;
  SDL_Event     event;
  SDL_Rect      raq_p1;
  SDL_Rect      raq_p2;
  SDL_Rect      ball;
  SDL_Rect      bold;

  quit = false;
  init_ball(&ball, &bold);
  y_p1 = 240;
  y_p2 = 240;
  while (!quit)
    {
      printf("x");
      SDL_Delay(2);
      SDL_PollEvent(&event);
      switch (event.type)
        {
        case SDL_KEYDOWN:
          switch(event.key.keysym.sym)
            {
            case SDLK_ESCAPE: 
              {
                quit = true; 
                return (0); 
              }
            case SDLK_UP: 
              {
                if (y_p1 > 0)
                  y_p1--; 
                else 
                  break; 
                break;
              }
            case SDLK_DOWN:
              {
                if (y_p1 < 480 - 64) 
                  y_p1++; 
                else 
                  break; 
                break;
              }
            case SDLK_LEFT:
              {
                if (y_p2 < 480 - 64) 
                  y_p2++; 
                else 
                  break; 
                break;
              }
            case SDLK_RIGHT:
              {
                if (y_p2 > 0) 
                  y_p2--; else 
                  break; 
                break;
              }
            }
          break;
        }
      pos_raq_p1(&raq_p1, &y_p1);
      pos_raq_p2(&raq_p2, &y_p2);
      ball_move(&ball, &bold, &raq_p1, &raq_p2);
      print_game(renderer, &raq_p1, &raq_p2, &ball);
    }
}