这是源代码。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define High 16 // 游戏画面尺寸
#define Width 16
#define radius 3 // 半径
// 全局变量
int position_x,position_y; // 挡板位置
int ball_x,ball_y; //小球位置
int ball_vx,ball_vy; //小球速度
int score; //得分
int left,right; //左右大小
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 startup() // 数据初始化
{
position_y = High;
position_x = Width/2;
ball_x=2/Width;
ball_y=1;
left=position_x-radius;
right=position_x+radius;
ball_vx=1;
ball_vy=1;
score = 0;
void show() // 显示画面
{
gotoxy(0,0); // 光标移动到原点位置,以下重画清屏
int i,j;
for (i=0;i<High;i++)
{
for (j=0;j<Width;j++)
{
if ((i==ball_y)&&(j==ball_x))
printf("O"); // 输出小球
else if (i==High) //输出下边界
printf("_");
else if (i==Width) //输出右边界
printf("|");
else if ((i==High)&&(j>=left)&&(j<=right))
printf("*"); // 输出挡板
else
printf(""); // 输出空格
}
printf("\n");
}
printf("得分:%d\n",score);
Sleep(20);
}
void updateWithoutInput() // 与用户输入无关的更新
{
if(ball_y==High)
{
if((ball_x>=left)&&(ball_x<=right))
{
score++;
ball_vx=-ball_vx;
}
else
{
printf("game over!!");
exit(0);
}
}
ball_x+=ball_vx;
ball_y+=ball_vy;
if (ball_x==0||ball_x==Width)
ball_vx=-ball_vx;
if (ball_y==High-1||ball_y==0)
ball_vy=-ball_vy;
sleep(50);
}
void updateWithInput() // 与用户输入有关的更新
{
char input;
if(kbhit()) // 判断是否有输入
{
input = getch(); // 根据用户的不同输入来移动,不必输入回车
if (input == 'a' )
{
position_x--;
left=position_x-radius;
right=position_x+radius;
}
else if (input == 'd')
{
position_x++;
left=position_x-radius;
right=position_x+radius;
}
}
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define High 16 // 游戏画面尺寸
#define Width 16
#define radius 3 // 半径
// 全局变量
int position_x,position_y; // 挡板位置
int ball_x,ball_y; //小球位置
int ball_vx,ball_vy; //小球速度
int score; //得分
int left,right; //左右大小
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 startup() // 数据初始化
{
position_y = High;
position_x = Width/2;
ball_x=2/Width;
ball_y=1;
left=position_x-radius;
right=position_x+radius;
ball_vx=1;
ball_vy=1;
score = 0;
void show() // 显示画面
{
gotoxy(0,0); // 光标移动到原点位置,以下重画清屏
int i,j;
for (i=0;i<High;i++)
{
for (j=0;j<Width;j++)
{
if ((i==ball_y)&&(j==ball_x))
printf("O"); // 输出小球
else if (i==High) //输出下边界
printf("_");
else if (i==Width) //输出右边界
printf("|");
else if ((i==High)&&(j>=left)&&(j<=right))
printf("*"); // 输出挡板
else
printf(""); // 输出空格
}
printf("\n");
}
printf("得分:%d\n",score);
Sleep(20);
}
void updateWithoutInput() // 与用户输入无关的更新
{
if(ball_y==High)
{
if((ball_x>=left)&&(ball_x<=right))
{
score++;
ball_vx=-ball_vx;
}
else
{
printf("game over!!");
exit(0);
}
}
ball_x+=ball_vx;
ball_y+=ball_vy;
if (ball_x==0||ball_x==Width)
ball_vx=-ball_vx;
if (ball_y==High-1||ball_y==0)
ball_vy=-ball_vy;
sleep(50);
}
void updateWithInput() // 与用户输入有关的更新
{
char input;
if(kbhit()) // 判断是否有输入
{
input = getch(); // 根据用户的不同输入来移动,不必输入回车
if (input == 'a' )
{
position_x--;
left=position_x-radius;
right=position_x+radius;
}
else if (input == 'd')
{
position_x++;
left=position_x-radius;
right=position_x+radius;
}
}
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
return 0;
}