谁能给我个最简单的游戏代码,最好加注释
⑤
程序代码:
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出句柄
int x, y;//定义坐标全局变量
//控制光标
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(hConsole, coord);
}
//设置颜色
void settextcolor(int i)
{
SetConsoleTextAttribute(hConsole, i);
}
//处理移动
void move(char key)
{
switch (key)
{
case 72:
if (y - 1 >= 0)//前面没有墙壁
{
--y;
gotoxy(x, y);
printf("■");
}
break;
case 80:
if (y + 1 < 48)//49和50行不作为游戏区
{
++y;
gotoxy(x, y);
printf("■");
}
break;
case 75:
if (x - 2 >= 0)
{
x = x - 2;
gotoxy(x, y);
printf("■");
}
break;
case 77:
if (x + 2 < 50)
{
x = x + 2;
gotoxy(x, y);
printf("■");
}
break;
default:
break;
}
}
//控制函数
void control()
{
char key1 = 0, key2 = 0;
while (key1 != 27)
{
key1 = getch();//接收键码
key2 = 0;
if (key1 <= 0)
key2 = getch();//继续接收键码
if (key2 == 72 || key2 == 75 || key2 == 77 || key2 == 80)//说明是方向键
{
move(key2);
}
}
}
void main()
{
srand((long)time(NULL));
system("mode con: cols=50 lines=50");//可以在命令提示符中输入mode /?查询此用法
system("title 小游戏");
settextcolor(0xe);
x = (rand() % 25) * 2, y = rand() % 48;
gotoxy(x, y);
printf("■");
control();
}