コード例 #1
0
void Breakout::play(int controls){
  moveBall();
  checkCollision();
  level[7-ball.y][ball.x] = 'O';
  movePaddle(controls);
  
}
コード例 #2
0
void Framework::handleMouseMove(int x, int y, unsigned char buttons)
{
	if (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT))
	{
		camera.adjustAngle((double)x / 10.0, (double)y / 10.0);
	} else if (buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE))
	{
		camera.adjustDistance((double)y / 10.0);
		resetGL();
	} else {
		if (!paused) movePaddle((double)x / 50.0, (double)-y / 50.0, SDL_GetTicks());
		if (camera.getMode() == Camera::FOLLOW_PADDLE)
			camera.adjustAngle((double)-x / 20.0, (double)-y / 20.0);
		else if (camera.getMode() == Camera::FOLLOW_PADDLE_REVERSE)
			camera.adjustAngle((double)x / 20.0, (double)y / 20.0);
	}
}
コード例 #3
0
ファイル: ballduino.c プロジェクト: benjmyers/Ballduino
/* Game play loop */
void  loop(){
  
  if (continueGame){                                                      // If the game is still in play
    drawFrame();                                                          // Draw the frame
    drawScore();                                                          // Draw the score
    movePaddle();                                                         // Update the location of the paddle
    boolean paddleCollision = checkPaddleCollision();                     // Determine if the ball has hit the paddle or block
    boolean blockCollision = checkBlockCollision();
    if(score == numBlocks)                                                // If the score is equivalent to the number of blocks, game is over
      winner();                                                           // Display message to user
    else{                                                                 // The game is still in play
      if(paddleCollision || blockCollision)                               // Redraw screen to draw over any collisions
        drawFrame();
      delay(50);                                                          // Slight delay
      continueGame = updatePos();                                         // Update the position of the ball
    }
  }
  else                                                                    // The game is over, the ball fell off the screen. Display message to user.
    gameOver();
}
コード例 #4
0
void loop() {

    is_diff_game_mode(); // different game modes

    movePaddle();
    render_ball();
    render_right_wall();
    render_score();
    render_lives();

    lBtn2 = GPIOPinRead(BTN2Port, BTN2);

    if (lBtn2 == BTN2)
    {
        render_credits();
    }

    ballMove();

    OrbitOledUpdate();
    oledReset();
}
コード例 #5
0
void breakoutUpdate()
{
	if(gameOver)
	{
		if(releasedKeys & KEY_TOUCH || releasedKeys & KEY_A)
			reset();	
	}
	else if(playing)
	{
		movePaddle(&paddle);
		moveBall(&ball);
		
		handleBallBricksCollisions(&ball);
		handleBallPaddleCollision(&ball, &paddle);
		
		addNewBricks();
	}
	else
	{
		ticks += timerElapsed(0);
		if(ticks > 50000 || releasedKeys & KEY_TOUCH || releasedKeys & KEY_A)
			playing = 1;
	}
}
コード例 #6
0
ファイル: pong.c プロジェクト: ECE1T6/game-o-matic
void pong(bool** ledArray) {

    //Playfield constants:
    const float TOP_MARGIN = 0.0; //The margins bound the area controlled by the game.
    const float BOT_MARGIN = 0.0;
    const float LEFT_MARGIN = 0.0;
    const float RIGHT_MARGIN = 0.0;
    const float BOT_END = ARRAY_HEIGHT - BOT_MARGIN - 1.0;
    const float RIGHT_END = ARRAY_WIDTH - RIGHT_MARGIN - 1.0;

    //Object constants:
    const int BALL_DIAMETER = 2;
    const float INIT_BALL_X_SPEED = 0.5;
    const float MAX_BALL_Y_SPEED = 0.5;

    const int PAD_HEIGHT = 8;
    const int PAD_WIDTH = 2;
    const int PAD_DISTANCE = 3; //The distance from each paddle to its side.
    const float LEFT_PAD_X = LEFT_MARGIN + PAD_DISTANCE; //Refers to the innermost pixel on the top of the paddle.
    const float RIGHT_PAD_X = RIGHT_END - (PAD_WIDTH - 1.0) - PAD_DISTANCE;
    const float LEFT_IMPACT_X = LEFT_PAD_X + PAD_WIDTH;
    const float RIGHT_IMPACT_X = RIGHT_PAD_X - BALL_DIAMETER;
    const float PAD_MOVE_DISTANCE = 1.0;

    //Object initialization:
    float ballDeltaX = INIT_BALL_X_SPEED;
    float ballDeltaY = 0;
    float ballX = LEFT_MARGIN + (RIGHT_END - BALL_DIAMETER - LEFT_MARGIN) / 2;
    float ballY = TOP_MARGIN + (BOT_END - BALL_DIAMETER - TOP_MARGIN) / 2; /*the last number is to test stationary impacts*/
    float prevBallX = ballX;
    float prevBallY = ballY;

    float leftPadY = TOP_MARGIN + (BOT_END + 1 - TOP_MARGIN) / 2 - PAD_HEIGHT / 2;
    float rightPadY = TOP_MARGIN + (BOT_END + 1 - TOP_MARGIN) / 2 - PAD_HEIGHT / 2;
    int leftPadDir = 0;
    int rightPadDir = 0;
    drawRectangle(ledArray, true, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH);
    drawRectangle(ledArray, true, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH);

    //Score initialization
    int leftScore = 0;
    int rightScore = 0;
    int scoreDelay = 10; //Deactivates ball movement for a number of cycles after someone scores

    while(1) {
        frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_END, RIGHT_END);

        //Paddle movement:
        leftPadDir = getLeftInput();
        leftPadDir = movePaddle(leftPadDir, &leftPadY, PAD_MOVE_DISTANCE, PAD_HEIGHT, ledArray, LEFT_PAD_X, PAD_WIDTH, TOP_MARGIN, BOT_END);
        rightPadDir = getRightInput();
        rightPadDir = movePaddle(rightPadDir, &rightPadY, PAD_MOVE_DISTANCE, PAD_HEIGHT, ledArray, RIGHT_PAD_X, PAD_WIDTH, TOP_MARGIN, BOT_END);

        //Ball movement:
        if (scoreDelay > 0) {
            scoreDelay--;
        }
        else {
            drawBall(ledArray, false, prevBallY + 0.5, prevBallX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END);
            drawBall(ledArray, true, ballY + 0.5, ballX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END);
            prevBallX = ballX;
            prevBallY = ballY;

            ballY += ballDeltaY;
            ballDeltaY *= checkWallImpact(&ballY, BALL_DIAMETER, TOP_MARGIN, BOT_END);

            ballX += ballDeltaX;
            if(ballX <= LEFT_IMPACT_X) { //A paddle collision or score is possible
                drawRectangle(ledArray, true, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH);
                checkLeftImpact(ballY, &ballX, LEFT_IMPACT_X, &ballDeltaY, &ballDeltaX, BALL_DIAMETER, leftPadY, PAD_HEIGHT, MAX_BALL_Y_SPEED);

                //Check if the ball hit the edge of the paddle (not yet done) (non-critical functionality)

                //Checking if the ball hit the left edge of the screen:
                if(ballX + BALL_DIAMETER < LEFT_MARGIN) {
                    drawBall(ledArray, false, prevBallY + 0.5, prevBallX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END);
                    rightScore++; //Also change displayed score, if it exists.
                    if(rightScore == 3) {
                        drawRectangle(ledArray, false, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH);
                        drawRectangle(ledArray, false, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH);
                        printWinner(1);
                        break;
                    }
                    resetBall(&ballX, &ballY, &ballDeltaX, BALL_DIAMETER, TOP_MARGIN, BOT_END, LEFT_MARGIN, RIGHT_END, INIT_BALL_X_SPEED);
                    scoreDelay = 10;
                }
            }
            else if(ballX > RIGHT_IMPACT_X) { //A paddle collision or score is possible
                drawRectangle(ledArray, true, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH);
                checkRightImpact(ballY, &ballX, RIGHT_IMPACT_X, &ballDeltaY, &ballDeltaX, BALL_DIAMETER, rightPadY, PAD_HEIGHT, MAX_BALL_Y_SPEED);

                //Checking if the ball hit the edge of the paddle: (not yet done) (non-critical functionality)

                //Checking if the ball hit the right edge of the screen:
                if(ballX > RIGHT_END) {
                    drawBall(ledArray, false, prevBallY + 0.5, prevBallX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END);
                    leftScore++; //Also change displayed score, if it exists.
                    if(leftScore == 3) {
                        drawRectangle(ledArray, false, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH);
                        drawRectangle(ledArray, false, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH);
                        printWinner(2);
                        break;
                    }
                    resetBall(&ballX, &ballY, &ballDeltaX, BALL_DIAMETER, TOP_MARGIN, BOT_END, LEFT_MARGIN, RIGHT_END, INIT_BALL_X_SPEED);
                    scoreDelay = 10;
                }
            }
        }
    }
    return;
}
コード例 #7
0
ファイル: spong.c プロジェクト: andereld/spong
int main(int argc, char *argv[])
{
	/* initialize SDL and its subsystems */
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1) die();
	if (TTF_Init() == -1) die();

	/* set the height and width of the main window, as well as the number
	   of bits per pixel; this needs to be done prior to initializing the 
	   main window (*screen, below) */
	initWindowAttributes();

	/* the frame buffer */
	SDL_Surface *screen = SDL_SetVideoMode(W_WIDTH, W_HEIGHT,
			W_COLOR_DEPTH, SDL_HWSURFACE|SDL_FULLSCREEN);
	if (!screen) die();

	/* hide the mouse cursor */
	SDL_ShowCursor(SDL_DISABLE);

	/* the background color of the screen */
	const Uint32 clearColor = CLEAR_COLOR(screen->format);
	clearScreen(screen, clearColor);

	/* clearColor as an SDL_Color, for use with TTF */
	Uint8 r, g, b;
	SDL_GetRGB(clearColor, screen->format, &r, &g, &b);
	SDL_Color bgColor = { r: r, g: g, b: b };

	/* the score font */
	TTF_Font *font = TTF_OpenFont("resources/VeraMono.ttf", FONT_SIZE);
	if (!font) die();
	SDL_Color fontColor = FONT_COLOR;

	/* the score text; we'll allow three digits plus the terminating '\0' */
	char *lScoreStr = malloc(sizeof(char) * 4);
	char *rScoreStr = malloc(sizeof(char) * 4);
	if (!lScoreStr || !rScoreStr) die();
	SDL_Surface *lScore = NULL, *rScore = NULL;
	SDL_Rect lScorePos = { x: C_X+FONT_SIZE, y: C_HEIGHT/5,
		w: F_WIDTH, h: F_HEIGHT };
	SDL_Rect rScorePos = { x: (C_X+C_WIDTH)-3*FONT_SIZE, y: C_HEIGHT/5,
		w: F_WIDTH, h: F_HEIGHT };

	/* set up the playing court */
	Court *court = makeCourt(screen);
	if (!court) die();

	/* set up the players and their paddles */
	Player *lPlayer = makePlayer(screen);
	Player *rPlayer = makePlayer(screen);
	if (!lPlayer || !rPlayer) die();
	rPlayer->paddle.rect.x = C_X + C_WIDTH - P_WIDTH;

	/* add the ball */
	Ball *ball = makeBall(screen);
	if (!ball) die();

	/* because SDL_KEY(UP|DOWN) occurs only once, not continuously while
	   the key is pressed, we need to keep track of whether a key is
	   (still) pressed */
	bool lPlayerShouldMoveUp = false, lPlayerShouldMoveDown = false,
	     rPlayerShouldMoveUp = false, rPlayerShouldMoveDown = false;

	Uint32 startTime;	/* denotes the beginning of each iteration
				   of the main event loop */
	bool running = true;	/* true till the application should exit */
	while (running) {
		startTime = SDL_GetTicks();

		/* clear the previous frame's paddles and ball */
		SDL_FillRect(screen, &lPlayer->paddle.rect, clearColor);
		SDL_FillRect(screen, &rPlayer->paddle.rect, clearColor);
		SDL_FillRect(screen, &ball->rect, clearColor);

		/* clear the previous frame's score */
		SDL_FillRect(screen, &lScorePos, clearColor);
		SDL_FillRect(screen, &rScorePos, clearColor);

		/* redraw the walls in case they were clipped by the ball
		   in a previous frame */
		SDL_FillRect(screen, &court->upperWall, court->color);
		SDL_FillRect(screen, &court->lowerWall, court->color);

		/* get the current state of the players' controls */
		readPlayerInput(&running,
				&lPlayerShouldMoveUp, &lPlayerShouldMoveDown,
				&rPlayerShouldMoveUp, &rPlayerShouldMoveDown);

		/* save the current position of the paddles */
		lPlayer->paddle.prevY = lPlayer->paddle.rect.y;
		rPlayer->paddle.prevY = rPlayer->paddle.rect.y;

		/* move the paddles if appropriate */
		if (lPlayerShouldMoveUp)
			movePaddle(court, &lPlayer->paddle, UP);
		else if (lPlayerShouldMoveDown)
			movePaddle(court, &lPlayer->paddle, DOWN);
		if (rPlayerShouldMoveUp)
			movePaddle(court, &rPlayer->paddle, UP);
		else if (rPlayerShouldMoveDown)
			movePaddle(court, &rPlayer->paddle, DOWN);

		/* move the ball */
		moveBall(court, ball, lPlayer, rPlayer);

		/* update the score */
		updateScore(ball, lPlayer, rPlayer);

		/* update the on-screen score */
		if (lScore) SDL_FreeSurface(lScore);
		snprintf(lScoreStr, 4, "%2d", lPlayer->points);
		lScore = TTF_RenderText_Shaded(font, lScoreStr, fontColor,
				bgColor);
		if (rScore) SDL_FreeSurface(rScore);
		snprintf(rScoreStr, 4, "%2d", rPlayer->points);
		rScore = TTF_RenderText_Shaded(font, rScoreStr, fontColor,
				bgColor);

		/* draw the score */
		SDL_BlitSurface(lScore, NULL, screen, &lScorePos);
		SDL_BlitSurface(rScore, NULL, screen, &rScorePos);

		/* draw the paddles */
		SDL_FillRect(screen, &lPlayer->paddle.rect,
				lPlayer->paddle.color);
		SDL_FillRect(screen, &rPlayer->paddle.rect,
				rPlayer->paddle.color);

		/* draw the ball */
		SDL_FillRect(screen, &ball->rect, ball->color);

		/* render frame to screen */
		SDL_Flip(screen);

		/* keep a steady frame rate */
		Uint8 elapsedTime = SDL_GetTicks() - startTime;
		if (elapsedTime < FRAME_DURATION)
			SDL_Delay(FRAME_DURATION - elapsedTime);
	}

	/* free resources */
	free(lScoreStr);
	free(rScoreStr);
	free(court);
	free(lPlayer);
	free(rPlayer);
	free(ball);

	TTF_CloseFont(font);
	TTF_Quit();
	SDL_FreeSurface(lScore);
	SDL_FreeSurface(rScore);
	SDL_Quit();

	return EXIT_SUCCESS;
}
コード例 #8
0
bool controllerClass::get()
{
  Uint8 *keyStates;
  Uint8 keyDown[3]; //Need this since its not a good idea to write to keyStates for some reason
  shotTime += globalTicks;
  SDL_PumpEvents();
  keyStates = SDL_GetKeyState( NULL );
  keyDown[0] = keyStates[setting.keyLeft];
  keyDown[1] = keyStates[setting.keyRight];
  keyDown[2] = keyStates[setting.keyShoot];
  
  itemSelectTime += globalTicks;
  //Read joystick here so we can override keypresses if the joystick is digital
  //We shouldn't need to check if the joystick is enabled, since it won't be opened if its not enabled anyway.
  if(setting.joyEnabled && SDL_JoystickOpened(0))
  {
    joystickx = SDL_JoystickGetAxis(joystick, 0);
    joysticky = SDL_JoystickGetAxis(joystick, 1);
    joystickbtnA = SDL_JoystickGetButton(joystick, 0);
    joystickbtnB = SDL_JoystickGetButton(joystick, 1);


    if(joystickbtnA)
    {
      keyDown[2] = 1;
    }
    if(joystickbtnB && itemSelectTime > ITEMSELECTTIME)
    {
      itemSelectTime=0;
      gVar.shopBuyItem=1;
    }

    if(setting.joyIsDigital)
    {
      if(joystickx < -200)
      {
        keyDown[0]=1;
      } else if(joystickx > 200)
      {
        keyDown[1]=1;
      }
      if(joysticky < -200 && itemSelectTime > ITEMSELECTTIME)
      {
        itemSelectTime=0;
        gVar.shopNextItem = 1;
      } else if(joysticky > 200 && itemSelectTime > ITEMSELECTTIME)
      {
        itemSelectTime=0;
        gVar.shopPrevItem = 1;
      }
      
    } else {
      GLfloat x; //This is the actual traveling speed of the paddle
      if(joystickx > setting.JoyCalHighJitter)
      {
        x = joystickRightX * joystickx;
      } else if(joystickx < setting.JoyCalLowJitter)
      {
        x = -(joystickLeftX * joystickx);
      }
      
      if(joysticky < setting.JoyCalLowJitter && itemSelectTime > ITEMSELECTTIME)
      {
        itemSelectTime=0;
        gVar.shopNextItem = 1;
      } else if(joysticky > setting.JoyCalHighJitter && itemSelectTime > ITEMSELECTTIME)
      {
        itemSelectTime=0;
        gVar.shopPrevItem = 1;
      }
      //Move the paddle:
      movePaddle( paddle->posx += (x*globalMilliTicks) );
    }
  }

  #ifdef WITH_WIIUSE
  if(var.wiiConnect)
  {
    if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) {
      switch(wiimotes[0]->event)
      {
        case WIIUSE_EVENT:
          if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_TWO))
          {
            keyDown[2]=1;
          } else if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_ONE) && itemSelectTime > ITEMSELECTTIME)
          {
            gVar.shopBuyItem = 1;
            itemSelectTime=0;
          }else if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_UP) && itemSelectTime > ITEMSELECTTIME)
          {
            itemSelectTime=0;
            gVar.shopPrevItem = 1;
          }else if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_DOWN) && itemSelectTime > ITEMSELECTTIME)
          {
            itemSelectTime=0;
            gVar.shopNextItem = 1;
          } else if(WIIUSE_USING_ACC(wiimotes[0]))
          {
            motePitch = wiimotes[0]->orient.pitch;
            motePitch *=-1;
	  }

        break;
        case WIIUSE_DISCONNECT:
        case WIIUSE_UNEXPECTED_DISCONNECT:
          var.wiiConnect=0;
          cout << "WiiMote disconnected." << endl;
          wiiuse_cleanup(wiimotes, MAX_WIIMOTES);
        break;
      }
    }
    if(motePitch < -0.2 || motePitch > 0.2)
    {
      movePaddle( paddle->posx += ( moteAccel*motePitch)*globalMilliTicks );
    }
  }
  #endif

  //React to keystates here, this way, if joyisdig it will press keys
  if(keyDown[0])
  {
    accel+=globalMilliTicks*setting.controlAccel;
    if(accel > setting.controlMaxSpeed)
      accel=setting.controlMaxSpeed;
    movePaddle( paddle->posx - ( accel*globalMilliTicks) );
  } else if(keyDown[1])
  {
    accel+=globalMilliTicks*setting.controlAccel;
    if(accel > setting.controlMaxSpeed)
      accel=setting.controlMaxSpeed;
    movePaddle( paddle->posx + ( accel*globalMilliTicks) );
  } else {
      accel = setting.controlStartSpeed;
  }

  if(keyDown[2])
  {
    btnPress();
    return(1);
  } else {
    return(0);
  }
}