| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2117 人关注过本帖
标题:[原创](好歹算个)贪吃蛇游戏[ c 彩色版]
只看楼主 加入收藏
I喜欢c
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:64
帖 子:1749
专家分:0
注 册:2007-3-2
收藏
得分:0 

 我是指针,却丢失了目标地址!          我是循环,却缺少了结束条件!      我是函数,却没有人来调用!   
2007-03-09 16:13
ccwan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-3-9
收藏
得分:0 

c

程序代码:
五彩贪吃蛇 C源代码  原创  作者:Marlio Sky
Hello!

好久没来C语言之家了, 由于最近工作太忙了, 我自己的空余时间太少, 个人的PROJECT就没法进行下去, 这个代码是我在最近一个星期内才完成的.

首先我个人先介绍一下, 我是一个PC机游戏的开发爱好者. 我的这个游戏代码其实是应该用VC++6.0 加 Direct X 9.0 来完成的, 但是, 由于VC太复杂, DIRECT也有点难, 我个人正在学习中, 我个人知道只有这样才能迎合MICRO SOFT的PC开发平台.

Turbo C 2.0 的开发平台虽然说有些过时, 但是对于锻炼一些'C的技巧'和'项目的思路'还是很有帮助的! 在这个代码中, 我主要引用了面向对象的思路, 成员数据, 成员方法, 事件 ... 好了, 看一下代码吧!

/*
The Game: Greedy Snake which be developed by Marlio in Turbo C 2.0

Copyright (c) Feb 25th, 2006 -- ... Wind_Dancing Studio All copy rights reserved.
*/

/*
GLO_GS.h info:

1. constants defined
2. data struct
3. global variable
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <bios.h>
#include <dos.h>

/* size and position of vessal(pixel) */
#define LEFT_MARGIN 140
#define FRAME_UNIT_WIDTH 25
#define FRAME_UNIT_HEIGHT 15
#define BOX_SIZE 15

/* the display mode (640*480) */
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

/*
When the game starts, the default position which the
snake will appear in the frame.
*/
#define SNAK_DEFAULT_POS_X 10
#define SNAK_DEFAULT_POS_Y 5

/* direction of the snake move towards to */
#define SMD_UP 1
#define SMD_DOWN 2
#define SMD_RIGHT 3
#define SMD_LEFT 4

/* the event of keyboard(ASCI code) */
#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

/* data type defined (Bool) */
#define Boolean int
#define TRUTH 1
#define FALS 0

/* data type defined (Game Status) */
#define Type_GAMESTATUS int
#define ON_PALYING 2
#define WINNER 1
#define GAMEOVER 0

/* clock interval */
#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;

/*
The head of snake: It is only head node in the link list.
There are some body nodes at the tail of head.
*/
SNAK obj_snake;

/*
the egg which the snake will eat in the frame.
*/
EggType obj_egg;

int Message_Event;

int ScoreTotal;

int SpeedLevel;

/* the flag label: sign the game status */
Type_GAMESTATUS GameStat;

/*
GS_ALG.c info:

In this file, basic method of data object, the function which used to initialize some data struct,
and graphic drawing interface defined in some method will be listed here.
*/

/* Message_Event.Method: 1 */
void set_Message_Event(int T)
{
Message_Event = T;
}
/* Message_Event.Method: 2 */
int get_Message_Event()
{
return(Message_Event);
}

/* ScoreTotal.Method: 1 */
void clear_ScoreTotal()
{
ScoreTotal = 0;
}
/* ScoreTotal.Method: 2 */
int get_ScoreTotal()
{
return(ScoreTotal);
}
/* ScoreTotal.Method: 3 */
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);
/* erase the previous score in window */
setcolor(BLUE);
sprintf(text_buffer, \"Score Total: %d\", get_ScoreTotal());
moveto(ul_X, ul_Y);
outtext(text_buffer);

/* draw the new score in window */
setcolor(YELLOW);
ScoreTotal++;
sprintf(text_buffer, \"Score Total: %d\", get_ScoreTotal());
moveto(ul_X, ul_Y);
outtext(text_buffer);
}

/* SpeedLevel.Method: 1 */
void init_SpeedLevel()
{
SpeedLevel = 1;
}
/* SpeedLevel.Method: 2 */
int get_SpeedLevel()
{
return(SpeedLevel);
}
/* SpeedLevel.Method: 3 */
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);
/* erase the old speed level */
setcolor(BLUE);
sprintf(text_buffer, \"Speed Level: %d\", old_l);
moveto(ul_X, ul_Y + textheight(\"H\") * 2);
outtext(text_buffer);
/* draw the new speed level */
setcolor(YELLOW);
sprintf(text_buffer, \"Speed Level: %d\", new_l);
moveto(ul_X, ul_Y + textheight(\"H\") * 2);
outtext(text_buffer);
}
/* SpeedLevel.Method: 4 */
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;
}
}

/* GameStat.Method: 1 */
void set_GameStat(Type_GAMESTATUS state)
{
GameStat = state;
}
/* GameStat.Method: 2 */
Type_GAMESTATUS get_GameStat()
{
return(GameStat);
}

/* obj_snake.Dead.Method: 1 */
void set_SnakeDead(Boolean T)
{
obj_snake.Dead = T;
}
/* obj_snake.Dead.Method: 2 */
Boolean Is_SnakeDead()
{
return(obj_snake.Dead);
}

/* get the opposit direction of the snake head */
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);
}
}

/* get the current direction of the snake head */
int get_SHD()
{
return(obj_snake.direction);
}

/* To find certian node in the snake link list */
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 = obj_snake.next;p != NULL;p = p->next)
{
if((p->cur_pos.p_X == pX) && (p->cur_pos.p_Y == pY))
return(TRUTH);
}

return(FALS);
}

/* judge that whether the snake is eating the egg */
Boolean Is_SnakEating()
{
if((obj_snake.Head_pre_pos.p_X == obj_egg.egg_pos.p_X) && (obj_snake.Head_pre_pos.p_Y == obj_egg.egg_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;

obj_snake.next = NULL;

obj_snake.Dead = FALS;
}

void Init_obj_egg()
{
obj_egg.egg_pos.p_X = 0;
obj_egg.egg_pos.p_Y = 0;

obj_egg.egg_color = BLACK;
}

void RegistryGraphicMode()
{
int Driver, Mode;

/* display mode(640*480) */
detectgraph(&Driver, &Mode);
registerbgidriver(EGAVGA_driver);
initgraph(&Driver, &Mode, \"c:\\turboc2\");
}

void Draw_FrameInWindow(int f_ul_X, int f_ul_Y, int f_dr_X, int f_dr_Y, int W, int H)
{
int i, x, y;

rectangle(f_ul_X - 1, f_ul_Y - 1, f_dr_X + 1, f_dr_Y + 1);

setfillstyle(SOLID_FILL, BLACK);
bar(f_ul_X, f_ul_Y, f_dr_X, f_dr_Y);

setcolor(DARKGRAY);

for(i = 1;i < W;i++)
{
x = f_ul_X + i * BOX_SIZE;
line(x, f_ul_Y, x, f_dr_Y);
}

for(i = 1;i < H;i++)
{
y = f_ul_Y + i * BOX_SIZE;
line(f_ul_X, y, f_dr_X, y);
}
}

void Draw_mainGraphWindow()
{
int ul_X, ul_Y, dr_X, dr_Y;
char text_buffer[33];

setfillstyle(SOLID_FILL, BLUE);
bar(0, 0, WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1);

ul_X = LEFT_MARGIN;
ul_Y = WINDOW_HEIGHT - BOX_SIZE * (FRAME_UNIT_HEIGHT * 2 - 3);
dr_X = LEFT_MARGIN + BOX_SIZE * FRAME_UNIT_WIDTH;
dr_Y = WINDOW_HEIGHT - BOX_SIZE * (FRAME_UNIT_HEIGHT - 3);

Draw_FrameInWindow(ul_X, ul_Y, dr_X, dr_Y, FRAME_UNIT_WIDTH, FRAME_UNIT_HEIGHT);

ul_X = LEFT_MARGIN;
ul_Y = WINDOW_HEIGHT - BOX_SIZE * (FRAME_UNIT_HEIGHT - 4);

settextjustify(LEFT_TEXT, TOP_TEXT);

settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);

setcolor(YELLOW);

sprintf(text_buffer, \"Score Total: %d\", get_ScoreTotal());
moveto(ul_X, ul_Y);
outtext(text_buffer);

sprintf(text_buffer, \"Speed Level: %d\", get_SpeedLevel());
moveto(ul_X, gety() + textheight(\"H\") * 2);
outtext(text_buffer);

strcpy(text_buffer, \"EXIT: ESC Button\");
moveto(ul_X, gety() + textheight(\"H\") * 2);
outtext(text_buffer);

strcpy(text_buffer, \"Created By: Marlio Sky\");
moveto(ul_X, gety() + textheight(\"H\") * 2);
outtext(text_buffer);

strcpy(text_buffer, \"Studio: Wind Dancing\");
moveto(ul_X, gety() + textheight(\"H\") * 2);
outtext(text_buffer);

strcpy(text_buffer, \"UP: Up Arrow\");
moveto(ul_X + textwidth(\"H\") * 30, ul_Y);
outtext(text_buffer);

strcpy(text_buffer, \"DOWN: Down Arrow\");
moveto(ul_X + textwidth(\"H\") * 30, gety() + textheight(\"H\") * 2);
outtext(text_buffer);

strcpy(text_buffer, \"LEFT: Left Arrow\");
moveto(ul_X + textwidth(\"H\") * 30, gety() + textheight(\"H\") * 2);
outtext(text_buffer);

strcpy(text_buffer, \"RIGHT: Right Arrow\");
moveto(ul_X + textwidth(\"H\") * 30, gety() + textheight(\"H\") * 2);
outtext(text_buffer);

strcpy(text_buffer, \"STOP: Space Button\");
moveto(ul_X + textwidth(\"H\") * 30, gety() + textheight(\"H\") * 2);
outtext(text_buffer);
}

/*
draw the egg in the Frame

Boolean T: -- TRUTH draw the egg with its own color in Frame
-- FALS draw the egg with BLACK color in Frame(erase snake)
*/
void draw_egg(Boolean T)
{
int ul_X, ul_Y;
int color;

ul_X = LEFT_MARGIN;
ul_Y = WINDOW_HEIGHT - BOX_SIZE * (FRAME_UNIT_HEIGHT * 2 - 3);

if(T == FALS)
color = BLACK;
else
color = obj_egg.egg_color;

setfillstyle(SOLID_FILL, color);

bar(ul_X + BOX_SIZE * obj_egg.egg_pos.p_Y + 1, ul_Y + BOX_SIZE * obj_egg.egg_pos.p_X + 1, ul_X + BOX_SIZE * (obj_egg.egg_pos.p_Y + 1) - 1, ul_Y + BOX_SIZE * (obj_egg.egg_pos.p_X + 1) - 1);
}

/*
refresh the runtime environment

Boolean T: -- TRUTH draw the snake node with their own color in Frame
-- FALS draw the snake node with BLACK color in Frame(erase snake)
*/
void Mapped_Draw(Boolean T)
{
int ul_X, ul_Y;
int Ax, Ay, Bx, By;
int color;
LSNode p;

ul_X = LEFT_MARGIN;
ul_Y = WINDOW_HEIGHT - BOX_SIZE * (FRAME_UNIT_HEIGHT * 2 - 3);

if(T == FALS)
color = BLACK;
else
color = obj_snake.Head_color;

setfillstyle(SOLID_FILL, color);

Ax = ul_X + BOX_SIZE * obj_snake.Head_cur_pos.p_Y + 1;
Ay = ul_Y + BOX_SIZE * obj_snake.Head_cur_pos.p_X + 1;
Bx = ul_X + BOX_SIZE * (obj_snake.Head_cur_pos.p_Y + 1) - 1;
By = ul_Y + BOX_SIZE * (obj_snake.Head_cur_pos.p_X + 1) - 1;

bar(Ax, Ay, Bx, By);

if(T == TRUTH)
{
setfillstyle(SOLID_FILL, YELLOW);
bar(Ax + 4, Ay + 4, Ax + 4 + 5, Ay + 4 + 5);
}

for(p = obj_snake.next;p != NULL;p = p->next)
{
if(T == FALS)
color = BLACK;
else
color = p->color;

setfillstyle(SOLID_FILL, color);

Ax = ul_X + BOX_SIZE * p->cur_pos.p_Y + 1;
Ay = ul_Y + BOX_SIZE * p->cur_pos.p_X + 1;
Bx = ul_X + BOX_SIZE * (p->cur_pos.p_Y + 1) - 1;
By = ul_Y + BOX_SIZE * (p->cur_pos.p_X + 1) - 1;

bar(Ax, Ay, Bx, By);
}
}

/* draw 'game start' or 'game over' surface */
void draw_GS_GO_Surface(Type_GAMESTATUS T)
{
char text_buffer[40];

settextjustify(LEFT_TEXT, TOP_TEXT);

switch(T)
{
case ON_PALYING:

settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);
setcolor(LIGHTCYAN);

strcpy(text_buffer, \"ITEM - 2: Game.Greed Snake\");
moveto(100, 100);
outtext(text_buffer);

settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 1);
setcolor(YELLOW);

strcpy(text_buffer, \"Please press any key to continue ...\");
moveto(150, 300);
outtext(text_buffer);
break;
case GAMEOVER:

settextstyle(GOTHIC_FONT, HORIZ_DIR, 8);
setcolor(RED);

strcpy(text_buffer, \"GAME OVER\");
moveto(70, 80);
outtext(text_buffer);

settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 2);
setcolor(YELLOW);

strcpy(text_buffer, \"Sorry!\");
moveto(160, 320);
outtext(text_buffer);

sprintf(text_buffer, \"Total Score: %d\", get_ScoreTotal());
moveto(240, 320);
outtext(text_buffer);

sprintf(text_buffer, \"Speed Level: %d\", get_SpeedLevel());
moveto(240, 350);
outtext(text_buffer);
break;
case WINNER:

settextstyle(GOTHIC_FONT, HORIZ_DIR, 8);
setcolor(RED);

strcpy(text_buffer, \"YOU WIN !\");
moveto(80, 80);
outtext(text_buffer);

settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 2);
setcolor(YELLOW);

strcpy(text_buffer, \"Congratulation!\");
moveto(120, 320);
outtext(text_buffer);

sprintf(text_buffer, \"Total Score: %d\", get_ScoreTotal());
moveto(280, 320);
outtext(text_buffer);

sprintf(text_buffer, \"Speed Level: %d\", get_SpeedLevel());
moveto(280, 350);
outtext(text_buffer);
break;
default:
break;
}
}

/*
GS_ALG1.c info:
In this file, some function and module is main caculation for Russion Block.
The dispatcher will directly invoke the action procedure.
*/

/* According to incoming parameter, set the predictly position of snake head. */
void set_SH_Pre_Pos(int T)
{
int H_pX, H_pY;

H_pX = obj_snake.Head_cur_pos.p_X;
H_pY = obj_snake.Head_cur_pos.p_Y;

switch(T)
{
case SMD_UP:

if((H_pX - 1) < 0 || findPointInSnake(H_pX - 1, H_pY) == TRUTH)
{
set_SnakeDead(TRUTH);
return;
}

obj_snake.Head_pre_pos.p_X = H_pX - 1;
obj_snake.Head_pre_pos.p_Y = H_pY;

break;
case SMD_DOWN:

if((H_pX + 1) >= FRAME_UNIT_HEIGHT || findPointInSnake(H_pX + 1, H_pY) == TRUTH)
{
set_SnakeDead(TRUTH);
return;
}

obj_snake.Head_pre_pos.p_X = H_pX + 1;
obj_snake.Head_pre_pos.p_Y = H_pY;

break;
case SMD_RIGHT:

if((H_pY + 1) >= FRAME_UNIT_WIDTH || findPointInSnake(H_pX, H_pY + 1) == TRUTH)
{
set_SnakeDead(TRUTH);
return;
}

obj_snake.Head_pre_pos.p_X = H_pX;
obj_snake.Head_pre_pos.p_Y = H_pY + 1;

break;
case SMD_LEFT:

if((H_pY - 1) < 0 || findPointInSnake(H_pX, H_pY - 1) == TRUTH)
{
set_SnakeDead(TRUTH);
return;
}

obj_snake.Head_pre_pos.p_X = H_pX;
obj_snake.Head_pre_pos.p_Y = H_pY - 1;

break;
}
}

/* set the direction of snake head */
Boolean set_SHD(int T)
{
if(get_SHD() == T || get_SHOD(get_SHD()) == T)
return(FALS);

obj_snake.direction = T;

return(TRUTH);
}

/*
the main processing algorithem flow:

+--------+ +------------+ +-----------+ +-----------+
| | | | | | | |
+-------+-------+----+ +-------+----+ +-------+----+ +-------+----+
|pre_pos|cur_pos|next---->|cur_pos|next---->|cur_pos|next---->|cur_pos|next---->
+-------+-------+----+ +-------+----+ +-------+----+ +-------+----+
snake head snake body 1 snake body 2 snake body3
*/
void process_SnakeNode(Boolean eated)
{
int i;
POS_in_Fram temp1_cur_pos, temp2_cur_pos;
LSNode p, q;

temp1_cur_pos = obj_snake.Head_cur_pos;

obj_snake.Head_cur_pos = obj_snake.Head_pre_pos;

p = obj_snake.next;

for(i = 1;i < obj_snake.len;i++)
{
temp2_cur_pos = p->cur_pos;
p->cur_pos = temp1_cur_pos;
temp1_cur_pos = temp2_cur_pos;
p = p->next;
}

if(eated == TRUTH)
{
q = (LSNode)malloc(sizeof(SNode));
q->color = obj_egg.egg_color;
q->cur_pos = temp1_cur_pos;

if(obj_snake.len == 1)
{
q->next = obj_snake.next;
obj_snake.next = q;

obj_snake.len++;

return;
}

for(p = obj_snake.next;p->next != NULL;p = p->next);

q->next = p->next;
p->next = q;

obj_snake.len++;
}
}

/* When an egg is eated, generating an new egg */
void generate_Egg()
{
int temp_pX, temp_pY, temp_color;

draw_egg(FALS);

temp_pX = random(FRAME_UNIT_HEIGHT);
temp_pY = random(FRAME_UNIT_WIDTH);

while(findPointInSnake(temp_pX, temp_pY) == TRUTH)
{
temp_pX = random(FRAME_UNIT_HEIGHT);
temp_pY = random(FRAME_UNIT_WIDTH);
}

obj_egg.egg_pos.p_X = temp_pX;
obj_egg.egg_pos.p_Y = temp_pY;

for(temp_color = random(16);temp_color == BLACK;temp_color = random(16));

obj_egg.egg_color = temp_color;

draw_egg(TRUTH);

set_Message_Event(NO_EVENT);
}

/* These movement includes Up, Down, Left, Right and Step move. */
void move_To(int T, Boolean step)
{
if(step == FALS)
{
if(set_SHD(T) == FALS)
goto Skip_1;
}

set_SH_Pre_Pos(T);
if(Is_SnakeDead() == TRUTH)
goto Skip_2;

if(Is_SnakEating() == TRUTH)
{
Mapped_Draw(FALS);

process_SnakeNode(TRUTH);

Mapped_Draw(TRUTH);

draw_ScoreTotal();

setSpeedLevel(get_ScoreTotal());

goto Skip_3;
}
else
{
Mapped_Draw(FALS);

process_SnakeNode(FALS);

Mapped_Draw(TRUTH);

goto Skip_1;
}

Skip_1:
set_Message_Event(NO_EVENT);
return;

Skip_2:
set_Message_Event(ESC_OUT);
return;

Skip_3:
set_Message_Event(GENERAT_EGG);
return;
}

void pause_game()
{
while((bioskey(0) >> 8) != GAME_STOP);

set_Message_Event(NO_EVENT);
}

void process_GameStat(Type_GAMESTATUS T)
{
LSNode p, q;

switch(T)
{
case ON_PALYING:

draw_GS_GO_Surface(ON_PALYING);
break;
case WINNER:

cleardevice();
draw_GS_GO_Surface(WINNER);
getch();
break;
case GAMEOVER:

cleardevice();
draw_GS_GO_Surface(GAMEOVER);
getch();
break;
default:
break;
}

/* delete the snake, free the node */
p = obj_snake.next;
while(p != NULL)
{
q = p->next;
free(p);
p = q;
}
}

/*
GS_ALG2.c info:
In this file, \"the kernal of game\".
Main function:
the clock time dispatched, getting message, dispatching the event
*/

Boolean IsWinner()
{
if(get_SpeedLevel() == 4)
return(TRUTH);
else
return(FALS);
}

Boolean IsGameOver()
{
if(get_Message_Event() == ESC_OUT)
return(TRUTH);
else
return(FALS);
}

Boolean IsTimeOut()
{
static long temp, sourc;

temp = biostime(0, sourc);

if((temp - sourc) < (TIMEINTERVAL - SpeedLevel))
return FALS;
else
{
sourc = temp;
return TRUTH;
}
}

void getMessageEvent()
{
if(get_Message_Event() == GENERAT_EGG || get_Message_Event() == ESC_OUT || get_Message_Event() == GAME_STOP)
return;

if(IsTimeOut() == TRUTH)
{
set_Message_Event(STEP_MOVE);
return;
}

if(bioskey(1))
{
set_Message_Event(bioskey(0) >> 8);
return;
}
}

void dispatchMessage_Event()
{
switch(get_Message_Event())
{
case UP_MOVE:
move_To(SMD_UP, FALS);
break;
case DOWN_MOVE:
move_To(SMD_DOWN, FALS);
break;
case LEFT_MOVE:
move_To(SMD_LEFT, FALS);
break;
case RIGHT_MOVE:
move_To(SMD_RIGHT, FALS);
break;
case STEP_MOVE:
move_To(get_SHD(), TRUTH);
break;
case GENERAT_EGG:
generate_Egg();
break;
case GAME_STOP:
pause_game();
break;
case ESC_OUT:
break;
default:
set_Message_Event(NO_EVENT);
}
}

void main()
{
RegistryGraphicMode();

set_GameStat(ON_PALYING);

process_GameStat(get_GameStat());

clear_ScoreTotal();

Init_obj_snake();

Init_obj_egg();

init_SpeedLevel();

randomize();

set_Message_Event(GENERAT_EGG);

getch();

cleardevice();

Draw_mainGraphWindow();

do
{
getMessageEvent();
dispatchMessage_Event();

if(IsWinner() == TRUTH)
{
set_GameStat(WINNER);
break;
}

if(IsGameOver() == TRUTH)
{
set_GameStat(GAMEOVER);
break;
}
}
while(1);

process_GameStat(get_GameStat());

closegraph();
}

[此贴子已经被作者于2007-3-9 17:50:04编辑过]

2007-03-09 17:48
wlly
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-3-9
收藏
得分:0 
程序编的很不错啊~复制回去仔细研究下~
2007-03-09 18:18
I喜欢c
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:64
帖 子:1749
专家分:0
注 册:2007-3-2
收藏
得分:0 

支持下12楼....
图形的~~还没学到那一步!


 我是指针,却丢失了目标地址!          我是循环,却缺少了结束条件!      我是函数,却没有人来调用!   
2007-03-09 21:29
nbhao
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-3-18
收藏
得分:0 
厉害,我是菜鸟
2007-03-18 13:43
neverTheSame
Rank: 3Rank: 3
来 自:江西农业大学
等 级:新手上路
威 望:9
帖 子:1511
专家分:0
注 册:2006-11-24
收藏
得分:0 
不错,还有发展的空间.

wap酷禾网(http://wap.),提供免费的、优质的、快捷的wap资源下载服务。
2007-03-18 14:32
mayudong1
Rank: 2
等 级:论坛游民
威 望:1
帖 子:348
专家分:21
注 册:2006-4-18
收藏
得分:0 

/*我也写了个,大家看看.*/

#define BARW 15 /*设置小方格的宽度*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920
#define W_UP 1/*运动方向*/
#define W_DOWN 2
#define W_LEFT 3
#define W_RIGHT 4
#define SPEED 50000/*设置游戏速度*/
#define BACKCOLOR 0

#include<stdio.h>
#include<graphics.h>
#include<dos.h>

typedef struct
{
int x;
int y;
int state;
}node;/*单元格位置坐标的声明*/

typedef struct
{
int x;
int y;
}node1;/*蛇身节点相对坐标的声明*/

typedef struct
{
int x;
int y;
int state; /*1表示有食物,0表示没有*/
}node2;

node map[28][23];/*蛇的节点可以出现的位置的坐标,x,y表示实际坐标*/
node1 snake[200];/*蛇体的相对坐标(将游戏空间看作23*28的网格,其中的x,y分别表示这个网格的横纵坐标,以左上为<0,0>)*/
node2 food;

int way;/*设置运动方向*/
int length;/*蛇的长度*/
int key;/*接受键盘输入*/
int life;/*表明蛇是否活着*/

void copyright()/*版权信息*/
{
setcolor(2);
settextjustify(0,0);
outtextxy((BARW+1)*25,(BARW+1)*30,"Copyright (C) 2007 mayudong1");
}


void init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void init_data()
{
int i,j;
for(i=0;i<28;i++)
for(j=0;j<23;j++)
{
map[i][j].x=(j+1)*(BARW+1);
map[i][j].y=(i+1)*(BARW+1);
map[i][j].state=0;
}
map[0][0].state=1;
map[0][1].state=1;
map[0][2].state=1;
for(i=3;i<200;i++)
{
snake[i].x=-1;
snake[i].y=-1;
}
snake[0].x=2;
snake[0].y=0;
snake[1].x=1;
snake[1].y=0;
snake[2].x=0;
snake[2].y=0;
length=3;
life=1;
way=W_RIGHT;
}
void draw_bar(int x,int y,int fillcolor)/*画最小方格*/
{
setfillstyle(1,fillcolor);
bar(x,y,BARW+x,BARW+y);
setcolor(7);
line(x,y,x+BARW,y);
line(x,y,x,y+BARW);
setcolor(1);
line(x+BARW,y+BARW,x+BARW,y);
line(x+BARW,y+BARW,x,y+BARW);
}

void clear_bar(int x,int y)/*清除方格*/
{
setfillstyle(1,BACKCOLOR);
bar(x,y,BARW+x,BARW+y);
}

void draw_background()/*背景和边框*/
{
int x,y;
setbkcolor(BACKCOLOR);
y=0;
for(x=0;x<(BARW+1)*25;x+=BARW+1)
{
draw_bar(x,y,8);
}
x=0;
for(y=0;y<(BARW+1)*30;y+=BARW+1)
{
draw_bar(x,y,8);
}
x=(BARW+1)*24;
for(y=0;y<(BARW+1)*30;y+=BARW+1)
{
draw_bar(x,y,8);
}
y=(BARW+1)*29;
for(x=0;x<(BARW+1)*25;x+=BARW+1)
{
draw_bar(x,y,8);
}
}

void draw_score(char oldresult[],char result[])/*输出得分*/
{
setcolor(BACKCOLOR);
settextjustify(0,0);
settextstyle(3,0,3);
outtextxy(450,120,oldresult);
setcolor(3);
outtextxy(450,120,result);
}

/*void gameover()
{
cleardevice();
setbkcolor(4);
setcolor(10);
settextjustify(1,0);
settextstyle(3,0,8);
outtextxy(640/2,480/2,"game over!!!");
}*/
void gameover()
{
int size;
int i;
void *buf;
size=imagesize(240-40,430,400-40,480);
buf=malloc(size);
if(!buf) return;
getimage(240-90,430,400-90,480,buf);
setcolor(4);
settextstyle(1,0,4);
settextjustify(0,2);
outtextxy(240-90,430,"gameover");
for(i=0;i<200;i+=5)
{
putimage(240-90,430-i,buf,COPY_PUT);
getimage(240-90,430-i-5,400-90,480-i-5,buf);
outtextxy(240-90,430-i-5,"gameover");
delay(10000);
}
free(buf);

}

void getfood()
{
int x,y;
int can;
int i;
srand((unsigned)time(NULL));
do
{
can=0;
food.x=rand()%23;
food.y=rand()%28;
if(map[food.y][food.x].state==1)
{
can=1;
/* printf("error");*/
}
}while(can);
draw_bar(map[food.y][food.x].x,map[food.y][food.x].y,4);
food.state=1;
}

void gameplay()
{
int i;
int score=0;
char result[10],oldresult[10]="score:";
draw_score(" ","score:0");
strcpy(oldresult,"score:0");
/*以下3行画出开始时的3个节点*/
draw_bar(map[snake[0].y][snake[0].x].x,map[snake[0].y][snake[0].x].y,6);
draw_bar(map[snake[1].y][snake[1].x].x,map[snake[1].y][snake[1].x].y,6);
draw_bar(map[snake[2].y][snake[2].x].x,map[snake[2].y][snake[2].x].y,6);
while(1)
{
while(!kbhit())
{
if(snake[0].x!=food.x||snake[0].y!=food.y)/*清除蛇尾*/
{
clear_bar(map[snake[length-1].y][snake[length-1].x].x,map[snake[length-1].y][snake[length-1].x].y);/*snake[i].x表示的是map中的第x列,即map中的第二个下标*/
map[snake[length-1].y][snake[length-1].x].state=0;
}
if(food.state==0)
getfood();
for(i=length-1;i>0;i--)/*蛇身前移*/
{
snake[i].x=snake[i-1].x;
snake[i].y=snake[i-1].y;
}
switch(way)
{
case W_UP:
snake[0].y--;
break;
case W_DOWN:
snake[0].y++;
break;
case W_LEFT:
snake[0].x--;
break;
case W_RIGHT:
snake[0].x++;
break;
}
if(snake[0].x>=23||snake[0].x<0||snake[0].y>=28||snake[0].y<0)
{
gameover();
life=0;
break;
}
if(map[snake[0].y][snake[0].x].state==1)
{
gameover();
life=0;
break;
}
if(snake[0].x==food.x&&snake[0].y==food.y)
{
food.state=0;
score++;
length++;
/*以下3行用来输出得分*/
sprintf(result,"score:%d",score);
draw_score(oldresult,result);
strcpy(oldresult,result);
}
draw_bar(map[snake[0].y][snake[0].x].x,map[snake[0].y][snake[0].x].y,6);
map[snake[0].y][snake[0].x].state=1;
delay(SPEED);
}
if(life==0)
break;
key=bioskey(0);
if(key==ESC)
break;
else if(key==UP&&way!=W_DOWN)
way=W_UP;
else if(key==DOWN&&way!=W_UP)
way=W_DOWN;
else if(key==LEFT&&way!=W_RIGHT)
way=W_LEFT;
else if(key==RIGHT&&way!=W_LEFT)
way=W_RIGHT;
else if(key==SPACE)
{
while((key=bioskey(0))!=SPACE);
}
}
}
main()
{
init();
init_data();
draw_background();
copyright();
gameplay();
bioskey(0);
closegraph();
}


[此贴子已经被作者于2007-3-18 15:35:02编辑过]


2007-03-18 15:17
neverTheSame
Rank: 3Rank: 3
来 自:江西农业大学
等 级:新手上路
威 望:9
帖 子:1511
专家分:0
注 册:2006-11-24
收藏
得分:0 
我来试一下.

wap酷禾网(http://wap.),提供免费的、优质的、快捷的wap资源下载服务。
2007-03-18 15:21
neverTheSame
Rank: 3Rank: 3
来 自:江西农业大学
等 级:新手上路
威 望:9
帖 子:1511
专家分:0
注 册:2006-11-24
收藏
得分:0 
我运行一下,非常不错.

wap酷禾网(http://wap.),提供免费的、优质的、快捷的wap资源下载服务。
2007-03-18 15:26
moonwalker
Rank: 1
等 级:新手上路
威 望:1
帖 子:909
专家分:2
注 册:2007-3-2
收藏
得分:0 
17楼的不错

“视频教程网”免费提供教学资源
C不限制你的自由!
条件是自己承担滥用自由的恶果!
2007-03-18 15:31
快速回复:[原创](好歹算个)贪吃蛇游戏[ c 彩色版]
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.081317 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved