#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
int width,high;
int bull_x,bull_y;
int bull_vx,bull_vy;
int position_x,position_y;
int left,right;
int r,course;
void gotoxy(int x, int y) //光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle( STD_OUTPUT_HANDLE);
COORD pos;
pos.X =x;
pos.Y =y;
SetConsoleCursorPosition (handle,pos) ;
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
starup() //设置初始值
{
high=30;
width=20;
bull_x=width/2;
bull_y=0;
bull_vx=1;
bull_vy=1;
position_x=width/2;
position_y=high-1;
r=2;
left=position_x-2;
right=position_x+2;
course=0;
}
show() //用户显示画面
{
int i,j;
for(i=0;i<=high;i++)
{
for(j=0;j<=width;j++)
{
if((i==bull_y)&&(j==bull_x))
printf("0");
else if(i==high)
printf("-");
else if(j==width)
printf("|");
else if((i==position_y)&&(j>=left)&&(j<=right))
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("得分:%d",course);
}
undatewhihouupput() //与用户有无的更新
{
static int speed=0,a=10;
if(speed<a)
speed++;
else if(speed=a)
{
bull_x=bull_x+bull_vx;
bull_y=bull_y+bull_vy;
speed=0;
}
int upput;
if(kbhit())
upput=getch();
if(upput=='a')
{
if(left!=0)
{
left--;
right--;
}
}
if(upput=='d')
{
if(left!=15)
{
left++;
right++;
}
}
if((bull_y==high-2)&&(bull_x>=left)&&(bull_x<=right))
{
bull_vy=-bull_vy;
course++;
}
}
undatewhihupput() //与用户有关的更新
{
if((bull_x<0))
bull_x=0;
if(bull_x>width)
bull_x=width;
if(bull_y<0)
bull_y=0;
if(bull_y>high)
bull_y=high;
if((bull_x==0)||(bull_x==width))
bull_vx=-bull_vx;
if(bull_y==0)
{
bull_vy=-bull_vy;
}
if(bull_y==high)
{
bull_x=width/2;
bull_y=0;
course--;
course--;
}
}
int main()
{
starup(); //设置初始值
while(1)
{
gotoxy(0,0);
HideCursor();
show(); //用户显示画面
undatewhihouupput() ; //与用户有无的更新
undatewhihupput(); //与用户有关的更新
}
}
为什么显示得分course不是++而是乱加,而--是正常的呢?