根据我们已经编写完成的input型贪吃蛇程序和智能型贪吃蛇程序,我们可以将两者结合在一起,制造出一款可以同时有玩家操控和程序操控的贪吃蛇小游戏,在一个方框内两条蛇争抢同一个果实。
这样,我们可以给程序加入一些新的内容:
首先,需要打印两条贪吃蛇。
其次,需要有一些限制条件:
IF 两条蛇相撞
游戏结束;
IF 其中一条蛇获得果实{
获得果实的蛇加长;
果实位置随机改变;
智能蛇的目标坐标相应改变;
}
IF 智能蛇即将与人工蛇相撞
智能蛇改变方向;
具体实施由于知识的缺陷部分未完善,如下:
#define ESC 27
#define N 200
static int play = 0;
struct Food
{
int x;
int y;
int eat;
};
struct Snake
{
int x[N];
int y[N];
int node;
int direction;
int life;
};
struct Config
{
Food food;
Snake snake;
char key;
COLORREF colorref;
};
//////snake.cpp
#include "stdafx.h"
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include <windows.h>
//#include <process.h>
#include "snake.h"
int score = 0;
int gamespeed = 150;
void init(void);
void goodbay(void);
//unsigned __stdcall Play(void* pArguments );
void Play2(void );
void PrScore(void);
int _tmain(int argc, _TCHAR* argv[])
{
init();
//unsigned threadID;
//Config config;
//config.colorref = RED;
//HANDLE hThread1 = (HANDLE)_beginthreadex( NULL, 0, &Play, &config, 0, &threadID );
//CloseHandle(hThread1);
Play2();
closegraph();
return 0;
}
void init(void)
{
int gd = 9,gm = 2;
initgraph(&gd,&gm,"");
cleardevice();
setcolor(LIGHTCYAN);
setlinestyle(PS_SOLID,0,1);
for(int i =8;i<500;i+=12)
{
rectangle(i,8,i+9,17);
rectangle(i,452,i+9,461);
}
for(int i=8;i<450;i+=12)
{
rectangle(8,i,17,i+9);
rectangle(502,i,511,i+9);
}
setcolor(RED);
outtextxy(520,60,L"红蛇:");
outtextxy(520,79,L" i(上)");
outtextxy(520,99,L"j(左),l(右)");
outtextxy(520,117,L" k(下)");
setcolor(BLUE);
outtextxy(520,155,L"蓝蛇:");
outtextxy(520,173,L" w(上)");
outtextxy(520,191,L"a(左),d(右)");
outtextxy(520,209,L" s(下)");
}
void Play2(void)
{
int i,j;
Food food ;
Snake snake ,snake2;
char key;
srand(time(NULL));
food.eat = 1;
snake.life = 1;
snake.direction = 1;
snake.x[0] = 90;
snake.x[1] = 100;
snake.y[0] = 90;
snake.y[1] = 90;
snake.node = 2;
snake2 = snake;
snake2.x[0] = 100;
snake2.x[1] = 110;
snake2.y[0] = 100;
snake2.y[1] = 100;
PrScore();
while(1)
{
while( !kbhit())
{
if(play>=2)
{
goodbay();
break;
}
if(food.eat == 1)
{
food.x = rand()@0 + 60;
food.y = rand()50 + 60;
while(food.x != 0)
{
food.x++;
}
while(food.y != 0)
{
food.y++;
}
food.eat = 0;
}
if(food.eat == 0)
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
setfont(12,0,L"宋体");
outtextxy(food.x,food.y-10,L"肉");
}
//////////////////////////////////////////////////////////////////////
if(snake.life )
{
for( i = snake.node -1;i>0;--i)
{
snake.x[i] = snake.x[i-1];
snake.y[i] = snake.y[i-1];
}
switch(snake.direction)
{
case 1:
snake.x[0] += 10;
break;
case 2:
snake.x[0] -= 10;
break;
case 3:
snake.y[0] -= 10;
break;
case 4:
snake.y[0] += 10;
break;
}
}
if(snake2.life )
{
for( j = snake2.node -1;j>0;--j)
{
snake2.x[j] = snake2.x[j-1];
snake2.y[j] = snake2.y[j-1];
}
switch(snake2.direction)
{
case 1:
snake2.x[0] += 10;
break;
case 2:
snake2.x[0] -= 10;
break;
case 3:
snake2.y[0] -= 10;
break;
case 4:
snake2.y[0] += 10;
break;
}
}
if(snake.life)
{
for(i =3;i
{
if((snake.x[i] == snake.x[0]) && (snake.y[i] == snake.y[0]))
{
play++;
snake.life = 0;
break;
}
}
if((snake.x[0] < 17) || (snake.x[0] > 490) || (snake.y[0] < 21) || (snake.y[0] > 452))
{
play++;
snake.life = 0;
}
}
if(snake2.life)
{
for(j =3;j
{
if((snake2.x[j] == snake2.x[0]) && (snake2.y[j] == snake.y[0]))
{
play++;
snake2.life = 0;
break;
}
}
if((snake2.x[0] < 17) || (snake2.x[0] > 490) || (snake2.y[0] < 21) || (snake2.y[0] > 452))
{
play++;
snake2.life = 0;
}
}
if(snake.life)
{
if((snake.x[0] == food.x)&&(snake.y[0] == food.y))
{
setcolor(BLACK);
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node] = -20;
snake.y[snake.node] = -20;//新节点放在看不见的位置
snake.node++;
food.eat = 1;
score += 10;
PrScore();
}
setcolor(RED);
//ellipse(snake.x[0],snake.y[0],snake.x[0]+10,snake.y[0]-10);
setfont(12,0,L"宋体");
outtextxy(snake.x[0],snake.y[0]-10,L"头");
for(i = 1;i
outtextxy(snake.x[i],snake.y[i]-10,L"身");
}
if(snake2.life)
{
if((snake2.x[0] == food.x)&&(snake2.y[0] == food.y))
{
setcolor(BLACK);
rectangle(food.x,food.y,food.x+10,food.y-10);
snake2.x[snake2.node] = -20;
snake2.y[snake2.node] = -20;//新节点放在看不见的位置
snake2.node++;
food.eat = 1;
score += 10;
PrScore();
}
setcolor(BLUE);
//ellipse(snake.x[0],snake.y[0],snake.x[0]+10,snake.y[0]-10);
setfont(12,0,L"宋体");
outtextxy(snake2.x[0],snake2.y[0]-10,L"头");
for(j = 1;j
outtextxy(snake2.x[j],snake2.y[j]-10,L"身");
//rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
//rectangle(snake.x[0],snake.y[0],snake.x[0]+10,snake.y[0]-10);
}
Sleep(gamespeed);
setcolor(BLACK);
//rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
outtextxy(snake.x[snake.node-1],snake.y[snake.node-1]-10,L" ");
outtextxy(snake2.x[snake2.node-1],snake2.y[snake2.node-1]-10,L" ");
}
if(play>=2)
{
goodbay();
break;
}
key = getch();
if(key == ESC)break;
switch(key)
{
case ' ':
{
while( key = getch())
{
//if(key == ESC )
//play++;
if( key = ' ')
break;
}
}
break;
if(snake.life)
{
case 'i':
if(snake.direction != 4)
snake.direction = 3;
break;
case 'l':
if(snake.direction != 2)
snake.direction = 1;
break;
case 'j':
if(snake.direction != 1)
snake.direction = 2;
break;
case 'k':
if(snake.direction != 3)
snake.direction = 4;
break;
}
//////////////////////////////////////////////////////////
if(snake2.life)
{
case 'w':
if(snake2.direction != 4)
snake2.direction = 3;
break;
case 'd':
if(snake.direction != 2)
snake2.direction = 1;
break;
case 'a':
if(snake2.direction != 1)
snake2.direction = 2;
break;
case 's':
if(snake2.direction != 3)
snake2.direction = 4;
break;
}
}
}
}
void goodbay(void)
{
cleardevice();
PrScore();
setcolor(RED);
setfont(56,0,L"黑体");
outtextxy(200,200,L"GAME OVER");
outtextxy(100,260,L"敲任意键两下退出");
getch();
}
void PrScore(void)
{
wchar_t str[20];
setfillstyle(YELLOW);
bar(520,10,620,50);
setcolor(RED);
setfont(16,0,L"宋体");
wsprintf(str,L"分数:%d",score);
outtextxy(528,16,str);
}