C++【观察系列】混乱的“蜜蜂”

这是一个使用C++编写的简单游戏类程序,包含了窗口设置、随机数生成、游戏对象移动等基本功能。游戏对象在指定宽度和高度的窗口内随机移动,遇到边界会改变方向。程序使用了Windows API进行控制台操作,并通过`srand`和`time`函数确保随机数生成的随机性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <ctime>
#include <iostream>
#include "windows.h"
using namespace std;

class Game {
	public:
		void Move ( );
		void MoveGo ( );
		void SetConsole ( int ic_x, int ic_y );
		int Random ( int ic_numberA, int ic_numberB );
		Game ( int ic_window_width, int ic_window_heigth, int ic_speed );
		~Game ( );
	private:
		int i_window_width,
			i_window_height,
			i_window_right,
			i_window_down,
			i_random_x,
			i_random_y,
			i_formerly_x,
			i_formerly_y,
			i_direction_id,
			i_speed;
};

int main ( ) {
	system ( "mode con cols=100 lines=30" );
	srand ( time ( NULL ) );
	
	Game game ( 100, 30, 10 );
	game.MoveGo ( );
	
	return 0;
}


Game::Game ( int ic_window_width, int ic_window_height, int ic_speed ) {
	i_window_width	= ic_window_width;
	i_window_height	= ic_window_height;
	i_window_right	= i_window_width - 1;
	i_window_down	= i_window_height - 1;
	i_random_x 		= Random ( 0, i_window_right );
	i_random_y 		= Random ( 0, i_window_down );
	i_direction_id 	= Random ( 1, 4 );
	i_speed 		= ic_speed;
}

Game::~Game ( ) {
}
int Game::Random ( int ic_numberA, int ic_numberB ) {
	return ic_numberA + rand ( ) % ic_numberB;
}

void Game::SetConsole ( int ic_x, int ic_y ) {
	CONSOLE_CURSOR_INFO info = { 1, 0 };
	HANDLE hand = GetStdHandle ( STD_OUTPUT_HANDLE );
	COORD coord = { ic_x, ic_y };
	SetConsoleCursorInfo ( hand, &info );
	SetConsoleCursorPosition ( hand, coord );
}
void Game::Move ( ) {
	while ( 1 ) {
		SetConsole ( i_random_x, i_random_y );
		cout << "*";
		i_formerly_x = i_random_x;
		i_formerly_y = i_random_y;
		if ( ( i_random_x == 0 && i_random_y == 0 ) ||
			 ( i_random_x == 0 && i_random_y == i_window_down ) ||
			 ( i_random_x == i_window_right && i_random_y == 0 ) ||
			 ( i_random_x == i_window_right && i_random_y == i_window_down ) ) {
			switch ( i_direction_id ) {
				case 1: i_direction_id = 3; break;
				case 2: i_direction_id = 4; break;
				case 3: i_direction_id = 1; break;
				case 4: i_direction_id = 2; break;
				default : break;
			}
		}
		else if ( i_random_x == 0 || i_random_x == i_window_right ) {
			switch ( i_direction_id ) {
				case 1: i_direction_id = 4; break;
				case 2: i_direction_id = 3; break;
				case 3: i_direction_id = 2; break;
				case 4: i_direction_id = 1; break;
				default : break;
			}
		}
		else if ( i_random_y == 0 || i_random_y == i_window_down ) {
			switch ( i_direction_id ) {
				case 1: i_direction_id = 2; break;
				case 2: i_direction_id = 1; break;
				case 3: i_direction_id = 4; break;
				case 4: i_direction_id = 3; break;
				default : break;
			}
		}
		switch ( i_direction_id ) {
			case 1: i_random_x++; i_random_y++; break;
			case 2: i_random_x++; i_random_y--; break;
			case 3: i_random_x--; i_random_y--; break;
			case 4: i_random_x--; i_random_y++; break;
			default : break;
		}
		Sleep ( i_speed / 2 );
		SetConsole ( i_random_x, i_random_y );
		cout << "*";
		Sleep ( i_speed / 2 );
		SetConsole ( i_formerly_x, i_formerly_y );
		cout << " ";
	}
}

void Game::MoveGo ( ) {
	while ( 1 ) {
		int i = Random ( 1, 3 );
		i_formerly_x 	= i_random_x;
		i_formerly_y 	= i_random_y;
		i_direction_id 	= Random ( 1, 4 );
		SetConsole ( i_random_x, i_random_y );
		cout << "*";
		if ( i_random_x == 0 && i_random_y == 0 ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 2; break;
				default : break;
			}
		}
		else if ( i_random_x == 0 && i_random_y == i_window_down ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_x == i_window_right && i_random_y == 0 ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 2; break;
				case 2: i_direction_id = 3; break;
				default : break;
			}
		}
		else if ( i_random_x == i_window_right && i_random_y == i_window_down ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 3; break;
				case 2: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_x == 0 ) {
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 2; break;
				case 3: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_x == i_window_right ) {
			switch ( i ) {
				case 1: i_direction_id = 2; break;
				case 2: i_direction_id = 3; break;
				case 3: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_y == 0 ) {
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 2; break;
				case 3: i_direction_id = 3; break;
				default : break;
			}
		}
		else if ( i_random_y == i_window_down ) {
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 3; break;
				case 3: i_direction_id = 4; break;
				default : break;
			}
		}
		switch ( i_direction_id ) {
			case 1: i_random_x++; break;
			case 2: i_random_y++; break;
			case 3: i_random_x--; break;
			case 4: i_random_y--; break;
			default : break;
		}
		Sleep ( i_speed );
		SetConsole ( i_formerly_x, i_formerly_y );
		cout << " ";
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值