好吧,又双叒叕来更新了
书接上上上上回(https://blog.csdn.net/gyoobhpty/article/details/125939750)
咱写了一个雷达,但后面拿来装X的时候,发现一次弹出两个窗口装X前还要排布好,就非常“好用”个屁,所以咱又双叒改了……
解决方法:
还有什么,把坐标放进图形化窗口里呗(简记为“人剑合一,XX”)
得了,方法有了,那就放代码
|
|
|
^
#include <iostream>
#include <cstdio>
#include <graphics.h>
#include <conio.h>
#include <ctime>
#include <cstdlib>
#include <Windows.h>
#define WIDTH 600 //窗口宽度
#define HEIGHT 600 //窗口高度
using namespace std;
int BIG = 20; //控制网格疏密程度
char xx[20], yy[20];
定义线条结构体1/
struct Line
{
int x1 = 0; //起点X坐标
int y1 = 0; //起点Y坐标
int x2 = WIDTH; //终点X坐标
int y2 = 0; //终点Y坐标
}L;
定义线条结构体2/
struct Linet
{
int x1 = 0; //起点X坐标
int y1 = 0; //起点Y坐标
int x2 = 0; //终点X坐标
int y2 = HEIGHT; //终点Y坐标
}Lt;
///定义重置按钮结构体///
struct button
{
int x1 = WIDTH - 45;
int y1 = HEIGHT - 20;
int x2 = WIDTH;
int y2 = HEIGHT;
}b;
/定义目标结构体
struct Point
{
int x = 0;
int y = 0;
int radius = 5;
}P;
//目标位置/
void point()
{
srand((unsigned)time(0));
P.x = rand() % (WIDTH + 1);
P.y = rand() % (HEIGHT + 1);
solidcircle(P.x, P.y, P.radius);
}
重置地图按钮/
void reset()
{
setbkmode(CYAN); //设置文字背景颜色
settextcolor(RED); //设置文字颜色
setfillcolor(CYAN); //设置填充颜色
setcolor(RED);
fillrectangle(b.x1, b.y1, b.x2, b.y2); //画出按钮(就是矩形)
outtextxy(WIDTH - 40, HEIGHT - 15, "重置"); //在矩形中打印文字
}
坐标显示///
void pxy()
{
setfillcolor(CYAN);
fillrectangle(b.x1 - 180, b.y1 - 10, b.x2 - 60, b.y2); //画出按钮(就是矩形)
//将整型数字转换为字符串
sprintf_s(xx, "%d", P.x);
sprintf_s(yy, "%d", P.y);
settextcolor(WHITE); //设置文字颜色
//打印坐标
outtextxy(b.x1 - 160, b.y1 - 7, xx);
outtextxy(b.x1 - 129, b.y1 - 7, ",");
outtextxy(b.x1 - 100, b.y1 - 7, yy);
}
界面绘制函数///
void Draw()
{
setlinecolor(RGB(255,0,255));
//画出等距的横线///
for (int i = 0;i <= HEIGHT;i += BIG)
{
line(0, i, WIDTH, i);
}
//画出等距的竖线///
for (int j = 0;j <= WIDTH;j += BIG)
{
line(j, 0, j, HEIGHT);
}
reset();
}
主函数部分/
int main()
{
ExMessage msg;
initgraph(WIDTH, HEIGHT);
setbkcolor(BLACK);
cleardevice();
Draw();
BeginBatchDraw(); //双缓冲,防止闪屏
setfillcolor(RED);
point();
while (true)
{
///判断纵向运动的线是否到达低端/
if (L.y1 < HEIGHT && L.y2 < HEIGHT)
{
L.y1 += 5;
L.y2 += 5;
}
else
{
L.y1 = 0;
L.y2 = 0;
}
///判断横向运动的线是否到达低端/
if (Lt.x1 < WIDTH && Lt.x2 < WIDTH)
{
Lt.x1 += 5;
Lt.x2 += 5;
}
else
{
Lt.x1 = 0;
Lt.x2 = 0;
}
///鼠标交互,实现伪放大、缩小
if (peekmessage(&msg, EM_MOUSE))
{
if (BIG > 5)
{
switch (msg.message)
{
case WM_LBUTTONDOWN:
BIG += 5;
P.radius += 1;
break;
case WM_RBUTTONDOWN:
BIG -= 5;
P.radius -= 1;
break;
}
}
/判断鼠标是否在按钮内并且按下鼠标左键
if (msg.message == WM_LBUTTONDOWN && msg.x > b.x1 && msg.y > b.y1 && msg.x < b.x2 && msg.y < b.y2)
{
///重置地图/
BIG = 20;
P.radius = 5;
}
}
setlinecolor(WHITE);
line(L.x1, L.y1, L.x2, L.y2); //画出纵向运动的线
line(Lt.x1, Lt.y1, Lt.x2, Lt.y2); //画出横向运动的线
setfillcolor(RED);
point();
pxy();
Sleep(10);
FlushBatchDraw();
cleardevice();
Draw();
}
EndBatchDraw();
return 0;
}
OK,代码就在你的眼前了,不过,请停下你代码的阅读,应为你读完了(People:????)
我们来说说重点:
这次不是关于写代码的重点,而是项目配置也许你的编译器会报错,就在这里:
//打印坐标
outtextxy(b.x1 - 160, b.y1 - 7, xx);
outtextxy(b.x1 - 129, b.y1 - 7, ",");
outtextxy(b.x1 - 100, b.y1 - 7, yy);这个时候,就要项目配置了:
找到项目-属性-高级-字符集,将它改成使用多字节字符集
这里还要声明:我用的是Visual Studio 2022,其他的可能会有些问题,具体的我也不知道,可以去百度上找一找
好了,这一期结束