C语言基础入门48篇_18_使用循环移动游戏人物(屏幕符号运动、while(1){}进行实时响应,if(表达式){},switch(表达式){},windows的API及API进行自行封装使)

这篇博客介绍了如何使用C++和Windows API在控制台上创建一个简单的游戏,允许用户通过WASD键控制星号(*)的移动。通过封装Windows API函数MoveCursorTo来改变光标位置,结合while循环和switch语句实时响应键盘输入,实现字符在屏幕上的移动。程序代码详细展示了如何检测键盘事件,以及在不同位置打印和删除字符。
摘要由CSDN通过智能技术生成

本篇介绍使用循环语句实现指定符号的坐标移动,使用到了while(1){}进行实时响应if(表达式){}switch(表达式){}windows中封装的API并对API进行自行封装使用

1.实现的功能

本例的程序实现了*可以在WASD的控制下做位置移动。
在这里插入图片描述

2.需要包含的头文件


因为这次课的内容需要 使用C标准库之外的一些函数(C的编译器中会包含标准的函数),因此,需要包含对应的 windows头文件。

  • Windows.h:我们将使用其提供的函数,实现任意位置输出
  • conio.h:我们将使用其中的函数,实现响应键盘按键

3.自行封装函数用于移动坐标


以下函数 封装了Windows的API,使得我们可以将光标移动到任意坐标位置,方便我们在任意位置打印字符。
GetStdHandle()SetConsoleCursorPosition()均为 windows中的 API

void MoveCursorTo(int nRow, int nCol)
{
    COORD crdLocation;
    crdLocation.X = nCol;
    crdLocation.Y = nRow;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}

调用封装的函数,可以用于在画面中在任意位置打印标识

#include <windows.h>
#include <conio.h>
#include <stdio.h>

void SetConsoleCursorPosition(int nRow, int nCol)
{
	COORD crdLocation;
	crdLocation.X = nCol;
	crdLocation.Y = nRow;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}

int main(int argc, char* argv[])
{
	SetConsoleCursorPosition(10,10);//输入行列坐标
	printf("*");

	SetConsoleCursorPosition(20, 20);//输入行列坐标
	printf("#\r\n");
	
	return 0;
}

在这里插入图片描述

4.程序中功能实现方法

  • 移动到原位置覆盖内容,并移动到新位置打印内容
            MoveCursorTo(nRow, nCol);
            printf(" ");
            nCol -= 1;
            MoveCursorTo(nRow, nCol);
            printf("*");
  • 循环响应用户输入
    (1)使用_kbhit响应键盘按键,_kbhit可以检测用户是否按下,而_getch可以配合_kbhit拿到用户输入。
    (2)当_kbhit返回值不为0的时候,代表用户有按键按下。

5.程序代码

// 17-move-player.cpp : 定义控制台应用程序的入口点。
//

#include <windows.h>
#include <conio.h>
#include <stdio.h>

void MoveCursorTo(int nRow, int nCol)
{
    COORD crdLocation;
    crdLocation.X = nCol;
    crdLocation.Y = nRow;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}

int main(int argc, char* argv[])
{
    char chInput = 0;


    int nRow = 10;
    int nCol = 10;
    MoveCursorTo(nRow, nCol);
    printf("*");
    
    while (1)//永为真,不断的响应需求
    {
        if (_kbhit() != 0)//当_kbhit返回值不为0的时候,代表用户有按键按下
        {
            chInput = _getch();//获取键盘输入,_getch可以配合_kbhit拿到用户输入
            switch (chInput)//
            {
            case 'a':
                //移动到原有位置
                MoveCursorTo(nRow, nCol);
                //覆盖掉原有内容
                printf(" ");//利用空格覆盖*,不覆盖的话原先的*仍会存在

                //改变坐标并移动、打印
                nCol -= 1;//列减1
                MoveCursorTo(nRow, nCol);
                printf("*");
                break;
            case 'w':
                //移动到原有位置
                MoveCursorTo(nRow, nCol);
                //覆盖掉原有内容
                printf(" ");

                //改变坐标并移动、打印
                nRow -= 1;
                MoveCursorTo(nRow, nCol);
                printf("*");
                break;
            case 's':
                //移动到原有位置
                MoveCursorTo(nRow, nCol);
                //覆盖掉原有内容
                printf(" ");

                //改变坐标并移动、打印
                nRow += 1;
                MoveCursorTo(nRow, nCol);
                printf("*");
                break;
            case 'd':
                //移动到原有位置
                MoveCursorTo(nRow, nCol);
                //覆盖掉原有内容
                printf(" ");

                //改变坐标并移动、打印
                nCol += 1;
                MoveCursorTo(nRow, nCol);
                printf("*");
                break;
            default:
                break;
            }
        }
        
    }
    
 return 0;
}

6.学习视频地址: 使用循环移动游戏人物

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十月旧城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值