/* 贪食蛇游戏
* 作者:金鑫
* QQ:12009412
* MSN:aihenry8848@hotmai.com
* 用鼠标点控制方向!
*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "graphics.h"
#include "extgraph.h"
#include "random.h"
#include "strlib.h"
#define STARTX 1
#define STARTY 0.6
#define DX 0.12
#define DY 0.12
typedef struct
{
int x;
int y;
}zbT;
typedef struct
{
double x;
double y;
}control;
/*初始化窗口并画开始的格子及相关一大堆东西*/
void Start(void);
/*画控制区*/
void DrawButton(void);
/*多搞几个颜色*/
void DefineColors (void);
/*画出蛇的一节*/
void DrawD(zbT zb);
/*擦去蛇的一节*/
void EraseD(zbT zb);
/*判断鼠标点的控制方向*/
char Judge(control click);
void Move(int length,int dx,int dy);
/*把作为墙的格子初始化一下*/
void InitCells(void);
void InitzbT(void);
void RandomD(void);
void Speed(int d);
void ScoreBoard(int length);
int cell[42][42];
double difficulty;
zbT she[30];
zbT hh;
void main (void)
{
int d;
string x;
//zbT hhhh;
/*这里让玩家选择蛇行走快慢*/
printf("这是贪食蛇游戏\n你将通过点击控制区的方位来控制方向\n");
printf("想试试哪种难度?\n1---Easy\n2---Medium\n3---Hard\n");
printf("请输入1或2或3!\n");
d=GetInteger();
while (d>3||d<1)
{
printf("请输入1或2或3!");
d=GetInteger();
}
Speed(d);
InitzbT();
//hhhh.x=1;hhhh.y=1;
Start();
// DrawD(hhhh);
Randomize();
InitCells();
/*给玩家一个准备时间,避免手忙脚乱。*/
do
{
printf("Are you ready!\nif you are, please enter \"y\" as a signal.");
x=GetLine();
}while (IthChar(x,0)!='y');
RandomD();
Move(3,1,0);
}
void Move(int length,int dx,int dy)
{
char got;
control click;
int i;
while ((!MouseButtonIsDown())&&(cell[she[0].x][she[0].y]==0))
{
cell[hh.x][hh.y]=0;
for(i=0;i<length;i++)
{
DrawD(she[i]);
cell[she[i].x][she[i].y]=-1;
}
Pause(difficulty/4);
Pause(difficulty/4);
Pause(difficulty/4);
Pause(difficulty/4);
for(i=0;i<length;i++)
{
EraseD(she[i]);
}
hh=she[length];
for (i=length-1;i>=0;i--)
{
she[i+1]=she[i];
}
she[0].x+=dx;
she[0].y+=dy;
}
click.x=GetMouseX();
click.y=GetMouseY();
WaitForMouseUp();
if (cell[she[0].x][she[0].y]==-1)
{
printf("You lose!");
MovePen(6.3,1);
SetPenColor("Black");
SetPointSize(30);
SetStyle(1);
DrawTextString("You Lose!");
}
else
{
got=Judge(click);
switch (got)
{
case 'a':{
if (dx>0) ;
else
{
dx=-1;
dy=0;
}
}break;
case 's':{
if (dy>0) ;
else
{
dy=-1;
dx=0;
}
}break;
case 'w':{
if (dy<0) ;
else
{
dy=1;
dx=0;
}
}break;
case 'd':{
if (dx<0) ;
else
{
dx=1;
dy=0;
}
}break;
}
if (cell[she[0].x][she[0].y]==1)
{
length+=1;
she[length]=she[length-1];
RandomD();
cell[she[0].x][she[0].y]=0;
ScoreBoard(length);
}
if (length==30)
{
printf("You Win!");
MovePen(6.3,1);
SetPenColor("Red");
SetPointSize(30);
SetStyle(1);
DrawTextString("You Win!");
}
else
Move(length,dx,dy);
}
}
void InitCells(void)
{
int i,j;
for (i=0;i<42;i++)
{
for (j=0;j<42;j++)
{
cell[i][j]=-1;
}
}
for (i=1;i<41;i++)
{
for (j=1;j<41;j++)
{
cell[i][j]=0;
}
}
}
void Speed(int d)
{
if (d==1) difficulty=0.15;
if (d==2) difficulty=0.1;
if (d==3) difficulty=0.05;
}
void InitzbT(void)
{
int i;
for (i=0;i<30;i++)
{
she[i].x=0;
she[i].y=0;
}
she[0].x=5;
she[0].y=5;
she[1].x=4;
she[1].y=5;
she[2].x=3;
she[2].y=5;
}
char Judge(control click)
{
double sx,sy;
sx=GetWindowWidth()-1.05;
sy=GetWindowHeight()-1.05;
if ((click.x+click.y)>(sx+sy))
{
if ((click.x-sx)>(click.y-sy)) return('d');
else return('w');
}
else
{
if ((click.x-sx)>(click.y-sy)) return('s');
else return('a');
}
}
void RandomD(void)
{
int i,j;
zbT zb;
do
{
i=RandomInteger(1,40);
j=RandomInteger(1,40);
}while (cell[i][j]==-1);
cell[i][j]=1;
zb.x=i;
zb.y=j;
DrawD(zb);
}
void Start(void)
{
int i;
SetWindowSize(GetFullScreenWidth() * 0.6,
GetFullScreenHeight() * 0.55);
SetWindowTitle("Jason----aihenry2980@sjtu.edu.cn");
InitGraphics();
DefineColors();
DrawButton();
MovePen(1,5.5);
SetPenColor("Red");
SetPointSize(20);
SetStyle(1);
DrawTextString("当蛇长度达到三十时你就赢了!");
MovePen(6.3,3.5);
SetPenColor("Purple");
DrawTextString("现在的长度是:");
MovePen(7,2.5);
SetPenColor("Red");
SetPointSize(50);
DrawTextString("3");
SetPenColor("Blue");
for (i=0;i<41;i++)
{
MovePen(STARTX+i*DX,STARTY);
DrawLine(0,40*DY);
}
for (i=0;i<41;i++)
{
MovePen(STARTX,STARTY+i*DX);
DrawLine(40*DY,0);
}
SetPenColor("Orange");
SetPointSize(15);
for (i=0;i<40;i++)
{
MovePen(STARTX+i*DX,STARTY-0.2);
if(i%5==0)
DrawTextString(IntegerToString(i+1));
}
for (i=0;i<40;i++)
{
MovePen(STARTX-0.3,STARTY+i*DY);
if(i%5==0)
DrawTextString(IntegerToString(i+1));
}
DrawD(she[0]);
DrawD(she[1]);
DrawD(she[2]);
}
void ScoreBoard(int length)
{
MovePen(7,2.5);
SetPenColor("White");
StartFilledRegion(1);
DrawLine(1,0);
DrawLine(0,0.9);
DrawLine(-1,0);
DrawLine(0,-0.9);
EndFilledRegion();
SetPenColor("Red");
SetPointSize(50);
DrawTextString(IntegerToString(length));
}
void DefineColors (void)
{
DefineColor("Orange", 0.9, 0.5, 0);
DefineColor("Gold", 0.9, 0.8, 0);
DefineColor("Emerald", 0, 0.8, 0);
DefineColor("Teal", 0, 0.8, 0.7);
DefineColor("Aqua", 0, 0.6, 1.0);
DefineColor("Periwinkle", 0.5, 0, 0.7);
DefineColor("Purple", 0.6, 0, 0.7);
DefineColor("Violet", 0.6, 0, 0.6);
DefineColor("HotPink", 1.0, 0, 0.5);
DefineColor("Black", 0.0,0.0,0.0);
}
void DrawD(zbT zb)
{
MovePen(STARTX+(zb.x-1)*DX+0.02,STARTY+(zb.y-1)*DY+0.01);
SetPenColor("Red");
StartFilledRegion(1);
DrawLine(0.09,0);
DrawLine(0,0.09);
DrawLine(-0.09,0);
DrawLine(0,-0.09);
EndFilledRegion();
}
void EraseD(zbT zb)
{
MovePen(STARTX+(zb.x-1)*DX+0.02,STARTY+(zb.y-1)*DY+0.01);
SetPenColor("White");
StartFilledRegion(1);
DrawLine(0.09,0);
DrawLine(0,0.09);
DrawLine(-0.09,0);
DrawLine(0,-0.09);
EndFilledRegion();
}
void DrawButton(void)
{
MovePen(GetWindowWidth()-1.6,GetWindowHeight()-0.4);
SetPenColor("Aqua");
SetStyle(1);
SetPointSize(25);
DrawTextString("控制区");
MovePen(GetWindowWidth()-1.5,GetWindowHeight()-1.5);
DrawLine(0.9,0.9);
MovePen(GetWindowWidth()-0.6,GetWindowHeight()-1.5);
DrawLine(-0.9,0.9);
SetPenColor("Teal");
SetPointSize(13);
SetStyle(1);
MovePen(GetWindowWidth()-1.15,GetWindowHeight()-0.75);
DrawTextString("上");
MovePen(GetWindowWidth()-1.5,GetWindowHeight()-1.15);
DrawTextString("左");
MovePen(GetWindowWidth()-0.8,GetWindowHeight()-1.15);
DrawTextString("右");
MovePen(GetWindowWidth()-1.15,GetWindowHeight()-1.5);
DrawTextString("下");
}