easyX模拟一个小球

重力加速度小球

非常简单的一个小东西 上代码

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<time.h>

int main()
{
	int width = 800;//界面宽度
	int height = 600;//界面高度
	float g = 0.3;//重力加速度
	float x = width / 6;//小球x坐标
	float y = height / 3;//小球y坐标
	float vy = 0;//y方向的速度
	//float vx = 1;//x方向的速度
	int radius = 20;//小球的半径
	initgraph(width, height);//开辟界面

	while (1)
	{
		if (_kbhit())//按下空格会弹跳
		{
			char input = _getch();
			if (input = ' ')
				vy = -10;
		}
		//重力加速度对速度加速
		vy = vy + g;
		//根据速度更新小球的位置
	//	x = x + vx;
		y = y + vy;
		//碰到上下边界反弹
		if (y <= radius)
			vy = -vy;
		if (y >= height - radius)
			vy = -vy;
		cleardevice();//清屏
		fillcircle(x, y, radius);//画小球
		Sleep(3);//暂停3毫秒
	}
	return 0;
}

空格键可以跳,也是为了之后做flappy bird做准备

重力加速度小球

反弹小球

#include<graphics.h>
#include<conio.h>
#include<stdio.h>

int main()
{
	int width = 800;//画面宽度
	int height = 600;//画面高度
	int x =400;//小球的x坐标
	int y = 300;//小球y坐标
	int radius = 10;//小球半径
	int vx = 4;//小球x方向的速度
	int vy = 4;//小球y方向的速度
	initgraph(width, height);//界面大小
	while (1)//一直循环
	{
		x = x + vx;
		y = y + vy;
		//小球碰到左右边界,x速度反向
		if (x <= radius)
			vx = -vx;
		if (x >= width - radius)
			vx = -vx;
		//小球碰到上下边界,y速度反向
		if (y <=radius)
			vy = -vy;
		if (y >= height - radius)
			vy = -vy;
		cleardevice();//清屏
		fillcircle(x, y, radius);
		Sleep(30);
	}
}

小球反弹

flappy ball

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<time.h>

int main()
{
	int width = 800;//界面宽度
	int height = 600;//界面高度
	float g = 0.3;//重力加速度
	float x = width / 6;//小球x坐标
	float y = height / 3;//小球y坐标
	float vy = 0;//y方向的速度
	//float vx = 1;//x方向的速度
	int radius = 20;//小球的半径



	float blockWidth = radius * 2;//障碍物宽度
	float xBlockLeft = width * 0.8;//障碍物左侧x坐标
	float xBlockRight = xBlockLeft + blockWidth;//障碍物右侧x坐标
	float gapHeight = height / 2;//障碍物中间的空隙
	float yGapTop = height / 3;//障碍物最高点y坐标
	float yGapBottom = yGapTop + gapHeight;//障碍物最低点的y坐标

	int score = 0;//得分

	initgraph(width, height);//开辟界面

	while (1)
	{
		if (_kbhit())//按下空格会弹跳
		{
			char input = _getch();
			if (input = ' ')
				vy = -10;
		}
		//重力加速度对速度加速
		vy = vy + g;
		//根据速度更新小球的位置
	//	x = x + vx;
		y = y + vy;
		//碰到上下边界反弹
		if (y <= radius || y >= height - radius)
		{
			y = height / 3;
			vy = 0;
		}


		//障碍物逐渐向左移动
		xBlockLeft = xBlockLeft - 3;//障碍物左侧x坐标
		xBlockRight = xBlockLeft + blockWidth;//障碍物右侧x坐标

		if (xBlockRight < 0)
		{
			xBlockLeft = width;
			xBlockRight = xBlockLeft + blockWidth;
			yGapTop = rand() % (height / 2);
			yGapBottom = yGapTop + gapHeight;
			score += 1;
		}

		if ((x + radius > xBlockLeft && x - radius < xBlockRight)
			&& (y - radius<yGapTop || y + radius>yGapBottom))
		{
			//如果小球和障碍物碰撞
			Sleep(30);
			score = 0;
		}
		cleardevice();//清屏

		fillrectangle(xBlockLeft, 0, xBlockRight, yGapTop);
		fillrectangle(xBlockLeft, yGapBottom,xBlockRight, height);
		fillcircle(x, y, radius);//画小球


		TCHAR s[20];//定义字符串数组
		swprintf_s(s, _T("%d"), score);//将score转换为字符串
		settextstyle(40, 0, _T("宋体"));//设置文字字体大小
		outtextxy(50, 30, s);
		Sleep(3);//暂停3毫秒
	}
	return 0;
}

flappy ball

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值