C++のeasyx特辑2:星空

浩瀚银河,不知有多少星星?

那就数一数吧。

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

#define MAXSTAR 200

struct STAR
{
	double	x;
	int		y;
	double	step;
	int		color;
};

STAR star[MAXSTAR];

void InitStar(int i)
{
	star[i].x = 0;
	star[i].y = rand() % 480;
	star[i].step = (rand() % 5000) / 1000.0 + 1;
	star[i].color = (int)(star[i].step * 255 / 6.0 + 0.5);
	star[i].color = RGB(star[i].color, star[i].color, star[i].color);
}

void MoveStar(int i)
{
	putpixel((int)star[i].x, star[i].y, 0);
	star[i].x += star[i].step;
	if (star[i].x > 640)	
        InitStar(i);
	putpixel((int)star[i].x, star[i].y, star[i].color);
}

int main()
{
	srand((unsigned)time(NULL));
	initgraph(640, 480);
	for(int i = 0; i < MAXSTAR; i++)
	{
		InitStar(i);
		star[i].x = rand() % 640;
	}
	while(!_kbhit())
	{
		for(int i = 0; i < MAXSTAR; i++)
			MoveStar(i);
		Sleep(
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 EasyX 图形库创建迷宫并进行自动寻路的完整 C++ 代码: ```cpp #include <graphics.h> #include <conio.h> #include <stack> using namespace std; const int SIZE = 20; // 迷宫单元格大小 const int ROWS = 20; // 迷宫行数 const int COLS = 20; // 迷宫列数 int maze[ROWS][COLS]; // 迷宫数组 IMAGE img[3]; // 图片数组 stack<POINT> s; // 用于存储路径的栈 // 初始化迷宫数组 void initMaze() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (i == 0 || i == ROWS - 1 || j == 0 || j == COLS - 1) { maze[i][j] = 1; // 边界为墙壁 } else { maze[i][j] = 0; // 其余为路 } } } } // 生成迷宫 void generateMaze() { int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; stack<POINT> stk; POINT p; p.x = 2; p.y = 2; stk.push(p); while (!stk.empty()) { p = stk.top(); stk.pop(); int x = p.x; int y = p.y; if (maze[x][y] == 1) { continue; } maze[x][y] = 1; int order[4] = {0, 1, 2, 3}; for (int i = 0; i < 4; i++) { int j = rand() % 4; int t = order[i]; order[i] = order[j]; order[j] = t; } for (int i = 0; i < 4; i++) { int nx = x + dx[order[i]]; int ny = y + dy[order[i]]; if (maze[nx][ny] == 0) { POINT np = {nx, ny}; stk.push(np); } } } } // 加载图片 void loadImages() { loadimage(&img[0], L"wall.jpg", SIZE, SIZE); loadimage(&img[1], L"road.jpg", SIZE, SIZE); loadimage(&img[2], L"foot.jpg", SIZE, SIZE); } // 绘制迷宫 void drawMaze() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (maze[i][j] == 1) { putimage(j * SIZE, i * SIZE, &img[0]); } else { putimage(j * SIZE, i * SIZE, &img[1]); } } } } // 判断是否为终点 bool isEnd(POINT p) { return p.x == ROWS - 2 && p.y == COLS - 2; } // 判断是否为可行路径 bool isPath(POINT p) { return maze[p.x][p.y] == 0; } // 绘制路径 void drawPath() { stack<POINT> tmp; while (!s.empty()) { tmp.push(s.top()); s.pop(); } while (!tmp.empty()) { POINT p = tmp.top(); putimage(p.y * SIZE, p.x * SIZE, &img[2]); s.push(p); tmp.pop(); } } // 自动寻路 bool findPath(POINT cur) { if (isEnd(cur)) { s.push(cur); return true; } if (isPath(cur)) { s.push(cur); maze[cur.x][cur.y] = 2; POINT next = cur; next.x--; if (findPath(next)) { return true; } next.x += 2; if (findPath(next)) { return true; } next.x--; next.y--; if (findPath(next)) { return true; } next.y += 2; if (findPath(next)) { return true; } s.pop(); maze[cur.x][cur.y] = 0; } return false; } int main() { initMaze(); generateMaze(); loadImages(); initgraph(COLS * SIZE, ROWS * SIZE); drawMaze(); POINT start = {1, 1}; findPath(start); drawPath(); getch(); closegraph(); return 0; } ``` 注:本代码使用了 EasyX 图形库,需要事先安装该库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值