2019程序设计报告
项目名称:反弹的小球
实验项目功能描述:显示一个小球和挡板,小球按照指定速度进行轨迹运动,在碰到挡板后,小球反弹,并继续原来的速度进行运动,直到下落,游戏停止!
实验代码:#include<stdio.h>
include<stdlib.h>
include<conio.h>
include<windows.h>
define High 15
define Width 20
//游戏画面尺寸
int ball_x,ball_y;
int ball_vx,ball_vy;
int position_x,position_y;
int ridus;
int left,right;
int canvas[High][Width] = {0};
//0为空格,1为小球,2为挡板*
void gotoxy(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup()
{
ball_x = 0;
ball_y = Width/2;
ball_vx = 1;
ball_vy = 1;
canvas[ball_x][ball_y] = 1;
ridus = 5;
position_x = High - 1;
position_y = Width/2;
left = position_y - ridus;
right = position_y + ridus;
int k;
for(k=left;k<=right;k++)
canvas[position_x][k] = 2;
}
void show()
{
gotoxy(0,0);
int i,j;
for(i0;i<High;i++)
{
for(j=0;j<Width;j++)
{
if(canvas[i][j]0)
printf(" ");
else if(canvas[i][j]1)
printf("0");
else if(canvas[i][j]2)
printf("*");
}
printf("|
");
}
for(j=0;j<Width;j++)
printf("-");
}
void updateWithoutInput()
{
if(ball_x==High-2)
{
if((ball_y>=left)&&(ball_y<=right))
{
printf("
");
system("pause");
exit(0);
}
}
canvas[ball_x][ball_y] = 0;
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if((ball_x==0)||(ball_x==High-2))
ball_vx = -ball_vx;
if((ball_y==0)||(ball_y==Width-1))
ball_vy = -ball_vy;
canvas[ball_x][ball_y] = 1;
Sleep(150);
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if(input == 'a'&&left>0)
{
canvas[position_x][right] = 0;
position_y--;
left = position_y - ridus;
right = position_y + ridus;
canvas[position_x][left] = 2;
}
if(input == 'd'&&right < Width - 1)
{
canvas[position_x][left] = 0;
position_y++;
left = position_y - ridus;
right = position_y + ridus;
canvas[position_x][right] = 2;
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
模块介绍:
实现界面
代码托管链接:https://gitee.com/website_jianing/wen/commit/120440fb3d2dac785e5323abf190626c3405d1ea
实验总结:本题其实有很多东西都是类似于飞机游戏的,开始在实现界面没有小球反弹,后来才发现没有在碰撞后没有让小球运动,其他其实问题不大,照着书打,问题并不是很大,再接再厉吧!哈哈