還記得之前曾發文討論過的NXC彈跳球程式嗎?現在功能越加越多了!在 V1.2中加入了球拍可以打球,並且用NXT上的左右鍵控制球拍的位置。
(其實我的目標是寫出在NXT上跑的敲磚塊遊戲…)
以下是程式碼,歡迎大家自己試玩看看:
void DrawRacket(int p1);
void ClearRacket(int p2);
void Initialize(void);
int ball_x = 49;
int ball_y = 15;
task main()
{
int del_x = 1;
int del_y = 1;
int p = 0;
int counter = 0;
bool leftkey;
bool rightkey;
Initialize();
while(true)
{
leftkey = ButtonPressed(BTNLEFT,false);
rightkey = ButtonPressed(BTNRIGHT,false);
if( counter%3 == 0 )
{
ClearScreen();
}
DrawRacket(41+p);
ball_x = ball_x + del_x;
ball_y = ball_y + del_y;
CircleOut(ball_x,ball_y,1);
if( ball_x <= 2 || ball_x>=96) // 偵測球是否打到左/右邊的牆
{
del_x = del_x*-1;
PlayTone(440,1);
}
if( ball_y >= 61) // 偵測球是否打到上/下邊的牆
{
del_y = del_y*-1;
PlayTone(440,1);
}
if(leftkey == true) // 偵測方向鍵是否壓下
{
if( 41+p > 2 )
{
ClearRacket(41+p);
p = p-3;
DrawRacket(41+p);
}
}
if(rightkey == true)
{
if( 41+p <= 80)
{
ClearRacket(41+p);
p = p+3;
DrawRacket(41+p);
}
}
if( ball_y == 13 ) // 偵測球是否打到球拍
{
if( (40+p) <= ball_x && ball_x <= (57+p) )
{
del_y = del_y*-1;
PlayTone(440,1);
}
}
if( ball_y == 3 ) // 偵測球是否碰到底部
{
PlayTone(330,1000);
Wait(2000);
break;
}
&
nbsp; counter++;
Wait(30);
}
TextOut(23,35,"Game Over!");
Wait(3000);
ClearScreen();
}
void Initialize(void)
{
bool centerkey = false;
DrawRacket(41);
CircleOut(49,15,1);
until( centerkey == true )
{
centerkey = ButtonPressed(BTNCENTER,true);
TextOut(5,45,"Press ORNG btn");
TextOut(5,30,"to start!");
}
TextOut(5,45," ");
TextOut(5,30," ");
}
void DrawRacket(int p1)
{
int y = 10;
int w = 15;
RectOut(p1,y,w,2);
}
void ClearRacket(int p2)
{
for(int y=10; y>7; y–)
{
TextOut(p2,y," ");
}
}
實測影片:
接下來就是加磚塊了!