View unanswered posts | View active topics It is currently Tue Nov 25, 2014 7:43 pm






Reply to topic  [ 3 posts ] 
Pong game ? 
Author Message
Rookie
User avatar

Joined: Sun Nov 11, 2012 4:26 am
Posts: 8
Location: France
Post Pong game ?
Hye,

Did you remember first video game ?


If you turn the motor the racket will move.
Press the sensor touch to start the game .

:lol:

Program with 3.55 B2 (FW 9.58)

Code:
#pragma config(Sensor, S2,     TOR,            sensorTouch)
#pragma config(Motor,  motorA,          GAUCHE,           tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//



const int LONG_RAQUETTE = 8;
const int LARG_BALLE = 2;
const int LARG_RAQUETTE = 2;
const int XG_RAQUETTE = LARG_RAQUETTE;


int yG_Raquette;
int xBalle,yBalle;
int bDirection;

bool bPerdu;

task Balle(){
int mxBalle;
int myBalle;
mxBalle = xBalle;
myBalle = yBalle;
bDirection = 0;

while(!SensorValue[TOR]){
}

while(true){
  switch(bDirection){
  case 0:
    xBalle++;
    yBalle++;
    if (yBalle > 63 - LARG_BALLE ){
      yBalle--;
      yBalle--;
      bDirection = 1;
    }
    if (xBalle > 99 - LARG_BALLE ){
      xBalle--;
      xBalle--;
      bDirection = 3;
    }
  break;
  case 1:
    xBalle++;
    yBalle--;
    if (xBalle > 99 - LARG_BALLE ){
      xBalle--;
      xBalle--;
      bDirection = 2;
    }
    if (yBalle < 0 ){
      yBalle++;
      yBalle++;
      bDirection = 0;
    }
   break;
   case 2:
    xBalle--;
    yBalle--;
    if (yBalle < 0 ){
      yBalle++;
      yBalle++;
      bDirection = 3;
    }
    if (xBalle < 1 + LARG_BALLE + LARG_RAQUETTE){
       if((yBalle >= yG_Raquette - LONG_RAQUETTE) & (yBalle <= yG_Raquette + LONG_RAQUETTE)){
          xBalle++;
         xBalle++;
        bDirection = 1;
      }else{
      bPerdu = true;
      StopTask (Balle);
     
      }
    }
   break;
   case 3:
    xBalle--;
    yBalle++;
    if (xBalle < 1 + LARG_BALLE + LARG_RAQUETTE ){
       if((yBalle >= yG_Raquette - LONG_RAQUETTE) & (yBalle <= yG_Raquette + LONG_RAQUETTE)){
        xBalle++;
        xBalle++;
        bDirection = 0;
      }else{
      bPerdu = true;
      StopTask (Balle);
      }
    }
    if (yBalle > 63 - LARG_BALLE ){
      yBalle--;
      yBalle--;
      bDirection = 2;
    }
   break;
  }

  wait10Msec(3);
  if ((mxBalle != xBalle) | (myBalle != yBalle)){

    nxtEraseRect( mxBalle, myBalle, mxBalle + LARG_BALLE, myBalle + LARG_BALLE);
    nxtDrawRect( xBalle, yBalle, xBalle + LARG_BALLE, yBalle + LARG_BALLE);
    mxBalle = xBalle;
    myBalle = yBalle;
  }
}
return;
}

task Gauche_Raquette(){
int yGm;
int mCodeur;
mCodeur =0;
  yGm = 31;
  nMotorEncoder[GAUCHE] = 0;
  nxtDrawRect(XG_RAQUETTE - LARG_RAQUETTE, yGm + LONG_RAQUETTE,XG_RAQUETTE + LARG_RAQUETTE  , yGm - LONG_RAQUETTE);
  while(true){
    mCodeur = nMotorEncoder[GAUCHE]/2 + 31;
    if (mCodeur > 63 - LONG_RAQUETTE){
      yG_Raquette = 63 - LONG_RAQUETTE;
    }
    else{
      if (mCodeur < LONG_RAQUETTE){
        yG_Raquette = LONG_RAQUETTE;
      }
      else {
        yG_Raquette = mCodeur;
      }
    }
    if (yG_Raquette != yGm){
      nxtEraseRect(XG_RAQUETTE - LARG_RAQUETTE, yGm + LONG_RAQUETTE,XG_RAQUETTE + LARG_RAQUETTE  , yGm - LONG_RAQUETTE);
      nxtDrawRect(XG_RAQUETTE - LARG_RAQUETTE, yG_Raquette + LONG_RAQUETTE,XG_RAQUETTE + LARG_RAQUETTE  , yG_Raquette - LONG_RAQUETTE);
      yGm = yG_Raquette;
    }
 }
  return;

}


task main(){
eraseDisplay();
xBalle = XG_RAQUETTE+LARG_RAQUETTE+1;
yBalle =random(63-LARG_BALLE)+LARG_BALLE;

nxtDrawRect( xBalle, yBalle, xBalle + LARG_BALLE, yBalle + LARG_BALLE);

StartTask(Gauche_Raquette);
StartTask(Balle);
while(true){
   if (bPerdu){
      StopTask(Gauche_Raquette);
    nxtDisplayCenteredBigTextLine(1, "You Lose");
      nxtDisplayTextLine(4, "    create by");
      nxtDisplayTextLine(6, "     Robotc");
      nxtDisplayTextLine(7, "   experiences");
   }
}
return;

}

_________________
ROBOTC 3.61.1

French guy - I Love ROCK and ROBOTS


Wed Jan 16, 2013 2:29 pm
Profile
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post Re: Pong game ?
Yeah, pong is awesome! I have a similar version, although it uses two touch sensors. I like your use of a motor!
Code:
#pragma config(Sensor, S2,     LeftButton,     sensorTouch)
#pragma config(Sensor, S3,     RightButton,    sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// Change this value to adjust the fps.
#define FRAMESPERSECOND 20.0

// Set to 0 if you don't want special bouncing.
#define BOUNCEADJUSTMENT 10.0

/*

Sensor Setup:
Left touch sensor in port S2
Right touch sensor in port S3

Display Screen:
Bottom left corner is (0,0).
Top right corner is (99, 63)
There are 8 text lines from 0 to 7, 0 at top.

*/

// Stores screen boundaries.
typedef struct
{
  short Upper;
  short Lower;
  short Left;
  short Right;
} Boundaries;

// Stores processed sensor data from the two sensors.
typedef struct
{
  bool bRightButtonPressed;
  bool bLeftButtonPressed;
} KeyPresses;

// Represents a 2-dimensional coordinate.
typedef struct
{
  float X;
  float Y;
} CartesianPoint;

// Represents a 2-dimensional velocity vector.
typedef struct
{
  float X;
  float Y;
} VelocityVector;

//
typedef struct
{
  CartesianPoint Position;
  short Width;
  short Height;
} Paddle;

typedef struct
{
  CartesianPoint Position;
  VelocityVector Velocity;
  short Radius;
} Ball;

// Declare necessary struct instances.
Boundaries BoxBoundaries;
Ball BouncyBall;
Paddle PlayerPaddle;
KeyPresses currentKeyPresses;

// Stores number of milliseconds per frame.
float framerate;

// Stores the player's score.
int numberBounces = 0;

// Initialize structs.
void Initialize()
{
  ClearTimer(T1);

  // Represents number of milliseconds for each frame.
  framerate = 1000.0 / (FRAMESPERSECOND);

  // Initialize  BoxBoundaries
  BoxBoundaries.Upper = 63;
  BoxBoundaries.Lower = 0;
  BoxBoundaries.Right = 99;
  BoxBoundaries.Left = 0;

  // Initialize BouncyBall
  BouncyBall.Position.X = 50;
  BouncyBall.Position.Y = 32;
  BouncyBall.Velocity.X = 1;
  BouncyBall.Velocity.Y = 1;
  BouncyBall.Radius = 4;

  // Initialize PlayerPaddle
  PlayerPaddle.Width = 16;
  PlayerPaddle.Height = 4;
  PlayerPaddle.Position.Y = 1 + BoxBoundaries.Lower + (PlayerPaddle.Height / 2);
  PlayerPaddle.Position.X = 50;
}

// Functions for retreiving pressed keys.  Definitions are after task main()
bool getLeftKeyPress();
bool getRightKeyPress();
void getKeyPresses();

// Has the ball has been missed and hit the bottom of the screen?
bool bMissedBall = false;

// Prevents ball frome exiting the screen, bounces on edges, etc.
void BallConstraints()
{
  if(BouncyBall.Position.X > BoxBoundaries.Right - BouncyBall.Radius)
  {
    BouncyBall.Position.X = BoxBoundaries.Right - BouncyBall.Radius;
    BouncyBall.Velocity.X *= -1;
  }
  if(BouncyBall.Position.X < BoxBoundaries.Left + BouncyBall.Radius)
  {
    BouncyBall.Position.X = BoxBoundaries.Left + BouncyBall.Radius;
    BouncyBall.Velocity.X *= -1;
  }
  if(BouncyBall.Position.Y > BoxBoundaries.Upper - BouncyBall.Radius)
  {
    BouncyBall.Position.Y = BoxBoundaries.Upper - BouncyBall.Radius;
    BouncyBall.Velocity.Y *= -1;
  }
  if(BouncyBall.Position.Y < BoxBoundaries.Lower + BouncyBall.Radius + PlayerPaddle.Height)
  {
    // If touching paddle
    if(abs(BouncyBall.Position.X - PlayerPaddle.Position.X) < PlayerPaddle.Width / 2 + BouncyBall.Radius)
    {
      // Bounce
      BouncyBall.Position.Y = BoxBoundaries.Lower + BouncyBall.Radius + PlayerPaddle.Height;
      BouncyBall.Velocity.Y *= -1;

      // Change x velocity proportional to distance to center of paddle.
      if((BOUNCEADJUSTMENT) != 0)
        BouncyBall.Velocity.X += (BouncyBall.Position.X - PlayerPaddle.Position.X) / (BOUNCEADJUSTMENT);

      // Increment score
      numberBounces++;
      BouncyBall.Velocity.Y *= 1.1;
    }

    if(BouncyBall.Position.Y < BoxBoundaries.Lower - BouncyBall.Radius)
      bMissedBall = true;
  }
  return;
}

// Prevents paddle from leaving screen.
void PaddleConstraints()
{
  if(PlayerPaddle.Position.X > BoxBoundaries.Right - PlayerPaddle.Width / 2)
    PlayerPaddle.Position.X = BoxBoundaries.Right - PlayerPaddle.Width / 2;
  if(PlayerPaddle.Position.X < BoxBoundaries.Left + PlayerPaddle.Width / 2)
    PlayerPaddle.Position.X = BoxBoundaries.Left + PlayerPaddle.Width / 2;
  return;
}

// Calls constraint functions
void calculatePhysics()
{
  BallConstraints();
  PaddleConstraints();
  return;
}

// Get keypresses.  Update physics.  If clauses.
void ConstantAction()
{
  // Stores to KeyPresses currentKeyPresses.
  getKeyPresses();
  // Deals with constraints and bounces.
  calculatePhysics();
  return;
}

// Change positions by velocities, etc.
void UpdatePositions()
{
  BouncyBall.Position.X += BouncyBall.Velocity.X;
  BouncyBall.Position.Y += BouncyBall.Velocity.Y;

  // Paddle right/left
  if(currentKeyPresses.bRightButtonPressed)
    PlayerPaddle.Position.X += 5;
  if(currentKeyPresses.bLeftButtonPressed)
    PlayerPaddle.Position.X -= 5;

  return;
}

// Draws ball and paddle to screen.
void Draw()
{
  eraseDisplay();

  nxtDrawCircle(
  (int)(BouncyBall.Position.X - BouncyBall.Radius),
  (int)(BouncyBall.Position.Y + BouncyBall.Radius),
  (int)(2 * BouncyBall.Radius));

  nxtDrawRect(
  (int)(PlayerPaddle.Position.X - PlayerPaddle.Width / 2),
  (int)(PlayerPaddle.Position.Y - PlayerPaddle.Height / 2),
  (int)(PlayerPaddle.Position.X + PlayerPaddle.Width / 2),
  (int)(PlayerPaddle.Position.Y + PlayerPaddle.Height /2));

  nxtDisplayTextLine(0, "Score: %i", numberBounces);

  return;
}

// Update positions.  Draw screen.
void FrameDependentAction()
{
  Draw();
  UpdatePositions();
  return;
}

task main()
{
  Initialize();
  while(bMissedBall == false)
  {
    if(time1[T1] > framerate)
    {
      FrameDependentAction(); // Updating positions, drawing
      ClearTimer(T1);
    }

    ConstantAction(); // Getting input, calculating physics
  }
}

bool getLeftKeyPress()
{
  if(SensorValue[LeftButton] == 1)
    return true;
  else
    return false;
}

bool getRightKeyPress()
{
  if(SensorValue[RightButton] == 1)
    return true;
  else
    return false;
}

void getKeyPresses()
{
  currentKeyPresses.bLeftButtonPressed = getLeftKeyPress();
  currentKeyPresses.bRightButtonPressed = getRightKeyPress();
  return;
}


Wed Jan 16, 2013 3:38 pm
Profile
Rookie
User avatar

Joined: Sun Nov 11, 2012 4:26 am
Posts: 8
Location: France
Post Re: Pong game ?
Hye ambercu,

I choose the motor because I can use a second one to play tennis game ...

coming soon :mrgreen:

_________________
ROBOTC 3.61.1

French guy - I Love ROCK and ROBOTS


Wed Jan 16, 2013 3:49 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.