cursor在c语言的用法,setCursorPosition(0,0);这个如何用

求助 setCursorPosition(0,0);这个怎么用

编写一个程序,在控制台打印出子弹和靶,需要用到这个函数,这个函数怎么用,有什么注意的地方么

------解决思路----------------------

仅供参考:#include 

#include 

void ConPrint(char *CharBuffer, int len);

void ConPrintAt(int x, int y, char *CharBuffer, int len);

void gotoXY(int x, int y);

void ClearConsole(void);

void ClearConsoleToColors(int ForgC, int BackC);

void SetColorAndBackground(int ForgC, int BackC);

void SetColor(int ForgC);

void HideTheCursor(void);

void ShowTheCursor(void);

int main(int argc, char* argv[])

{

HideTheCursor();

ClearConsoleToColors(15, 1);

ClearConsole();

gotoXY(1, 1);

SetColor(14);

printf("This is a test...\n");

Sleep(5000);

ShowTheCursor();

SetColorAndBackground(15, 12);

ConPrint("This is also a test...\n", 23);

SetColorAndBackground(1, 7);

ConPrintAt(22, 15, "This is also a test...\n", 23);

gotoXY(0, 24);

SetColorAndBackground(7, 1);

return 0;

}

//This will clear the console while setting the forground and

//background colors.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的飞机大战游戏的C语言程序,你可以根据自己的需求进行修改和优化。 ```c #include <stdio.h> #include <conio.h> #include <windows.h> #include <time.h> #define WIDTH 40 #define HEIGHT 20 int score = 0; void setCursorPosition(int x, int y) { COORD position = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); } void hideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void drawBorder() { for (int i = 0; i < WIDTH + 2; ++i) { setCursorPosition(i, 0); printf("#"); setCursorPosition(i, HEIGHT + 1); printf("#"); } for (int i = 0; i < HEIGHT + 2; ++i) { setCursorPosition(0, i); printf("#"); setCursorPosition(WIDTH + 1, i); printf("#"); } } void drawPlane(int x, int y) { setCursorPosition(x, y); printf(" /\\ "); setCursorPosition(x, y + 1); printf(" / \\ "); setCursorPosition(x, y + 2); printf("/____\\"); } void drawBullet(int x, int y) { setCursorPosition(x, y); printf("*"); } void drawEnemy(int x, int y) { setCursorPosition(x, y); printf(" | "); setCursorPosition(x, y + 1); printf("\\_o_/"); setCursorPosition(x, y + 2); printf(" / \\ "); } void updateScore() { setCursorPosition(WIDTH + 4, 3); printf("Score: %d", score); } int main() { hideCursor(); drawBorder(); drawPlane(WIDTH / 2 - 3, HEIGHT - 3); int planeX = WIDTH / 2 - 3; int bulletX = -1; int bulletY = -1; int enemyX = -1; int enemyY = 1; srand((unsigned) time(NULL)); while (1) { Sleep(50); // Move plane if (kbhit()) { char c = getch(); if (c == 'a' && planeX > 1) { drawPlane(planeX, HEIGHT - 3); planeX--; } if (c == 'd' && planeX < WIDTH - 7) { drawPlane(planeX, HEIGHT - 3); planeX++; } if (c == ' ') { if (bulletY == -1) { bulletX = planeX + 3; bulletY = HEIGHT - 4; drawBullet(bulletX, bulletY); } } } // Move bullet if (bulletY > 0) { drawBullet(bulletX, bulletY); bulletY--; if (bulletY == 0) { drawBullet(bulletX, bulletY); bulletY = -1; } } // Move enemy if (enemyX == -1 && enemyY == -1) { enemyX = rand() % (WIDTH - 4) + 2; enemyY = 1; drawEnemy(enemyX, enemyY); } else if (enemyY < HEIGHT) { drawEnemy(enemyX, enemyY); enemyY++; if (bulletX != -1 && bulletY == enemyY && bulletX >= enemyX && bulletX <= enemyX + 4) { drawBullet(bulletX, bulletY); bulletY = -1; enemyX = -1; enemyY = -1; score++; updateScore(); } if (enemyY == HEIGHT) { drawEnemy(enemyX, enemyY); enemyX = -1; enemyY = -1; } } } return 0; } ``` 这个程序使用了Windows控制台API来实现游戏界面和控制游戏元素的移动。通过键盘输入来控制飞机的移动和发射子弹,敌机会自动向下移动,当子弹击中敌机时,得分加一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值