贪吃蛇C++版.zip

  • shxmer
    了解作者
  • C/C++
    开发工具
  • 3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2019-02-26 17:33
    上传日期
一个tongguo C=++语言编写的应用太小程序,以实现贪吃蛇游戏功能
贪吃蛇C++版.zip
  • 贪吃蛇C++版.txt
    17.2KB
内容介绍
#include <graphics.h> #include <conio.h> #include <stdio.h> #include <time.h> #include <stdlib.h> #define C 10 //单位长 #define MAXLEN 200 #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 #define ESC 27 #define SPACE 32 #define ENTER 13 int LEVER=100; int MODE=0; int sum = 0; //全局变量分数 struct Point //标记点 { int x, y; }; struct Food //食物结构体 { struct Point p; int exist; }food; struct Snake //蛇结构体 { struct Point body[MAXLEN]; int nodes; int direction; int live; }sn; void menu(); //菜单 void mode(); //模式 void lever(); //等级 void develop(); //开发 void Game(); //游戏 void init(); //开始 void playGame(); //进行游戏 void close(); //关闭 void drawWall(); //画墙 void initGraphics(); //初始化画布 void creatfood(); //初始化食物 void initSnake(); //初始化蛇 void movesnake(); //蛇的移动 void drawsnake(); //画蛇 int crash(); //是否撞墙 void wudi(); //无敌 void gameover(); //游戏结束 int eatfood(); //是否吃到食物 void drawfood(); //画食物 int suiji(); //随机食物的限定 int stop(); //暂停 void main() { initgraph(800, 600); //画板大小 settextstyle(30, 15, _T("微软雅黑"));//字体设置 initGraphics(); develop(); menu(); Game(); gameover(); //结束 } void initGraphics() { LOGFONT f; setbkcolor(BLACK); //背景色黑色 setcolor(GREEN); //前景色绿色 setlinecolor(RED); //设置线段的颜色 getfont(&f); f.lfQuality = ANTIALIASED_QUALITY; f.lfWeight = FW_NORMAL; setfont(&f); } void lever() { int m,key=0; initGraphics(); outtextxy(350, 50, "级别"); outtextxy(350, 100,"低速"); outtextxy(350, 150,"中速"); outtextxy(350, 200,"高速"); outtextxy(350, 250,"极速"); outtextxy(450, 450,"选择:UP键和DOWN键"); outtextxy(450, 500,"确定:ENTER键"); setcolor(WHITE); rectangle(310,90,450, 139); do { key = _getch(); if (getpixel(310,90) == WHITE) m = 1; if (getpixel(310,140) == WHITE)m = 2; if (getpixel(310,190) == WHITE)m = 3; if (getpixel(310,240) == WHITE)m = 4; if (key == DOWN) { switch (m) { case 1: setcolor(WHITE); rectangle(310,140,450, 189); setcolor(BLACK); rectangle(310,90,450, 139); break; case 2: setcolor(WHITE); rectangle(310,190,450, 239); setcolor(BLACK); rectangle(310,140,450, 189); break; case 3: setcolor(WHITE); rectangle(310,240,450, 289); setcolor(BLACK); rectangle(310,190,450, 239); break; case 4: setcolor(WHITE); rectangle(310,90,450, 139); setcolor(BLACK); rectangle(310,240,450, 289); break; } } if (key == UP) { switch (m) { case 1: setcolor(WHITE); rectangle(310,240,450, 289); setcolor(BLACK); rectangle(310,90,450, 139); break; case 2: setcolor(WHITE); rectangle(310,90,450, 139); setcolor(BLACK); rectangle(310,140,450, 189); break; case 3: setcolor(WHITE); rectangle(310,140,450, 189); setcolor(BLACK); rectangle(310,190,450, 239); break; case 4: setcolor(WHITE); rectangle(310,190,450, 239 ); setcolor(BLACK); rectangle(310,240,450, 289); break; } } if (key == ENTER) { switch (m) { case 1:LEVER=300; cleardevice(); initGraphics(); outtextxy(200, 150, "您以设置游戏速度为低速"); getch(); menu();break; case 2:LEVER=150; cleardevice(); initGraphics(); outtextxy(200,150, "您以设置游戏速度为中速"); getch(); menu();break; case 3:LEVER=85; cleardevice(); initGraphics(); outtextxy(200,150, "您以设置游戏速度为高速"); getch(); menu();break; case 4:LEVER=50; cleardevice(); initGraphics(); outtextxy(200,150, "您以设置游戏速度为极速"); getch(); menu();break; } } } while (1); } void mode() { int key=0; initGraphics(); outtextxy(350, 50, "模式"); outtextxy(320, 100,"普通模式"); outtextxy(320, 150,"无敌模式"); outtextxy(320, 200,"返回菜单"); outtextxy(450, 450,"选择:UP键和DOWN键"); outtextxy(450, 500,"确定:ENTER键"); setcolor(WHITE); rectangle(310,90,450, 139); do { int m,key=0; key = _getch(); if (getpixel(310,90) == WHITE) m = 1; if (getpixel(310,140) == WHITE)m = 2; if (getpixel(310,190) == WHITE)m = 3; if (key == DOWN) { switch (m) { case 1: setcolor(WHITE); rectangle(310,140,450, 189); setcolor(BLACK); rectangle(310,90,450, 139); break; case 2: setcolor(WHITE); rectangle(310,190,450, 239); setcolor(BLACK); rectangle(310,140,450, 189); break; case 3: setcolor(WHITE); rectangle(310,90,450, 139); setcolor(BLACK); rectangle(310,190,450, 239); break; } } if (key == UP) { switch (m) { case 1: setcolor(WHITE); rectangle(310,190,450, 239); setcolor(BLACK); rectangle(310,90,450, 139); break; case 2: setcolor(WHITE); rectangle(310,90,450, 139); setcolor(BLACK); rectangle(310,140,450, 189); break; case 3: setcolor(WHITE); rectangle(310,140,450, 189); setcolor(BLACK); rectangle(310,190,450, 239); break; } } if (key == ENTER) { switch (m) { case 1: MODE=0; cleardevice(); initGraphics(); outtextxy(200, 150, "您以设置游戏模式为普通模式"); getch(); menu();break; case 2: MODE=1; cleardevice(); initGraphics(); outtextxy(200, 150, "您以设置游戏模式为无敌模式"); getch(); menu();break; case 3: cleardevice(); menu();break; } } }while(1); } void develop() { setbkcolor(WHITE); setcolor(BLACK); setlinecolor(RED); setfillcolor(BLACK); cleardevice(); solidrectangle(400,50,420,65); solidrectangle(420,70,440,85);solidrectangle(400,70,420,85);solidrectangle(380,70,400,85); solidrectangle(360,90,380,105);solidrectangle(380,90,400,105);solidrectangle(400,90,420,105);solidrectangle(440,90,460,105); solidrectangle(340,110,360,125);solidrectangle(360,110,380,125);solidrectangle(400,110,420,125);solidrectangle(420,110,440,125); solidrectangle(340,130,360,145);solidrectangle(360,130,380,145); solidrectangle(360,150,380,165);solidrectangle(380,150,400,165); solidrectangle(380,170,400,185);solidrectangle(400,170,420,185); solidrectangle(400,190,420,205);solidrectangle(420,190,440,205); solidrectangle(400,210,420,225);solidrectangle(380,210,400,225);solidrectangle(360,210,380,225); solidrectangle(340,190,360,205);solidrectangle(330,170,340,185); setfillcolor(RED); solidrectangle(420,90,440,105); settextstyle(30, 15, _T("微软雅黑")); setcolor(BLUE); outtextxy(300,350, "BEST SNAKE"); settextstyle(18,10, _T("微软雅黑")); outtextxy(370,390,"V2.0"); settextstyle(22,8, _T("微软雅黑")); outtextxy(500,480,"---BY 桂ILLL"); settextstyle(16,8, _T("微软雅黑")); setcolor(DARKGRAY); outtextxy(280,540,"c 2016 liangwei. All Right Reserved"); circle(283,549,5); Sleep(1700); } void menu() { int m,key=0; initGraphics(); settextstyle(30, 15, _T("微软雅黑")); cleardevice(); outtextxy(350, 50, "菜单"); outtextxy(320, 100, "开始游戏"); outtextxy(320, 150,"选择等级"); outtextxy(320, 200,"游戏模式"); outtextx
评论
    相关推荐