C语言如何做出简易贪食蛇

如何使用C语言做出来简易的贪食蛇#include<stdio.h>#include<graphics.h> //easy图形库#include<conio.h> //_getch#include<stdlib.h>#include<time.h>//坐标属性typedef struct Point{ int x, y;}MYPOINT;//蛇的属性struct snake{ MYPOINT xy[100];
摘要由CSDN通过智能技术生成

如何使用C语言做出来简易的贪食蛇

#include<stdio.h>
#include<graphics.h>  //easy图形库
#include<conio.h>     //_getch
#include<stdlib.h>
#include<time.h>
//坐标属性
typedef struct Point
{
   
	int x, y;

}MYPOINT;
//蛇的属性
struct snake
{
   
	MYPOINT xy[100];          //最多长度为100
	int num;                  //节数,长度
	char position;            //方向
}snake;                    
//食物属性
struct Food
{
   
	MYPOINT  fdxy;              //食物的坐标
	int grade;                  //经验值(分数)
	int flag;                   //标记
}food;
//枚举方向
enum snakePosition{
    up,down,left,right};
//模块化设计(函数实现模块化功能)
//初始化蛇
void initSnake()
{
   
	//一开始前三节
	snake.xy[2].x = 0;
	snake.xy[2].y = 0;

	snake.xy[1].x = 10;
	snake.xy[1].y = 0;

	snake.xy[0]. x = 20;
	snake.xy[0].y = 0;

	snake.num = 3;             
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用windows api 做的 #include #include"resource.h" #include"Node.h" #include #include TCHAR szAppname[] = TEXT("Snack_eat"); #define SIDE (x_Client/80) #define x_Client 800 #define y_Client 800 #define X_MAX 800-20-SIDE //点x的范围 #define Y_MAX 800-60-SIDE //点y的范围 #define TIME_ID 1 #define SECOND 100 #define NUM_POINT 10 //点的总个数 #define ADD_SCORE 10 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; //窗口句柄 MSG msg; //消息 WNDCLASS wndclass; //窗口类 HACCEL hAccel;//加速键句柄 wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口的水平和垂直尺寸被改变时,窗口被重绘 wndclass.lpfnWndProc = WndProc; //窗口过程为WndProc函数 wndclass.cbClsExtra = 0; //预留额外空间 wndclass.cbWndExtra = 0; //预留额外空间 wndclass.hInstance = hInstance; //应用程序的实例句柄,WinMain的第一个参数 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //设置图标 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //载入预定义的鼠标指针 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //设置画刷 wndclass.lpszMenuName = szAppname; //设置菜单 wndclass.lpszClassName = szAppname; //设置窗口类的名字 if (!RegisterClass(&wndclass))//注册窗口类 { MessageBox(NULL, TEXT("这个程序需要windows NT!"), szAppname, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppname, TEXT("Snack_eat"),//CreateWindow函数调用时,WndProc将受到WM_CREATE WS_OVERLAPPEDWINDOW&~WS_THICKFRAME& ~WS_MAXIMIZEBOX,//普通的层叠窗口&禁止改变大小&禁止最大化 CW_USEDEFAULT, //初始x坐标(默认) CW_USEDEFAULT, //初始y坐标 x_Client, //初始x方向尺寸 770 y_Client, //初始y方向尺寸 750 NULL, //父窗口句柄 NULL, //窗口菜单句柄 hInstance, //程序实例句柄 WinMain函数中第二个参数 NULL); //创建参数 ShowWindow(hwnd, iCmdShow);//显示窗口,iCmdShow是WinMain的第四个参数,决定窗口在屏幕中的初始化显示形式,例:SW_SHOWNORMAL表示正常显示 UpdateWindow(hwnd);//使窗口客户区重绘,通过向WndProc发送一条WM_PAINT消息而完成的 hAccel = LoadAccelerators(hInstance, szAppname);//加载加速键 while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }/* while (GetMessage(&msg, NULL, 0, 0))//GetMessage函数从消息队列中得到消息,填充msg。如果msg.message等于WM_QUIT,返回0,否则返回非0 { TranslateMessage(&msg);//将msg返回给windows已进行某些键盘消息的转换 DispatchMessage(&msg);//将msg再次返回给windows }*/ return msg.wParam;//msg.wParam是PostQuitMessage函数的参数值,通常是0 } ...
好的,以下是C语言的代码: ``` #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <Windows.h> #define UP 72 #define LEFT 75 #define RIGHT 77 #define DOWN 80 int length=3;//初始长度为3 int point=0;//初始得分为0 int speed=150;//初始速度(毫秒) int flag=0;//检测是否死亡 int head_x,head_y,tail_x,tail_y,food_x,food_y;//头坐标,尾坐标,物坐标 int snake[30][2];//的位置,最大长度为30 char dir=RIGHT;//初始方向为向右 void init();//初始化函数 void gotoxy(int x,int y);//光标移动函数 void hideCursor();//隐藏光标 void createFood();//生成物 void showSnake();//输出的位置 void updateSnake();//更新的位置 int main() { init();//进行初始化 while (1)//游戏循环 { if (_kbhit())//判断是否有按键输入 { char c=_getch();//读取按键 dir=c;//改变方向 } updateSnake();//更新的位置 if (flag==1)//判断是否死亡 { gotoxy(30,15); printf("YOU ARE DEAD!");//输出游戏结束 break;//退出游戏循环 } Sleep(speed);//延时,控制速度 } return 0; } void init() { srand((unsigned)time(NULL));//设置随机数种子 head_x=15; head_y=15; tail_x=12; tail_y=15;//确定的初始位置 for (int i=0;i<length;i++) { snake[i][0]=tail_x+i; snake[i][1]=tail_y; }//初始化的位置 createFood();//生成物 hideCursor();//隐藏光标 showSnake();//显示的位置 } void gotoxy(int x,int y) { COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); } void hideCursor() { HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle,&CursorInfo); CursorInfo.bVisible=false; SetConsoleCursorInfo(handle,&CursorInfo); } void showSnake() { gotoxy(head_x,head_y); printf("@");//输出头 for (int i=0;i<length-1;i++) { gotoxy(snake[i][0],snake[i][1]); printf("#");//输出身 } gotoxy(tail_x,tail_y); printf("#");//输出尾 gotoxy(food_x,food_y); printf("*");//输出物 } void updateSnake() { int next_x,next_y; switch (dir)//根据方向计算下一步位置 { case UP: next_x=head_x-1; next_y=head_y; break; case DOWN: next_x=head_x+1; next_y=head_y; break; case LEFT: next_x=head_x; next_y=head_y-1; break; case RIGHT: next_x=head_x; next_y=head_y+1; break; } if (next_x==0||next_x==30||next_y==0||next_y==30) { flag=1;//检测是否撞墙死亡 return; } if (next_x==food_x&&next_y==food_y) { length++;//吃到物,长度+1 point++;//得分+1 createFood();//生成新的物 } else { gotoxy(tail_x,tail_y); printf(" ");//删除尾 for (int i=0;i<length-1;i++) { snake[i][0]=snake[i+1][0]; snake[i][1]=snake[i+1][1];//更新的位置 } } head_x=next_x; head_y=next_y;//更新头位置 for (int i=0;i<length-1;i++) { if (head_x==snake[i][0]&&head_y==snake[i][1]) { flag=1;//检测是否撞到自己死亡 return; } } snake[length-1][0]=head_x; snake[length-1][1]=head_y;//更新身位置 gotoxy(0,0); printf("Point:%d",point);//输出得分 showSnake();//显示的位置 } void createFood() { int x,y; do { x=rand()%28+1; y=rand()%28+1; } while (x==head_x&&y==head_y||x==tail_x&&y==tail_y);//生成随机位置 food_x=x; food_y=y; } ``` 希望能帮到你,如果还有其他问题,可以随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值