1、c 语言 UI 界面版贪吃蛇使用 ege 图形库,游戏实现单双人功能。道具有炸弹,随机魔盒,炸弹。有随机障碍物生成。不附加图片 。/*出品方:债组 *成员:冯强 陈志豪 邓青松 俄木木果 *时间:2016/7/11 */#include “graphics.h“#include #include#include#include#include#include#include #define printf outtextint speed=5;#define U 1#define D 2#define L 3#define R 4#define A 5#define S 6#define W 7
2、#define Dd 8#define VK_A 0x41/虚拟键值宏定义#define VK_S 0x53#define VK_W 0x57#define VK_D 0X44#define VK_N 0X4E#define VK_M 0x4d#define wd 10/蛇身的宽#define hg 10/蛇身的高/蛇身的一个节点typedef struct SNAKE int x;int y;struct SNAKE *next;snake;/全局变量/int score = 0, add = 10, score1 = 0, add1 = 10;/总得分与每次吃食物得分。int status
3、, status1, sleeptime=130 ,bomb_suiji = 1234,wall_suiji=5678,magic_suiji=6589,magic_suiji1=3256;/每次运行的时间间隔int oneortwo = 0;int dwall=1,isdwall=0;snake *head, *head1, *food, *bomb,*wall,*magic;snake *q;/便利第一条蛇用到的指针snake *q1;/遍历第二条蛇的时候用到的指针int endgamestatus = 0; /游戏结束的情况,int wall_suiji_u = 1111, wall_s
4、uiji_d = 2222, wall_suiji_r = 333, wall_suiji_l = 444;int wall_u, wall_d, wall_l, wall_r;int uwall_u, uwall_d, uwall_l, uwall_r;int magic_x=0,magic_xx;/后台的函数void initsnake();void initsnake1();void creatmagic();void gomagic();void createfood();void creatbomb();void creatwall();void drawwall();void sn
5、akemove();void pausegame();void gamecircle();void welcomeintogame();int biteself();void iswin();void gamestart();void cleanwall();void updatewall();void endgame();/iswin 改变void cantcrosswall();/撞墙,改变值/UI 函数void prompt();void pause();void UI();void Fence();void welcometogame();/后台函数void gomagic()int
6、i = 5;PIMAGE ss;PIMAGE bj;MUSIC muc;MUSIC muc1;muc.OpenFile(“good.mp3“);muc1.OpenFile(“bomm.mp3“);bj = newimage();ss = newimage();getimage(ss, “ss.png“, 0, 0);/加载蛇身图片if (oneortwo)getimage(bj, “m.png“, 0, 0);/加载背景图片elsegetimage(bj, “danren.png“, 0, 0);/加载背景图片snake * nexthead;snake * nexthead1;nexthea
7、d = (snake*)malloc(sizeof(snake);nexthead1 = (snake*)malloc(sizeof(snake);srand(time_t)magic_suiji);wall_suiji = wall_suiji + 3652;magic_x = rand() % 4;i+;if (magic_x = 0 | magic_x=1) if (magic_xx = 1)PlaySound(“mgwall.wav“, NULL, SND_FILENAME | SND_ASYNC);score = score + add;nexthead-next = head;he
8、ad = nexthead;q = head;while (q-next-next != NULL)putimage(q-x, q-y, wd, hg, ss, 0, 0);q = q-next;putimage(q-next-x, q-next-y, wd, hg, bj, q-next-x, q-next-y);free(q-next);q-next = NULL;cleanwall();drawwall();else if (magic_xx = 3)PlaySound(“mgwall.wav“, NULL, SND_FILENAME | SND_ASYNC);score1 = scor
9、e1 + add1;nexthead1-next = head1;head1 = nexthead1;q1 = head1;while (q1-next-next != NULL)putimage(q1-x, q1-y, wd, hg, ss, 0, 0);q1 = q1-next;putimage(q1-next-x, q1-next-y, wd, hg, bj, q1-next-x, q1-next-y);free(q1-next);q1-next = NULL;cleanwall();drawwall();else if (magic_x = 2)if (magic_xx = 1)Pla
10、ySound(“mgbomb.wav“, NULL, SND_FILENAME | SND_ASYNC);nexthead-next = head;head = nexthead;q = head;while (q-next-next-next != NULL)putimage(q-x, q-y, wd, hg, ss, 0, 0);q = q-next;putimage(q-next-x, q-next-y, wd, hg, bj, q-next-x, q-next-y);putimage(q-next-next-x, q-next-next-y, wd, hg, bj, q-next-ne
11、xt-x, q-next-next-y);updatewall();free(q-next-next);free(q-next);q-next = NULL;iswin();else if (magic_xx = 2)PlaySound(“mgbomb.wav“, NULL, SND_FILENAME | SND_ASYNC);nexthead1-next = head1;head1 = nexthead1;q1 = head1;while (q1-next-next-next != NULL)putimage(q1-x, q1-y, wd, hg, ss, 0, 0);q1 = q1-nex
12、t;putimage(q1-next-x, q1-next-y, wd, hg, bj, q1-next-x, q1-next-y);putimage(q1-next-next-x, q1-next-next-y, wd, hg, bj, q1-next-next-x, q1-next-next-y);updatewall();free(q1-next-next);free(q1-next);q1-next = NULL;iswin();else if (magic_x = 3)if (magic_xx = 2)PlaySound(“mggood.wav“, NULL, SND_FILENAM
13、E | SND_ASYNC);nexthead1-next = head1;head1 = nexthead1;q1 = head1;while (q1 != NULL)putimage(q1-x, q1-y, wd, hg, ss, 0, 0);q1 = q1-next;updatewall();score1 = score1 + add1;else if (magic_xx = 1)PlaySound(“mggood.wav“, NULL, SND_FILENAME | SND_ASYNC);nexthead-next = head;head = nexthead;q = head;whi
14、le (q != NULL)putimage(q-x, q-y, wd, hg, ss, 0, 0);q = q-next;updatewall();score = score + add;if (!oneortwo)sleeptime = sleeptime - 50;void creatmagic()re:q = head;q1 = head1;PIMAGE magics;magics = newimage();getimage(magics, “magic.png“, 0, 0);/加载食物图片snake *magic_1;magic_1 = (snake *)malloc(sizeof
15、(snake);srand(time_t)magic_suiji+220);magic_suiji = magic_suiji + 66;magic_1-x = rand() % 280;magic_1-y = rand() % 260;magic_1-x = (magic_1-x / wd) * wd + 250;magic_1-y = (magic_1-y / wd) * wd + 110;/控制坐标 保证食物出现在正确的位置 并且不会和蛇头错开if (isdwall = 0 q = q-next;if (oneortwo)while (q1-next != NULL)if (magic_
16、1-x = q1-x q1 = q1-next;q = head;q1 = head;while (q-next = NULL)if (q-x = magic_1-x | q-y = magic_1-y) /判断蛇身是否与食物重合free(magic_1);creatmagic();q = q-next;while (q1-next = NULL)if (q1-x = magic_1-x creatmagic();q1 = q1-next;magic = magic_1;putimage(magic_1-x, magic_1-y, wd, hg,magics, 0, 0);void updat
17、ewall()if (isdwall)int i;PIMAGE upwall;upwall = newimage();getimage(upwall, “m.png“, 0, 0);for (i = 0; i x = rand() % 280;wall_1-y = rand() % 260;wall_1-x = (wall_1-x / wd) * wd + 250;wall_1-y = (wall_1-y / wd) * wd + 110; /控制坐标 保证炸弹出现在正确的位置 并且不会和蛇头错开if (isdwall = 0 q = q-next;if (oneortwo)while (q1
18、-next != NULL)if (wall_1-x = q1-x q1 = q1-next;q = head;q1 = head1;while (q-next = NULL)if (q-x = wall_1-x creatbomb();q = q-next;while (q1-next = NULL)if (q1-x = wall_1-x creatwall();q1 = q1-next;wall = wall_1;putimage(wall_1-x, wall_1-y, wd, hg, walls, 0, 0);void iswin()int snake1 = 0;int snake2 =
19、 0;q1 = head1;q = head;if (score = 100 q = q-next;while (q1-next != NULL)snake2 = snake2 + 1;q1= q1-next;if (snake1 x = 200;tail-y = 200;tail-next = NULL;for (i = 0; i next = tail;head-x = 200 + i;head-y = 200;tail = head;while (tail != NULL)/从头到为,输出蛇身putimage(tail-x, tail-y, wd, hg, ss, 0, 0);tail
20、= tail-next;void initsnake1()/初始化第二条蛇身PIMAGE ss1;ss1 = newimage();getimage(ss1, “bs.png“, 0, 0);/加载蛇身图片snake *tail1;int i;tail1 = (snake*)malloc(sizeof(snake);/从蛇尾开始,头插法,以 x,y 设定开始的位置/tail1-x = 200;tail1-y = 300;tail1-next = NULL;for (i = 0; i next = tail1;head1-x = 200 + i;head1-y = 300;tail1 = hea
21、d1;while (tail1 != NULL)/从头到为,输出蛇身if (oneortwo)putimage(tail1-x, tail1-y, wd, hg, ss1, 0, 0);tail1 = tail1-next;int biteself()/判断是否咬到了自己snake *self;snake *self1;self = head-next;self1 = head1-next;while (self != NULL)if (self-x = head-x self = self-next;while (self1 != NULL)if (self1-x = head1-x sel
22、f1 = self1-next;return 0;void createfood()/随机出现食物 re2:q = head;q1 = head1;PIMAGE foods;foods = newimage();getimage(foods, “food.png“, 0, 0);/加载食物图片snake *food_1;food_1 = (snake *)malloc(sizeof(snake);srand(time(NULL);food_1-x = rand() % 280;food_1-y = rand() % 260;food_1-x=(food_1-x / wd) * wd + 250
23、;food_1-y = (food_1-y / wd) * wd + 110;/控制坐标 保证食物出现在正确的位置 并且不会和蛇头错开if (isdwall = 0 q = q-next;if (oneortwo)while (q1-next != NULL)if (food_1-x = q1-x q1 = q1-next;q = head;q1 = head;while (q-next = NULL)if (q-x = food_1-x | q-y = food_1-y) /判断蛇身是否与食物重合free(food_1);createfood();q = q-next;while (q1-n
24、ext = NULL)if (q1-x = food_1-x createfood();q1 = q1-next;food = food_1;putimage(food_1-x, food_1-y, wd, hg, foods, 0, 0);void creatbomb()re3:q = head;q1 = head1;PIMAGE bombs;bombs = newimage();getimage(bombs, “bomb.png“, 0, 0);snake *bomb_1;bomb_1 = (snake*)malloc(sizeof(snake);srand(time_t)bomb_suiji);bomb_suiji = bomb_suiji + 1234;bomb_1-x = rand() % 280;bomb_1-y = rand() % 260;bomb_1-x = (bomb_1-x / wd) * wd + 250;