|
Page 1 of 1
|
[ 4 posts ] |
|
| Author |
Message |
|
chrismamo1
Rookie
Joined: Fri May 20, 2011 6:01 pm Posts: 20
|
 two games in robotc
I really don't want to describe them too much, so here's pong (it requires a servo hooked up to the NXT in port A to control your paddle  |  |  |  | Code: #include "naturalFTC.h"
int AIleft=5; int AIright;
int AIcannon;
int PlayerLeft=5; int PlayerRight;
int PlayerCannon;
int score=0;
int ScoreTimeout=0;
int c1; int c2; int c3; int c4; int d1; int d2; int d3; int d4; int e1; int e2; int e3; int e4;
int projectileYARR1[9]; int projectileYARR2[9]; int projectileXARR[9]; int f1=1; int LCount1=0;
//////////////////////////////////////////////////////////////////////////////////////////////// task ARrowcontrol() {
////////////////////////////////////////////////////
projectileYARR1[1]=9;
while(true) { ////////////////////////////////// if(nNxtButtonPressed==1) { while(nNxtButtonPressed==1) { PlayerLeft++; wait1Msec(32); } } else{} ////////////////////////////////// if(nNxtButtonPressed==2) { while(nNxtButtonPressed==2) { PlayerLeft--; wait1Msec(32); } } else{} //////////////////////////////////Start Solid Projectile Burst Sequence // if(Button("back")==true&&LCount1>=200) // { // projectileXARR[f1]=PlayerCannon; // f1++; // LCount1=0; // } // else{}
// if(f1=9){ // f1=1; // } // else{}
// nxtDrawLine(projectileXARR[1],projectileYARR1[1],projectileXARR[1],projectileYARR2[1]); // // wait10Msec(3); // LCount1++; // projectileYARR1[1]++; // projectileYARR2[1]=projectileYARR1[1]+3; //////////////////////////////////Saving the completion of this sequence for later }
////////////////////////////////////////////////////
return; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
task AIplayer() {
while(ScoreTimeout<1750) { if(nNxtButtonPressed==3){ PlayTone(4000,3); PlayTone(4500,3); } else{}
AIleft=AIleft+((random(10)*(-1))+random(10));
AIright=AIleft+4; AIcannon=AIleft+2;
nxtDrawRect(AIleft,49,AIright,59); nxtDrawLine(AIcannon,49,AIcannon,40); ///////////////////////////////////////////////////// if(AIleft<=0) { AIleft=AIleft+15; } else {
if(AIright>=99) { AIleft=AIleft-15; } else{}
} ///////////////////////////////////////////////////// wait1Msec(75); eraseDisplay();
ScoreTimeout++;
}
StopTask(ARrowcontrol);
while(true) { eraseDisplay();
wait10Msec(1);
nxtDisplayCenteredTextLine(4,"Score:%d",ScoreTimeout);
}
return; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
task main() { // nNxtExitClicks=30;
StartTask(ARrowcontrol); StartTask(AIplayer); //////////////////////////////////////////////////////////////////// while(true) { PlayerRight=PlayerLeft+4; PlayerCannon=PlayerLeft+2;
nxtDrawRect(PlayerLeft,1,PlayerRight,11); nxtDrawLine(PlayerCannon,11,PlayerCannon,20);
nxtDisplayTextLine(5,"%d",ScoreTimeout); nxtDisplayStringAt(AIleft-4,45,"%d",score); //////////////////////////////// if(nNxtButtonPressed==3) { nxtDrawLine(PlayerCannon,11,PlayerCannon,49);
if(PlayerCannon>=AIleft&&PlayerCannon<=AIright) { score++;
c1=random(22); c2=((-1)*random(18))+random(18); c3=49+c1; c4=AIcannon+c2; nxtSetPixel(c4,c3);
d1=random(22); d2=((-1)*random(18))+random(18); d3=49+d1; d4=AIcannon+d2; nxtSetPixel(d4,d3);
e1=random(22); e2=((-1)*random(18))+random(18); e3=49+e1; e4=AIcannon+e2; nxtSetPixel(e4,e3); } else{} } else{} ////////////////////////////////
} //////////////////////////////////////////////////////////////////// return; }
|  |  |  |  |
NaturalFTC.h is this file here, though i don't think i used it at all.  |  |  |  | Code: #ifndef __STATS_H__FTC #define __STATS_H__FTC
/////////////////////////////////////////////////////////////////////
float hypotenuse(float x,float y){ float hyp=sqrt((x*x)+(y*y)); return hyp; }
/////////////////////////////////////////////////////////////////////
int nMotorsAandB(int A,bool A1,int B,bool B1){
bMotorReflected[motorA]=A1; bMotorReflected[motorB]=B1;
motor[motorA]=A; motor[motorB]=B;
//nxtDisplayCenteredTextLine(1,"motorA:%d",A); //nxtDisplayCenteredTextLine(2,"motorB:%d",B);
return true; }
/////////////////////////////////////////////////////////////////////
bool Button(string a1){
bool b1=false;
if(a1=="right"&&nNxtButtonPressed==1) { b1=true; } else{}
if(a1=="left"&&nNxtButtonPressed==2) { b1=true; } else{}
if(a1=="enter"&&nNxtButtonPressed==3) { b1=true; } else{}
if(a1=="back"&&nNxtButtonPressed==0) { b1=true; } else{}
return b1; }
//////////////////////////////////////////////////////////////////
bool EnterButton(){ if(nNxtButtonPressed==3) { return true; } else{} }
//int joystickControlTank(bool leftA,bool rightA,int turnDown){ // bMotorReflected[motorA]=leftA; // motor[motorA]=joystick.joy1_y1/turnDown; // // bMotorReflected[motorB]=rightA; // motor[motorB]=joystick.joy1_y2/turnDown; // // return; //}
/////////////////////////////////////////////////////////////////////
#endif
|  |  |  |  |
and here's Pong  |  |  |  | Code: int paddle_X=20; int paddle_XRIGHT;
int i1;
task main() {
int score=0;
int SpeedIncreaseThreshold=100;
int LoopCount;
int ballPos_X=30; int ballPos_Y=25;
//seed(1324);
int BallMov_X=2; int BallMov_Y=2;
bool b1=true; bool b2=true;
if(BallMov_X<2) { BallMov_X++; } else { b1=false; }
if(BallMov_Y<2) { BallMov_Y++; } else { b2=false; }
PlayTone(800,10);
// nxtSetPixel(ballPos_X,ballPos_Y);
while(1==1) { paddle_X=paddle_X+((nMotorEncoder[motorA])/10); paddle_XRIGHT=paddle_X+25;
nMotorEncoder[motorA]=0;
eraseDisplay(); nxtDisplayStringAt(ballPos_X,ballPos_Y,"%d",LoopCount);
nxtDrawLine(paddle_X,5,paddle_XRIGHT,5);
wait10Msec(32);
ballPos_X=ballPos_X+BallMov_X; ballPos_Y=ballPos_Y+BallMov_Y; ///////////////////////////////////////// if(ballPos_X<=0) { BallMov_X=BallMov_X*(-1); PlayTone(800,10); } else{} ///////////////////////////////////////// if(ballPos_X>=92) { BallMov_X=BallMov_X*(-1); PlayTone(800,10); } else{} ///////////////////////////////////////// if(ballPos_Y>=59) { BallMov_Y=BallMov_Y*(-1); PlayTone(800,10); } else{} ///////////////////////////////////////// if(ballPos_Y<10) { if(ballPos_X>=paddle_X&&ballPos_X<=paddle_XRIGHT) { BallMov_Y=BallMov_Y*(-1); PlayTone(800,10); score++; } else { while(true) { nxtDisplayCenteredTextLine(3,"Score:%d",score); } } } else{} ////////////////////////////////////////
LoopCount++;
if(LoopCount==SpeedIncreaseThreshold) { if(BallMov_X>0){ BallMov_X=BallMov_X+1; } else{ BallMov_X=BallMov_X-1; }
if(BallMov_Y>0){ BallMov_Y=BallMov_Y+1; } else{ BallMov_Y=BallMov_Y-1; }
SpeedIncreaseThreshold=SpeedIncreaseThreshold+50; } else{} ////////////////////////////////////////
if(BallMov_X==0&&BallMov_Y==0) { BallMov_X=3; BallMov_Y=8; } else{}
} }
|  |  |  |  |
|
| Sun Aug 21, 2011 10:35 pm |
|
 |
|
mightor
Moderator
Joined: Wed Mar 05, 2008 8:14 am Posts: 2864 Location: Rotterdam, The Netherlands
|
 Re: two games in robotc
Hiya Chris,
Thank you for sharing!
- Xander
_________________| Some people, when confronted with a problem, think, "I know, I'll use threads," | and then two they hav erpoblesms. (@nedbat)| My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
| Mon Aug 22, 2011 1:09 am |
|
 |
|
chrismamo1
Rookie
Joined: Fri May 20, 2011 6:01 pm Posts: 20
|
 Re: two games in robotc
Thanks! I'm trying to clean up the code a bit though so it doesn't look like a robotc help guide threw up on the page  ~chris
|
| Mon Aug 22, 2011 10:17 pm |
|
 |
|
mightor
Moderator
Joined: Wed Mar 05, 2008 8:14 am Posts: 2864 Location: Rotterdam, The Netherlands
|
 Re: two games in robotc
Hiya Chris, Add liberal amounts of comments so people can learn from your programs, that's always the biggest improvement you can make  - Xander
_________________| Some people, when confronted with a problem, think, "I know, I'll use threads," | and then two they hav erpoblesms. (@nedbat)| My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
| Tue Aug 23, 2011 1:22 am |
|
|
|
Page 1 of 1
|
[ 4 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 3 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
|
|