盒子接小球改进版

改进:
1. 旧版:颜色搭配上,绿边会因为黑色变得很不明显。新版:换了一种有吸引力的颜色。比如,“黑底、白球、绿边”等。
2. 旧版:控制上不是很流畅。新版:使用流畅的按键控制。
3. 旧版:处理移动的方法,是擦掉每一个球。这样做有一个问题:画球的过程涉及到三角函数,当球很多时,擦掉全部的球会消耗很多时间,新版:用 cleardevice 清除整个屏幕更快一些。并且逻辑上也会更简单一些。
4. 旧版:造成屏幕闪烁的问题,是因为不能保证在一瞬间画出来所有物体。新版:建议你使用批量绘图指令。
5. 新版:游戏的逻辑上,用另一种方式。屏幕右侧空出一定区域,写上游戏的运行时间、接到的球的数量以及版权等信息。小球没有一次性全部落下,采用一会儿落一个,然后让屏幕上始终保持 10 个球(或其他数量)。每当接到一个球,就重新产生一个新球落下。
6. 新版:windows.h 已经在 graphics.h 里面包含了,不需要再次引用。
代码如下:
/*
盒子接球
copyright: 圣石
date: 2013.12.01
*/
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define NUM 10
#define	CMD_LEFT		4
#define	CMD_RIGHT		8
#define	CMD_QUIT		64
int box_x = 10, box_y = 420;


struct Ball
{
	int x;
	int y;
	int v;
	bool exist;
};

int GetCommand()
{
	int c = 0;
	if (GetAsyncKeyState(VK_LEFT) & 0x8000)		c |= CMD_LEFT;
	if (GetAsyncKeyState(VK_RIGHT) & 0x8000)	c |= CMD_RIGHT;
	if (GetAsyncKeyState(VK_ESCAPE) & 0x8000)	c |= CMD_QUIT;
	return c;
}

void time(int hour,int minute, int second)
{
	SYSTEMTIME stLocalagain;
	GetLocalTime(&stLocalagain);
	char strsec[10];
	int houragain = stLocalagain.wHour, minuteagain = stLocalagain.wMinute, 
		secondagain = stLocalagain.wSecond, sec;

	sec = (houragain - hour)*3600 + (minuteagain - minute)*60 + (secondagain - second);
	itoa(sec, strsec, 10);
	outtextxy(570, 110, strcat(strsec,"s"));
}

void menu(int score)
{
	line(449, 0, 449, 480);	
	char strScore[10], runTime[] = "游戏运行时间    : ", 
		receiveBallNum[] = "接到的球的数量:", copyRight[] = "版权所有: 圣石 ",
		finishWorkDate[] = "完成日期:2012年12月1日", 
		introductiona[] = "按方向键控制盒子移动", introductionb[] = "接住小球,按Esc键退出";

	settextcolor(GREEN);
	outtextxy(450, 10, introductiona);
	outtextxy(450, 30, introductionb);
	outtextxy(450, 110, runTime);
	itoa(score, strScore, 10);
	outtextxy(450, 210, receiveBallNum);
	outtextxy(570, 210, strScore);
	outtextxy(450, 310, copyRight);
	outtextxy(450, 410, finishWorkDate);
}

void calculateScore(Ball ball[],int &n,int &score)
{
	
	for(int i=0; i<NUM; i++)
	{
		if(ball[i].exist)
		{
			fillcircle(ball[i].x, ball[i].y, 8);
			if(ball[i].y >= 472)
			{
				ball[i].exist = false;
				n--; continue;
			}
			if(box_x+8<=ball[i].x && ball[i].x<=box_x+72 && ball[i].y>=412)
			{
				score++; n--;
				ball[i].exist = false;
				
			}
		}
		else
		{
			ball[i].x = 16 + 45*i; 
			ball[i].y = 8 + rand()%32;
			ball[i].v = 1 + rand()%5;
			ball[i].exist = true;
			n++;
		}
	}
}

int main()
{
	SYSTEMTIME stLocal;
	GetLocalTime(&stLocal);
	int hour = stLocal.wHour, minute = stLocal.wMinute, second = stLocal.wSecond;
	initgraph(640, 480);
	srand(time(NULL));

	struct Ball ball[100];
	int dx, i, n = NUM,score = 0;
	bool flag = true;

	for(i=0; i<NUM; i++)
	{
		ball[i].x = 16 + 45*i; 
		ball[i].y = 8 + rand()%32;
		ball[i].v = 1 + rand()%5;
		ball[i].exist = true;
	}

	char strScore[10], str[] = "your score:";
	while(flag)
	{
		//画球和盒子
		dx = 0; time(hour, minute, second);
		setlinecolor(GREEN); setfillcolor(WHITE);
		BeginBatchDraw();
	
		menu(score);

		calculateScore(ball,n,score);

		fillrectangle(box_x, box_y, box_x+80, box_y+60);
		FlushBatchDraw();

		if (GetCommand() & CMD_LEFT)	dx = -10;
		if (GetCommand() & CMD_RIGHT)	dx = 10;
		if (GetCommand() & CMD_QUIT) flag = false;
		Sleep(25);
		//擦除球和盒子

		cleardevice();
		for(i=0; i<NUM; i++)
		{
			ball[i].y += ball[i].v;
		}
		box_x = box_x + dx;

	}

	itoa(score, strScore, 10);
	outtextxy(292, 250, strcat(str, strScore));
	outtextxy(290, 310, "按任意键退出");
	EndBatchDraw();
	getchar();
	closegraph();

	return 0;
}


根据引用\[1\]中提到的OpenMV4 Cam H7,OpenMV的型号可以选择不同的型号,主要影响的是图像的清晰度和性能。至于追踪小球的云台的线,由于没有提供相关引用内容,我无法提供具体的线信息。但是一般来说,追踪小球的云台需要连OpenMV摄像头和云台控制器。你可以参考OpenMV和云台控制器的说明书或者文档,查找相关的线图和线说明。根据你使用的具体型号和云台控制器的型号,可能会有不同的线方式。建议你仔细阅读相关文档并按照说明进行线。 #### 引用[.reference_title] - *1* [【毕业设计】基于STM32及OpenMV的云台追踪装置](https://blog.csdn.net/JIE15164031299/article/details/119617832)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [STM32单片机RT Thread + Micropython + OpenMV + USB摄像头移植整合过程](https://blog.csdn.net/chummyhe/article/details/109516924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值