C语言——控制小球移动(贪吃蛇移动原理)

这篇博客详细介绍了如何使用C语言和Windows API实现一个简单的控制台游戏,玩家通过W/S/A/D键控制角色在2D环境中移动。通过监听键盘输入并更新角色坐标,作者展示了如何隐藏光标并保持清晰的界面。
摘要由CSDN通过智能技术生成

实现效果如图:

在这里插入图片描述

代码如下:

         通过控制W/S/A/D方向移动。

#include<stdio.h>
#include<windows.h>//使用gotoxy(光标 移动函数)
#include<conio.h>//监听键盘输入
struct Coord
{
	int x;
	int y;
}coord;
int direct = '0';
void Move();
void gotoxy(int x, int y);
void HideCursor();
int main(int argc,char *argv[])
{
	//初始化位置
	coord.x = 20;
	coord.y = 20;
	gotoxy(coord.x, coord.y);
	printf("o");
	HideCursor();
	while (1)
	{
		Move();
	}
	
	return 0;
}
//移动函数
void Move()
{
	int i = 0;
	if (_kbhit)//监听键盘输入
	{
		fflush(stdin);
		direct = _getch();
	}
	//移动时,清空上一位置
	gotoxy(coord.x,coord.y);
	printf(" ");

	switch (direct)
	{
	case 'W':
	case 'w':
		coord.y--; break;
	case 'S':
	case 's':
		coord.y++; break;
	case 'A':
	case 'a':
		coord.x--; break;
	case 'D':
	case 'd':
		coord.x++; break;
	}
	gotoxy(coord.x, coord.y);
	printf("o");
}
//移动光标函数
void gotoxy(int x, int y)
{
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//隐藏光标函数
void HideCursor() 
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	GetConsoleCursorInfo(hOut, &cci);
	cci.bVisible = FALSE;
	SetConsoleCursorInfo(hOut, &cci);
}

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个简单的 C 语言实现可用键盘移动贪吃蛇游戏的代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define WIDTH 40 #define HEIGHT 20 int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; enum eDirection dir; void Setup() { dir = STOP; x = WIDTH / 2; y = HEIGHT / 2; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; score = 0; } void Draw() { system("cls"); for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (j == 0) printf("#"); if (i == y && j == x) printf("O"); else if (i == fruitY && j == fruitX) printf("F"); else { int bDrawTail = 0; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { printf("o"); bDrawTail = 1; } } if (!bDrawTail) printf(" "); } if (j == WIDTH - 1) printf("#"); } printf("\n"); } for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); printf("Score: %d\n", score); } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': exit(0); break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x >= WIDTH) x = 0; else if (x < 0) x = WIDTH - 1; if (y >= HEIGHT) y = 0; else if (y < 0) y = HEIGHT - 1; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) exit(0); if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; nTail++; } } int main() { Setup(); while (1) { Draw(); Input(); Logic(); Sleep(50); } return 0; } ``` 这个代码实现了一个简单的贪吃蛇游戏,可以使用键盘控制蛇的移动方向。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值