EasyX之跳跳球

EasyX之跳跳球

  1. 实现绘制小球和矩形到固定位置
  2. 让矩形向左移动
  3. 使小球实现起跳与下落
  4. 解决小球二次起跳问题
  5. 判断小球起跳的过程中是否碰到矩形的左边,上边,以及右边,若碰到,游戏结束,分数清零
  6. 分数的打印
#define _CRT_SECURE_NO_WARNINGS
#include<graphics.h>    //图形库头文件
#include<cstdio>
#include<conio.h>
#include<time.h>
#define WIDTH 800
#define HEIGHT 400

//打印分数
void printScore(int score)
{
	char buffer[20];    //注意使用项目属性为多字节字符集
	sprintf(buffer, "分数: %d ", score);
	settextcolor(RED);              //设置字体颜色
	settextstyle(40, 0, "宋体");   //设置字体大小以及风格
	outtextxy(50, 50, buffer);      //输出字体
}
int main()
{
	srand((unsigned int)time(NULL));   //随机数种子
	initgraph(WIDTH, HEIGHT);         //创建窗口
	setbkcolor(WHITE);                //设置窗口背景色
	cleardevice();                   //清屏
	setfillcolor(RED);                //给小球和方块一个初始填充颜色
	//初始化小球数据
	int radius = 10;
	int ball_x = WIDTH / 4;
	int ball_y = HEIGHT - radius;
	int ball_vy = 0;                  //y方向速度
	int ball_a = 14;                  //小球加速度
	//初始化矩形数据
	int rec_width = 20;
	int rec_height = 100;
	int rec_left_topx = 3*WIDTH/4;
	int rec_left_topy = HEIGHT- rec_height;
	int rec_vx = -20;                //矩形X方向速度

	int score = 0;                //分数
	bool flag = false;           //判断小球是否能起跳标志

	char userKey = '\0';         //用户按键输入变量接收
	while (1)
	{
		BeginBatchDraw();   //双缓冲绘图
		//如果矩形跑到最左边然后重新生成一个矩形
		if ((rec_left_topx + rec_width) <= 0)
		{
			score+=10;
			rec_width = rand() % 21 + 10;
			rec_height = rand() % 81 + 80;
			rec_left_topx = WIDTH;
			rec_left_topy = HEIGHT - rec_height;
			rec_vx = rand() % 5 + (-40);
			setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
		}
		if (_kbhit())        //判断是否有按键按下
		{
			char userKey = _getch();
			if (userKey == ' ' && flag == true)  //小球在地上并且用户输入为空格
			{
				flag = false; 
				ball_vy = -80;            //给小球一个向上的速度
			}
		}
		ball_vy = ball_vy + ball_a;      //赋予加速度
		ball_y = ball_y + ball_vy;       //小球起跳
		//小球落地
		if (ball_y >= HEIGHT - radius)
		{
			flag = true;
			ball_vy = 0;                      //将速度置为0
			ball_y = HEIGHT - radius;        //防止小球落到地上
		}
		rec_left_topx = rec_left_topx + rec_vx;             //设置矩形速度
		//设置游戏结束标志
		if ((ball_x + radius >= rec_left_topx) &&            //小球在矩形左边
			(ball_y + radius >= HEIGHT - rec_height) &&      //小球在矩形上边
			(ball_x - radius <= rec_left_topx + rec_width))  //小球在矩形右边
		{
			score = 0;
			Sleep(100);
			MessageBox(GetHWnd(), "You die,Now B!", "游戏结束", MB_OK);  //告知玩家游戏结束
		}

		cleardevice();
		//绘制小球
		fillcircle(ball_x, ball_y, radius);
		//绘制矩形
		fillrectangle(rec_left_topx, rec_left_topy,
			          rec_left_topx + rec_width, rec_left_topy + rec_height);
		//打印分数
		printScore(score);
		Sleep(100);
		EndBatchDraw();
	}
	
	closegraph();

	system("pause");
	return 0;
}

跳跳球

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石小浪♪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值