自制windows系统(一天完成)
2025-07-19 19:16:23
发布于:广东
#include <iostream>
#include <vector>
#include <windows.h>
#include <ctime>
#include <iostream>
#include <conio.h> // Windows专用,提供_kbhit()和_getch()
#include <cstdlib>
#include <unistd.h> // UNIX下的usleep
#include<bits/stdc++.h>
#include <chrono>
#include <thread>
using namespace std;
//系统命令
int color;
class print_system_INT {
public:
vector<string> document_create;
void systeminfo() {
cout << "主机名: LAPTOP-6L85EDQT\n"
<< "OS 名称: Microsoft Windows 10 家庭中文版" << endl;
}
void create() {
if (document_create.size() > 256) {
cout << "创建失败,已超出硬件负荷" << endl;
} else {
cout << "创建成功" << endl;
document_create.push_back("document " + to_string(document_create.size() + 1));
}
}
void colorChange() {
cout << "请输入颜色(只支持蓝色,黄色和红色):" << endl;
cout << "1 - 蓝色" << endl;
cout << "2 - 黄色" << endl;
cout << "3 - 红色" << endl;
cin >> color;
if (color == 1) {
system("color 1");
} else if (color == 2) {
system("color 6");
} else {
system("color 4");
}
}
};
//游戏
void SetFont(int size) {
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = size; // 设置字体大小
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL; // 字体粗细
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
}
const int width = 20;
const int height = 10;
char m[height][width + 1];
struct Position {
int x;
int y;
};
struct Tank {
Position pos;
char direction; // 'W', 'A', 'S', 'D'
};
struct Bullet {
Position pos;
char direction;
bool active;
};
Tank player;
Bullet bullet;
void initMap() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
m[i][j] = ' ';
}
m[i][width] = '\0';
}
}
void drawMap() {
system("cls"); // 清屏
for (int i = 0; i < width + 2; i++) cout << "#";
cout << endl;
for (int i = 0; i < height; i++) {
cout << "#";
for (int j = 0; j < width; j++) {
cout << m[i][j];
}
cout << "#" << endl;
}
for (int i = 0; i < width + 2; i++) cout << "#";
cout << endl;
}
void placeTank() {
m[player.pos.y][player.pos.x] = 'A'; // 用'A'代表坦克
}
void moveTank(char dir) {
m[player.pos.y][player.pos.x] = ' ';
if (dir == 'W' && player.pos.y > 0) player.pos.y--;
else if (dir == 'S' && player.pos.y < height - 1) player.pos.y++;
else if (dir == 'A' && player.pos.x > 0) player.pos.x--;
else if (dir == 'D' && player.pos.x < width - 1) player.pos.x++;
placeTank();
}
void fireBullet() {
if (bullet.active) return; // 已有子弹,不能再次发射
bullet.pos.x = player.pos.x;
bullet.pos.y = player.pos.y;
bullet.direction = player.direction;
bullet.active = true;
}
void updateBullet() {
if (!bullet.active) return;
m[bullet.pos.y][bullet.pos.x] = ' ';
if (bullet.direction == 'W') bullet.pos.y--;
else if (bullet.direction == 'S') bullet.pos.y++;
else if (bullet.direction == 'A') bullet.pos.x--;
else if (bullet.direction == 'D') bullet.pos.x++;
// 判断边界
if (bullet.pos.x < 0 || bullet.pos.x >= width || bullet.pos.y < 0 || bullet.pos.y >= height) {
bullet.active = false;
return;
}
// 可以添加障碍物或敌人检测
m[bullet.pos.y][bullet.pos.x] = '*'; // 子弹表示为'*'
}
void playMazeGame(int WIDTH = 21, int HEIGHT = 11) {
struct Position { int x, y; };
vector<vector<int>> maze(HEIGHT, vector<int>(WIDTH, 1));
Position player, exitPos;
vector<Position> directions = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} };
auto generateMaze = [&](int x,int y,auto&&generateMazeRef) -> void {
maze[y][x] = 0;
// 打乱方向
for (int i = 0; i < 4; ++i) swap(directions[i], directions[rand() % 4]);
for (auto dir : directions) {
int nx = x + dir.x * 2, ny = y + dir.y * 2;
if (nx > 0 && nx < WIDTH - 1 && ny > 0 && ny < HEIGHT - 1 && maze[ny][nx] == 1) {
maze[y + dir.y][x + dir.x] = 0;
generateMazeRef(nx, ny, generateMazeRef);
}
}
};
// 生成全墙
for (auto& row : maze) fill(row.begin(), row.end(), 1);
srand((unsigned)time(0));
generateMaze(1, 1, generateMaze);
// 随机入口出口(奇数行)
int ey1 = (rand() % ((HEIGHT - 2) / 2)) * 2 + 1;
int ey2 = (rand() % ((HEIGHT - 2) / 2)) * 2 + 1;
maze[ey1][0] = 0; maze[ey2][WIDTH - 1] = 0;
player = {1, ey1};
exitPos = {WIDTH - 2, ey2};
auto draw = [&]() {
system("cls");
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x)
if (player.x == x && player.y == y) cout << 'P';
else if (maze[y][x] == 1) cout << '#';
else if (x == exitPos.x && y == exitPos.y) cout << 'E';
else cout << ' ';
cout << endl;
}
cout << "用WASD控制,达到E。Q退出~" << endl;
};
auto movePlayer = [&](char ch) -> bool {
Position np = player;
if (ch=='w'||ch=='W') np.y--;
else if (ch=='s'||ch=='S') np.y++;
else if (ch=='a'||ch=='A') np.x--;
else if (ch=='d'||ch=='D') np.x++;
if (np.x>=0 && np.x<WIDTH && np.y>=0 && np.y<HEIGHT && maze[np.y][np.x]==0) {
player = np;
return true;
}
return false;
};
while (true) {
draw();
if (_kbhit()) {
char ch = _getch();
if (ch=='q'||ch=='Q') break;
movePlayer(ch);
}
if (player.x == exitPos.x && player.y == exitPos.y) {
draw();
cout << "恭喜你到达出口!" << endl;
break;
}
_sleep(100);
}
}
struct P {
int x, y;
};
class SnakeGame {
public:
int width, height;
vector<P> snake;
Position food;
char dir; // 当前方向
int score;
bool gameOver;
int speed; // 调整速度(越小越快)
SnakeGame() {
srand(time(0));
// 随机地图尺寸(20-40宽,10-20高)
width = rand() % 21 + 20;
height = rand() % 11 + 10;
init();
}
void init() {
score = 0;
gameOver = false;
dir = 'd'; // 初始方向向右
snake.clear();
snake.push_back({width/2, height/2});
generateFood();
speed = 150000; // 初始速度
}
void generateFood() {
while (true) {
food.x = rand() % width + 1;
food.y = rand() % height + 1;
bool onSnake = false;
for (auto s : snake) {
if (s.x == food.x && s.y == food.y) {
onSnake = true;
break;
}
}
if (!onSnake) break;
}
}
void draw() {
system("cls"); // Windows清屏
// 头部
for (int y = 0; y <= height + 1; y++) {
for (int x = 0; x <= width + 1; x++) {
if (y == 0 || y == height + 1 || x == 0 || x == width + 1) {
cout << "#"; // 边框
} else {
bool printed = false;
if (food.x == x && food.y == y) {
cout << "@"; // 食物
printed = true;
} else {
for (size_t i = 0; i < snake.size(); i++) {
if (snake[i].x == x && snake[i].y == y) {
if (i == 0)
cout << "O"; // 蛇头
else
cout << "o"; // 蟒身
printed = true;
break;
}
}
}
if (!printed) cout << " ";
}
}
cout << endl;
}
cout << "Score: " << score << endl;
cout << "hehe" << endl; // 署名
cout << "控制:WASD,退出:Q" << endl;
}
void run() {
while (!gameOver) {
// 处理输入
if (_kbhit()) {
char ch = _getch();
switch (ch) {
case 'w': case 'W':
if (dir != 's') dir = 'w'; break;
case 'a': case 'A':
if (dir != 'd') dir = 'a'; break;
case 's': case 'S':
if (dir != 'w') dir = 's'; break;
case 'd': case 'D':
if (dir != 'a') dir = 'd'; break;
case 'q': case 'Q':
gameOver = true;
break;
}
}
// 移动
P newHead = snake[0];
switch (dir) {
case 'w': newHead.y -= 1; break;
case 'a': newHead.x -= 1; break;
case 's': newHead.y += 1; break;
case 'd': newHead.x += 1; break;
}
// 边界检测
if (newHead.x <= 0 || newHead.x > width || newHead.y <= 0 || newHead.y > height) {
gameOver = true;
break;
}
// 自咬检测
for (auto s : snake) {
if (s.x == newHead.x && s.y == newHead.y) {
gameOver = true;
break;
}
}
if (gameOver) break;
// 添加新头
snake.insert(snake.begin(), newHead);
// 吃到食物
if (newHead.x == food.x && newHead.y == food.y) {
score += 10;
generateFood();
// 难度调节
if (speed > 50000) speed -= 10000;
} else {
snake.pop_back(); // 移除尾巴
}
draw();
usleep(speed);
}
cout << "Game Over! 你的分数是:" << score << endl;
}
};
void guessNumberGame() {
// 初始化随机数种子
srand(std::time(nullptr));
int secretNumber = std::rand() % 100 + 1; // 1-100之间的随机数
int guess;
int attempts = 0;
cout << "欢迎来到猜数字游戏!我已经想好一个1到100之间的数字了。\n";
while (true) {
cout << "请输入你的猜测(输入0退出):";
cin >> guess;
if (guess == 0) {
cout << "游戏退出!正确的数字是 " << secretNumber << "。\n";
break;
}
attempts++;
if (guess > secretNumber) {
std::cout << "太大了!再试试。\n";
} else if (guess < secretNumber) {
std::cout << "太小了!再试试。\n";
} else {
std::cout << "恭喜你!猜对了,答案就是 " << secretNumber << "。\n";
std::cout << "你一共猜了 " << attempts << " 次。\n";
break;
}
}
}
void runCalcGame(){
srand(time(nullptr));
int a=rand()%100+1;
int b=rand()%100+1;
cout<<a<<"+"<<b<<"=?";
int n;
int ans;
while(1){
cout<<"请输入你认为的结果(输入0退出)"<<endl;
cin>>n;
if(n==0){
cout<<"退出成功!"<<"正确答案:"<<a+b<<endl;
break;
}
if(n==a+b){
cout<<"对了!牛逼"<<endl;
break;
if(ans==0){
cout<<"你用了1次就答对了!真厉害"<<endl;
break;
}
}else{
cout<<"错了!"<<endl;
ans++;
}
}
}
void gamesystem(){
system("color 79");
cout<<"_____________________________________"<<endl;
cout<<"| 欢迎来到GameBox Demo 2.0 |"<<endl;
cout<<"|____________________________________|"<<endl;
Sleep(1000);
system("cls");
for(int i=1;i<=100;i++){
cout<<"正在加载中请等待..."<<endl;
cout<<"%"<<i<<"MB/s"<<endl;
system("cls");
}
while(1){
cout<<"/t/t/t__________________________________"<<endl;
cout<<"/t/t/t| 1.贪吃蛇 |"<<endl;
cout<<"/t/t/t| 2.迷宫 |"<<endl;
cout<<"/t/t/t| 3.飞机大战 |"<<endl;
cout<<"/t/t/t| 4.猜数字 |"<<endl;
cout<<"/t/t/t| 5.计算小能手 |"<<endl;
cout<<"/t/t/t| 6.玛丽奥 |"<<endl;
cout<<"/t/t/t| 7.吃豆人 |"<<endl;
cout<<"/t/t/t| 8.坦克大战 |"<<endl;
cout<<"/t/t/t|________________________________|"<<endl;
int c;
cin>>c;
if(c==1){
SnakeGame game;
game.run();
}else if(c==2){
playMazeGame();
}else if(c==4){
guessNumberGame();
}else if(c==5){
runCalcGame();
}else if(c==8){
// 初始化
initMap();
player.pos.x = width / 2;
player.pos.y = height / 2;
player.direction = 'W'; // 默认向上
placeTank();
bullet.active = false;
while (true) {
drawMap();
// 处理用户输入
if (_kbhit()) {
char ch = _getch();
if (ch == 'w' || ch == 'W') {
player.direction = 'W';
moveTank('W');
} else if (ch == 's' || ch == 'S') {
player.direction = 'S';
moveTank('S');
} else if (ch == 'a' || ch == 'A') {
player.direction = 'A';
moveTank('A');
} else if (ch == 'd' || ch == 'D') {
player.direction = 'D';
moveTank('D');
} else if (ch == ' ') { // 空格键发射子弹
fireBullet();
}
}
updateBullet();
Sleep(200); // 控制游戏速度
}
}else{
cout<<"未知数字"<<endl;
}
}
}
void print_version(){
MessageBox(NULL,"From Windows HeHe","By hehe Windows HeHe Verson 1.0 系统",MB_OK);
}
int main() {
//加速运行速度//
ios::sync_with_stdio(false);
cout.tie();
//cout太多,要加速//
print_system_INT sys;
while (true) {
system("cls"); // 每次循环开始前清屏
// 获取时间每次都更新
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "\t\t\tWindows HeHe Verson 1.0 系统\n";
cout << "\t\t现在时间:" << 1900 + ltm->tm_year << "年"
<< 1 + ltm->tm_mon << "月" << ltm->tm_mday << "日"
<< ltm->tm_hour << "时" << ltm->tm_min << "分" << ltm->tm_sec << "秒" << endl;
system("pause");
cout<<"\t\t\t_____________________________"<<endl;
cout<<"\t\t\t| MS-DOS EXCUTIVE |"<<endl;
cout<<"\t\t\t| 1.改变系统颜色 |"<<endl;
cout<<"\t\t\t| 2.查看系统信息 |"<<endl;
cout<<"\t\t\t| 3.创建文件 |"<<endl;
cout<<"\t\t\t| 4.玩游戏 |"<<endl;
cout<<"\t\t\t| 5.版本号 |"<<endl;
cout<<"\t\t\t| 6.关机 |"<<endl;
cout<<"\t\t\t____________________________"<<endl;
cout<<"输入(1到5)"<<endl;
int c;
cin>>c;
if(c==1){
sys.colorChange();
}else if(c==2){
sys.systeminfo();
Sleep(1000);
}else if(c==3){
sys.create();
Sleep(1000);
}else if(c==4){
gamesystem();
}else if(c==5){
print_version();
}else if(c==6){
cout<<"已关机..."<<endl;
exit(0);
//什么?你说:为什么不用return 0?//
//我答:因为这是**代码(bushi) //
}
}
return 0;
}
下一期敲粉丝要求代码
Pro,评论吧!(求了!)
这里空空如也
有帮助,赞一个