C语言-DOS游戏开发之贪吃蛇
贪吃蛇(C语言知识学习)
![贪吃蛇(C语言知识学习)](https://img.taocdn.com/s3/m/3f524160eefdc8d376ee32c9.png)
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <windows.h>//蛇头移动方向#define UP 1#define DOWN 2#define LEFT 3#define RIGHT 4//死亡判定(怼墙或怼自己)#define KISSASS 1#define KISSWALL 2//坐标转化#define POINT(x,y) ((y)*80+(x)+1) //将(x,y)坐标转化为一个int类数值#define GETX(x) (((x)-1)%80)//将int类数值提取出原先的x#define GETY(y) (((y)-1)/80)//同理,提取出yHANDLE Console;void Position(int x, int y); //移动光标到(x,y)//Windows.hvoid DrawMap();//画墙void ShowText(char *text);//根据字符串的长短智能打印出包围字符串的笑脸void ResetSnake();//初始化贪吃蛇int RefreshSnake();//更新蛇的移动void CreatFood();//生成食物void Draw();//画出蛇身和食物void RefreshScreen();//屏幕刷新void GameOver(int Type);//游戏结束条件int Gaming();//代码跑起来char play = 0; ////值为1则继续游戏,值为0游戏退出char pause = 0; //值为1则暂停,值为0游戏继续char direction; //蛇头方向int snake[500]; //snake[0]为蛇头int body; //蛇身体长度int newbody;//吃完食物新长的蛇身int addHead,deleteT ail;//增加蛇头擦去蛇尾,使贪吃蛇动起来int food = 0; //食物void main(){CONSOLE_CURSOR_INFO CurrInfo = { sizeof(CONSOLE_CURSOR_INFO), 0 };Console = GetStdHandle(STD_OUTPUT_HANDLE);DrawMap();ShowText("Copyright reserve by 张博元");while(Gaming()){if (play)DrawMap();};return 0;}void Position(int x, int y) //移动光标到(x,y)//Windows.h{COORD coors = { x, y };SetConsoleCursorPosition(Console, coors);}void DrawMap() //画墙{int i;for (i = 3; i < 70; i = i + 12){Position(i, 0);printf("===我是墙===");}for (i = 0; i < 25; i = i + 1){Position(0, i);printf("|| ");}for (i = 3; i < 70; i = i + 12){Position(i, 24);printf("============");}for (i = 0; i < 25; i = i + 1){Position(76, i);printf("|| ");}}void ShowText(char *text) //根据字符串的长短智能打印出包围字符串的笑脸{int i;int strLength = strlen(text); //得到字符串长度Position(40 - (strLength / 2)-1, 11);printf("%c", 1);for (i = 0; i < strLength + 2; i++){printf("%c", 1);}printf("%c", 1);Position(40 - (strLength / 2)-1, 12);printf("%c ", 1);printf(text);printf(" %c", 1);Position(40 - (strLength / 2)-1, 13);printf("%c", 1);for (i = 0; i < strLength + 2; i++){printf("%c", 1);}printf("%c", 1);}void ResetSnake() //初始化贪吃蛇{int x, y;for (x = 39, y = 0; y < 2; y++){snake[y] = POINT(x, 7 - y);}body = 2; //设定蛇身初始长度为2newbody = 6;addHead = 0;deleteTail = 0;direction = LEFT;}int RefreshSnake() //更新蛇的移动//返回值为1游戏结束{int x, y;memcpy(snake + 1, snake, sizeof(int)*body); if (!newbody){deleteTail = snake[body];snake[body] = 0;}else{body++;newbody--;}x = GETX(snake[0]);y = GETY(snake[0]);switch (direction) //控制蛇头移动方向{case UP:y -= 1; //蛇头向上移动一格(对应坐标y-1)snake[0] = POINT(x, y);break;case DOWN:y += 1;snake[0] = POINT(x, y);break;case LEFT:x -= 1;snake[0] = POINT(x, y);break;case RIGHT:x += 1;snake[0] = POINT(x, y);break;}addHead = snake[0];if (x > 75 || x < 3 ||y > 23 || y < 1) //检测是否撞墙{GameOver(KISSWALL);return 1;}int i;for (i = 1; i < body; i++)if (snake[0] == snake[i]) //检测是否撞到自己身体{GameOver(KISSASS);return 1;}if (snake[0] == food){while (1) //食物的位置不与蛇身重合{food = (rand() % (75 * 23)) ;for (i = 0; snake[i]; i++)if (food == snake[i]) //检测是否吃到食物,吃到则reset食物food = 0;if (food) //如果food==0则重新建立一个食物坐标break;}CreatFood(); //吃了?再来一个!newbody = (rand() % 6) + 1; //吃完食物蛇身增长一节}return 0;}void CreatFood() //生成食物{if (GETX(food) > 75 || GETX(food) < 3 || GETY(food) > 23 || GETY(food) < 1) CreatFood;elsePosition(GETX(food), GETY(food));printf("%c",4);}void Draw() //画出蛇身和食物{system("cls");int i;for (i = 0; snake[i]; i++){Position(GETX(snake[i]), GETY(snake[i]));printf("%c",1);}CreatFood();}void RefreshScreen() //屏幕刷新{if (deleteT ail){Position(GETX(deleteTail), GETY(deleteTail));printf(" ");}if (addHead){Position(GETX(addHead), GETY(addHead));printf("%c",1);}addHead = deleteT ail = 0;}void GameOver(int Type) //游戏结束条件{switch (Type){case KISSASS://撞到自己身体ShowText("NOOB!当你以光速绕着一棵树奔跑就会发现自己在怼自己!");break;case KISSWALL://撞墙ShowText("NOOB!你有考虑过墙的感受吗→→");}food = 0;play = 0;memset(snake, 0, sizeof(int) * 500); //内存初始化}int Gaming() //执行int KeyboardInput;Sleep(60);//速度if (kbhit()){KeyboardInput = getch();if (KeyboardInput == 0 || KeyboardInput == 0xE0) {KeyboardInput = getch();switch (KeyboardInput) //方向控制{case 72:if (direction != DOWN)direction = UP;break;case 80:if (direction != UP)direction = DOWN;break;case 75:if (direction != RIGHT)direction = LEFT;break;case 77:if (direction != LEFT)direction = RIGHT;break;}}if (KeyboardInput == '\r') //暂停{if (!play){play = 1;if (pause){Draw();pause = 0;}}else{ShowText("不许暂停,继续怼!!");play = 0;pause = 1;}}else if (KeyboardInput == 0x1B) //退出return 0;}if (play){if (!food){srand(clock());food = (rand() % (75 * 23));ResetSnake();Draw();}else{if (!RefreshSnake()){RefreshScreen();}}}return 1;}// Allrights reserve by 博元。
C语言编写方案-贪吃蛇游戏
![C语言编写方案-贪吃蛇游戏](https://img.taocdn.com/s3/m/0913241ef78a6529647d53f8.png)
编写贪吃蛇游戏。
功能:
1、由键盘控制贪吃蛇的移动方向。
2、显示分数每吃掉一个事物加10分。
3、当碰到墙壁或者自身时,结束游戏。
4、其它功能可以自由发挥。
该任务完成人员:
班级
姓名
学号
计041
朱文瀚
0408123151
计041
俞桦
0408123149
计041
黄轶晨
0408123139
计041
樊钧
用户使用ASWD分别控制左下上右等四个方向,来让贪吃蛇进行行走。食物则自动生成,当贪吃蛇迟到食物后身长增长一段。当贪吃蛇碰到墙壁或者自身时则结束游戏。
0408123136
计042
ห้องสมุดไป่ตู้郝敏
0408123207
计042
单恺霞
0408123203
计042
郭小红
0408123206
计042
胡倩
0408123208
计042
杜长纪
0408123234
计042
花俊峰
0408123237
计042
张志成
0408123248
贪吃蛇游戏操作说明:
该系统在TC2.0下调试通过。
贪吃蛇游戏c语言源代码
![贪吃蛇游戏c语言源代码](https://img.taocdn.com/s3/m/e56464d5c0c708a1284ac850ad02de80d4d80655.png)
#include <stdlib.h>#include <graphics.h>#include <bios.h>#include <dos.h>#include <conio.h>#define Enter 7181#define ESC 283#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#ifdef __cplusplus#define __CPPARGS ...#else#define __CPPARGS#endifvoid interrupt (*oldhandler)(__CPPARGS);void interrupt newhandler(__CPPARGS);void SetTimer(void interrupt (*IntProc)(__CPPARGS));void KillTimer(void);void Initgra(void);void TheFirstBlock(void);void DrawMap(void);void Initsnake(void);void Initfood(void);void Snake_Headmv(void);void Flag(int,int,int,int);void GameOver(void);void Snake_Bodymv(void);void Snake_Bodyadd(void);void PrntScore(void);void Timer(void);void Win(void);void TheSecondBlock(void);void Food(void);void Dsnkorfd(int,int,int);void Delay(int);struct Snake{int x;int y;int color;}Snk[12];struct Food{int x;int y;int color;}Fd;int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse; char Sco[2],Time[6];void main(){ Initgra();SetTimer(newhandler);TheFirstBlock();while(1){DrawMap();Snake_Headmv();GameOver();Snake_Bodymv();Snake_Bodyadd();PrntScore();Timer();Win();if(key==ESC)break;if(key==Enter){cleardevice();TheFirstBlock();}TheSecondBlock();Food();Delay(Snkspeed);}closegraph();KillTimer();}void interrupt newhandler(__CPPARGS){TimerCounter++;oldhandler();}void SetTimer(void interrupt (*IntProc)(__CPPARGS)) {oldhandler=getvect(0x1c);disable();setvect(0x1c,IntProc);enable();}void KillTimer(){disable();setvect(0x1c,oldhandler);enable();}void Initgra(){int gd=DETECT,gm;initgraph(&gd,&gm,"d:\\tc");}void TheFirstBlock(){setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The First Block");loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=10;num=2;Times=0;key=0;TureorFalse=1;TimerCounter=0;Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0'; }else if(key==ESC) cleardevice();else goto loop;}void DrawMap(){line(10,10,470,10);line(470,10,470,470);line(470,470,10,470);line(10,470,10,10);line(480,20,620,20);line(620,20,620,460);line(620,460,480,460);line(480,460,480,20);}void Initsnake(){randomize();num=2;Snk[0].x=random(440);Snk[0].x=Snk[0].x-Snk[0].x%20+50;Snk[0].y=random(440);Snk[0].y=Snk[0].y-Snk[0].y%20+50;Snk[0].color=4;Snk[1].x=Snk[0].x;Snk[1].y=Snk[0].y+20;Snk[1].color=4;}void Initfood(){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;}void Snake_Headmv(){if(bioskey(1)){key=bioskey(0);switch(key){case UP:Flag(1,0,0,0);break;case DOWN:Flag(0,1,0,0);break;case LEFT:Flag(0,0,1,0);break;case RIGHT:Flag(0,0,0,1);break;default:break;}}if(flag1){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color); }if(flag2){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color); }if(flag3){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}if(flag4){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}}void Flag(int a,int b,int c,int d){flag1=a;flag2=b;flag3=c;flag4=d;}void GameOver(){int i;if(Snk[0].x<20||Snk[0].x>460||Snk[0].y<20||Snk[0].y>460) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop1:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();elsegoto loop1;}for(i=3;i<num;i++){if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop2:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();else goto loop2;}}}void Snake_Bodymv(){int i,s,t;for(i=1;i<num;i++){Dsnkorfd(checkx,checky,Snk[i].color); Dsnkorfd(Snk[i].x,Snk[i].y,0);s=Snk[i].x;t=Snk[i].y;Snk[i].x=checkx;Snk[i].y=checky;checkx=s;checky=t;}}void Food(){if(flag5){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;flag5=0;}Dsnkorfd(Fd.x,Fd.y,Fd.color);}void Snake_Bodyadd(){if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y) {if(Snk[num-1].x>Snk[num-2].x){num++;Snk[num-1].x=Snk[num-2].x+20;Snk[num-1].y=Snk[num-2].y;Snk[num-1].color=Fd.color;}elseif(Snk[num-1].x<Snk[num-2].x){num++;Snk[num-1].x=Snk[num-2].x-20;Snk[num-1].y=Snk[num-2].y;Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y>Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y+20; Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y<Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y-20; Snk[num-1].color=Fd.color;}flag5=1;Score++;}}void PrntScore(){if(Hscore!=Score){setcolor(11);settextstyle(0,0,3); outtextxy(490,100,"SCORE"); setcolor(2);setfillstyle(1,0);rectangle(520,140,580,180); floodfill(530,145,2);Sco[0]=(char)(Score+48);Sco[1]='\0';Hscore=Score;setcolor(4);settextstyle(0,0,3); outtextxy(540,150,Sco);}}void Timer(){if(TimerCounter>18){Time[4]=(char)(Time[4]-1); if(Time[4]<'0'){Time[4]='9';Time[3]=(char)(Time[3]-1);}if(Time[3]<'0'){Time[3]='5';Time[1]=(char)(Time[1]-1);}if(TureorFalse){setcolor(11);settextstyle(0,0,3);outtextxy(490,240,"TIMER");setcolor(2);setfillstyle(1,0);rectangle(490,280,610,320);floodfill(530,300,2);setcolor(11);settextstyle(0,0,3);outtextxy(495,290,Time);TureorFalse=0;}if(Time[1]=='0'&&Time[3]=='0'&&Time[4]=='0') {setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}else if(key==ESC) cleardevice();else goto loop;}TimerCounter=0;TureorFalse=1;}}void Win(){if(Score==3)Times++;if(Times==2){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"You Win");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();key=0;}else if(key==ESC) cleardevice();else goto loop;}}void TheSecondBlock(){if(Score==3){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The Second Block"); loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=8;num=2;key=0;}else if(key==ESC) cleardevice();else goto loop;}}void Dsnkorfd(int x,int y,int color) {setcolor(color);setfillstyle(1,color);circle(x,y,10);floodfill(x,y,color);}void Delay(int times){int i;for(i=1;i<=times;i++)delay(15000);}。
C语言项目案例之贪吃蛇
![C语言项目案例之贪吃蛇](https://img.taocdn.com/s3/m/6cdfb6e99a89680203d8ce2f0066f5335a816704.png)
C语⾔项⽬案例之贪吃蛇项⽬案例:贪吃蛇下载链接:1. 初始化墙代码:// 初始化墙void init_wall(void){for (size_t y = 0; y <= HIGH; ++y){for (size_t x = 0; x <= WIDE; ++x){if (x == WIDE || y == HIGH) // 判断是否到墙{printf("=");}else{printf(" ");}}printf("\n");}}效果:2. 定义蛇和⾷物类型typedef struct{int x;int y;}FOOD; // ⾷物typedef struct{int x;int y;}BODY; // ⾝体typedef struct{int size; // ⾝体长度BODY body[WIDE*HIGH];}SNAKE; // 蛇3. 初始化蛇和⾷物// 定义⼀个蛇和⾷物SNAKE snake;FOOD food;// 初始化⾷物void init_food(void){food.x = rand() % WIDE; // 随机⽣成坐标food.y = rand() % HIGH;}// 初始化蛇void init_snake(void){snake.size = 2;// 将蛇头初始化到墙中间snake.body[0].x = WIDE / 2;snake.body[0].y = HIGH / 2;// 蛇⾝紧跟蛇头snake.body[1].x = WIDE / 2 - 1;snake.body[1].y = HIGH / 2;}4. 显⽰UI// 显⽰UIvoid showUI(void){// 显⽰⾷物// 存放光标位置COORD coord;coord.X = food.x;coord.Y = food.y;// 光标定位SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); putchar('$');// 显⽰蛇for (size_t i = 0; i < snake.size; ++i){// 设置光标coord.X = snake.body[i].x;coord.Y = snake.body[i].y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); if (i == 0)}else{putchar('#');}}}效果:最终代码// main.c#define _CRT_SECURE_NO_WARNINGS#include "./snakeGame.h"int main(void){// 取消光标CONSOLE_CURSOR_INFO cci;cci.bVisible = FALSE; // 取消光标cci.dwSize = sizeof(cci);SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cci); system("color 2");printf("欢迎来到贪吃蛇\n准备好了吗?按s/S开始,q/Q退出\n"); char ch = _getch();switch (ch){case 's':case 'S':system("color 0");system("cls");break;default:return 0;}init_wall();init_food();init_snake();showUI();playGame();return 0;}// snakeGame.c#include "./snakeGame.h"// 定义⼀个蛇和⾷物SNAKE snake;FOOD food;// ⽅向增量int dx = 0;int dy = 0;int lx, ly; // 尾节点// 初始化⾷物void init_food(void){food.x = rand() % WIDE; // 随机⽣成坐标food.y = rand() % HIGH;}// 初始化蛇void init_snake(void){snake.size = 2;snake.fraction = 0;// 将蛇头初始化到墙中间snake.body[0].x = WIDE / 2;snake.body[0].y = HIGH / 2;snake.body[1].x = WIDE / 2 - 1;snake.body[1].y = HIGH / 2;}// 初始化墙void init_wall(void){for (size_t y = 0; y <= HIGH; ++y){for (size_t x = 0; x <= WIDE; ++x){if (x == WIDE || y == HIGH) // 判断是否到墙{printf("=");}else{printf(" ");}}printf("\n");}printf("分数:0\n");}// 显⽰UIvoid showUI(void){// 显⽰⾷物// 存放光标位置COORD coord;coord.X = food.x;coord.Y = food.y;// 光标定位SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);putchar('$');// 显⽰蛇for (size_t i = 0; i < snake.size; ++i){// 设置光标coord.X = snake.body[i].x;coord.Y = snake.body[i].y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);if (i == 0){putchar('@');}else{putchar('#');}}// 处理尾节点coord.X = lx;coord.Y = ly;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);putchar(' ');coord.X = WIDE;coord.Y = HIGH;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);printf("\n分数:%d\n",snake.fraction);}void playGame(void){COORD _coord;system("color 7");char key = 'D';// 蛇不能撞墙while (snake.body[0].x >= 0 && snake.body[0].x <= WIDE && snake.body[0].y >= 0 && snake.body[0].y <= HIGH) {// 蛇不能撞⾃⼰for (size_t i = 1; i < snake.size; ++i){if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y){goto OVER;}}// 撞⾷物if (snake.body[0].x == food.x && snake.body[0].y == food.y){++snake.size;++snake.fraction;// 随机出现⾷物init_food();}// 控制蛇移动// 判断是否按下按键if (_kbhit()){key = _getch(); // 不需要敲回车,按下就⽴马确认}// 判断W A S D中哪个按键按下switch (key){case 'w':case 'W':dx = 0;dy = -1;break;case 'a':case 'A':dx = -1;dy = 0;break;case 's':case 'S':dx = 0;dy = 1;break;case 'd':dx = 1;dy = 0;break;case 'q':case 'Q':_coord.X = WIDE;_coord.Y = HIGH;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), _coord); putchar('\n');return;}// 蛇移动// 记录尾节点位置lx = snake.body[snake.size - 1].x;ly = snake.body[snake.size - 1].y;for (size_t i = snake.size - 1; i > 0; --i){snake.body[i].x = snake.body[i - 1].x;snake.body[i].y = snake.body[i - 1].y;}// 更新蛇头snake.body[0].x += dx;snake.body[0].y += dy;showUI();Sleep(500); // 延时}// 游戏结束OVER:system("color 4");_coord.X = 6;_coord.Y = HIGH + 1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), _coord); printf("\n游戏结束");printf("按r/R重新开始,按q/Q退出\n");char _key;_key = _getch();switch (_key){case 'r':case 'R':system("cls");init_wall();init_food();init_snake();showUI();playGame();case 'Q':case 'q':default:system("color 7");return;}}// snakeGame.h#pragma once#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <Windows.h>#define WIDE 60 // 长#define HIGH 20 // ⾼typedef struct{int x;int y;}FOOD; // ⾷物typedef struct{int x;int y;}BODY; // ⾝体typedef struct{int size; // ⾝体长度int fraction; // 分数BODY body[WIDE*HIGH];}SNAKE; // 蛇void init_wall(void);void init_food(void);void init_snake(void);void showUI(void);void playGame(void);。
贪吃蛇的c语言源程序
![贪吃蛇的c语言源程序](https://img.taocdn.com/s3/m/576663f9910ef12d2af9e7e9.png)
#include<stdio.h>#include<graphics.h>#include<stdlib.h>#include<dos.h>#include<bios.h>#include<time.h>#define NULL 0#define UP 4471#define LEFT 7777#define DOWN 8051#define RIGHT 8292#define PAUSE 6512#define ESC 283#define SNAKE_COLOR 4#define SNAKE_BOND_COLOR 2#define SNAKE_STYLE LTBKSLASH_FILL #define FOOD_STYLE CLOSE_DOT_FILL #define MAP_COLOR 8#define MAP_BOND_COLOR 6#define MAP_STYLE SOLID_FILLtypedef struct Snake_Node{int x,y;struct Snake_Node *next;}Snake_Node,*P_Snake_Node;struct{int x,y,color;}Food;struct{int dx;int dy;}Direct={0,1};static int speed=1,len=4,px,py;static P_Snake_Node Head;static P_Snake_Node Map;void paint_node(int,int,int,int,int);void Put_Food(int);void Put_Snake_Node(P_Snake_Node,int); void Init();void Grow_up(int,int);void Make_Map();void Auto_Make_Map();void Load_Game();void Auto_Start();int Snake_Dead();void GO_GO_GO();void Play_Game();void Exit_Save();void Failed();void main();void paint_node(int x,int y,int color1,int color2,int style){setfillstyle(SOLID_FILL,color2);bar(x*10,479-y*10,x*10+9,479-y*10-9);setfillstyle(style,color1);bar(x*10+1,479-y*10-1,x*10+9-1,479-y*10-9+1);setfillstyle(SOLID_FILL,15);}void Put_Food(int color){randomize();Food.x=random(46)+1;Food.y=random(46)+1;Food.color=color;paint_node(Food.x,Food.y,Food.color,Food.color,FOOD_STYLE);}void Put_Snake_Node(P_Snake_Node p,int flag){if(flag)paint_node(p->x,p->y,SNAKE_COLOR,SNAKE_BOND_COLOR,SNAKE_STYLE);elsepaint_node(p->x,p->y,getbkcolor(),getbkcolor(),SOLID_FILL);}void Init(){int gdrive=VGA,gmode=VGAHI;initgraph(&gdrive,&gmode,"d:\\TC20\\turboc2");setfillstyle(XHATCH_FILL,2);bar(490,0,509,479);setfillstyle(SOLID_FILL,6);bar(480,0,489,479);setfillstyle(SOLID_FILL,15);setcolor(9);setlinestyle(CENTER_LINE,0,3);line(490,0,490,479);setcolor(3);setlinestyle(SOLID_LINE,0,3);line(509,0,509,479);line(509,479,639,479);line(639,479,639,0);line(639,0,509,0);setlinestyle(SOLID_LINE,0,0);setcolor(10);settextstyle(TRIPLEX_FONT,HORIZ_DIR,3); outtextxy(530,12,"GREEDY");outtextxy(537,40,"SNAKE");settextstyle(0,0,0);setcolor(13);outtextxy(510,80,"<<============>>"); setcolor(1);setlinestyle(CENTER_LINE,0,3);line(573,90,573,305);outtextxy(510,310,"+++++++++++++++++"); setcolor(5);settextstyle(GOTHIC_FONT,0,5);outtextxy(512,390,"~~~~~~~");outtextxy(512,392,"~~~~~~~");outtextxy(512,388,"~~~~~~~");setcolor(10);settextstyle(SANS_SERIF_FONT,1,1); outtextxy(510,115,"->");outtextxy(510,165,"<-");settextstyle(SANS_SERIF_FONT,0,1); outtextxy(512,215,"->");outtextxy(512,265,"<-");setcolor(14);settextstyle(DEFAULT_FONT,0,1); outtextxy(550,128,":W");outtextxy(550,175,":S");outtextxy(550,222,":A");outtextxy(550,272,":D");setcolor(10);settextstyle(SMALL_FONT,0,5);outtextxy(575,125,"PAUSE /");outtextxy(575,140,"CONTINUE:");outtextxy(580,250,"EXIT:");/*************/}void Grow_Up(int x,int y){P_Snake_Node p;p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=x;p->y=y;p->next=Head->next;Head->next=p;Head=p;++len;++speed;}void Auto_Make_Map(){P_Snake_Node p,q;p=q=Map=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=0;p->y=0;p->next=NULL;while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=(q->x)+1;p->y=q->y;q->next=p;p->next=NULL;q=p;if((p->x)==47) break;}while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=q->x;p->y=(q->y)+1;q->next=p;p->next=NULL;q=p;if((p->y)==47) break;}while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=(q->x)-1;p->y=q->y;q->next=p;p->next=NULL;q=p;if((p->x)==0) break;}while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=q->x;p->y=(q->y)-1;q->next=p;p->next=NULL;q=p;if((p->y)==1) break;}p=Map;while(p){paint_node(p->x,p->y,MAP_COLOR,MAP_BOND_COLOR,MAP_STYLE);p=p->next;}}void Auto_Start(){int i=1;P_Snake_Node p,q;Head=p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=24;p->y=24-3;p->next=NULL;q=p;while(i<=3){q=(P_Snake_Node)malloc(sizeof(Snake_Node));p->next=q;q->x=p->x;q->y=(p->y)+1;q->next=NULL;p=q;++i;}p->next=Head;Head=p;while(i>=1){Put_Snake_Node(p,1);p=p->next;i--;}Put_Food(14);Auto_Make_Map();}int Snake_Dead(){P_Snake_Node p;int i=1;p=Head->next->next;while(i<=len-4){if(p->x==px && p->y==py)return(1);++i;p=p->next;}p=Map;while(p){if(p->x==px && p->y==py)return(1);p=p->next;}return(0);}void GO_GO_GO(){px=(Head->x)+(Direct.dx);py=(Head->y)+(Direct.dy);if(!Snake_Dead()){if(px==Food.x&&py==Food.y){Grow_Up(px,py);Put_Snake_Node(Head,1);Put_Food(14);}else{Head=Head->next;Put_Snake_Node(Head,0);Head->x=px;Head->y=py;Put_Snake_Node(Head,1);}}elseFailed();}void Play_Game(){int wait;while(1){while(!kbhit()){GO_GO_GO();wait=0;while(wait<=2){delay(30000);++wait;}}switch(bioskey(0)){case UP:{if(Direct.dx!=0&&Direct.dy!=-1){Direct.dx=0;Direct.dy=1;}break;}case LEFT:{if(Direct.dx!=1&&Direct.dy!=0){Direct.dx=-1;Direct.dy=0;}break;}case DOWN:{if(Direct.dx!=0&&Direct.dy!=1){Direct.dx=0;Direct.dy=-1;}break;}case RIGHT:{if(Direct.dx!=-1&&Direct.dy!=0){Direct.dx=1;Direct.dy=0;}break;}case ESC:{exit(1);break;}}}}void Failed(){setcolor(4);settextstyle(TRIPLEX_FONT, HORIZ_DIR, 6);outtextxy(90,200,"GAME OVER!");getch();exit(1);/**********/ }void main(){Init();getch();Auto_Start();Play_Game();getch(); closegraph();}。
超简单贪吃蛇c语言代码编写
![超简单贪吃蛇c语言代码编写](https://img.taocdn.com/s3/m/2ffd1394783e0912a3162ac1.png)
超简单贪吃蛇c语言代码编写贪吃蛇其实就是实现以下几步——1:蛇的运动(通过“画头擦尾”来达到蛇移动的视觉效果)2:生成食物3:蛇吃食物(实现“画头不擦尾”)4:游戏结束判断(也就是蛇除了食物,其余东西都不能碰)#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<conio.h>#include<time.h>#define width 60#define hight 25#define SNAKESIZE 200//蛇身的最长长度int key=72;//初始化蛇的运动方向,向上int changeflag=1;//用来标识是否生成食物,1表示蛇还没吃到食物,0表示吃到食物int speed=0;//时间延迟struct {int len;//用来记录蛇身每个方块的坐标int x[SNAKESIZE];int y[SNAKESIZE];int speed;}snake;struct{int x;int y;}food;void gotoxy(int x,int y)//调用Windows的API函数,可以在控制台的指定位置直接操作,这里可暂时不用深究{COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }//■○void drawmap(){//打印图框for (int _y = 0; _y < hight; _y++){for (int x = 0; x < width; x+=2){if (x == 0 || _y == 0 || _y == hight - 1 || x == width - 2){gotoxy(x, _y);printf("■");}}}//打印蛇头snake.len=3;snake.x[0]=width/2;snake.y[0]=hight/2;gotoxy(snake.x[0],snake.y[0]);printf("■");//打印蛇身for(int i=1;i<snake.len;i++){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1]+1;gotoxy(snake.x[i],snake.y[i]);printf("■");}//初始化食物的位置food.x=20;food.y=20;gotoxy(food.x,food.y);printf("○");}/**控制台按键所代表的数字*“↑”:72*“↓”:80*“←”:75*“→”:77*/void snake_move()//按键处理函数{int history_key=key;if (_kbhit()){fflush(stdin);key = _getch();key = _getch();}if(changeflag==1)//还没吃到食物,把尾巴擦掉{gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);printf(" ");}for(int i=snake.len-1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];}if(history_key==72&&key==80)key=72;if(history_key==80&&key==72)key=80;if(history_key==75&&key==77)key=75;if(history_key==77&&key==75)key=77;switch(key){case 72:snake.y[0]--;break;case 75:snake.x[0]-= 2;break;case 77:snake.x[0]+= 2;break;case 80:snake.y[0]++;break;}gotoxy(snake.x[0],snake.y[0]);printf("■");gotoxy(0,0);changeflag=1;}void creatfood(){if(snake.x[0] == food.x && snake.y[0] == food.y)//只有蛇吃到食物,才能生成新食物{changeflag=0;snake.len++;if(speed<=100)speed+=10;while(1){srand((unsigned int) time(NULL));food.x=rand()%(width-6)+2;//限定食物的x范围不超出围墙,但不能保证food.x 为偶数food.y=rand()%(hight-2)+1;for(int i=0;i<snake.len;i++){if(food.x==snake.x[i]&&food.y==snake.y[i])//如果产生的食物与蛇身重合则退出break;}if(food.x%2==0)break;//符合要求,退出循环}gotoxy(food.x,food.y);printf("○");}}bool Gameover(){//碰到围墙,OVERif(snake.x[0]==0||snake.x[0]==width-2)return false;if(snake.y[0]==0||snake.y[0]==hight-1) return false;//蛇身达到最长,被迫OVERif(snake.len==SNAKESIZE)return false;//头碰到蛇身,OVERfor(int i=1;i<snake.len;i++){if(snake.x[0]==snake.x[i]&&snake.y[0]==snake.y[i])return false;}return true;}int main(){system("mode con cols=60 lines=27");drawmap();while(Gameover()){snake_move();creatfood();Sleep(350-speed);//蛇的移动速度}return 0;}。
C语言课程设计报告——贪吃蛇源程序
![C语言课程设计报告——贪吃蛇源程序](https://img.taocdn.com/s3/m/9c213114c950ad02de80d4d8d15abe23482f038f.png)
C 语言课程设计(小游戏贪吃蛇得程序设计报告)设计人:班级:201年月号目录一:概述1:研究背景及意义2:设计得任务与需要知识点3:具体完成设计内容二:需求分析1:功能需求2:操作方法三:总体设计1:模块划分2:数据结构设计四:详细设计1:主空摸块设计2:绘制游戏界面3:游戏得具体过程4:游戏得结束处理5:显示排行榜信息模块五:程序得调试与测试1:动画与音乐同步2:蛇得运行3:终止程序六:结论七::结束语八:程序清单九:参考文献一. 概述本课程设计以软件工程方法为指导,采用了结构化,模块化得程序设计方法,以C语言技术为基础,使用TurboC++3、0为主要开发工具,对贪吃蛇游戏进行了需求分析,总体设计,详细设计,最终完成系统得实现与测试。
1、1 研究得背景及意义随着社会得发展,人们生活得节奏日益加快,越来越多得人加入了全球化得世界。
人们不再拘泥与一小块天地,加班,出差成了现代人不可避免得公务。
而此时一款可以随时随地娱乐得游戏成为了人们得需要。
此次课程设计完成得贪吃蛇小游戏,正就是为了满足上述需求而设计出来得。
贪吃蛇游戏虽小,却设计诸多得知识点。
通过开发贪吃蛇游戏系统,可使读者初步了解使用软件工程得与那个发,技术与工具开发软件得过程,进一步掌握结构化,模块化得程序设计方法与步骤,进一步掌握总体数据结构设计,模块划分方法,掌握局部变量,全局变量,结构体,共用体,数组,指针,文件等数据结构得使用方法,掌握图形,声音,随机数等多种库函数得使用方法,学习动画,音乐,窗口,菜单,键盘等多项编程技术,进一步学会软件调试,测试,组装等软件测试方法,为后续课程得学习与将来实际软件开发打下坚实得基础。
1、2设计得任务与需要得知识点1、2、1 课程设计主要完成得任务1)、通过编写“贪吃蛇游戏”程序,掌握结构化,模块块化程序设计得思想,培养解决实际问题得能力。
2)有同步播放动画,声音效果。
3)设计好数组元素与蛇,食物得对应关系。
贪吃蛇C语言编码
![贪吃蛇C语言编码](https://img.taocdn.com/s3/m/76ef784dfe4733687e21aaf6.png)
#include<bios.h>#include<conio.h>#include<stdio.h>#include<process.h>#include<stdlib.h>#define MAX_LENGTH 100#define LEFT 0x4b00#define RIGHT 0x4d00#define UP 0x4800#define DOWN 0x5000#define PAUSE 0x1970 /* p key */#define ESC 0x11b#define TIME_LIMIT 4 /*Òƶ¯ËÙ¶È*/#define SNAKE 2#define WALL 1#define WCOLOR WHITE#define BKCOLOR BLACK#define SCOLOR WHITE#define FOOD 15#define FCOLOR RED#define X 20#define Y 2struct snake{int x;int y;}snk[MAX_LENGTH];int bkey;int box[22][22];int begin ,end,where_x,where_y,eat;int direction = 0,dead=0,pre_direction;void initbox(){int i,j;for(i=0;i<22;i++)for(j=0;j<22;j++)box[i][j]=0;for(i=0;i<22;i++)box[i][21] = box[21][i] = box[0][i] = box[i][0] = 1;textbackground(BKCOLOR);textcolor(WCOLOR);for(i=0;i<22;i++)for(j=0;j<22;j++)if( box[i][j]==1 ) { gotoxy(X+i,Y+j); putch(W ALL); }}int timeover(){static long int tm1,tm2;tm1=biostime(0,0l);if(tm1 - tm2 >TIME_LIMIT ) { tm2 = tm1; return 1;}return 0;}void go_ahead(){int mid = begin;if(direction == 0 )return;if(begin == 0 ) begin = MAX_LENGTH-1;else begin -= 1;if(direction == UP) { snk[begin].x = snk[mid].x ; snk[begin].y = snk[mid].y-1; }else if(direction == DOWN ){snk[begin].x = snk[mid].x ; snk[begin].y = snk[mid].y+1; } else if(direction == LEFT){snk[begin].x = snk[mid].x-1 ; snk[begin].y = snk[mid].y; } else if(direction == RIGHT){snk[begin].x = snk[mid].x+1 ; snk[begin].y = snk[mid].y; } if(box[snk[begin].x][snk[begin].y] == 1) dead=1;gotoxy(X+snk[begin].x,Y+snk[begin].y);textcolor(SCOLOR);putch(SNAKE);box[snk[begin].x][snk[begin].y] = 1;if(eat != 1){ gotoxy(X+snk[end].x,Y+snk[end].y);textcolor(BKCOLOR);putch('');if(end == 0) end = MAX_LENGTH-1;else end -= 1;box[snk[end].x][snk[end].y] = 0;}}void do_action(){switch(bkey){case UP : if(direction != DOWN) direction = UP; break;case DOWN : if(direction != UP ) direction = DOWN; break;case LEFT : if(direction != RIGHT) direction = LEFT; break;case RIGHT: if(direction != LEFT ) direction = RIGHT; break;case PAUSE : getch();bkey = direction; break;case ESC : exit(0);break;default : return;}}do_eat(){if((snk[begin].x == where_x) && (snk[begin].y == where_y) ) {eat = 1;if(begin == 0 ) begin = MAX_LENGTH -1 ;else begin -= 1;snk[begin].x = where_x ;snk[begin].y = where_y;}}void putfood(){do{where_x = random(20)+1;where_y = random(20)+1;}while(box[where_x][where_y]==1);textcolor(FCOLOR);gotoxy(X+where_x,Y+where_y);putch(FOOD);}main(){do{dead= 0;bkey= 0;direction = 0;clrscr();textcolor(WHITE);gotoxy(1,1);printf("press direction keys for start\n");printf("press p for PAUSE\n");printf("press ESC for EXIT\n ");initbox();begin = end = MAX_LENGTH-1;box[9][9] = 0;snk[begin].x = 9;snk[begin].y = 9;gotoxy(X+snk[begin].x,Y+snk[begin].y);textcolor(SCOLOR);putch(SNAKE);putfood();eat = 0;do{ do_eat();if(eat == 1 ) { putfood(); eat = 0; }while( !timeover() )if( bioskey(1) )bkey = bioskey(0);do_action();go_ahead();}while( !dead );textcolor(RED);gotoxy(1,24);printf("dou you want to try again y/n");do { bkey = bioskey(0); }while(bkey!=0x1579 && bkey!=0x316e); if(bkey == 0x316e )break;}while(1);}。
贪吃蛇c语言代码
![贪吃蛇c语言代码](https://img.taocdn.com/s3/m/0058361290c69ec3d5bb75f8.png)
#include<stdio.h>#include<stdlib.h>#include<Windows.h>#include<conio.h>#include<time.h>char gamemap[20][40];//游戏地图大小20*40 int score=0;//当前分数//记录蛇的结点int x[800];//每个结点的行编号int y[800];//每个结点的列编号int len = 0;//蛇的长度//记录水果信息int fx=;//食物的横坐标int fy=00;//食物的纵坐标int fcount=0;//食物的数目//主要函数操作void createfood();//生成食物void PrintgameMap(int x[],int y[]);//画游戏地图void move(int x[],int y[]);//移动蛇int main(){srand(time(NULL));//初始化蛇头和身体的位置,默认刚开始蛇长为2 x[len] = 9;y[len] = 9;len++;x[len] = 9;y[len] = 8;len++;createfood();PrintgameMap(x,y);move(x,y);return 0;}void createfood(){if(0==fcount){int tfx=rand()%18+1;int tfy=rand()%38+1;int i,j;int have=0;//为0表示食物不是食物的一部分for(i=0;i<len;i++){for(j=0;j<len;j++){if(x[i]==fx&&y[j]==fy){have=1;break;}else{have=0;}}if(1==have)//若为蛇的一部分,执行下一次循环{continue;}else//否则生成新的水果{fcount++;fx=tfx;fy=tfy;break;}}}}//游戏地图void PrintgameMap(int x[],int y[]){int snake = 0,food=0;int i, j;//画游戏地图,并画出蛇的初始位置for (i = 0; i < 20; i++){for (j = 0; j < 40; j++){if (i == 0 && j >= 1 && j <= 38){gamemap[i][j] = '=';}else if (i == 19 && j >= 1 && j <= 38){gamemap[i][j] = '=';}else if (j == 0 || j == 39){gamemap[i][j] = '#';}else{gamemap[i][j] = ' ';}//判断蛇是否在当前位置int k;for ( k = 0; k < len; k++){if (i == x[k]&&j == y[k]){snake = 1;break;}else{snake = 0;}}{if(fcount&&fx==i&&fy==j){food=1;}else{food=0;}}//若蛇在当前位置if (1==snake ){printf("*");}else if(1==food){printf("f");}//若蛇不在当前位置并且当前位置没有水果else{printf("%c", gamemap[i][j]);}}printf("\n");}printf("score:%d",score);}//移动void move(int x[],int y[]){char s;s=getch();int move=0,beat=0;while (1){int cx[800];int cy[800];memcpy(cx, x, sizeof(int)*len); memcpy(cy, y, sizeof(int)*len); //头if (s=='w'){x[0]--;move=1;if(x[0]<=0){printf("Game over\n"); break;}}else if (s=='s'){x[0]++;move=1;if(x[0]>=19){printf("Game over\n"); break;}}else if (s=='a'){y[0] --;move=1;if(y[0]<=0){printf("Game over\n");break;}}else if (s=='d'){y[0]++;move=1;if(y[0]>=39){printf("Game over\n");break;}}//身体int i;for ( i = 1; i < len; i++){x[i] = cx[i - 1];y[i] = cy[i - 1];}for(i=1;i<len;i++)//要是咬到了自己{if(x[0]==x[i]&&y[0]==y[i]){beat=1;}else{beat=0;}}if(1==beat){printf("Game over\n");break;}if(1==move){if(fcount&&x[0]==fx&&y[0]==fy)//如果吃到了果子{//拷贝当前蛇头地址到第二个结点memcpy(x+1,cx,sizeof(int)*len); memcpy(y+1,cy,sizeof(int)*len); len++;fcount--;fx=0;fy=0;score++;createfood();}Sleep(70);system("cls");PrintgameMap( x, y);}elsecontinue;if(kbhit())//判断是否按下按键{s=getch();}}}。
c语言课程设计贪吃蛇设计
![c语言课程设计贪吃蛇设计](https://img.taocdn.com/s3/m/3045806adc36a32d7375a417866fb84ae55cc317.png)
Part Three
C语言基础知识
数据类型和变量
基本数据类型:int、float、char、double等 复合数据类型:数组、结构体、指针等 变量声明:使用关键字"int"、"float"等声明变量 变量赋值:使用"="为变量赋值 变量作用域:局部变量、全局变量等 变量生命周期:从声明到释放的过程
结构体和联合体:包括结构 体定义、结构体初始化、结 构体访问、联合体定义、联 合体初始化、联合体访问等
函数和数组
函数:C语言中的基本单元,用于实现特定功能
数组:C语言中的基本数据类型,用于存储一组相同类型 的数据
数组函数:如strlen()、strcpy()等,用于操作字符串
指针:C语言中的重要概念,用于指向内存地址
Part Four
贪吃蛇游戏设计
游戏逻辑设计
游戏结束:当蛇碰到边界或 自己时结束
游戏循环:不断更新蛇的位 置和方向
游戏开始:初始化蛇的位置 和方向
得分计算:根据吃到的食物 数量计算得分
游戏难度:根据得分调整游 戏难度,如增加蛇的速度或
改变食物的位置
游戏界面:设计游戏界面, 包括蛇、食物、边界等元素
Part Seven
总结和展望
课程设计收获和体会
掌握了C语言的基本语法和编 程技巧
学会了如何设计并实现一个完 整的游戏项目
提高了解决问题的能力和团队 协作能力
对游戏开发有了更深入的了解 和兴趣
C语言在游戏开发中的应用前景
游戏开发中,C语言具有高效、稳定的特点,适合开发大型游戏。 C语言具有广泛的应用领域,可以开发各种类型的游戏,如动作、冒险、策略等。 C语言具有强大的社区支持,可以找到大量的游戏开发资源和教程。 C语言在游戏开发中具有广泛的应用前景,可以开发出更多优秀的游戏作品。
贪吃蛇c语言代码
![贪吃蛇c语言代码](https://img.taocdn.com/s3/m/a550876a87c24028905fc323.png)
贪吃蛇c语言代码#include <graphics.h>#include <conio.h>#include <stdlib.h>#include <dos.h>#define NULL 0#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ESC 283#define ENTER 7181struct snake{int centerx;int centery;int newx;int newy;struct snake *next;};struct snake *head;int grade=60; /*控制速度的*******/int a,b; /* 背静遮的位置*/void *far1,*far2,*far3,*far4; /* 蛇身指针背静遮的指针虫子*/int size1,size2,size3,size4; /* **全局变量**/int ch=RIGHT; /**************存按键开始蛇的方向为RIGHT***********/int chy=RIGHT;int flag=0; /*********判断是否退出游戏**************/int control=4; /***********判断上次方向和下次方向不冲突***/int nextshow=1; /*******控制下次蛇身是否显示***************/int scenterx; /***************随即矩形中心坐标***************/int scentery;int sx; /*******在a b 未改变前得到他们的值保证随机矩形也不在此出现*******/int sy;/************************蛇身初始化**************************/void snakede(){struct snake *p1,*p2;head=p1=p2=(struct snake *)malloc(sizeof(struct snake));p1->centerx=80;p1->newx=80;p1->centery=58;p1->newy=58;p1=(struct snake *)malloc(sizeof(struct snake));p2->next=p1;p1->centerx=58;p1->newx=58;p1->centery=58;p1->newy=58;p1->next=NULL;}/*******************end*******************/void welcome() /*************游戏开始界面,可以选择速度**********/ {int key;int size;int x=240;int y=300;int f;void *buf;setfillstyle(SOLID_FILL,BLUE);bar(98,100,112,125);setfillstyle(SOLID_FILL,RED);bar(98,112,112,114);setfillstyle(SOLID_FILL,GREEN);bar(100,100,110,125);size=imagesize(98,100,112,125);buf=malloc(size);getimage(98,100,112,125,buf);cleardevice();setfillstyle(SOLID_FILL,BLUE);bar(240,300,390,325);outtextxy(193,310,"speed:");setfillstyle(SOLID_FILL,RED);bar(240,312,390,314);setcolor(YELLOW);outtextxy(240,330,"DOWN");outtextxy(390,330,"UP");outtextxy(240,360,"ENTER to start..." );outtextxy(270,200,"SNAKE");fei(220,220);feiyang(280,220);yang(340,220);putimage(x,y,buf,COPY_PUT);setcolor(RED);rectangle(170,190,410,410);while(1){ if(bioskey(1)) /********8选择速度部分************/ key=bioskey(0);switch(key){case ENTER:f=1;break;case DOWN:if(x>=240){ putimage(x-=2,y,buf,COPY_PUT);grade++;key=0;}case UP:if(x<=375){ putimage(x+=2,y,buf,COPY_PUT);grade--;key=0;break;}}if (f==1)break;} /********** end ****************/ free(buf);}/*************************随即矩形*****************//***********当nextshow 为1的时候才调用此函数**********/void ran(){ int nx;int ny;int show; /**********控制是否显示***********/int jump=0;struct snake *p;p=head;if(nextshow==1) /***********是否开始随机产生***************/{show=1;randomize();nx=random(14);ny=random(14);scenterx=nx*22+58;scentery=ny*22+58;while(p!=NULL){if(scenterx==p->centerx&&scentery==p->centery||scenterx==sx&&scentery==sy) {show=0;jump=1;break;}elsep=p->next;if(jump==1)break;}if(show==1){putimage(scenterx-11,scentery-11,far3,COPY_PUT);nextshow=0;break;}}}/***********过关动画**************/ void donghua(){ int i;cleardevice();setbkcolor(BLACK);randomize();while(1){for(i=0;i<=5;i++){putpixel(random(640),random(80),13); putpixel(random(640),random(80)+80,2); putpixel(random(640),random(80)+160,3); putpixel(random(640),random(80)+240,4); putpixel(random(640),random(80)+320,1); putpixel(random(640),random(80)+400,14); }setcolor(YELLOW);settextstyle(0,0,4);outtextxy(130,200,"Wonderful!!"); setfillstyle(SOLID_FILL,10);bar(240,398,375,420);feiyang(300,400);fei(250,400);yang(350,400);if(bioskey(1))if(bioskey(0)==ESC){flag=1;break;}}}/*************************end************************//***********************初始化图形系统*********************/ void init(){int a=DETECT,b;int i,j;initgraph(&a,&b,"");}/***************************end****************************/ /***画立体边框效果函数******/void tline(int x1,int y1,int x2,int y2,int white,int black){ setcolor(white);line(x1,y1,x2,y1);line(x1,y1,x1,y2);setcolor(black);line(x2,y1,x2,y2);line(x1,y2,x2,y2);}/****end*********//*************飞洋标志**********/int feiyang(int x,int y){int feiyang[18][18]={ {0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,0}, {0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0},{0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,0,1,1,1,1,1,0,0,1,0,0,1,1,0,0,0,0},{0,0,1,1,1,0,0,0,0,1,0,1,1,1,0,0,0,0},{0,0,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0},{0,0,0,1,1,1,0,1,1,1,1,1,0,0,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (feiyang[i][j]==1)putpixel(j+x,i+y,RED);}}/********"飞"字*************/int fei(int x,int y){int fei[18][18]={{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (fei[i][j]==1)putpixel(j+x,i+y,BLUE);}}/*********"洋"字**************/int yang(int x,int y){int yang[18][18]={{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, {1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0},{0,1,1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0},{0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0},{0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0},{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (yang[i][j]==1)putpixel(j+x,i+y,BLUE);}}/******************主场景**********************/ int bort(){ int a;setfillstyle(SOLID_FILL,15);bar(49,49,71,71);setfillstyle(SOLID_FILL,BLUE);bar(50,50,70,70);size1=imagesize(49,49,71,71);far1=(void *)malloc(size1);getimage(49,49,71,71,far1);cleardevice();setfillstyle(SOLID_FILL,12);bar(49,49,71,71);size2=imagesize(49,49,71,71);far2=(void *)malloc(size2);getimage(49,49,71,71,far2);setfillstyle(SOLID_FILL,12);bar(49,49,71,71);setfillstyle(SOLID_FILL,GREEN);bar(50,50,70,70);size3=imagesize(49,49,71,71);far3=(void *)malloc(size3);getimage(49,49,71,71,far3);cleardevice(); /*取蛇身节点背景节点虫子节点end*/ setbkcolor(8);setfillstyle(SOLID_FILL,GREEN);bar(21,23,600,450);tline(21,23,600,450,15,8); /***开始游戏场景边框立体效果*******/ tline(23,25,598,448,15,8);tline(45,45,379,379,8,15);tline(43,43,381,381,8,15);tline(390,43,580,430,8,15);tline(392,45,578,428,8,15);tline(412,65,462,85,15,8);tline(410,63,464,87,15,8);tline(410,92,555,390,15,8);tline(412,94,553,388,15,8);tline(431,397,540,420,15,8);tline(429,395,542,422,15,8);tline(46,386,377,428,8,15);tline(44,384,379,430,8,15);setcolor(8);outtextxy(429,109,"press ENTER ");outtextxy(429,129,"---to start"); /*键盘控制说明*/outtextxy(429,169,"press ESC ");outtextxy(429,189,"---to quiet");outtextxy(469,249,"UP");outtextxy(429,289,"LEFT");outtextxy(465,329,"DOWN");outtextxy(509,289,"RIGHT");setcolor(15);outtextxy(425,105,"press ENTER ");outtextxy(425,125,"---to start");outtextxy(425,165,"press ESC ");outtextxy(425,185,"---to quiet");outtextxy(465,245,"UP");outtextxy(425,285,"LEFT");outtextxy(461,325,"DOWN");outtextxy(505,285,"RIGHT"); /*******end*************/ setcolor(8);outtextxy(411,52,"score");outtextxy(514,52,"left");setcolor(15);outtextxy(407,48,"score");outtextxy(510,48,"left");size4=imagesize(409,62,465,88); /****分数框放到内存********/ far4=(void *)malloc(size4);getimage(409,62,465,88,far4);putimage(500,62,far4,COPY_PUT); /*******输出生命框***********/setfillstyle(SOLID_FILL,12);setcolor(RED);outtextxy(415,70,"0"); /***************输入分数为零**********/outtextxy(512,70,"20"); /*************显示还要吃的虫子的数目*********/ bar(46,46,378,378);feiyang(475,400);fei(450,400);yang(500,400);outtextxy(58,390,"mailto:");outtextxy(58,410,"snake game");outtextxy(200,410,"made by yefeng");while(1){ if(bioskey(1))a=bioskey(0);if(a==ENTER)break;}}/******************gameover()******************/void gameover(){ char *p="GAME OVER";int cha;setcolor(YELLOW);settextstyle(0,0,6);outtextxy(100,200,p);while(1){if(bioskey(1))cha=bioskey(0);if(cha==ESC){flag=1;break;}}}/***********显示蛇身**********************/void snakepaint(){struct snake *p1;p1=head;putimage(a-11,b-11,far2,COPY_PUT);while(p1!=NULL){putimage(p1->newx-11,p1->newy-11,far1,COPY_PUT);p1=p1->next;}}/****************end**********************//*********************蛇身刷新变化游戏关键部分*******************/ void snakechange(){struct snake *p1,*p2,*p3,*p4,*p5;int i,j;static int n=0;static int score;static int left=20;char sscore[5];char sleft[1];p2=p1=head;while(p1!=NULL){ p1=p1->next;if(p1->next==NULL){a=p1->newx;b=p1->newy; /************记录最后节点的坐标************/ sx=a;sy=b;}p1->newx=p2->centerx;p1->newy=p2->centery;p2=p1;}p1=head;while(p1!=NULL){p1->centerx=p1->newx;p1->centery=p1->newy;p1=p1->next;}/********判断按键方向*******/if(bioskey(1)){ ch=bioskey(0);if(ch!=RIGHT&&ch!=LEFT&&ch!=UP&&ch!=DOWN&&ch!=ESC) /********chy为上一次的方向*********/ch=chy;}switch(ch){case LEFT: if(control!=4){head->newx=head->newx-22;head->centerx=head->newx;control=2;if(head->newx<47)gameover();}else{ head->newx=head->newx+22;head->centerx=head->newx;control=4;if(head->newx>377)gameover();}chy=ch;break;case DOWN:if(control!=1){ head->newy=head->newy+22;head->centery=head->newy; control=3;if(head->newy>377)gameover();}else{ head->newy=head->newy-22; head->centery=head->newy;control=1;if(head->newy<47)gameover();}chy=ch;break;case RIGHT: if(control!=2){ head->newx=head->newx+22;head->centerx=head->newx;control=4;if(head->newx>377)gameover();}else{ head->newx=head->newx-22;head->centerx=head->newx;control=2;if(head->newx<47)gameover();}chy=ch;break;case UP: if(control!=3){ head->newy=head->newy-22;head->centery=head->newy;control=1;if(head->newy<47)gameover();}else{ head->newy=head->newy+22;head->centery=head->newy;control=3;if(head->newy>377)gameover();}chy=ch;break;case ESC:flag=1;break;}/* if 判断是否吃蛇*/if(flag!=1){ if(head->newx==scenterx&&head->newy==scentery){ p3=head;while(p3!=NULL){ p4=p3;p3=p3->next;}p3=(struct snake *)malloc(sizeof(struct snake));p4->next=p3;p3->centerx=a;p3->newx=a;p3->centery=b;p3->newy=b;p3->next=NULL;a=500;b=500;putimage(409,62,far4,COPY_PUT); /********** 分数框挡住**************/ putimage(500,62,far4,COPY_PUT); /*********把以前的剩下虫子的框挡住********/ score=(++n)*100;left--;itoa(score,sscore,10);itoa(left,sleft,10);setcolor(RED);outtextxy(415,70,sscore);outtextxy(512,70,sleft);nextshow=1;if(left==0) /************判断是否过关**********/donghua(); /*******如果过关,播放过关动画*********************/}p5=head; /*********************判断是否自杀***************************/ p5=p5->next;p5=p5->next;p5=p5->next;p5=p5->next; /****从第五个节点判断是否自杀************/while(p5!=NULL){if(head->newx==p5->centerx&&head->newy==p5->centery){ gameover();break;}elsep5=p5->next;}}}/************snakechange()函数结束*******************//*****************************主函数******************************************/ int main(){ int i;init(); /**********初始化图形系统**********/ welcome(); /*********8欢迎界面**************/ bort(); /*********主场景***************/ snakede(); /**********连表初始化**********/ while(1){ snakechange();if(flag==1)break;snakepaint();ran();for(i=0;i<=grade;i++)delay(3000);}free(far1);free(far2);free(far3);free(far4);closegraph();return 0;}。
c程序课程设计贪吃蛇
![c程序课程设计贪吃蛇](https://img.taocdn.com/s3/m/34dc861a68eae009581b6bd97f1922791788be7c.png)
c程序课程设计贪吃蛇一、教学目标本章节的教学目标是让学生掌握C程序设计的基本知识,学会使用C语言编写简单的程序,并通过编写“贪吃蛇”游戏,提高学生的编程能力和逻辑思维能力。
1.掌握C语言的基本语法和数据类型。
2.学会使用C语言进行控制结构和函数的编写。
3.了解C语言的面向对象编程思想。
4.能够使用C语言编写简单的程序。
5.能够运用面向对象编程思想进行程序设计。
6.能够独立完成“贪吃蛇”游戏的编写。
情感态度价值观目标:1.培养学生对计算机编程的兴趣和热情。
2.培养学生解决问题的能力和团队协作精神。
3.培养学生遵守编程规范和道德准则的意识。
二、教学内容本章节的教学内容主要包括C语言的基本语法、数据类型、控制结构、函数和面向对象编程思想。
通过编写“贪吃蛇”游戏,让学生将这些知识点运用到实际编程中。
1.C语言的基本语法和数据类型。
2.控制结构的使用和面向对象编程思想。
3.函数的编写和调用。
4.“贪吃蛇”游戏的编写和调试。
教材章节:《C程序设计原理与应用》(第1章至第4章)三、教学方法本章节的教学方法采用讲授法、案例分析法和实验法相结合的方式。
1.讲授法:通过讲解C语言的基本语法、数据类型、控制结构和函数等内容,使学生掌握C语言的基础知识。
2.案例分析法:通过分析“贪吃蛇”游戏的编程案例,使学生了解如何将所学知识点运用到实际编程中。
3.实验法:让学生动手编写“贪吃蛇”游戏,提高学生的编程能力和解决问题的能力。
四、教学资源本章节的教学资源包括教材、参考书、多媒体资料和实验设备。
1.教材:《C程序设计原理与应用》2.参考书:《C语言程序设计》3.多媒体资料:C语言编程教程视频4.实验设备:计算机、编程软件(如Visual Studio、Code::Blocks等)五、教学评估本章节的评估方式包括平时表现、作业和考试三个方面,以保证评估的客观性和公正性,全面反映学生的学习成果。
1.平时表现:通过课堂参与、提问、小组讨论等方式评估学生的学习态度和积极性。
C语言贪吃蛇
![C语言贪吃蛇](https://img.taocdn.com/s3/m/79d4179702d276a200292ef8.png)
printf("│ │ │\n");
{
food();
exist_food=1;
}
if(x[0]==xi&&y[0]==yi)
{
exist_food=0;
score[3]++;
case 27:
return false;
}
if(x[0]>60)
x[0]=2;
if(x[0]<2)
x[0]=60;
if(y[0]<1)
printf("│ │ │\n");
printf(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
void InitSnake()
{
x[0]=20;
y[0]=10;
score[3]=0;
len=1;
exist_food=0;
}
void food() // 生成食物 x坐标为偶数
{
int i;
fag=0;
srand((unsigned)time( NULL ));
break;
case 'a': // 纵坐标之间的距离大约是横坐标之间的距离的两倍
x[0]-=2;
break;
case 'd':
x[0]+=2;
break;
SetConsoleCursorInfo(hOut,&cci);
DOS简易版C语言贪吃蛇
![DOS简易版C语言贪吃蛇](https://img.taocdn.com/s3/m/d1b62d186d85ec3a87c24028915f804d2b1687cc.png)
DOS简易版C语⾔贪吃蛇本⽂实例为⼤家分享了C语⾔实现贪吃蛇的具体代码,供⼤家参考,具体内容如下#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <time.h>#include <windows.h>#define WALL_LENGTH 22#define LEFT 0x4b#define RIGHT 0x4d#define DOWN 0x50#define UP 0x48struct Snakes{int x;int y;struct Snakes *prev;struct Snakes *next;};struct Food{int x;int y;};struct Snakes *header;struct Snakes *tailer;struct Food *food;int wall[WALL_LENGTH][WALL_LENGTH];int direction = RIGHT;/**/void init();void draw();void move();void doMove(int x1, int y1);void eat();void keydown();void foods();int isOver();int isDrawSnake(int x, int y);int isDrawFood(int x, int y);int main(){init();while(1){if(isOver()){break;}move();eat();draw();_sleep( 100 );keydown();}printf("GAME OVER!");system("pause");}void init(){int y, x;for(y=0; y < WALL_LENGTH; y++){for(x=0; x < WALL_LENGTH; x++){if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1){ wall[y][x] = 1;}}}header=(struct Snakes *)malloc(sizeof(struct Snakes));header->x=10;header->y=10;header->prev=NULL;tailer=(struct Snakes *)malloc(sizeof(struct Snakes));tailer->x=9;tailer->y=10;tailer->next=NULL;tailer->prev=header;header->next=tailer;foods();}void draw(){int y, x;system("cls");for(y=0; y < WALL_LENGTH; y++){for(x=0; x < WALL_LENGTH; x++){if(wall[y][x] == 1){printf("[]");}else if(isDrawSnake(x, y)){printf("[]");}else if(isDrawFood(x, y)){printf("()");}else{printf(" ");}}printf("\n");}}void move(){switch(direction){case LEFT :doMove(-1, 0);break;case RIGHT :doMove(1, 0);break;case UP :doMove(0, -1);break;case DOWN :break;}}void doMove(int x1, int y1){struct Snakes *temp_tailer = tailer->prev;tailer->x=header->x + x1;tailer->y=header->y + y1;tailer->next=header;tailer->prev->next = NULL;tailer->prev = NULL;header->prev=tailer;header = tailer;tailer = temp_tailer;}void eat(){if(header->x == food->x && header->y == food->y){int x1=0, y1 =0;struct Snakes *temp_tailer = tailer;tailer=(struct Snakes *)malloc(sizeof(struct Snakes));switch(direction){case LEFT :x1 = -1;y1 = 0;break;case RIGHT :x1 = 1;y1 = 0;break;case UP :x1 = 0;y1 = -1;break;case DOWN :x1 = 0;y1 = 1;break;}tailer->x=temp_tailer->x + x1;tailer->y=temp_tailer->y + y1;tailer->next=NULL;tailer->prev=temp_tailer;temp_tailer->next = tailer;foods();}}void foods(){int y,x;struct Snakes *temp = header;_sleep(20);srand((unsigned)time(NULL));y=rand()%WALL_LENGTH;x=rand()%WALL_LENGTH;if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1 ){}do{if(temp->x == x && temp->y == y){return foods();}temp = temp->next;}while(temp != NULL);if(food == NULL){food=(struct Food *)malloc(sizeof(struct Food)); }food->x = x;food->y = y;}void keydown(){char keycode;if(_kbhit()&&(keycode =_getch())) {switch(keycode) {case LEFT:if(RIGHT!=direction) {direction=LEFT;// move();// draw();}break;case RIGHT:if(LEFT!=direction) {direction=RIGHT;// move();// draw();}break;case UP:if(DOWN!=direction) {direction=UP;// move();// draw();}break;case DOWN:if(UP!=direction){direction=DOWN;// move();// draw();}break;}}}int isDrawSnake(int x, int y){struct Snakes *temp = header;do{if(temp->x == x && temp->y == y){return 1;}temp = temp->next;}while(temp != NULL);return 0;}int isDrawFood(int x, int y){if(food->x == x && food->y == y){return 1;}return 0;}int isOver(){int x1=0, y1 =0;switch(direction){case LEFT :x1 = -1;y1 = 0;break;case RIGHT :x1 = 1;y1 = 0;break;case UP :x1 = 0;y1 = -1;break;case DOWN :x1 = 0;y1 = 1;break;}if(header->x + x1 <= 0 || header->x + x1 >= WALL_LENGTH - 1|| header->y + y1 <= 0 || header->y + y1 >= WALL_LENGTH - 1){return 1;}return 0;}好久没写过C语⾔了,随便写个贪吃蛇玩⼀玩,BUG不少,当记录了。
贪吃蛇游戏代码(C语言编写)
![贪吃蛇游戏代码(C语言编写)](https://img.taocdn.com/s3/m/0ae42d66f6ec4afe04a1b0717fd5360cba1a8d1a.png)
贪吃蛇游戏代码(C语言编写)#include "graphics.h"#include "stdio.h"#define MAX 200#define MAXX 30#define MAXY 30#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ESC 283#define ENTER 7181#define PAGEUP 18688#define PAGEDOWN 20736#define KEY_U 5749#define KEY_K 9579#define CTRL_P 6512#define TRUE 1#define FALSE 0#define GAMEINIT 1#define GAMESTART 2#define GAMEHAPPY 3#define GAMEOVER 4struct SPlace{int x;int y;int st;} place[MAX];int speed;int count;int score;int control;int head;int tear;int x,y;int babyx,babyy;int class;int eat;int game;int gamedelay[]={5000,4000,3000,2000,1000,500,250,100}; int gamedelay2[]={1000,1};static int hitme=TRUE,hit = TRUE; void init(void);void nextstatus(void);void draw(void);void init(void){int i;for(i=0;i<max;i++)< p="">{place[i].x = 0;place[i].y = 0;place[i].st = FALSE;}place[0].st = TRUE;place[1].st = TRUE;place[1].x = 1;speed = 9;count = 0;score = 0;control = 4;head = 1;tear = 0;x = 1;y = 0;babyx = rand()%MAXX;babyy = rand()%MAXY;eat = FALSE;game = GAMESTART;}void nextstatus(void){int i;int exit;int xx,yy;xx = x;yy = y;switch(control){case 1: y--; yy = y-1; break;case 2: y++; yy = y+1; break;case 3: x--; xx = x-1; break;case 4: x++; xx = x+1; break;}hit = TRUE;if ( ((control == 1) || (control ==2 )) && ( (y < 1) ||(y >= MAXY-1)) || (((control == 3) || (control == 4)) && ((x < 1) ||(x >= MAXX-1) ) ) ){}if ( (y < 0) ||(y >= MAXY) ||(x < 0) ||(x >= MAXX) ){game = GAMEOVER;control = 0;return;}for (i = 0; i < MAX; i++){if ((place[i].st) &&(x == place[i].x) &&(y == place[i].y) ){game = GAMEOVER;control = 0;return;}if ((place[i].st) &&(xx == place[i].x) &&(yy == place[i].y) ){hit = FALSE;goto OUT;}}OUT:if ( (x == babyx) && (y == babyy) ) {count ++;score += (1+class) * 10;}head ++;if (head >= MAX) head = 0;place[head].x = x;place[head].y = y;place[head].st= TRUE;if (eat == FALSE){place[tear].st = FALSE;tear ++;if (tear >= MAX) tear = 0;}else{eat = FALSE;exit = TRUE;while(exit){babyx = rand()%MAXX;babyy = rand()%MAXY;exit = FALSE;for( i = 0; i< MAX; i++ )if( (place[i].st)&&( place[i].x == babyx) && (place[i].y == babyy))exit ++;}}if (head == tear) game = GAMEHAPPY;}void draw(void){char temp[50];int i,j;for (i = 0; i < MAX; i++ ){setfillstyle(1,9);if (place[i].st)bar(place[i].x*15+1,place[i].y*10+1,place[i].x*15+14,place[i]. y*10+9);}setfillstyle(1,4);bar(babyx*15+1,babyy*10+1,babyx*15+14,babyy*10+9);setcolor(8);setfillstyle(1,8);bar(place[head].x*15+1,place[head].y*10+1,place[head].x*1 5+14,place[head].y*10+9); /* for( i = 0; i <= MAXX; i++ ) line( i*15,0, i*15, 10*MAXY);for( j = 0; j <= MAXY; j++ )line( 0, j*10, 15*MAXX, j*10); */rectangle(0,0,15*MAXX,10*MAXY);sprintf(temp,"Count: %d",count);settextstyle(1,0,2);setcolor(8);outtextxy(512,142,temp);setcolor(11);outtextxy(510,140,temp);sprintf(temp,"1P: %d",score);settextstyle(1,0,2);setcolor(8);outtextxy(512,102,temp); setcolor(12);outtextxy(510,100,temp); sprintf(temp,"Class: %d",class); setcolor(8);outtextxy(512,182,temp); setcolor(11);outtextxy(510,180,temp);}main(){int pause = 0;char temp[50];int d,m;int key;int p;static int keydown = FALSE; int exit = FALSE;int stchange = 0;d = VGA;m = VGAMED;initgraph( &d, &m, "" ); setbkcolor(3);class = 3;init();p = 1;while(!exit){if (kbhit()){key = bioskey(0);switch(key){case UP: if( (control != 2)&& !keydown)control = 1;keydown = TRUE;break;case DOWN: if( (control != 1)&& !keydown)control = 2;keydown = TRUE;break;case LEFT: if( (control != 4)&& !keydown)control = 3;keydown = TRUE;break;case RIGHT: if( (control != 3)&& !keydown)control = 4;keydown = TRUE;break;case ESC: exit = TRUE;break;case ENTER: init();break;case PAGEUP: class --; if (class<0) class = 0; break;case PAGEDOWN: class ++;if (class>7) class = 7; break;case KEY_U: if( ( (control ==1) ||(control ==2))&& !keydown) control = 3;else if(( (control == 3) || (control == 4))&& !keydown)control = 1;keydown = TRUE;break;case KEY_K: if( ( (control ==1) ||(control ==2))&& !keydown) control = 4;else if(( (control == 3) || (control == 4))&& !keydown)control = 2;keydown = TRUE;break;case CTRL_P:pause = 1 - pause; break;}}stchange ++ ;putpixel(0,0,0);if (stchange > gamedelay[class] + gamedelay2[hit]){stchange = 0;keydown = FALSE;p = 1 - p;setactivepage(p);cleardevice();if (!pause)nextstatus();else{settextstyle(1,0,4);setcolor(12);outtextxy(250,100,"PAUSE");}draw();if(game==GAMEOVER){settextstyle(0,0,6);setcolor(8);outtextxy(101,101,"GAME OVER"); setcolor(15);outtextxy(99,99,"GAME OVER"); setcolor(12);outtextxy(100,100,"GAME OVER"); sprintf(temp,"Last Count: %d",count); settextstyle(0,0,2);outtextxy(200,200,temp);}if(game==GAMEHAPPY){settextstyle(0,0,6);setcolor(12);outtextxy(100,300,"YOU WIN"); sprintf(temp,"Last Count: %d",count); settextstyle(0,0,2);outtextxy(200,200,temp);}setvisualpage(p);}}closegraph();}</max;i++)<>。
贪吃蛇c语言精简版
![贪吃蛇c语言精简版](https://img.taocdn.com/s3/m/57d50fb548d7c1c709a14581.png)
贪吃蛇c语言精简版//贪吃蛇#include<stdio.h>#include <windows.h>#include <stdlib.h>#include <time.h>#include <conio.h>struct all_xy{POINT point;struct all_xy *next;};int x=2,y=0,key,i,found_time;POINT save_point,save_point2,food_xy={20,10};BOOL end_self=FALSE,flag;struct all_xy *head=NULL,*node1,*node2;void gotoxy(int x, int y){COORD coord;coord.X = x,coord.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);}void snake_add(){for(node2=head;head!=NULL&&node2->next!=NULL;node2=node2->next);node1=(struct all_xy *)malloc(sizeof(struct all_xy));node1->point=(head==NULL?food_xy:node2->point);node1->next=NULL;if(head==NULL){head=node1;return;}else{for(node2=head;node2->next!=NULL;node2=node2->next);node2->next=node1;} }void make_food(long *x,long *y){for(found_time=0;found_time<500;found_time++){*x=(rand()%38+1)*2,*y=rand()%23+1;for(node1=head,flag=FALSE;node1!=NULL;node1=node1->next)if(*x==node1->point.x&&*y==node1->point.y){flag=TRUE;break;}if(!flag){gotoxy(*x,*y);printf("○");return;}}MessageBox(NULL,"已找不到空点放食物了,程序结束!","你太牛了!",MB_ICONASTERISK);exit(0);}int main(){for(i=0;i<3;i++)snake_add();for(srand(time(NULL)),make_food(&food_xy.x,&food_xy.y);;Sleep(400)){if(kbhit()){if((key=getch())==224)key=getch();switch(key){case 80:y!=-1?(x=0,y=1):printf("\a");break;case 72:y!=1?(x=0,y=-1):printf("\a");break;case 75:x!=2?(x=-2,y=0):printf("\a");break;case 77:x!=-2?(x=2,y=0):printf("\a");break;}}node2=head,node1=node2->next,save_point=node1->point;node1->point=node2->point,node1=node1->next;head->point.x+=x,head->point.y+=y;for(node2=head->next;node2!=NULL;node2=node2->next)//依次检查是否自己撞到自己if(node2->point.x==head->point.x&&node2->point.y==head->point.y)//如果发现头结点和任意一个结点的X Y相同则设置end_self的值并跳出{end_self=TRUE;break;}if(head->point.x<0||head->point.x>=78||head->point.y<0||head->point.y>=25| |end_self==TRUE){gotoxy(32,5);printf("游戏结束!");getch();return 0;}for(;node1!=NULL;node1=node1->next){save_point2=node1->point,node1->point=save_point;node2=node1,save_point=save_point2;}gotoxy(save_point.x,save_point.y);printf(" ");if(head->point.x==food_xy.x&&head->point.y==food_xy.y)snake_add(),make_food(&food_xy.x,&food_xy.y);for(node1=head;node1!=NULL;node1=node1->next)gotoxy(node1->point.x,node1->point.y),node1==head?printf("⊙"):printf("□");}}。
贪吃蛇-c语言课程设计源码
![贪吃蛇-c语言课程设计源码](https://img.taocdn.com/s3/m/e73b0e1bff00bed5b9f31de4.png)
贪吃蛇-c语言课程设计源码#define N 200#include <graphics.h>#include <stdlib.h>#include <dos.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bint i,key;int score=0;/*得分*/int gamespeed=50000;/*游戏速度自己调整*/ struct Food{int x;/*食物的横坐标*/int y;/*食物的纵坐标*/int yes;/*判断是否要出现食物的变量*/ }food;/*食物的结构体*/struct Snake{int x[N];int y[N];int node;/*蛇的节数*/int direction;/*蛇移动方向*/int life;/* 蛇的生命,0活着,1死亡*/ }snake;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){Init();/*图形驱动*/DrawK();/*开始画面*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/void Init(void){int gd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/ void DrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(11);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/for(i=50;i<=600;i+=10)/*画围墙*/{rectangle(i,40,i+10,49); /*上边*/rectangle(i,451,i+10,460);/*下边*/}for(i=40;i<=450;i+=10){rectangle(50,i,59,i+10); /*左边*/ rectangle(601,i,610,i+10);/*右边*/ }}。
C语言贪吃蛇文档
![C语言贪吃蛇文档](https://img.taocdn.com/s3/m/d099952717fc700abb68a98271fe910ef12dae98.png)
C语言贪吃蛇文档#includestdio.h#includebios.h#includeconio.h#includedos.h#includegraphics.h#includealloc.h#includestdlib.h#includetime.h#define LEFT 0x4B00#define RIGHT 0x4D00#define UP 0x4800#define DOWN 0x5000#define ESC 0x011B#define ENTER 0x1C0Dchar s1_title[]=Snake game;char s1_choose[3][11]={start game,author,exit};char s2_title[]=Snake game,made by HungryAnt!; char s2_fail[]=Game over!;char s2_win[]=You win;int s1_x=320-45;int s1_y=240-33;int s2_x=320-150;int s2_y=240-150;int s3_x=320-120;int s3_y=240-50;char map[30][30];空地0,蛇身1,食物2int length;int direction=2;上,下,左,右0,1,2,3int delay_time=20;延时,单位10毫秒int difficult=0;游戏难度int game_out=0;struct snake{蛇结构体char x;struct snake previous;前struct snake next;}head,tail;void initsnake(){蛇初始化struct snake sn;head=sn=(struct snake )malloc(sizeof(struct snake));蛇头sn-x=14;sn-y=15;map[15][14]=1;sn-previous=NULL;sn-next=(struct snake )malloc(sizeof(struct snake));sn-next-previous=sn;sn=sn-next;sn-x=15;sn-y=15;map[15][15]=1;sn-next=(struct snake )malloc(sizeof(struct snake));sn-next-previous=sn;sn=sn-next;sn-x=16;sn-y=15;map[15][16]=1;sn-next=NULL;tail=sn;蛇尾}void barbox(int x,int y,int color,int width,int height){填充一定范围的函数setfillstyle(SOLID_FILL,color);bar(x,y,x+width-1,y+height-1);}void box(int x,int y,int color){填充地图小方格函数barbox(x10+1,y10+1,color,9,9);}int choose(){在s1窗口里的选择框里进行选择int key,i=0,j;do{j=i;barbox(1,i(240-s1_y)23+1,LIGHTGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条setcolor(BLUE);outtextxy(4,i(240-s1_y)23+9,s1_choose);while(bioskey(1)==0);key=bioskey(0);switch(key){case UPif(i0)j=i--;break;case DOWNif(i2)j=i++;break;case ESCexit(0);break;case ENTERreturn i;}if(j!=i){barbox(1,j(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条setcolor(WHITE);outtextxy(3,j(240-s1_y)23+8,s1_choose[j]);}}while(1);}void s1_window(){进入程序的第一个界面--中间的窗口int i=0;setviewport(s1_x,s1_y,640-s1_x-1,480-s1_y-1,0);setcolor(LIGHTBLUE);画两个连在一起的框架rectangle(-1,-26,(320-s1_x)2,-1); 标题部分rectangle(-1,-1,(320-s1_x)2,(240-s1_y)2+1);选择部分barbox(0,-25,BLUE,(320-s1_x)2,24);设置标题框架填充settextstyle(0,0,1);setcolor(WHITE);outtextxy(5,-17,s1_title);输出标题while(i3){barbox(1,i(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置深灰色的条i++;}}void s2_window(){第二个界面--游戏界面int x,y;setviewport(s2_x,s2_y,640-s2_x-1,480-s2_y-1,0);clearviewport();setcolor(LIGHTBLUE);画两个连在一起的框架rectangle(-1,-30,(320-s2_x)2+1,-1); 分值框架barbox(0,-29,BLUE,(320-s2_x)2+1,28);设置分值框架填充settextstyle(0,0,1);setcolor(WHITE);outtextxy(5,-17,s2_title);输出标题setcolor(LIGHTGRAY);rectangle(-1,0,(320-s2_x)2+1,(240-s2_y)2+1);地图框架rectangle(0,0,(320-s2_x)2,(240-s2_y)2);for(y=0;y300;y+=10)地图绘制for(x=0;x300;x+=10)barbox(x+1,y+1,DARKGRAY,9,9);}void initmap(){初始化地图int x,y;for(y=0;y30;y++)for(x=0;x30;x++)map[y][x]=0;}void setfood(){随机产生一个食物int x,y;do{x=rand()%30;y=rand()%30;}while(map[y][x]==1);map[y][x]=2;box(x,y,YELLOW);}struct snake sn=head;box(sn-x,sn-y,LIGHTBLUE);sn=head-next;while(sn!=NULL){box(sn-x,sn-y,LIGHTGREEN);sn=sn-next;}}void cleartime(){时间设置为0struct time time_0;time_0.ti_min=0;time_0.ti_hour=0;time_0.ti_hund=0;time_0.ti_sec=0;settime(&time_0);}void freesnake(){释放蛇所占的内存struct snake sn,sn1;sn=head;head=NULL;while(sn!=NULL){free蛇所占的内存sn1=sn-next;free(sn);sn=sn1;}}void snakemove(){蛇移动struct snake sn;sn=head;head=(struct snake )malloc(sizeof(struct snake));蛇移动一格head-x=sn-x;head-y=sn-y;switch(direction){case 0head-y--;break;case 1head-y++;break;head-x--;break;case 3head-x++;break;}if(head-x0 head-x29 head-y0 head-y29 map[head-y][head-x]==1){如果蛇超出地图或者撞到自己,游戏失败freesnake();setviewport(0,0,639,479,0);settextstyle(0,0,3);setcolor(CYAN);outtextxy(205,210,s2_fail);显示game overgetch();clearviewport();清除屏幕内容game_out=1;return;}else{head-next=sn;sn-previous=head;head-previous=NULL;if(map[head-y][head-x]==2){如果遇到食物length++;长度增加if(length==30){freesnake();setviewport(0,0,639,479,0); settextstyle(0,0,3);setcolor(WHITE);outtextxy(206,211,s2_win);显示you win setcolor(LIGHTRED);outtextxy(205,210,s2_win);显示you win getch();clearviewport();清除屏幕内容game_out=1;return;}difficult=(length-3)3;delay_time=20-difficult;setfood();再添加一个食物}else{如果没有遇到食物则清除蛇尾map[tail-y][tail-x]=0;sn=tail;tail=tail-previous;tail-next=NULL;map[sn-y][sn-x]=0;box(sn-x,sn-y,DARKGRAY);free(sn);map[head-y][head-x]=1;}}}void gamestart(){游戏进行中int key;char l[4];struct time t_last;setfood();while(1){barbox((320-s2_x)2-28,-29,BLUE,27,28);覆盖上次蛇长sprintf(l,%d,length);setcolor(YELLOW);outtextxy((320-s2_x)2-28,-17,l);输出蛇长printsnake();cleartime();key=0;while(1){gettime(&t_last);if(t_last.ti_hunddelay_time)break;如果超过延时时间就退出if(bioskey(1)){key=bioskey(0);switch(key){case UPif(direction!=1)direction=0;break;case DOWNif(direction!=0)direction=1;break;case LEFTif(direction!=3)direction=2;break;if(direction!=2)direction=3;break;}break;}}snakemove();if(key==ESC){freesnake();game_out=1;setviewport(0,0,639,479,0);clearviewport();return;}if(game_out==1)return;}}void s2(){游戏length=3;direction=2;game_out=0;s2_window();界面initmap();地图初始化initsnake();蛇初始化gamestart();游戏开始if(game_out==1)return;}void s3_window(){char author[]=Author HungryAnt;char email[]=E-mail ljsunlin@/doc/1b13302096.html,;charblog[]=/doc/1b13302096.html,zhongji;char QQ[]=QQ517377100;setviewport(0,0,639,479,0);首先清屏clearviewport();setviewport(s3_x,s3_y,640-s3_x-1,480-s3_y-1,0); setcolor(LIGHTBLUE);rectangle(-1,-1,(320-s3_x)2,(240-s3_y)2);个人信息barbox(0,0,BLUE,(320-s3_x)2,(240-s3_y)2);设置标题框架填充settextstyle(0,0,1);输出作者信息setcolor(BLACK);outtextxy(6,35,email);outtextxy(6,60,blog);outtextxy(6,85,QQ);setcolor(WHITE);outtextxy(5,9,author);outtextxy(5,34,email);outtextxy(5,59,blog);outtextxy(5,84,QQ);getch();setviewport(0,0,639,479,0);清屏clearviewport();}void s3(){显示作者信息s3_window();}void s1(){int i;while(1){s1_window();i=choose();switch(i){case 0s2();游戏break;case 1s3();作者break;case 2exit(0);}}}void s_detectgraph(){自定义图形检测int gdriver,gmode,errorcode;gdriver=VGA;gmode=VGAHI;initgraph(&gdriver,&gmode,);if (errorcode !=0){printf(ntttGame Snake Gamen);printf(tttAuthor HungryAntn);printf(nterrort%sn, grapherrormsg(errorcode));getch();exit(1);}}int main(){srand((unsigned) time(NULL));设置随机数不同s_detectgraph(); 图形检测s1();getch();closegraph();}本文来自CSDN博客,转载请标明出处:/doc/1b13302096.html,/lilixiong08/archiv e/2008/01/16/2047086.aspx。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
江西省南昌市2015-2016学年度第一学期期末试卷(江西师大附中使用)高三理科数学分析一、整体解读试卷紧扣教材和考试说明,从考生熟悉的基础知识入手,多角度、多层次地考查了学生的数学理性思维能力及对数学本质的理解能力,立足基础,先易后难,难易适中,强调应用,不偏不怪,达到了“考基础、考能力、考素质”的目标。
试卷所涉及的知识内容都在考试大纲的范围内,几乎覆盖了高中所学知识的全部重要内容,体现了“重点知识重点考查”的原则。
1.回归教材,注重基础试卷遵循了考查基础知识为主体的原则,尤其是考试说明中的大部分知识点均有涉及,其中应用题与抗战胜利70周年为背景,把爱国主义教育渗透到试题当中,使学生感受到了数学的育才价值,所有这些题目的设计都回归教材和中学教学实际,操作性强。
2.适当设置题目难度与区分度选择题第12题和填空题第16题以及解答题的第21题,都是综合性问题,难度较大,学生不仅要有较强的分析问题和解决问题的能力,以及扎实深厚的数学基本功,而且还要掌握必须的数学思想与方法,否则在有限的时间内,很难完成。
3.布局合理,考查全面,着重数学方法和数学思想的考察在选择题,填空题,解答题和三选一问题中,试卷均对高中数学中的重点内容进行了反复考查。
包括函数,三角函数,数列、立体几何、概率统计、解析几何、导数等几大版块问题。
这些问题都是以知识为载体,立意于能力,让数学思想方法和数学思维方式贯穿于整个试题的解答过程之中。
二、亮点试题分析1.【试卷原题】11.已知,,A B C 是单位圆上互不相同的三点,且满足AB AC →→=,则AB AC →→⋅的最小值为( )A .14-B .12-C .34-D .1-【考查方向】本题主要考查了平面向量的线性运算及向量的数量积等知识,是向量与三角的典型综合题。
解法较多,属于较难题,得分率较低。
【易错点】1.不能正确用OA ,OB ,OC 表示其它向量。
2.找不出OB 与OA 的夹角和OB 与OC 的夹角的倍数关系。
【解题思路】1.把向量用OA ,OB ,OC 表示出来。
2.把求最值问题转化为三角函数的最值求解。
【解析】设单位圆的圆心为O ,由AB AC →→=得,22()()OB OA OC OA -=-,因为1OA OB OC ===,所以有,OB OA OC OA ⋅=⋅则()()AB AC OB OA OC OA ⋅=-⋅-2OB OC OB OA OA OC OA =⋅-⋅-⋅+ 21OB OC OB OA =⋅-⋅+设OB 与OA 的夹角为α,则OB 与OC 的夹角为2α所以,cos 22cos 1AB AC αα⋅=-+2112(cos )22α=--即,AB AC ⋅的最小值为12-,故选B 。
【举一反三】【相似较难试题】【2015高考天津,理14】在等腰梯形ABCD 中,已知//,2,1,60AB DC AB BC ABC ==∠= ,动点E 和F 分别在线段BC 和DC 上,且,1,,9BE BC DF DC λλ==则AE AF ⋅的最小值为 .【试题分析】本题主要考查向量的几何运算、向量的数量积与基本不等式.运用向量的几何运算求,AE AF ,体现了数形结合的基本思想,再运用向量数量积的定义计算AE AF ⋅,体现了数学定义的运用,再利用基本不等式求最小值,体现了数学知识的综合应用能力.是思维能力与计算能力的综合体现. 【答案】2918【解析】因为1,9DF DC λ=12DC AB =,119199918CF DF DC DC DC DC AB λλλλλ--=-=-==, AE AB BE AB BC λ=+=+,19191818AF AB BC CF AB BC AB AB BC λλλλ-+=++=++=+,()221919191181818AE AF AB BC AB BC AB BC AB BCλλλλλλλλλ+++⎛⎫⎛⎫⋅=+⋅+=+++⋅⋅ ⎪ ⎪⎝⎭⎝⎭19199421cos1201818λλλλ++=⨯++⨯⨯⨯︒2117172992181818λλ=++≥+= 当且仅当2192λλ=即23λ=时AE AF ⋅的最小值为2918. 2.【试卷原题】20. (本小题满分12分)已知抛物线C 的焦点()1,0F ,其准线与x 轴的交点为K ,过点K 的直线l 与C 交于,A B 两点,点A 关于x 轴的对称点为D . (Ⅰ)证明:点F 在直线BD 上; (Ⅱ)设89FA FB →→⋅=,求BDK ∆内切圆M 的方程. 【考查方向】本题主要考查抛物线的标准方程和性质,直线与抛物线的位置关系,圆的标准方程,韦达定理,点到直线距离公式等知识,考查了解析几何设而不求和化归与转化的数学思想方法,是直线与圆锥曲线的综合问题,属于较难题。
【易错点】1.设直线l 的方程为(1)y m x =+,致使解法不严密。
2.不能正确运用韦达定理,设而不求,使得运算繁琐,最后得不到正确答案。
【解题思路】1.设出点的坐标,列出方程。
2.利用韦达定理,设而不求,简化运算过程。
3.根据圆的性质,巧用点到直线的距离公式求解。
【解析】(Ⅰ)由题可知()1,0K -,抛物线的方程为24y x =则可设直线l 的方程为1x my =-,()()()112211,,,,,A x y B x y D x y -,故214x my y x =-⎧⎨=⎩整理得2440y my -+=,故121244y y m y y +=⎧⎨=⎩则直线BD 的方程为()212221y y y y x x x x +-=--即2222144y y y x y y ⎛⎫-=- ⎪-⎝⎭令0y =,得1214y yx ==,所以()1,0F 在直线BD 上.(Ⅱ)由(Ⅰ)可知121244y y m y y +=⎧⎨=⎩,所以()()212121142x x my my m +=-+-=-,()()1211111x x my my =--= 又()111,FA x y →=-,()221,FB x y →=-故()()()21212121211584FA FB x x y y x x x x m →→⋅=--+=-++=-,则28484,93m m -=∴=±,故直线l 的方程为3430x y ++=或3430x y -+=213y y -===±,故直线BD 的方程330x -=或330x -=,又KF 为BKD ∠的平分线,故可设圆心()(),011M t t -<<,(),0M t 到直线l 及BD 的距离分别为3131,54t t +--------------10分 由313154t t +-=得19t =或9t =(舍去).故圆M 的半径为31253t r +== 所以圆M 的方程为221499x y ⎛⎫-+= ⎪⎝⎭【举一反三】【相似较难试题】【2014高考全国,22】 已知抛物线C :y 2=2px(p>0)的焦点为F ,直线y =4与y 轴的交点为P ,与C 的交点为Q ,且|QF|=54|PQ|.(1)求C 的方程;(2)过F 的直线l 与C 相交于A ,B 两点,若AB 的垂直平分线l′与C 相交于M ,N 两点,且A ,M ,B ,N 四点在同一圆上,求l 的方程.【试题分析】本题主要考查求抛物线的标准方程,直线和圆锥曲线的位置关系的应用,韦达定理,弦长公式的应用,解法及所涉及的知识和上题基本相同. 【答案】(1)y 2=4x. (2)x -y -1=0或x +y -1=0. 【解析】(1)设Q(x 0,4),代入y 2=2px ,得x 0=8p,所以|PQ|=8p ,|QF|=p 2+x 0=p 2+8p.由题设得p 2+8p =54×8p ,解得p =-2(舍去)或p =2,所以C 的方程为y 2=4x.(2)依题意知l 与坐标轴不垂直,故可设l 的方程为x =my +1(m≠0). 代入y 2=4x ,得y 2-4my -4=0. 设A(x 1,y 1),B(x 2,y 2), 则y 1+y 2=4m ,y 1y 2=-4.故线段的AB 的中点为D(2m 2+1,2m), |AB|=m 2+1|y 1-y 2|=4(m 2+1).又直线l ′的斜率为-m ,所以l ′的方程为x =-1m y +2m 2+3.将上式代入y 2=4x ,并整理得y 2+4m y -4(2m 2+3)=0.设M(x 3,y 3),N(x 4,y 4),则y 3+y 4=-4m,y 3y 4=-4(2m 2+3).故线段MN 的中点为E ⎝ ⎛⎭⎪⎫2m2+2m 2+3,-2m ,|MN|=1+1m 2|y 3-y 4|=4(m 2+1)2m 2+1m 2.由于线段MN 垂直平分线段AB ,故A ,M ,B ,N 四点在同一圆上等价于|AE|=|BE|=12|MN|,从而14|AB|2+|DE|2=14|MN|2,即 4(m 2+1)2+⎝ ⎛⎭⎪⎫2m +2m 2+⎝ ⎛⎭⎪⎫2m 2+22=4(m 2+1)2(2m 2+1)m 4,化简得m 2-1=0,解得m =1或m =-1, 故所求直线l 的方程为x -y -1=0或x +y -1=0.三、考卷比较本试卷新课标全国卷Ⅰ相比较,基本相似,具体表现在以下方面: 1. 对学生的考查要求上完全一致。
即在考查基础知识的同时,注重考查能力的原则,确立以能力立意命题的指导思想,将知识、能力和素质融为一体,全面检测考生的数学素养,既考查了考生对中学数学的基础知识、基本技能的掌握程度,又考查了对数学思想方法和数学本质的理解水平,符合考试大纲所提倡的“高考应有较高的信度、效度、必要的区分度和适当的难度”的原则. 2. 试题结构形式大体相同,即选择题12个,每题5分,填空题4 个,每题5分,解答题8个(必做题5个),其中第22,23,24题是三选一题。
题型分值完全一样。
选择题、填空题考查了复数、三角函数、简易逻辑、概率、解析几何、向量、框图、二项式定理、线性规划等知识点,大部分属于常规题型,是学生在平时训练中常见的类型.解答题中仍涵盖了数列,三角函数,立体何,解析几何,导数等重点内容。
3. 在考查范围上略有不同,如本试卷第3题,是一个积分题,尽管简单,但全国卷已经不考查了。
四、本考试卷考点分析表(考点/知识点,难易程度、分值、解题方式、易错点、是否区分度题)。