c實現坦克移動
阿新 • • 發佈:2018-02-01
背景 clock ret tex ase oid window set cdir
#include<Windows.h>
#include <stdio.h>
#include<stdlib.h>
#include <conio.h>
#include <time.h>
#define USERTANKMAP 1
#define ENETANKMAP 2
#define USERTANK "■"
#define TANK_DIR_UP 0
#define TANK_DIR_DW 2
#define TANK_DIR_LF 1
#define TANK_DIR_RH 3
//用戶坦克
typedef struct USER //速度為3
{
int x;
int y;
int life; //默認為50
int aggressivity; //默認為25
int dir;
}user, *Puser;
//全局的用戶坦克
user myuser;
//字符打印
void PrintChar(int Wide, int High, char*pszChar, WORD wAttr) {
//1.設置光標屬性
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 1;
cci.bVisible = FALSE;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(hOut, &cci);
//2.修改光標在控制臺中的坐標位置
COORD loc;
loc.X = Wide * 2;
loc.Y = High;
//3.設置光標在控制臺中的坐標位置
SetConsoleCursorPosition(hOut, loc);
// 設置打印符號的前景色背景色
SetConsoleTextAttribute(hOut, wAttr);
// 輸出你想輸出的
printf("%s", pszChar);
}
void tankchar(int Wide, int High, char*pszChar, WORD wAttr) {
switch (myuser.dir)
{
case TANK_DIR_LF:
PrintChar(myuser.x, myuser.y, USERTANK, wAttr);
///////////////////////////////////////////////////////////
PrintChar(myuser.x, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x - 1, myuser.y, USERTANK, wAttr);
PrintChar(myuser.x, myuser.y + 1, USERTANK, wAttr);
PrintChar(myuser.x + 1, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x + 1, myuser.y + 1, USERTANK, wAttr);
break;
case TANK_DIR_RH:
PrintChar(myuser.x, myuser.y, USERTANK, wAttr);
///////////////////////////////////////////////////////////
PrintChar(myuser.x + 1, myuser.y, USERTANK, wAttr);
PrintChar(myuser.x, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x, myuser.y + 1, USERTANK, wAttr);
PrintChar(myuser.x - 1, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x - 1, myuser.y + 1, USERTANK, wAttr);
break;
case TANK_DIR_UP:
PrintChar(myuser.x, myuser.y, USERTANK, wAttr);
//////////////////////////////////////////////////////////
PrintChar(myuser.x + 1, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x + 2, myuser.y, USERTANK, wAttr);
PrintChar(myuser.x + 2, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x + 1, myuser.y - 2, USERTANK, wAttr);
break;
case TANK_DIR_DW:
PrintChar(myuser.x, myuser.y, USERTANK, wAttr);
///////////////////////////////////////////////////////////
PrintChar(myuser.x - 1, myuser.y, USERTANK, wAttr);
PrintChar(myuser.x - 1, myuser.y - 1, USERTANK, wAttr);
PrintChar(myuser.x, myuser.y + 1, USERTANK, wAttr);
PrintChar(myuser.x + 1, myuser.y, USERTANK, wAttr);
PrintChar(myuser.x + 1, myuser.y - 1, USERTANK, wAttr);
break;
default:
break;
}
}
void TankMove(int m_nDir)
{
if (myuser.x > 3 || myuser.x<37 || myuser.y >3 || myuser.y<37)
{
PrintChar(myuser.x + 1, myuser.y, " ", 0x00);
system("cls");
switch (m_nDir) {
case TANK_DIR_UP:
myuser.y--;
break;
case TANK_DIR_DW:
myuser.y++;
break;
case TANK_DIR_LF:
myuser.x--;
break;
case TANK_DIR_RH:
myuser.x++;
break;
}
tankchar(myuser.x, myuser.y, USERTANK, 0x44);
}
}
bool ChangeDir(char cDir) {
int nNewDir = -1;
switch (cDir) {
case ‘W‘:
case ‘w‘:
nNewDir = TANK_DIR_UP;
break;
case ‘s‘:
case ‘S‘:
nNewDir = TANK_DIR_DW;
break;
case ‘a‘:
case ‘A‘:
nNewDir = TANK_DIR_LF;
break;
case ‘d‘:
case ‘D‘:
nNewDir = TANK_DIR_RH;
break;
case ‘q‘:
case ‘Q‘:
exit(0);
break;
case ‘e‘:
case ‘E‘:
_getch();
break;
default:
break;
}
myuser.dir = nNewDir;
TankMove(nNewDir);
return true;
}
void main()
{
//////////////////模擬按下shit鍵////////////////
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
myuser.x = 20;
myuser.y = 20;
myuser.dir = TANK_DIR_DW;
PrintChar(myuser.x, myuser.y, USERTANK, 0x55);
char ch = _getch();
if (ch == ‘w‘ || ch == ‘W‘ || ch == ‘a‘ || ch == ‘A‘ || ch == ‘s‘ || ch == ‘S‘ || ch == ‘d‘ || ch == ‘D‘) {
// 設置坦克的移動時間間隔
clock_t tBegin = clock(), tEnd = 0;
while (true) {
if (_kbhit())
{
char ch = _getch();
ChangeDir(ch);
}
tEnd = clock();
if (tEnd - tBegin > 200)
{
tBegin = tEnd;
TankMove(myuser.dir);
}
}
}
}
c實現坦克移動