c - 如何更新通过 CursorPosition 插入的字符(C/Windows API)

标签 c windows api

将 C ON CODELIGHT 与 MinGW 编译器结合使用。

我正在尝试制作一个简单的 ASCII 游戏。我使用一个 2D 数组来制作 map ,然后使用一个函数将光标放在数组上的特定点上,以通过击键 WASD 跟踪玩家的移动(函数是 gotoXY(int x, int y)。我能够让它工作(有点),但我对一些没有按预期结果的事情有疑问。

  1. 每当用户按下 a 键(向左移动)时,就会出现这段代码。未注释的部分在正确的位置打印出字符“@”,但是它不会更新之前应该有“-”(代表空白图 block )的位置。这就是我使用注释代码的目的,但是,当我运行所有这些未注释的行的程序时,每当我按“a”时,它都会崩溃。 gotoXY() 函数只是将光标指向它作为参数的 x、y 坐标。为什么添加注释部分后我的代码会崩溃?(第 18-22 行)

            //gotoXY(x, y);
            //printf('-');
            x-=2;
            gotoXY(x, y);
            printf("@");
    
  2. 此外,每当我按下按键来移动我的角色时,它就会以太快的速度穿过屏幕。我尝试使用系统(“暂停”)来解决这个问题,但这不起作用。那么,有没有办法在按下某些键后延迟一段时间呢?这样,@ 字符在屏幕上移动的速度会更慢且更一致。

功能:

  printBoard(char board[50][50]); //Prints the gameboard just a 50 by 50 tile
  createBoard(int size, int xPos, int yPos); //Creates the array
  gotoXY(int x, int y); //Sets position of Cursor                 

图片:

-Program before key is pressed.

-Program after key is pressed (lines still commented)

-Program after key is pressed (lines un-commented)

问题:

1.为什么我的代码崩溃了?

2.如何减慢我的角色在屏幕上快速输入的速度。

完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>


void createBoard(int size, int xPos, int yPos);
void printBoard(char board[50][50]);
int main()
{
    int xPos = 32, yPos = 21, bSize = 50;
    createBoard(bSize, xPos, yPos);
    int x = 42, y = 32;                  //Starting coords for player

    while(1){
        if(GetKeyState(65)<0){  //left
            //gotoXY(x, y);
            //printf('-');
            x-=2;               //Moves 1 space left
            gotoXY(x, y);
            printf("@");
        }
        if(GetKeyState(83)<0){  //down
             y+=2;
             gotoXY(x, y);
             printf("@");
        }
        if(GetKeyState(68)<0){  //right
            x+=2;
            gotoXY(x, y);
            printf("@");
        } 
        if(GetKeyState(87)<0){  //up
             y-=2;
             gotoXY(x, y);
             printf("@");
        }

    }
    return 0;
}

void printBoard(char board[50][50]){
     HANDLE  hConsole;
     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
     int i, j;

     for(i = 0; i < 50; i++){
        for(j = 0; j < 50; j++){
            SetConsoleTextAttribute(hConsole,7); //Sets tiles to white
            if(board[i][j]=='@'){
                SetConsoleTextAttribute(hConsole, 4);  //Sets @ to red
            }
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
}

void createBoard(int size, int xPos, int yPos){
    int i, j;
    char sampleBoard[size][size], person = '@';

    for(i = 0; i < size; i++){
        for(j = 0; j < size; j++){
            sampleBoard[i][j] = '-';
        }
    }

    sampleBoard[xPos][yPos] = person;
    printBoard(sampleBoard);
}

void gotoXY(int x, int y) {
        //Initialize the coordinates
        COORD coord = {x, y};
        //Set the position
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
        return;

}

最佳答案

我们不能这样写代码

printf('-');

但是您可以将此代码编写为

printf("-");

关于c - 如何更新通过 CursorPosition 插入的字符(C/Windows API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712142/

相关文章:

c - c - 如何将数据从一个进程传递到另一个进程?

c - 编译 ARM 演示项目时出现链接器错误

node.js - 是否有可能做到这一点? (涉及解构和重新分配)

javascript - 尝试检索经过身份验证的 Google+ 用户的电子邮件地址时获取 "gapi.client is undefined"

php - Laravel 5.4 POST 到 API 重定向 302 而不是返回验证错误

c - 将 float 分配给 int 变量不会导致警告

python - 如何在 Python C API 中定义类方法?

mysql - 桌面应用程序 GUI 设计 - 最佳工具

windows - 使用批处理将多个文件的内容重新混合到 .mkv

java - 将 SQLite 数据库从 Android 转换为 Windows (Java)