应天论坛

 找回密码
 参与我们

QQ登录

只需一步,快速开始

搜索
查看: 885|回复: 3

C语言撸了一个贪吃蛇,发出来交流下

[复制链接]

276

主题

303

帖子

3197

积分

管理员

湘南小侠客

Rank: 9Rank: 9Rank: 9

积分
3197

优质服务勋章论坛元老

QQ
发表于 2018-4-4 21:28:15 | 显示全部楼层 |阅读模式
C语言撸了一个贪吃蛇,发出来交流下

[mw_shl_code=c,true]// 项目名称:贪吃蛇
// by: 慕鸢
#include<stdio.h>
#include<graphics.h> //图形库
#include<conio.h> //键盘按键相关
#include<time.h>
#include <mmsystem.h>
 
#pragma comment(lib,"winmm.lib")
 
#define MAX 1000  //蛇的最大节数
 
//  点 坐标 的改变 int x , int y
struct COOR{
        int x;
        int y;
};
 
//上下左右四个方向
enum CH {
        up = 72,
        down = 80,
        left = 75,
        right = 77
};
 
//蛇 n节 数组
struct SNAKE {
        int n;                //蛇的节数
        struct COOR scr[MAX]; // 1000节蛇的坐标
        enum CH ch;           // 蛇的方向
}snake;
 
struct FOOD
{
        struct COOR fsr;
        int flag;//标记食物有没有被吃掉 1 0
 
}food;
 
void initGame(); //初始化地图
void draw_snake(); //画蛇
void snake_move();  //蛇移动
void change_snake_ch();  //改变蛇的方向
void food_coor(); //食物坐标
void draw_food(); //绘制食物
void eat_food(); //吃掉食物
void game_break(); //判断游戏是否结束
void game_over(); //游戏结束提示
 
//主函数
//
//
//在此
int main() {
        initGame();
        mciSendString(L"open 甩葱歌.mp3 alias scg", 0, 0, 0);
        mciSendString(L"play scg repeat", 0, 0, 0);
        while (1) {
                while (!kbhit()) //检测按键
                {
                        if (food.flag == 0) {
                                food_coor();
                        }
                        cleardevice();
                        draw_food();
                        draw_snake();
                        snake_move();
                        Sleep(100); //动一下停一百毫秒
                        eat_food();
                        game_break();
 
                }
 
                change_snake_ch();
 
        }
 
        return 0;
}
 
//游戏界面初始化
 
void initGame() {
        //创建自己的窗口
        initgraph(640, 480); //设置窗口大小 三个参数大小,前两个是长和宽,最后一个是样式.
        srand((unsigned)time(NULL));
        setbkcolor(RGB(255, 128, 128));// 背景颜色
        cleardevice(); // 刷新
 
        snake.n = 1;
        snake.scr[0].x = 0;
        snake.scr[0].y = 0;
        snake.ch = down;
 
        //rectangle(0,0,10,10);
        food.flag = 0;
}
 
//绘制蛇
void draw_snake() {
        //数组下标
        for (int i = snake.n - 1; i >= 0; i--) {
                setlinecolor(RGB(rand() % 180, rand() % 180, rand() % 180));
                rectangle(snake.scr.x, snake.scr.y, snake.scr.x + 10, snake.scr.y + 10);
 
        }
}
 
//蛇移动
void snake_move() {
        for (int i = snake.n - 1; i > 0; i--) {
                snake.scr.x = snake.scr[i - 1].x;
                snake.scr.y = snake.scr[i - 1].y;
        }
        switch (snake.ch) {
        case up:
                snake.scr[0].y -= 10;
                break;
        case down:
                snake.scr[0].y += 10;
                break;
        case left:
                snake.scr[0].x -= 10;
                break;
        case right:
                snake.scr[0].x += 10;
                break;
 
        }
}
 
//改变蛇的方向,通过键盘去控制
void change_snake_ch() {
        switch (getch()) {//获取键盘敲下的字符
        case 72:
                if (snake.ch != down) {
                        snake.ch = up;
                }
                break;
        case 80:
                if (snake.ch != down) {
                        snake.ch = down;
                }
                break;
        case 75:
                if (snake.ch != right) {
                        snake.ch = left;
                }
                break;
        case 77:
                if (snake.ch != left) {
                        snake.ch = right;
                }
                break;
        }
}
 
//食物坐标 随机
void food_coor() {
        food.fsr.x = rand() % 64 * 10;     // 0-630
        food.fsr.y = rand() % 48 * 10;
 
        food.flag = 1; //食物初始化为没有被吃掉
}
 
//绘制食物 1.28
void draw_food() {
        setfillcolor(LIGHTRED);
        solidroundrect(food.fsr.x,
                food.fsr.y,
                food.fsr.x + 10,
                food.fsr.y + 10,
                10,
                10);
}
 
//吃食物
void eat_food() {
        if (snake.scr[0].x == food.fsr.x && snake.scr[0].y == food.fsr.y) {
                snake.n++;
                food.flag = 0;
        }
}
 
//判断游戏是否结束
void game_break() {
        //撞墙
        if (snake.scr[0].x < 0 || snake.scr[0].y > 630 || snake.scr[0].y < 0 || snake.scr[0].y > 470) {
                game_over();
        }
        //头撞到身体
        for (int i = snake.n - 1; i > 0; i--) {
                if (snake.scr[0].x == snake.scr.x && snake.scr[0].y == snake.scr.y) {
                        game_over();
                }
        }
}
 
void game_over() { //游戏结束提示.
        cleardevice();
        settextcolor(LIGHTGREEN);
        settextstyle(50, 0, L"黑体");
        outtextxy(200, 200, L"Game Over");
        Sleep(3000);
        exit(0);
}[/mw_shl_code]

相关帖子

每次见你穿短裤打领带,还穿个拖鞋,下次再这样穿不要从我家门口过了!
http://gsh.yzqz.cn/CassettePlayer/index.html

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 参与我们

本版积分规则

QQ|Archiver|手机版|小黑屋|应天社区 ( 湘ICP备17015224号 )

GMT+8, 2024-12-22 18:22 , Processed in 0.078125 second(s), 31 queries .

Powered by Discuz!

© 2001-2017 Comsenz Inc.


免责声明:
本站所发布的第三方软件及资源(包括但不仅限于文字/图片/音频/视频等仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢某程序或某个资源,请支持正版软件及版权方利益,注册或购买,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To: admin@yzqz.cn

快速回复 返回顶部 返回列表