linux gotoxy(int x, int y)

本文介绍了一种在控制台中使用ANSI转义码进行光标定位的方法,通过一个简单的C语言函数voidgotoxy(intx,inty),实现将光标移动到指定位置的功能,这对于开发控制台应用或游戏非常有用。

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

void gotoxy(int x,int y)   //Fantasy
{
   printf("%c[%d;%df",0x1B,y,x);
}
#include <stdio.h> #include <windows.h> #include <conio.h> #include <stdbool.h> #define MAP_WIDTH 70 // 地图区域宽度 #define MAP_HEIGHT 20 // 地图区域高度 #define INFO_WIDTH 10 // 信息栏宽度 #define CONSOLE_WIDTH (MAP_WIDTH + INFO_WIDTH) #define CONSOLE_HEIGHT (MAP_HEIGHT + 5) #define PLAYER_COUNT 3 #define TRAIL_LENGTH 30 #define ATTACK_DURATION 10 typedef enum { STATE_IDLE, STATE_MOVE, STATE_ATTACK } PlayerState; typedef struct { int x; int y; PlayerState state; int attack_timer; int prev_x; int prev_y; int hp; int score; WORD color; char trail[TRAIL_LENGTH][2]; int trail_index; } Player; typedef struct { char buffer[16]; int head; int tail; int count; } InputBuffer; HANDLE hConsole; Player players[PLAYER_COUNT]; InputBuffer input_buffer; char game_map[MAP_HEIGHT][MAP_WIDTH]; // 初始化控制台颜色 void init_console() { hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole, &csbi); csbi.dwSize.X = CONSOLE_WIDTH; csbi.dwSize.Y = CONSOLE_HEIGHT; SetConsoleScreenBufferSize(hConsole, csbi.dwSize); } void gotoxy(int x, int y) { COORD coord = {x, y}; SetConsoleCursorPosition(hConsole, coord); } void set_color(WORD color) { SetConsoleTextAttribute(hConsole, color); } void reset_color() { SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); } void init_map() { for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { if (x == 0 || x == MAP_WIDTH-1 || y == 0 || y == MAP_HEIGHT-1) { game_map[y][x] = '#'; } else if (x % 10 == 0 && y % 7 == 0) { game_map[y][x] = '#'; } else { game_map[y][x] = ' '; } } } } void init_players() { // 初始化三个不同颜色的玩家 Player p1 = {5, 5, STATE_IDLE, 0, 5, 5, 100, 0, FOREGROUND_RED | FOREGROUND_INTENSIT
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值