C语言—用EaxyX绘制实时钟表

 代码效果如图

#undef UNICODE
#undef _UNICODE
#include<graphics.h>
#include<conio.h>
#include<math.h>

#define width 640
#define high 480
#define PI 3.14159

int main()
{
	initgraph(width, high);
	int center_x, center_y;
	center_x = width / 2;
	center_y = high / 2;
	int secondlen = width / 5;
	int minutelen = width / 6;
	int hourlen = width / 7;

	int secondend_x, secondend_y;
	int minuteend_x, minuteend_y;
	int hourend_x, hourend_y;
	float secondangle;
	float minuteangle;
	float hourangle;

	SYSTEMTIME ti;

	BeginBatchDraw();
	while (1)
	{
		setbkcolor(RGB(0,200,200));//只是设置背景色,还未填充背景
		cleardevice();//用背景色来清空背景

		setlinestyle(PS_SOLID, 2);//画表盘
		setcolor(WHITE);
		circle(center_x, center_y, width / 4);

		int x, y, i;//画刻度
		for (i = 0; i < 60; i++)
		{
			x = center_x + int(width / 4.3 * sin(PI * 2 * i / 60));
			y = center_y - int(width / 4.3 * cos(PI * 2 * i / 60));

			if (i % 15 == 0)
				solidrectangle(x - 5, y - 5, x + 5, y + 5);
			else if (i % 5 == 0)
				circle(x, y, 3);
			else
				putpixel(x, y, WHITE);
		}

		outtextxy(center_x - 25, center_y + width / 6, "我的时钟");

		GetLocalTime(&ti);//获取当前电脑时间
		secondangle = ti.wSecond * 2 * PI / 60;
		minuteangle = ti.wMinute * 2 * PI / 60 + secondangle / 60;
		hourangle = ti.wHour * 2 * PI / 12 + minuteangle / 12;

		secondend_x = center_x + secondlen * sin(secondangle);
		secondend_y = center_y - secondlen * cos(secondangle);

		minuteend_x = center_x + minutelen * sin(minuteangle);
		minuteend_y = center_y - minutelen * cos(minuteangle);

		hourend_x = center_x + hourlen * sin(hourangle);
		hourend_y = center_y - hourlen * cos(hourangle);

		setlinestyle(PS_SOLID, 2);//画时针分针和秒针
		setcolor(YELLOW);
		line(center_x, center_y, secondend_x, secondend_y);
		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(center_x, center_y, minuteend_x, minuteend_y);
		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(center_x, center_y, hourend_x, hourend_y);

		FlushBatchDraw();
		Sleep(10);

		setcolor(BLACK);//隐藏前一秒的时针分针和秒针
		setlinestyle(PS_SOLID, 2);
		line(center_x, center_y, secondend_x, secondend_y);
		setlinestyle(PS_SOLID, 4);
		line(center_x, center_y, minuteend_x, minuteend_y);
		setlinestyle(PS_SOLID, 6);
		line(center_x, center_y, hourend_x, hourend_y);
	}
	EndBatchDraw();
	_getch();
	closegraph();
	return 0;
}

  • 21
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
捕鱼达人是一款非常经典的休闲游戏,可以通过C语言编写EasyX程序代码来实现。这个游戏的主要玩法是通过操作一个渔夫,利用鱼钩捕捉各种鱼类,并在规定时间内获得尽可能高的分数。下面是一个简单的捕鱼达人C语言EasyX程序代码: ```c++ #include <graphics.h> #include <conio.h> #include <iostream> #include <time.h> using namespace std; IMAGE bg; // 背景图片 IMAGE f[10]; // 鱼的图片 IMAGE hook; // 鱼钩图片 IMAGE fish[10]; int main() { srand((unsigned int)time(NULL)); // 随机数种子 initgraph(600, 600); // 创建图形窗口 loadimage(&bg, _T("bg.jpg")); // 加载背景图片 loadimage(&hook, _T("hook.png")); // 加载鱼钩图片 for (int i = 0; i < 10; i++) { string filename = "fish" + to_string(i + 1) + ".png"; loadimage(&f[i], _T(filename.c_str())); // 加载鱼的图片 } int score = 0; // 分数 int fish_x = rand() % 500 + 50; // 鱼的初始横坐标 int fish_y[10] = { 0 }; // 鱼的初始纵坐标 int fish_speed[10] = { 0 }; // 鱼的初始速度 int hook_x = 0, hook_y = 570; // 鱼钩的初始坐标 while (!kbhit()) // 当没有按键时循环 { putimage(0, 0, &bg); // 显示背景图片 for (int i = 0; i < 10; i++) // 循环显示鱼 { if (fish_y[i] >= 600) // 当鱼跑出屏幕时重新生成 { fish_x = rand() % 500 + 50; fish_y[i] = 0; fish_speed[i] = rand() % 5 + 5; } fish_y[i] += fish_speed[i]; putimage(fish_x, fish_y[i], &f[i], SRCINVERT); putimage(fish_x, fish_y[i], &f[i], SRCAND); putimage(fish_x, fish_y[i], &f[i], SRCPAINT); } if (hook_y <= 0) // 当鱼钩到达顶部时重置 { score--; hook_y = 570; } putimage(hook_x, hook_y, &hook); // 显示鱼钩图片 for (int i = 0; i < 10; i++) // 判断鱼和鱼钩是否相交 { if (fish_y[i] > 0 && fish_y[i] < 570 && abs(hook_x - fish_x) < 60 && abs(hook_y - fish_y[i]) < 60) { score++; fish_y[i] = 650; } } if (GetAsyncKeyState(VK_LEFT)) // 当按下左键时鱼钩向左移动 { hook_x -= 5; if (hook_x < 0) hook_x = 0; putimage(0, 0, &bg); } if (GetAsyncKeyState(VK_RIGHT)) // 当按下右键时鱼钩向右移动 { hook_x += 5; if (hook_x > 500) hook_x = 500; putimage(0, 0, &bg); } _itoa_s(score, (char*)fish, 10); // 显示分数 outtextxy(550, 20, (char*)fish); hook_y -= 5; // 鱼钩向上移动 Sleep(20); // 控制循环速度 } _getch(); // 读取按键并结束程序 closegraph(); return 0; } ``` 以上是一个简单的捕鱼达人C语言EasyX程序代码,其中通过使用EasyX图形库实现了游戏界面和动画效果,并使用随机数、碰撞检测等技术实现游戏逻辑。同时,也包含了一些基础的C语言编程知识,如循环、条件语句、数组、字符串等。在日常C语言编程练习中,可以通过编写类似的小游戏程序来提高自己的编程能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值