easyx C语言贪吃蛇

编程入门 行业动态 更新时间:2024-10-24 14:26:10

easyx C语言<a href=https://www.elefans.com/category/jswz/34/1769263.html style=贪吃蛇"/>

easyx C语言贪吃蛇

本程序使用VS2022编写,预先安装好easyx图形库

tip:记得设置字符集使用多字节字符集

在b站上学习并自己加以修改

在网络上下载了一些声音素材

源码程序与素材下载

链接: =wnsa 提取码: wnsa

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
#define SNAKE_NUM 500  
int score = 0;
struct Snake
{int size;int dir;int speed;POINT coor[SNAKE_NUM];
}snake;
struct Food
{int x;int y;int r;bool flag;DWORD color; 
}food;
enum Dir
{UP,DOWN,LEFT,RIGHT,
};
void GamaInit()
{	initgraph(640, 480);SetWindowText(GetHWnd(), "贪吃snake by 话说");mciSendString("play src/bgm.mp3 repeat", 0, 0, 0);snake.size = 3;snake.speed = 10;snake.dir=RIGHT;for (int i = 0; i < snake.size ; i++){snake.coor[i].x = 40 - 10*i;snake.coor[i].y = 10;} food.x = rand() % 640; food.y = rand() % 480;food.color = RGB(rand()%256, rand() % 256, rand() % 256);food.r = rand() % 10 + 5;food.flag = true;
}
void GameDraw()
{//BeginBatchDraw();setbkcolor(RGB(30, 30, 30));cleardevice();setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));for (int i = 0; i < snake.size; i++){solidcircle(snake.coor[i].x, snake.coor[i].y, 5);}if (food.flag){solidcircle(food.x, food.y, food.r);}//EndBatchDraw();
}
void snakeMove()
{for (int i = snake.size - 1; i > 0 ; i--){snake.coor[i] = snake.coor[i - 1];}switch (snake.dir){case UP: snake.coor[0].y -= snake.speed;if (snake.coor[0].y+10<=0){snake.coor[0].y = 480;}break;case DOWN: snake.coor[0].y += snake.speed;if (snake.coor[0].y - 10 >= 480){snake.coor[0].y = 0;}break;case LEFT: snake.coor[0].x -= snake.speed;if (snake.coor[0].x + 10 <= 0){snake.coor[0].x = 640;}break;case RIGHT: snake.coor[0].x += snake.speed;if (snake.coor[0].x - 10 >= 640){snake.coor[0].x = 0;}break;}
}
void keyContorl()
{if (_kbhit()){switch (_getch()){case 'w':case 'W':case 72:	if (snake.dir != DOWN){snake.dir = UP;}break;case 's':case 'S':case 80:	if (snake.dir != UP){snake.dir = DOWN;}break;case 'a':case 'A':case 75:	if (snake.dir != RIGHT){snake.dir = LEFT;}break;case 'd':case 'D':case 77:	if (snake.dir != LEFT){snake.dir = RIGHT;}break;case ' ':while (1){if (_getch() == ' ')return;}break;case 't':case 'T': snake.speed += 2;if (snake.speed >= 20)snake.speed = 20;break;case 'y':case 'Y':  snake.speed -= 2;if (snake.speed <= 5)snake.speed = 5;break;}}
}
void Eatfood()
{if (food.flag&&snake.coor[0].x>=food.x-food.r&& snake.coor[0].x <= food.x + food.r&& snake.coor[0].y >= food.y - food.r&& snake.coor[0].y <= food.y + food.r){mciSendString("play src/eat.mp3", 0, 0, 0);food.flag = false;snake.size++; score++;}if (!food.flag){food.x = rand() % 640;food.y = rand() % 480;food.color = RGB(rand() % 256, rand() % 256, rand() % 256);food.r = rand() % 10 + 5;food.flag = true;}
}
void score_write(int score)
{TCHAR s[30];TCHAR sp[30];_stprintf_s(s, _T("%d"), score);//需要先把数字进行转换_stprintf_s(sp, _T("%d"),snake.speed);//BeginBatchDraw();setbkmode(TRANSPARENT);settextcolor(RGB(255, 255, 255));settextstyle(30, 0, "楷体");outtextxy(5, 450, "你的分数:");outtextxy(150, 450, s);outtextxy(180, 450, "速度:");outtextxy(250, 450, sp);//EndBatchDraw();
}
void Gameover()
{for (int i = 1; i < snake.size; i++){if (snake.coor[0].x > snake.coor[i].x - 4 && snake.coor[0].x < snake.coor[i].x + 4  && snake.coor[0].y > snake.coor[i].y - 4 && snake.coor[0].y < snake.coor[i].y + 4){mciSendString("close src/bgm.mp3", 0, 0, 0);mciSendString("play src/dead.mp3", 0, 0, 0);while (1){	if(score<=15)outtextxy(200, 240, "牛马 按空格继续");else if(score<30)outtextxy(200, 240, "666 按空格继续");elseouttextxy(200, 240, "驭蛇大师 按空格继续");if (_getch() == ' '){snake.size = 3;score = 0;GamaInit();return; }}}}
}
int main()
{GamaInit();while (1){	snakeMove();BeginBatchDraw();GameDraw();score_write(score);EndBatchDraw();keyContorl();Eatfood();Gameover();Sleep(50);}return 0;
}

更多推荐

easyx C语言贪吃蛇

本文发布于:2024-03-06 07:41:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1714753.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:贪吃蛇   语言   easyx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!