贪吃蛇游戏源代码
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <bios.h>
#include <dos.h>
#include<string.h>
#define LEFT_MARGIN 140
#define FRAME_UNIT_WIDTH 25
#define FRAME_UNIT_HEIGHT 15
#define BOX_SIZE 15
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define SNAK_DEFAULT_POS_X 10
#define SNAK_DEFAULT_POS_Y 5
#define SMD_UP 1
#define SMD_DOWN 2
#define SMD_RIGHT 3
#define SMD_LEFT 4
#define UP_MOVE 72
#define DOWN_MOVE 80
#define LEFT_MOVE 75
#define RIGHT_MOVE 77
#define STEP_MOVE 7
#define GAME_STOP 57
#define ESC_OUT 1
#define GENERAT_EGG 8
#define NO_EVENT 0
#define Boolean int
#define TRUTH 1
#define FALS 0
#define Type_GAMESTATUS int
#define ON_PALYING 2
#define WINNER 1
#define GAMEOVER 0
#define TIMEINTERVAL 6
typedef struct
{
int p_X;
int p_Y;
}POS_in_Fram;
typedef struct SNode
{
POS_in_Fram cur_pos;
int color;
struct SNode *next;
}SNode, *LSNode;
typedef struct
{
int direction;
int len;
POS_in_Fram Head_pre_pos;
POS_in_Fram Head_cur_pos;
int Head_color;
LSNode next;
Boolean Dead;
}SNAK;
typedef struct
{
POS_in_Fram egg_pos;
int egg_color;
}EggType;
SNAK obj_snake;
EggType obj_egg;
int Message_Event;
int ScoreTotal;
int SpeedLevel;
Type_GAMESTATUS GameStat;
void set_Message_Event(int T)
{
Message_Event = T;
}
int get_Message_Event()
{
return(Message_Event);
}
void clear_ScoreTotal()
{
ScoreTotal = 0;
}
int get_ScoreTotal()
{
return(ScoreTotal);
}
void draw_ScoreTotal()
{
char text_buffer[20];
int ul_X, ul_Y;
ul_X = LEFT_MARGIN;
ul_Y = WINDOW_HEIGHT - BOX_SIZE * (FRAME_UNIT_HEIGHT - 4);
setcolor(BLUE);
sprintf(text_buffer, "Score Total: %d", get_ScoreTotal());
moveto(ul_X, ul_Y);
outtext(text_buffer);
setcolor(YELLOW);
ScoreTotal++;
sprintf(text_buffer, "Score Total: %d", get_ScoreTotal());
moveto(ul_X, ul_Y);
outtext(text_buffer);
}
void init_SpeedLevel()
{
SpeedLevel = 1;
}
int get_SpeedLevel()
{
return(SpeedLevel);
贪吃蛇的编程代码}
void draw_SpeedLevel(int old_l, int new_l)
{
char text_buffer[20];
int ul_X, ul_Y;
ul_X = LEFT_MARGIN;
ul_Y = WINDOW_HEIGHT - BOX_SIZE * (FRAME_UNIT_HEIGHT - 4);
setcolor(BLUE);
sprintf(text_buffer, "Speed Level: %d", old_l);
moveto(ul_X, ul_Y + textheight("H") * 2);
outtext(text_buffer);
setcolor(YELLOW);
sprintf(text_buffer, "Speed Level: %d", new_l);
moveto(ul_X, ul_Y + textheight("H") * 2);
outtext(text_buffer);
}
void setSpeedLevel(int score)
{
if(score >= 0 && score < 20)
{
draw_SpeedLevel(get_SpeedLevel(), 1);
SpeedLevel = 1;
}
else if(score >= 20 && score < 50)
{
draw_SpeedLevel(get_SpeedLevel(), 2);
SpeedLevel = 2;
}
else if(score >= 50 && score < 100)
{
draw_SpeedLevel(get_SpeedLevel(), 3);
SpeedLevel = 3;
}
else
{
draw_SpeedLevel(get_SpeedLevel(), 4);
SpeedLevel = 4;
}
}
void set_GameStat(Type_GAMESTATUS state)
{
GameStat = state;
}
Type_GAMESTATUS get_GameStat()
{
return(GameStat);
}
void set_SnakeDead(Boolean T)
{
obj_snake.Dead = T;
}
Boolean Is_SnakeDead()
{
return(obj_snake.Dead);
}
int get_SHOD(int T)
{
switch(T)
{
case SMD_UP:
return(SMD_DOWN);
case SMD_DOWN:
return(SMD_UP);
case SMD_RIGHT:
return(SMD_LEFT);
case SMD_LEFT:
return(SMD_RIGHT);
}
}
int get_SHD()
{
return(obj_snake.direction);
}
Boolean findPointInSnake(int pX, int pY)
{
LSNode p;
if((obj_snake.Head_cur_pos.p_X == pX) && (obj_snake.Head_cur_pos.p_Y == pY))
return(TRUTH);
for(p = ;p != NULL;p = p->next)
{
if((p->cur_pos.p_X == pX) && (p->cur_pos.p_Y == pY))
return(TRUTH);
}
return(FALS);
}
Boolean Is_SnakEating()
{
if((obj_snake.Head_pre_pos.p_X == _pos.p_X) && (obj_snake.Head_pre_pos.p_Y == _pos.p_Y))
return(TRUTH);
else
return(FALS);
}
void Init_obj_snake()
{
obj_snake.direction = SMD_RIGHT;
obj_snake.len = 1;
obj_snake.Head_cur_pos.p_X = SNAK_DEFAULT_POS_X;
obj_snake.Head_cur_pos.p_Y = SNAK_DEFAULT_POS_Y;
obj_snake.Head_pre_pos = obj_snake.Head_cur_pos;
obj_snake.Head_color = RED;
= NULL;
obj_snake.Dead = FALS;
}
void Init_obj_egg()
{
_pos.p_X = 0;
_pos.p_Y = 0;
_color = BLACK;
}
void RegistryGraphicMode()
{
int Driver, Mode;
detectgraph(&Driver, &Mode);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论