1

我刚开始使用 Visual Studio 2008,我之前使用过代码块,关于 Visual Studio 的一切都令人困惑。

// Emilie Sutterlin 的 PONG,97 年 10 月 10 日 // 描述:这个程序是乒乓球游戏。//================================================= =================== #include #include #include #include #include #include #include #include //==========全局和常量========================= const MAX = 10; int lprow=2,rprow=2,col=2,colinc=2,rowinc=1,row=1,oldrow,oldcol,scorr=0,scorl=0;

//==========Prototypes===================================
void ball_bounce ();
void paddle_pong ();
//======================================================
int main (void)
{
clrscr (); fflush(stdin); int num;
textbackground (BLACK);
textcolor (WH|TE); clrscr(); _setcursortype(_NOCURSOR);
//|NTRODUCT|ON
cout<<"WELCOME TO PONG"<<endl<<"This is a two person game."
<<endl<<"The object is to gain points by having your opponent miss hitting the ball with    the paddle."
<<endl<<"The game ends when a player reaches 20 points."
<<endl<<"You can stop the game early anytime by pressing either the Enter or Escape   key."
<<endl<<"The paddle on the left can be controlled with the arrow up and down keys."
<<endl<<"The paddle on the right can be controlled using the page up and down keys."
<<endl<<"Press any key to begin";
getch(); clrscr();
//CREATE PADDLES
gotoxy (1,2); cout<<"|"<<endl<<"|"<<endl<<"|"<<endl<<"|"<<endl<<"|"<<endl;
gotoxy (79,2); cout<<"|";
gotoxy (79,3); cout<<"|";
gotoxy (79,4); cout<<"|";
gotoxy (79,5); cout<<"|";
gotoxy (79,6); cout<<"|";
//RUN BOTH FUNT|ONS "TOGETHER"
for (;;)
{
ball_bounce ();
fflush (stdin);
paddle_pong ();
}//end for loop
}//endmain
//--------------------------------------------------------------
void ball_bounce ()
{
int oldcol,oldrow;
while (!kbhit())
{
oldcol = col;
oldrow = row;

if (row == 1) rowinc = 1;
if (row == 23) rowinc = -1;
if ((lprow <= row && row < lprow+5) && col == 2) colinc = 1;
if ((rprow <= row && row < rprow+5) && col == 78) colinc = -1;

if (col == 1)
{
scorl += 1;
gotoxy (78,24); cout<<scorl;
if (scorl == 20)
{
gotoxy (22, 10); cout<<"The winner is the player on the right.";
_setcursortype(_NOCURSOR);
}//end if right wins statement
colinc = 1;
 }//end if right scores statement

if (col == 80)
{
scorr += 1;
gotoxy (1,24); cout<<scorr;
if (scorr == 20)
{
gotoxy (22,10); cout<<"The winner is the player on the left.";
_setcursortype(_NOCURSOR);
}//end if left wins statement
colinc = -1;
}//end if left scores statement
//set coordinates for next ball to be drawn:
col += colinc;
row += rowinc;

gotoxy (col, row); cout <<"o";//draws new ball
gotoxy (oldcol, oldrow); cout <<" ";//erases old ball
delay (75);
}//end while loop
}//end ball_bounce function
//-------------------------------------------------------------------
void paddle_pong ()
{
unsigned ch;
ch = getch ();

if (ch == 13||ch==27)
{
gotoxy(10,10); cout<<"GAME OVER. Press any key to exit.";
getch();
_setcursortype(_NOCURSOR);
exit (0);
}
else if (ch != 0);
else
{
switch (getch ())
{
case 73:
{
if (1<lprow)
{
lprow -=1;
gotoxy (1, lprow); cout<<"|";
gotoxy (1, lprow+5); cout<<" ";
}//end if
break;
}//end case 73

case 81:
{
if (lprow<19)
{
lprow += 1;
gotoxy (1, lprow+4); cout<<"|";
gotoxy (1, lprow-1); cout<<" ";
}//end if
break;
 }//end case 81

case 72:
{
if (1<rprow)
{
rprow -=1 ;
gotoxy (79, rprow); cout<<"|";
gotoxy (79, rprow+5); cout<<" ";
}//end if
break;
}//end case 72

case 80:
{
if (rprow<19)
{
rprow += 1;
gotoxy (79, rprow+4); cout<<"|";
gotoxy (79, rprow-1); cout<<" ";
}//end if
break;
}//end case 80
}//end switch
}//end else
}//end paddle function
//END OF PROGRAM BY EMILIE SUTTERLIN
4

1 回答 1

2

通过使用:

#include <iostream>

因为这是标准库为您提供的。

于 2013-07-20T15:00:36.900 回答