“别动方块”完整代码
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float width,height,gravity; // 游戏画面大小、重力加速度
float ball_x,ball_y,ball_vy,radius; // 小球圆心坐标、y方向速度、半径大小
float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx; // 方块障碍物的相关参数
int score = 0; // 得分
int isBallOnFloor = 1; // 小球是否在地面上,避免重复起跳
width = 600; // 游戏画面宽度
height = 400; // 游戏画面高度
gravity = 0.6; // 重力加速度
initgraph(width, height); // 新建一个画布
radius = 20; // 小球半径
ball_x = width/4; // 小球x位置
ball_y = height-radius; // 小球y位置
ball_vy = 0; // 小球初始y速度为0
rect_height = 100; // 方块高度
rect_width = 20; // 方块宽度
rect_left_x = width*3/4; // 方块左边x坐标
rect_top_y = height - rect_height; // 方块顶部y坐标
rect_vx = -3; // 方块x方向速度
while(1) // 一直循环
{
if (kbhit()) // 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ' && isBallOnFloor==1) // 当按下空格键,并且小球在地面上时
{
ball_vy = -17; // 给小球一个向上的速度
isBallOnFloor = 0; // 表示小球不在地面了,不能重复起跳
}
}
ball_vy = ball_vy + gravity; // 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy; // 根据小球y方向速度更新其y坐标
if (ball_y >= height-radius) // 如果小球落到地面上
{
ball_vy = 0; // y速度为0
ball_y = height-radius; // 规范其y坐标,避免落到地面下
isBallOnFloor = 1; // 表示小球在地面上
}
rect_left_x = rect_left_x + rect_vx; // 方块向左移
if (rect_left_x <= 0) // 如果方块跑到最左边
{
rect_left_x = width; // 在最右边重新出现
score = score + 1; // 得分+1
rect_height = rand() % int(height/4) + height/4; // 设置随机高度
rect_vx = rand()/float(RAND_MAX) *4 - 7; // 设置方块随机速度
}
// 如果小球碰到方块
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius) )
{
Sleep(50); // 慢动作效果
score = 0; // 得分清零
}
cleardevice(); // 清空画面
fillcircle(ball_x, ball_y, radius); // 绘制小球
// 画方块
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width,height);
TCHAR s[20]; // 定义字符串数组
_stprintf(s, _T("%d"), score); // 将score转换为字符串
settextstyle(40, 0, _T("宋体")); // 设置文字大小、字体
outtextxy(50, 30, s); // 输出得分文字
Sleep(10); // 暂停10毫秒
}
closegraph();
return 0;
}
3.1 字符
整型(int)、浮点型(float)、字符型(char)数据类型:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
int i = 10;//整型
printf("%d\n",i);
float g;//浮点数
g = 9.8;
printf("%f\n",g);
char c;//字符型
c = _getch();
printf("%c\n",c);
while (1)
{
c = _getch();
printf("输入的字符为:%c\n",c);
}
while (1)
{
if(kbhit())
{
c = _getch();
if(c==' ')
{
printf("输入了空格\n");
}
}
}
while(1) // 一直循环
{
if (kbhit())// 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ') // 当按下空格键
printf("按下了空格!\n");
}
}
_getch();
return 0;
}
3.2 按空格键控制小球起跳
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float width,height,gravity; // 游戏画面大小、重力加速度
float ball_x,ball_y,ball_vy,radius; // 小球圆心坐标、y方向速度、半径大小
width = 600; // 游戏画面宽度
height = 400; // 游戏画面高度
gravity = 0.6; // 重力加速度
initgraph(width, height); // 新建一个画布
radius = 20; // 小球半径
ball_x = width/4; // 小球x位置
ball_y = height-radius; // 小球y位置
ball_vy = 0; // 小球初始y速度为0
while(1) // 一直循环
{
if (kbhit()) // 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ') // 当按下空格键时
{
ball_vy = -16; // 给小球一个向上的速度
}
}
ball_vy = ball_vy + gravity; // 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy; // 根据小球y方向速度更新其y坐标
if (ball_y >= height-radius) // 如果小球落到地面上
{
ball_vy = 0; // y速度为0
ball_y = height-radius; // 规范其y坐标,避免落到地面下
}
cleardevice(); // 清空画面
fillcircle(ball_x, ball_y, radius); // 绘制小球
Sleep(10); // 暂停10毫秒
}
closegraph();
return 0;
}
3.3 方块的绘制与移动
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float width,height,gravity; // 游戏画面大小、重力加速度
float ball_x,ball_y,ball_vy,radius; // 小球圆心坐标、y方向速度、半径大小
float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx; // 方块障碍物的相关参数
width = 600; // 游戏画面宽度
height = 400; // 游戏画面高度
gravity = 0.6; // 重力加速度
initgraph(width, height); // 新建一个画布
radius = 20; // 小球半径
ball_x = width/4; // 小球x位置
ball_y = height-radius; // 小球y位置
ball_vy = 0; // 小球初始y速度为0
rect_height = 100; // 方块高度
rect_width = 20; // 方块宽度
rect_left_x = width*3/4; // 方块左边x坐标
rect_top_y = height - rect_height; // 方块顶部y坐标
rect_vx = -3; // 方块x方向速度
while(1) // 一直循环
{
if (kbhit()) // 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ') // 当按下空格键时
{
ball_vy = -16; // 给小球一个向上的速度
}
}
ball_vy = ball_vy + gravity; // 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy; // 根据小球y方向速度更新其y坐标
if (ball_y >= height-radius) // 如果小球落到地面上
{
ball_vy = 0; // y速度为0
ball_y = height-radius; // 规范其y坐标,避免落到地面下
}
rect_left_x = rect_left_x + rect_vx; // 方块向左移
if (rect_left_x <= 0) // 如果方块跑到最左边
{
rect_left_x = width; // 在最右边重新出现
}
cleardevice(); // 清空画面
fillcircle(ball_x, ball_y, radius); // 绘制小球
// 画方块
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width,height);
Sleep(10); // 暂停10毫秒
}
closegraph();
return 0;
}
3.4 小球和方块的碰撞判断
C语言提供了3种逻辑运算符:!(非)、&&(与)、||(或),用于实现多个逻辑条件的组合。
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
printf("%d\n",(2>3));
printf("%d\n",(5<6));
printf("%d\n",!(2>3));
printf("%d\n",(2>3)&&(5<6));
printf("%d\n",(2>3)||(5<6));
_getch();
return 0;
}
// 如果小球碰到方块
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius) )
{
Sleep(100); // 慢动作效果
}
3.5 随机方块的速度和高度
rand()函数可以生成随机整数:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
int i;
while (1)
{
i = rand();
printf("%d\n",i);
Sleep(100);
}
return 0;
}
为了得到设定范围内的随机数,需要利用浮点数除法、整数除法、取余运算符。输入并运行代码:
实行浮点数除法,即5.0/2=2.5。
实行整数除法,得到两个整数相除的商,即5/2=2。
得到两个整数相除的余数,即10%3=1。
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float a = 5.0/2;
printf("%f\n",a);
int b = 5/2;
printf("%d\n",b);
int c = 10%3;
printf("%d\n",c);
_getch();
return 0;
}
类型转换
float()、int()形式称为强制类型转换。
float(5)把5转换为浮点型,进行浮点数除法后,float(5)/2等于2.5;
int(6.3)把6.3转换为整数6,进行整数除法后,int(6.3)/2等于3;
int(9.0/2)把浮点数除法结果4.5转换为整数,结果为4。
RAND_MAX存储了rand( )函数所能生成的最大整数,rand( )/float(RAND_MAX)即可生成0 ~ 1的随机小数。
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
int a = 1.1;
printf("%d\n",a);//1
float b = 2;
printf("%f\n",b);//2.000000
float c = float(5)/2;
printf("%f\n",c);//2.500000
int d = int(6.3)/2;
printf("%d\n",d);//3
int e = int(9.0/2);
printf("%d\n",e);//4
_getch();
return 0;
}
方块随机高度速度:
当方块重新出现时,添加代码设置其随机高度范围为height/4到height/2,
随机速度为-3到7,添加代码如下:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float width,height,gravity; // 游戏画面大小、重力加速度
float ball_x,ball_y,ball_vy,radius; // 小球圆心坐标、y方向速度、半径大小
float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx; // 方块障碍物的相关参数
width = 600; // 游戏画面宽度
height = 400; // 游戏画面高度
gravity = 0.6; // 重力加速度
initgraph(width, height); // 新建一个画布
radius = 20; // 小球半径
ball_x = width/4; // 小球x位置
ball_y = height-radius; // 小球y位置
ball_vy = 0; // 小球初始y速度为0
rect_height = 100; // 方块高度
rect_width = 20; // 方块宽度
rect_left_x = width*3/4; // 方块左边x坐标
rect_top_y = height - rect_height; // 方块顶部y坐标
rect_vx = -3; // 方块x方向速度
while(1) // 一直循环
{
if (kbhit()) // 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ') // 当按下空格键时
{
ball_vy = -16; // 给小球一个向上的速度
}
}
ball_vy = ball_vy + gravity; // 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy; // 根据小球y方向速度更新其y坐标
if (ball_y >= height-radius) // 如果小球落到地面上
{
ball_vy = 0; // y速度为0
ball_y = height-radius; // 规范其y坐标,避免落到地面下
}
rect_left_x = rect_left_x + rect_vx; // 方块向左移
if (rect_left_x <= 0) // 如果方块跑到最左边
{
rect_left_x = width; // 在最右边重新出现
rect_height = rand() % int(height/4) + height/4; // 设置随机高度
rect_vx = rand()/float(RAND_MAX) *4 - 7; // 设置方块随机速度
}
// 如果小球碰到方块
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius) )
{
Sleep(100); // 慢动作效果
}
cleardevice(); // 清空画面
fillcircle(ball_x, ball_y, radius); // 绘制小球
// 画方块
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width,height);
Sleep(10); // 暂停10毫秒
}
closegraph();
return 0;
}
3.6 得分的计算与显示
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float width,height,gravity; // 游戏画面大小、重力加速度
float ball_x,ball_y,ball_vy,radius; // 小球圆心坐标、y方向速度、半径大小
float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx; // 方块障碍物的相关参数
int score = 0; // 得分
width = 600; // 游戏画面宽度
height = 400; // 游戏画面高度
gravity = 0.6; // 重力加速度
initgraph(width, height); // 新建一个画布
radius = 20; // 小球半径
ball_x = width/4; // 小球x位置
ball_y = height-radius; // 小球y位置
ball_vy = 0; // 小球初始y速度为0
rect_height = 100; // 方块高度
rect_width = 20; // 方块宽度
rect_left_x = width*3/4; // 方块左边x坐标
rect_top_y = height - rect_height; // 方块顶部y坐标
rect_vx = -3; // 方块x方向速度
while(1) // 一直循环
{
if (kbhit()) // 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ') // 当按下空格键时
{
ball_vy = -17; // 给小球一个向上的速度
}
}
ball_vy = ball_vy + gravity; // 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy; // 根据小球y方向速度更新其y坐标
if (ball_y >= height-radius) // 如果小球落到地面上
{
ball_vy = 0; // y速度为0
ball_y = height-radius; // 规范其y坐标,避免落到地面下
}
rect_left_x = rect_left_x + rect_vx; // 方块向左移
if (rect_left_x <= 0) // 如果方块跑到最左边
{
rect_left_x = width; // 在最右边重新出现
score = score + 1; // 得分+1
rect_height = rand() % int(height/4) + height/4; // 设置随机高度
rect_vx = rand()/float(RAND_MAX) *4 - 7; // 设置方块随机速度
}
// 如果小球碰到方块
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius) )
{
Sleep(100); // 慢动作效果
score = 0; // 得分清零
}
cleardevice(); // 清空画面
fillcircle(ball_x, ball_y, radius); // 绘制小球
// 画方块
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width,height);
TCHAR s[20]; // 定义字符串数组
_stprintf(s, _T("%d"), score); // 将score转换为字符串
settextstyle(40, 0, _T("宋体")); // 设置文字大小、字体
outtextxy(50, 30, s); // 输出得分文字
Sleep(10); // 暂停10毫秒
}
closegraph();
return 0;
}
3.7 避免空中起跳
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float width,height,gravity; // 游戏画面大小、重力加速度
float ball_x,ball_y,ball_vy,radius; // 小球圆心坐标、y方向速度、半径大小
float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx; // 方块障碍物的相关参数
int score = 0; // 得分
int isBallOnFloor = 1; // 小球是否在地面上,避免重复起跳
width = 600; // 游戏画面宽度
height = 400; // 游戏画面高度
gravity = 0.6; // 重力加速度
initgraph(width, height); // 新建一个画布
radius = 20; // 小球半径
ball_x = width/4; // 小球x位置
ball_y = height-radius; // 小球y位置
ball_vy = 0; // 小球初始y速度为0
rect_height = 100; // 方块高度
rect_width = 20; // 方块宽度
rect_left_x = width*3/4; // 方块左边x坐标
rect_top_y = height - rect_height; // 方块顶部y坐标
rect_vx = -3; // 方块x方向速度
while(1) // 一直循环
{
if (kbhit()) // 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ' && isBallOnFloor==1) // 当按下空格键,并且小球在地面上时
{
ball_vy = -17; // 给小球一个向上的速度
isBallOnFloor = 0; // 表示小球不在地面了,不能重复起跳
}
}
ball_vy = ball_vy + gravity; // 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy; // 根据小球y方向速度更新其y坐标
if (ball_y >= height-radius) // 如果小球落到地面上
{
ball_vy = 0; // y速度为0
ball_y = height-radius; // 规范其y坐标,避免落到地面下
isBallOnFloor = 1; // 表示小球在地面上
}
rect_left_x = rect_left_x + rect_vx; // 方块向左移
if (rect_left_x <= 0) // 如果方块跑到最左边
{
rect_left_x = width; // 在最右边重新出现
score = score + 1; // 得分+1
rect_height = rand() % int(height/4) + height/4; // 设置随机高度
rect_vx = rand()/float(RAND_MAX) *4 - 7; // 设置方块随机速度
}
// 如果小球碰到方块
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius) )
{
Sleep(50); // 慢动作效果
score = 0; // 得分清零
}
cleardevice(); // 清空画面
fillcircle(ball_x, ball_y, radius); // 绘制小球
// 画方块
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width,height);
TCHAR s[20]; // 定义字符串数组
_stprintf(s, _T("%d"), score); // 将score转换为字符串
settextstyle(40, 0, _T("宋体")); // 设置文字大小、字体
outtextxy(50, 30, s); // 输出得分文字
Sleep(10); // 暂停10毫秒
}
closegraph();
return 0;
}
补充:代码左边行号设置
工具——选项——文本编译器——所有语言——行号