为什么我做的反弹球得分有问题,

flightless__bird 2017-12-20 05:45:03
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>

int wide, high;
int ball_x, ball_y;
int ball_vx, ball_vy;
int board_mid, board_y;
int board_half;
int score;

void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}

void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info= {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

void start()
{
HideCursor();
wide = 30, high = 18;
ball_x = wide/2, ball_y = 0;
ball_vx = 1, ball_vy = 1;
board_mid = wide/2,board_y = high - 2;
board_half = 3;
score = 0;
}

void show()
{
int i,k;
gotoxy(0,0);
for(i = 0; i < high; i++)
{
for(k = 0; k < wide; k++)
{

if((i == ball_y) && (k == ball_x))
printf("o");
else if(i == board_y && (k >= board_mid-board_half&&k <= board_mid+board_half))
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("\nscore = %d\n",score);
}

void notdo()
{
static int slow = 0;
if(slow < 10)
slow++;
else
{
ball_x += ball_vx;
ball_y += ball_vy;
slow = 0;
}
if(ball_x == wide-1 || ball_x == 0)
ball_vx = -ball_vx;
if(ball_y == high || ball_y == 0)
ball_vy = -ball_vy;
if(ball_y == board_y-1)
{
if((ball_x >= board_mid-board_half) && (ball_x <= board_mid+board_half))
{
ball_vy = - ball_vy;
score++;
}
else
{
printf("game over !\n");
system("pause");
exit(0);
}
}

}

void isdo()
{
char input;
if(kbhit())
{
input = getch();
if(input=='d')
board_mid++;
if(input=='a')
board_mid--;

}
}
int main()
{
start();
while(1)
{
show();
notdo();
isdo();
}
return 0;
}
...全文
549 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-12-22
  • 打赏
  • 举报
回复
仅供参考:
// processor: x86
#include <windows.h>
#include <process.h>    /* _beginthread, _endthread */
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>

void Bounce( void *ch );
void CheckKey( void *dummy );

/* GetRandom returns a random integer between min and max. */
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))

BOOL repeat = TRUE;     /* Global repeat flag and video variable */
HANDLE hStdOut;         /* Handle for console window */
CONSOLE_SCREEN_BUFFER_INFO csbi;    /* Console information structure */

int main()
{
    CHAR    ch = 'A';

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

    /* Get display screen's text row and column information. */
   GetConsoleScreenBufferInfo( hStdOut, &csbi );

    /* Launch CheckKey thread to check for terminating keystroke. */
    _beginthread( CheckKey, 0, NULL );

    /* Loop until CheckKey terminates program. */
    while( repeat )
    {
        /* On first loops, launch character threads. */
        _beginthread( Bounce, 0, (void *) (ch++)  );

        /* Wait one second between loops. */
        Sleep( 1000L );
    }
	return 0;
}

/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
void CheckKey( void *dummy )
{
    _getch();
    repeat = 0;    /* _endthread implied */

}

/* Bounce - Thread to create and and control a colored letter that moves
 * around on the screen.
 *
 * Params: ch - the letter to be moved
 */
void Bounce( void *ch )
{
    /* Generate letter and color attribute from thread argument. */
    char    blankcell = 0x20;
    char    blockcell = (char) ch;
    BOOL    first = TRUE;
   COORD   oldcoord, newcoord;
   DWORD   result;


    /* Seed random number generator and get initial location. */
    srand( _threadid );
    newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
    newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
    while( repeat )
    {
        /* Pause between loops. */
        Sleep( 100L );

        /* Blank out our old position on the screen, and draw new letter. */
        if( first )
            first = FALSE;
        else
         WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
         WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );

        /* Increment the coordinate for next placement of the block. */
        oldcoord.X = newcoord.X;
        oldcoord.Y = newcoord.Y;
        newcoord.X += GetRandom( -1, 1 );
        newcoord.Y += GetRandom( -1, 1 );

        /* Correct placement (and beep) if about to go off the screen. */
        if( newcoord.X < 0 )
            newcoord.X = 1;
        else if( newcoord.X == csbi.dwSize.X )
            newcoord.X = csbi.dwSize.X - 2;
        else if( newcoord.Y < 0 )
            newcoord.Y = 1;
        else if( newcoord.Y == csbi.dwSize.Y )
            newcoord.Y = csbi.dwSize.Y - 2;

        /* If not at a screen border, continue, otherwise beep. */
        else
            continue;
        Beep( ((char) ch - 'A') * 1000, 500 );
    }
    /* _endthread given to terminate */
    _endthread();
}


33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧