C# ShowCursor使用心得

在C#中,可通过ShowCursor()函数,进行光标的显示和隐藏,使用方式如下:

1. 首先引用命名空间:using System.Runtime.InteropServices;

2. 写入以下语句:

[DllImport("user32", EntryPoint = "ShowCursor")]

public extern static int ShowCursor(bool show);

3. 调用函数:

ShowCursor(true); //显示光标

ShowCursor(false); //隐藏光标

注意事项:

ShowCursor(true)和ShowCursor(false)一定要成对使用,即:当调用一次ShowCursor(true)时,必须在适当的动作、事件中调用一次ShowCursor(false)相对应。如果仅调用一次ShowCursor(true)或ShowCursor(false),或者重复调用ShowCursor(true)和ShowCursor(false)后,将会导致下次函数调用失效。个人猜测,原因大概是因为调用不当导致该函数的内部显示计数器混乱。

通过如下方式,可避免ShowCursor()失效。

1)在需要显示光标的操作中:

while (ShowCursor(true) < 0)

{

      ShowCursor(true); //显示光标

}

2)在需要隐藏光标的操作中:

while (ShowCursor(false) >= 0)

{

      ShowCursor(false); //隐藏光标

}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的飞机大战游戏的C++代码示例: ```c++ #include <iostream> #include <cstring> #include <conio.h> #include <windows.h> using namespace std; const int WIDTH = 80; // 游戏窗口的宽度 const int HEIGHT = 30; // 游戏窗口的高度 const int MAX_ENEMY = 10; // 最多敌机数量 // 飞机类 class Plane { public: int x, y; // 飞机的位置 int hp; // 飞机的生命值 char shape[3][7]; // 飞机的形状 // 构造函数 Plane(int _x, int _y, int _hp) { x = _x; y = _y; hp = _hp; memset(shape, ' ', sizeof(shape)); strcpy(shape[0], " /A\\ "); strcpy(shape[1], "|===|"); strcpy(shape[2], " \\V/ "); } // 绘制飞机 void draw() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 7; j++) { gotoxy(x+j, y+i); cout << shape[i][j]; } } } // 移动飞机 void move(int dx, int dy) { x += dx; y += dy; if (x < 0) x = 0; if (x > WIDTH-7) x = WIDTH-7; if (y < 0) y = 0; if (y > HEIGHT-3) y = HEIGHT-3; } // 发射子弹 void shoot() { gotoxy(x+3, y-1); cout << "|"; } }; // 敌机类 class Enemy { public: int x, y; // 敌机的位置 int hp; // 敌机的生命值 char shape[3][7]; // 敌机的形状 // 构造函数 Enemy(int _x, int _y, int _hp) { x = _x; y = _y; hp = _hp; memset(shape, ' ', sizeof(shape)); strcpy(shape[0], "/\\_/\\"); strcpy(shape[1], "| o o|"); strcpy(shape[2], "\\_^_/"); } // 绘制敌机 void draw() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 7; j++) { gotoxy(x+j, y+i); cout << shape[i][j]; } } } // 移动敌机 void move(int dx, int dy) { x += dx; y += dy; if (x < 0) x = 0; if (x > WIDTH-7) x = WIDTH-7; if (y < 0) y = 0; if (y > HEIGHT-3) y = HEIGHT-3; } // 发射子弹 void shoot() { gotoxy(x+3, y+3); cout << "|"; } }; // 隐藏光标 void hideCursor() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(handle, &cursorInfo); cursorInfo.bVisible = false; SetConsoleCursorInfo(handle, &cursorInfo); } // 显示光标 void showCursor() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(handle, &cursorInfo); cursorInfo.bVisible = true; SetConsoleCursorInfo(handle, &cursorInfo); } // 设置光标位置 void gotoxy(int x, int y) { COORD pos = {x, y}; HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(handle, pos); } // 清屏 void clear() { system("cls"); } // 获取随机数 int random(int min, int max) { return rand() % (max - min + 1) + min; } int main() { srand(time(NULL)); // 初始化随机数种子 hideCursor(); // 隐藏光标 Plane myPlane(WIDTH/2-3, HEIGHT-3, 3); // 创建自己的飞机 Enemy enemies[MAX_ENEMY]; // 创建敌机数组 int score = 0; // 得分 int enemyCount = 0; // 敌机数量 // 游戏循环 while (true) { clear(); // 清屏 // 绘制自己的飞机 myPlane.draw(); // 移动自己的飞机 if (_kbhit()) { // 检测键盘输入 char ch = _getch(); switch (ch) { case 'a': case 'A': myPlane.move(-1, 0); break; case 'd': case 'D': myPlane.move(1, 0); break; case 'w': case 'W': myPlane.move(0, -1); break; case 's': case 'S': myPlane.move(0, 1); break; case ' ': myPlane.shoot(); break; } } // 绘制敌机 for (int i = 0; i < enemyCount; i++) { enemies[i].draw(); } // 移动敌机 for (int i = 0; i < enemyCount; i++) { enemies[i].move(0, 1); } // 生成新敌机 if (enemyCount < MAX_ENEMY && random(0, 100) < 10) { enemies[enemyCount++] = Enemy(random(0, WIDTH-7), 0, 2); } // 检测子弹是否击中敌机 for (int i = 0; i < enemyCount; i++) { for (int j = 0; j < 3; j++) { if (gotoxy(enemies[i].x+3, enemies[i].y+j) == gotoxy(myPlane.x+3, myPlane.y-1)) { enemies[i].hp--; if (enemies[i].hp == 0) { score += 10; enemyCount--; enemies[i] = enemies[enemyCount]; } } } } // 绘制得分 gotoxy(0, HEIGHT); cout << "Score: " << score; Sleep(50); // 等待一段时间 } return 0; } ``` 这个代码示例是一个简单的控制台游戏,使用了Windows API来控制光标位置和清屏。你可以运行它来体验一下。当然,这个代码还有很多可以改进的地方,比如加入音效、增加游戏关卡等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值