一、弹跳的小球
1、静止的小球
利用printf函数在屏幕坐标(x,y)处显示一个静止的小球字符“〇”,注意屏幕坐标系的原点在左上角。
#include <stdio.h>
int main()
{
int i,j;
int x=5;
int y=10;
//输入小球上的空行
for(i=0;i<x;i++)
{
printf("\n");
}
//输入小球左边的空格
for(y=0;j<y;j++)
{
printf(" ");
}
printf("〇");
printf("\n") ;
return 0;
}
2、下落的小球
改变小球的坐标变量,即让小球的x坐标增加,从而让小球下落,在每次显示之前使用清屏函数systerm(“cls”),注意需要包含头文件#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int i,j;
int x=1;
int y=10;
for(x=1;x<10;x++)
{
system("cls"); //清屏函数
//输入小球上面的空行
for(i=0;i<x;i++)
{
printf("\n");
}
//输入小球前面的空格
for(j=0;j<y;j++)
{
printf(" ");
}
printf("〇"); //输出小球〇
printf("\n");
Sleep(50);
}
return 0;
}
3、上下弹跳的小球
在上一步代码的基础上增加纪录速度的变量velocity,小球的新位置=旧位置x+速度velocity。当判断小球到达边界时改变方向,即改变velocity的正负号。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int i,j;
int x=0;
int y=5;
int velocity_x = 1;
int velocity_y = 1;
int left = 0;
int right = 20;
int top = 0;
int bottom = 10;
while (1)
{
x = x+velocity_x;
y = y+velocity_y;
system("cls"); //清屏函数
//输入小球上面的空行
for(i=0;i<x;i++)
{
printf("\n");
}
//输入小球前面的空格
for(j=0;j<y;j++)
{
printf(" ");
}
printf("o"); //输出小球〇
printf("\n");
Sleep(100);
if( (x == top) || (x == bottom) )
{
velocity_x = -velocity_x;
}
if( (y == left) || (y == right) )
{
velocity_y = -velocity_y;
}
}
return 0;
}
4、斜着跳的小球
主要思路为增加x,y两个方向的速度控制变量velocity_x,velocity_y,初始值为1;velocity_x碰到上下边界后改变正负号,velocity_y碰到左右边界后改变正负号
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int i,j;
int x=5;
int y=10;
int height = 20;
int velocity = 1;
while(1)
{
x = x + velocity;
system("cls");
//输入小球上面的空行
for(i=0;i<x;i++)
{
printf("\n");
}
//输入小球前面的空格
for(j=0;j<y;j++)
{
printf(" ");
}
printf("o"); //输出小球〇
printf("\n");
Sleep(100);
if(x==height)
{
velocity = -velocity;
}
if(x==0)
{
velocity = -velocity;
}
}
return 0;
}
控制速度(使用sleep函数)
#include <windows.h>
…
Sleep(100); //单位ms(毫秒)
二、Getch控制小飞机
1、scanf控制
利用scanf输入不同的字符,按a,s,d,w键改变x,y的值,从而控制飞机*字符上下左右移动
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j;
int x=5;
int y=10;
char input;
while(1)
{
system("cls"); //清屏函数
//输出飞机上面的空行
for(i=0;i<x;i++)
printf("\n");
//输出飞机左边的空格
for(j=0;j<y;j++)
printf(" ");
printf("*");//输出飞机
printf("\n");
scanf("%c",&input);
if(input=='a');
y--;
if(input=='d');
y++;
if(input=='w');
x--;
if(input=='s');
x++;
}
return 0;
}
2、getch控制
Scanf()函数要求每输入一个字符按回车后才能执行,交互效果不好,因此第二步使用一个新函数getch()(#include <conio.h>),不需要回车就可以得到输入的控制字符。另外,kbhit()函数在用户有键盘输入时返回1,否则返回0;在没有键盘输入时if(kbhit())下面的语句不会运行,从而避免出现用户不输入游戏就暂停的情况。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int main()
{
int i,j;
int x=5;
int y=10;
char input;
while(1)
{
system("cls"); //清屏函数
// 输出飞机上面的空行
for(i=0;i<x;i++)
{
printf("\n");
}
//输入飞机图案
for(j=0;j<y;j++)
{
printf(" ");
}
printf(" *\n");
for(j=0;j<y;j++)
{
printf(" ");
}
printf("*****\n");
for(j=0;j<y;j++)
{
printf(" ");
}
printf(" * * \n");
Sleep(100);
if(kbhit())
{
input = getch();
if(input == 'a')
y--;
if(input == 'd')
y++;
if(input == 'w')
x--;
if(input == 's')
x++;
}
}
return 0;
}
3、发射激光
按空格键后让飞机发射激光子弹,即在飞机上方显示一条竖线‘|’,定义变量isFire,用来记录飞机是否处于发射子弹的状态。当飞机等于1时,将在飞机正上方输出激光竖线
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int main()
{
int i,j;
int x=5;
int y=10;
char input;
while(1)
{
system("cls"); //清屏函数
// 输出飞机上面的空行
for(i=0;i<x;i++)
{
printf("\n");
}
//输入飞机图案
for(j=0;j<y;j++)
{
printf(" ");
}
printf(" *\n");
for(j=0;j<y;j++)
{
printf(" ");
}
printf("*****\n");
for(j=0;j<y;j++)
{
printf(" ");
}
printf(" * * \n");
Sleep(100);
if(kbhit())
{
input = getch();
if(input == 'a')
y--;
if(input == 'd')
y++;
if(input == 'w')
x--;
if(input == 's')
x++;
}
}
return 0;
}
4、打靶练习
在第一行增加一个靶子“+”,控制飞机发射激光击中它,变量isKilled用来存储是否击中,isKilled等于0显示靶子,当isKilled为1是不在显示靶子
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int main()
{
int i,j;
int x=5;
int y=10;
char input;
int isFire=0;
int ny = 5;
int isKilled = 0;
while(1)
{
system("cls"); //清屏函数
if(!isKilled)
{
for(j=0;j<ny;j++)
printf(" ");
printf("+\n");
}
if( isFire == 0 )
{
for(i=0;i<x;i++)
printf("\n");
}
else
{
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
printf(" ");
printf(" |\n");
}
if(y+2 == ny)
isKilled = 1;
isFire = 0;
}
//输入飞机图案
for(j=0;j<y;j++)
{
printf(" ");
}
printf(" *\n");
for(j=0;j<y;j++)
{
printf(" ");
}
printf("*****\n");
for(j=0;j<y;j++)
{
printf(" ");
}
printf(" * * \n");
Sleep(100);
if(kbhit())
{
input = getch();
if(input == 'a')
y--;
if(input == 'd')
y++;
if(input == 'w')
x--;
if(input == 's')
x++;
if (input==' ')
isFire=1;
}
}
return 0;
}