c小游戏程序
① 小游戏C语言程序
下落的小鸟
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定义一种Birds数据类型(含3个成员)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定义Birds类型 指针变量Bird并赋初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定义一种Bg数据类型(含5个成员)
Bg *Bg1 = (Bg*)malloc(sizeof(Bg)); //定义Bg类型 指针变量Bg1并赋初值
void Position(int x, int y) //光标定位函数(用于指定位置输出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void Csh( ) //初始化界面
{
printf("══════════════════════════════════════ ");
printf(" ■■ ■■ C语言版 Flappy Bird ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ 瞎搞人:yyposs原创 ");
printf(" ■■ ■■ 瞎搞日期:2014.2 ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ 改编:鸣蝉百2021.7 ");
printf(" ■■ ■■ 操作:按向上方向键让小鸟起飞 ");
printf(" ■■ ");
printf(" ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ DEVc++运行通过 ");
printf("══════════════════════════════════════ ");
printf(" 按键继续…");
getch( );
system("cls");
}
void PrFK( ) //输出方框(游戏范围区)
{int i;
Position(1, 1); printf("╔"); Position(79, 1); printf("╗");
Position(1, 24); printf("╚"); Position(79, 24); printf("╝");
for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}
for(i=2;i<=23;i++)
{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);
Position(79,i); printf("║");
}
Position(4, 25); printf("小鸟即将出现,请准备按键起飞… ");
getch( );
Position(4, 25); printf(" ");
}
void CreatBg( ) //创建障碍物坐标(便于打印输出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //确定障碍物的一对基本坐标(此时值是在游戏框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障碍物的基本坐标x、y
Bg1->l_blank = Max_blank - Grade; //障碍物上下两部分之间的空白距离l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}
void InsertBg(Bg *p) //随机改变障碍物的y坐标,让空白通道有上下变化
{int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //启动随机数发生器
temp = rand( ); //产生一个随机数并赋值给temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}
void CreatBird( ) //建立小鸟的坐标(初始打印输出小鸟的位置)
{Bird->x = 41; Bird->y = 10;}
int CheckYN(Bg *q) //判断游戏结束与否(值为0是要结束,为1没有要结束)
{Bg *p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}
void Check_Bg(Bg *q) //核查开头的障碍物坐标是否在游戏区内
{Bg *p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}
void Prt_Bg(Bg *q) //打印输出障碍物(依据其x、y坐标进行相应输出)
{Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}
void PrtBird( ) //打印输出小鸟
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障碍物x坐标左移,并记录成绩
{Bg *p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}
int main( )
{int i = 0; int t;
while (1)
{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP)) //按下了向上方向键
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200); //程序延时200毫秒(数值大小决定游戏速度快慢)
i = 0;
}
Position(6, 25);
printf("游戏结束! 请输入:0.退出 1.重玩");
scanf("%d",&t);
if (t==0)break;
system("cls"); Score = 0;
}
return 0;
}
② 用C语言编写的小游戏代码是什么
“猜数字小游戏”,每个数字后按空格,最后按回车确认
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a[4],b[4];
int count=0; //计算猜测次数
void csh( ); //初始化
void start( ); //开始游戏
int main( )
{ csh( );
start( );
}
void csh( ) //初始化
{ printf(" 猜 数 字 小 游 戏 ");
printf(“ 猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B. ”);
}
void start( ) //开始游戏
{int m,n; //m是完全猜对的个数,n是顺序不对的个数
while(1)
{srand((unsigned)time(NULL)); //初始化随机数发生器srand( )
while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; //rand( )函数每次随机产生一个0-9的数
if( (a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&
(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; } //4个随机数各自不相等
printf(" 请依次输入4个一位整数: ");
while(1)
{for(int i=0;i<4;i++) scanf(“%d”,&b[i]);
printf(" 你输入的是:%d %d %d %d ",b[0],b[1],b[2],b[3]);
m=0;n=0;
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1; }
}
count=count+1;
printf(" %dA %dB 你试了%d次 ",m,n,count);
if(m==4)break;
if(count==8){ count=0; break; }
}
printf(" ");
if(m==4)printf(" 你猜对了(^-^)! 就是:%d %d %d %d ",a[0],a[1],a[2],a[3]);
else printf(" 你输了(T-T)!哈哈!应该是:%d %d %d %d ",a[0],a[1],a[2],a[3]);
int z;
printf(" (要继续吗?1或0) ");
scanf(“%d”,&z);
if(z==0) break;
}
}
③ C语言编写小游戏
//相当精简的贪吃蛇程序,在vc完全可以运行
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#include<conio.h>
#define width 25
#define height 60
#define w '-'
#define h '|'
#define SNAKE 's'
#define FOOD 'o'
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
char wall[width][height] = { 0 };
//定义位置结构体,变量仅两个
typedef struct _spos
{
int x;
int y;
}Spos;
//定义节点,用于产生蛇身子节点变量。蛇一般有三个节点,每个节点可以指向前后身子,用于来调整蛇的前进与后退
typedef struct _node
{
Spos pos;
struct _node *next;
struct _node *head;
}Node;
//定义食物变量,在特定位置产生食物符号
typedef struct _food
{
int x;
int y;
}Food;
//定义初始化节点函数,用于动态创建初始节点所需的空间。
Node *CreateNode()
{
Node *phead = NULL;
phead = (Node *)malloc(sizeof(Node));
return phead;
}
//声明全局变量,蛇头;以供初始化及销毁特定位置节点
extern Node *snake=NULL;
//定义动作函数吃食物,当蛇遇到食物时,食物消失。
BOOL EatFood(Spos *snake )
{
if (wall[snake->x][snake->y] == FOOD)
return TRUE;
else
return FALSE;
}
//定义贪吃蛇游戏结束函数,当违反游戏规则(即就是蛇头撞墙)时退出。
BOOL GameOver(Spos *snake)
{
if (wall[snake->x][snake->y] == w || wall[snake->x][snake->y] == h)
return TRUE;
else
return FALSE;
}
//定义删除蛇尾函数
void Delsnake()
{
Node *pNew = NULL;
Node *snake1 = snake;
Node *p;
p=pNew = snake1;/*->head*/
while (pNew->next != NULL)
{
p = pNew;
pNew = pNew->next;
}
wall[pNew->pos.x][pNew->pos.y] = ' ';
free(pNew);
p->next = NULL;
}
//定义食物产生(即就是旧的食物消失)动作函数
void Getfood()
{
Food foods;
int x, y;
do{
x = rand() % (width -1) + 1;
y = rand() % (height-1 ) + 1;
} while (wall[x][y] != ' ');
foods.x = x;
foods.y = y;
wall[x][y] = FOOD;
}
void initsanke()
{
Node *pNew = NULL;
Node *head = (Node *)malloc(sizeof(Node));
int snake_len = 3;
int x = 10;
int y = 20;
int i;
pNew =head;
pNew->pos.x = x;
pNew->pos.y = y;
wall[x][y] = SNAKE;
//关键的一句,头结点赋值给蛇头
snake = pNew;
//循环产生蛇身,蛇尾并打印图像s
for (i = 1; i < snake_len; i++)
{
y = pNew->pos.y;
pNew->next = CreateNode();
pNew = pNew->next;
pNew->pos.x = x;
pNew->pos.y = y + 1;
pNew->next = NULL;
wall[x][y + 1] = SNAKE;
}
}
void showwall()
{
int i, j;
for (i=0; i<width; i++)
{
for (j = 0; j < height; j++)
{
printf("%c", wall[i][j]);
}
if(i!= width-1) printf("\n");
}
}
void initwall()
{
int i, j;
for (i = 0; i < width; i++)
{
wall[i][0] = h;
wall[i][height-1] =h;
for (j = 1; j < height - 1; j++)
wall[i][j] = ' ';
}
for (i = 0; i < height; i++)
{
wall[0][i] = w;
wall[width-1][i] =w;
}
}
BOOL MoveToup()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;
spos.x = snake->pos.x - 1;
spos.y = snake->pos.y;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}
return TRUE;
}
BOOL MoveToright()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;
spos.x = snake->pos.x;
spos.y = snake->pos.y + 1;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}
return TRUE;
}
BOOL MoveTodown()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;
spos.x = snake->pos.x + 1;
spos.y = snake->pos.y;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}
return TRUE;
}
BOOL MoveToleft()
{
//Node *snake = NULL;
Node *pnew = NULL;
Food *foods = NULL;
BOOL eat;
Spos spos;
spos.x = snake->pos.x;
spos.y = snake->pos.y - 1;
eat = EatFood(&spos);
if (GameOver(&spos))
return FALSE;
pnew = CreateNode();
pnew->pos.x = spos.x;
pnew->pos.y = spos.y;
pnew->next = snake;
snake = pnew;
wall[spos.x][spos.y] = SNAKE;
if (eat)
{
Getfood();
}
else
{
Delsnake();
}
return TRUE;
}
void Menu()
{
BOOL move=FALSE;
char temp;
char ch=LEFT;
initwall();
initsanke();
srand((unsigned int)time(0));;
Getfood();
showwall();
while (1)
{
Sleep(100);
temp = ch;
if (_kbhit())//检查是否有键盘输入
ch = _getch();
if (ch != UP && ch != DOWN && ch != RIGHT && ch != LEFT)
ch = temp;
/*方向相反则无效化操作*/
if ((temp == UP && ch == DOWN) || (temp == DOWN && ch == UP))
ch = temp;
if ((temp == LEFT && ch == RIGHT) || (temp == RIGHT && ch == LEFT))
ch = temp;
switch (ch)
{
case 'a': move =MoveToleft(); break;
case 'w': move =MoveToup(); break;
case 's': move =MoveTodown(); break;
case 'd': move =MoveToright(); break;
default:
break;
}
if (move)
{
system("cls");
showwall();
}
else
break;
}
}
int main()
{
Menu();
printf("\nSorry!Game over!\n");
system("pause");
return 0;
}
④ 教你如何使用C语言编写简单小游戏
编写程序,实现如下表所示的5-魔方阵。
17
24
1
8
15
23
5
7
14
16
4
6
13
20
22
10
12
19
21
3
11
18
25
2
9
5-魔方阵
问题分析
所谓“n-魔方阵”,指的是使用1〜n2共n2个自然数排列成一个n×n的方阵,其中n为奇数;该方阵的每行、每列及对角线元素之和都相等,并为一个只与n有关的常数,该常数为n×(n2+1)/2。
例如5-魔方阵,其第一行、第一列及主对角线上各元素之和如下:
第一行元素之和:17+24+1+8+15=65
第一列元素之和:17+23+4+10+11=65
主对角线上元素之和:17+5+13+21+9=65
而
n×(n2+1)/2=5×(52+1)/2=65
可以验证,5-魔方阵中其余各行、各列及副对角线上的元素之和也都为65。
假定阵列的行列下标都从0开始,则魔方阵的生成方法为:在第0行中间置1,对从2开始的其余n2-1个数依次按下列规则存放:
(1)
假定当前数的下标为(i,j),则下一个数的放置位置为当前位置的右上方,即下标为(i-1,j+1)的位置。
(2)
如果当前数在第0行,即i-1小于0,则将下一个数放在最后一行的下一列上,即下标为(n-1,j+1)的位置。
(3)
如果当前数在最后一列上,即j+1大于n-1,则将下一个数放在上一行的第一列上,即下标为(i-1,0)的位置。
(4)
如果当前数是n的倍数,则将下一个数直接放在当前位置的正下方,即下标为(i+1,j)的位置。
算法设计
在设计算法时釆用了下面一些方法:
定义array()函数,array()函数的根据输入的n值,生成并显示一个魔方阵,当发现n不是奇数时,就加1使之成为奇数。
使用动态内存分配与释放函数malloc()与free(),在程序执行过程中动态分配与释放内存,这样做的好处是使代码具有通用性,同时提高内存的使用率。
在分配内存时还要注意,由于一个整型数要占用两个内存,因此,如果魔方阵中要存放的数有max个,则分配内存时要分配2*max个单元,从而有malloc(max+max)。在malloc()函数中使用max+max而不是2*max是考虑了程序运行的性能。
显然应该使用二维数组来表示魔方阵,但虽然数组是二维形式的,而由于内存是一维线性的,因此在存取数组元素时,要将双下标转换为单个索引编号。在程序中直接定义了指针变量来指向数组空间,即使用malloc()函数分配的内存。
⑤ 小游戏的C++代码
/*一个火柴人游戏,亲自验证,可运行*/
/*在编译时添加如下命令:-std=c++11,否则会编译错误*/
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include <thread>
#include <conio.h>
using namespace std;
const unsigned char CTRL_KEY = 0XE0;
const unsigned char LEFT = 0X4B;
const unsigned char RIGHT = 0X4D;
const unsigned char DOWN = 0X50;
const unsigned char UP = 0X48;
int men2[2] = {0,0};
int women2[2]={10,10};
int Game();
void gotoxy( int x, int y ) //光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
int clean( int mm, int nn )
{
gotoxy ( mm, nn );
printf ( " " );
gotoxy ( mm,nn+1);
printf ( " " );
gotoxy ( mm,nn+2);
printf (" ");
}
int men( int x, int y )
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN);
gotoxy( x, y );
printf(" O");
gotoxy( x, y+1 );
printf("<H>");
gotoxy( x, y+2 );
printf("I I");
}
int women( int i, int j )
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
gotoxy( i+1,j );
printf(" O");
gotoxy( i+1,j+1 );
printf("<H>");
gotoxy( i,j+2 );
printf("/I I\\");
}
int m=10, n=10;
int x=0;int y=0;
int TorF()
{
if ( x == m && y == n ) return 1;
else return 0;
}
int womenmove()
{
int turn;
int YNbreak=0;
while( YNbreak == 0 )
{
YNbreaak = TorF();
turn=rand()%3;
clean( m, n );
if( m < x ) m++;
else m--;
if( m == x )
{
if( n < y ) n++;
else n--;
}
if ( m < 0 ) m = 0;
if ( m >= 75 ) m = 75;
if ( n < 0 ) n = 0;
if ( n >= 22 ) n = 22;
women( m,n );
women2[0]=m;
women2[1]=n;
Sleep(100);
}
system ( "cls" );
gotoxy ( 28, 10 );
printf ( "You died!!!\n" );
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE);
system ( "pause" );
exit(0);
return 0;
}
int menmove()
{
system( "cls" );
while (1)
{
switch( getch())
{
case UP:y--;break;
case DOWN:y++;break;
case LEFT:x--;break;
case RIGHT:x++;break;
}
system( "cls" );
if ( x < 0 ) x = 0;
if ( x > 77 ) x = 77;
if ( y < 0 ) y = 0;
if ( y > 22 ) y = 22;
men( x, y );
men2[0] = x;
men2[1] = y;
}
}
int Game()
{
women( 10, 10 );
men( 0, 0 );
int t = 0;
thread qq( womenmove );
menmove();
qq.join();
return 0;
}
int main()
{
system( "mode con cols=80 lines=25" );
printf ( "游戏开始后,随机按下一个键,唤醒你的蓝色小人.如果你被红色的老女人碰到了,那么你就死了\n" );
printf ( "方向键操控小人\n" );
system ( "pause" );
system ( "cls" );
Game();
return 0;
}
/*留下您的赞再拿走,谢谢!*/
⑥ 谁有一些简单小游戏的C语言程序
可以学写“贪吃蛇”代码:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#define W 78 //游戏框的宽,x轴
#define H 26 //游戏框的高,y轴
int dir=3; //方向变量,初值3表示向“左”
int Flag=0; //吃了食物的标志(1是0否)
int score=0; //玩家得分
struct food{ int x; //食物的x坐标
int y; //食物的y坐标
}fod; //结构体fod有2个成员
struct snake{ int len; //蛇身长度
int speed; //速度
int x[100];
int y[100];
}snk; //结构体snk有4个成员
void gtxy( int x,int y) //控制光标移动的函数
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void gtxy( int x,int y); //以下声明要用到的几个自编函数
void csh( ); //初始化界面
void keymove( ); //按键操作移动蛇
void putFod( ); //投放食物
int Over( ); //游戏结束(1是0否)
void setColor(unsigned short p, unsigned short q); //设定显示颜色
int main( ) //主函数
{ csh( );
while(1)
{ Sleep(snk.speed);
keymove( );
putFod( );
if(Over( )) {system(“cls”);
gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);
gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);
getch( ); break;
}
}
return 0;
}
void csh( ) //初始化界面
{ int i; gtxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置
{ setColor(2, 0); //设定打印颜色为绿字黑底
gtxy(i,0); printf("■"); //打印上边框
gtxy(i,H); printf("■"); //打印下边框
}
for(i=1;i<H;i++)
{ gtxy(0,i); printf("■"); //打印左边框
gtxy(W,i); printf("■"); //打印右边框
}
while(1)
{ srand((unsigned)time(NULL)); //启动随机数发生器srand( )
fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2
fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1
if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)
}
setColor(12, 0); //设定打印颜色为淡红字黑底
gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物
snk.len=3; //蛇身长
snk.speed=350; //刷新蛇的时间,即是移动速度
snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)
snk.y[0]=H/2; //蛇头纵坐标
setColor(9, 0); //设定打印颜色为淡蓝字黑底
gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头
for(i=1;i<snk.len;i++)
{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];
gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身
}
setColor(7, 0); //恢复默认的白字黑底
return;
}
void keymove( ) //按键操作移动蛇
{ int key;
if( kbhit( ) ) //如有按键输入才执行下面操作
{ key=getch( );
if (key==224) //值为224表示按下了方向键,下面要再次获取键值
{ key=getch( );
if(key==72&&dir!=2)dir=1; //72表示按下了向上方向键
if(key==80&&dir!=1)dir=2; //80为向下
if(key==75&&dir!=4)dir=3; //75为向左
if(key==77&&dir!=3)dir=4; //77为向右
}
if (key==32)
{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停
}
if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾
{gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }
int i;
for (i = snk.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)
{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }
switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值
{ case 1: snk.y[0]--; break; //dir=1要向上移动
case 2: snk.y[0]++; break; //dir=2要向下移动
case 3: snk.x[0]-=2; break; //dir=3要向左移动
case 4: snk.x[0]+=2; break; //dir=4要向右移动
}
setColor(9, 0);
gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头
if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物则执行以下操作
{ printf("