第一次写的游戏的代码,贪吃蛇

老师给了打印字符的代码,我看了看,写了个图标的,写了八个钟。

#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<conio.h>
#include<easyx.h>
#include<mmsystem.h>
#pragma comment (lib,"winmm.lib")
#include<time.h>
#define unit 40
#define width 600
#define length 600
IMAGE body, heada, headd, heads, headw, ground, point, speed;
int spot[width][length] = { 0 }, direction;
int pointx, pointy, speedx, speedy, headx, heady, tailx, taily;
int score = 0, win = 0;
int cnt = 0, cnt1 = 0, cnt2 = 20000, cnt3 = 40000, judge = 1;
int slow = 200, doubleit = 1;

void start()
{
    int i, j;
    mciSendString("open music.mp3", 0, 0, 0);
    mciSendString("play music.mp3", 0, 0, 0);
    initgraph(600, 600);
    loadimage(&body, "body.jpg");
    loadimage(&heada, "heada.jpg");
    loadimage(&heads, "heads.jpg");
    loadimage(&headd, "headd.jpg");
    loadimage(&ground, "ground.jpg");
    loadimage(&headw, "headw.jpg");
    loadimage(&point, "point.jpg");
    loadimage(&speed, "speed.jpg");
    headx = 5; heady = 5;
    spot[headx * unit][heady * unit] = 1;
    for (i = 1; i <= 4; i++)
    {
        spot[(heady + i) * unit][headx * unit] = 1 + i;
    }
    direction = 40001;
    do {
        pointx = rand() % 15;
        pointy = rand() % 15;
    } while (pointy == 0);
    do {
        speedx = rand() % 15;
        speedy = rand() % 15;
    } while (speedx == pointx && speedy == pointy || speedy == 0);
    spot[pointy * unit][pointx * unit] = -2;
    spot[speedy * unit][speedx * unit] = -3;
}

void update()
{
    int i, j; int max = 0;
    for (i = 0; i < 600; i += unit)
        for (j = 0; j < 600; j += unit)
        {

            if (spot[i][j] == 1) { heady = i / unit; headx = j / unit; }                   //找出头
            if (spot[i][j] > max) { max = spot[i][j]; taily = i / unit; tailx = j / unit; }//找出尾巴
            if (spot[i][j] > 0) spot[i][j] += 1;                    //全变身体
        }
    if (direction % 4 == 0) headx += 1;//换方向
    if (direction % 4 == 1) heady -= 1;
    if (direction % 4 == 2) headx -= 1;
    if (direction % 4 == 3) heady += 1;
    if (headx == -1 && direction % 4 == 2)headx = 14;
    if (headx == 15 && direction % 4 == 0)headx = 0;
    if (heady == 15 && direction % 4 == 3)heady = 0;
    if (heady == 0 && direction % 4 == 1)heady = 14;
    if (headx == pointx && heady == pointy)//吃苹果
    {
        do {
            pointx = rand() % 15;
            pointy = rand() % 15;
        } while (speedx == pointx && speedy == pointy || pointy == 0);
        spot[pointy * unit][pointx * unit] = -2;
        score += doubleit;
        judge = 0;
        cnt2 += 2000 * doubleit;
    }
    if (headx == speedx && heady == speedy)//吃加速器
    {
        do {
            speedx = rand() % 15;
            speedy = rand() % 15;
        } while (speedx == pointx && speedy == pointy || speedy == 0);
        spot[speedy * unit][speedx * unit] = -3;
        cnt1 = 3000;
    }
    if (spot[heady * unit][headx * unit] > 0)
    {
        getchar(); exit(0);
    }
    spot[heady * unit][headx * unit] = 1;//变出头坐标
    if (judge)spot[taily * unit][tailx * unit] = 0;//消除尾巴
    else judge = 1;
    if (cnt1 > 0) {
        doubleit = 2; slow = 100;
    }                                              //调速度
    else { slow = 200; doubleit = 1; }
}

void draw()
{
    int i, j;
    BeginBatchDraw();
    if (direction % 4 == 0) putimage(headx * unit, heady * unit, &headd);//direction   0-右 1-上 2-左 3-下
    if (direction % 4 == 1) putimage(headx * unit, heady * unit, &headw);//画出头
    if (direction % 4 == 2) putimage(headx * unit, heady * unit, &heada);
    if (direction % 4 == 3) putimage(headx * unit, heady * unit, &heads);
    for (i = 0; i < 600; i += unit)
        for (j = 0; j < 600; j += unit)
        {
            if (spot[i][j] == -2)putimage(j, i, &point);//画苹果
            if (spot[i][j] == -3)putimage(j, i, &speed);//画加速器
            if (spot[i][j] > 1)putimage(j, i, &body);//画身体
        }
    settextcolor(RGB(18, 255, 255));//打印文字
    settextstyle(30, 0, "华文行楷");
    char s[5]; char a[13]; char b[13]; char c[13] = { "挑战成功" }; char d[5];
    sprintf_s(a, "加速还剩%d", cnt1 / 50);
    sprintf_s(d, "%d", cnt3 / 400);
    sprintf_s(s, "%d分", score);
    sprintf_s(b, "体力还剩%d", cnt2 / 400);
    outtextxy(0, 0, s);
    outtextxy(460, 0, a);
    outtextxy(100, 0, b);
    outtextxy(300, 0, d);
    if (win)outtextxy(200, 200, c);
    EndBatchDraw();
    if (win) { getchar(); exit(0); }
}

void control()
{
    char cmd;
    if (_kbhit())
    {
        cmd = _getch();
        switch (cmd)
        {
        case 'a':direction++; break;
        case 'd':direction--; break;
        default:break;
        }
    }
}

int main()
{
    srand((unsigned)time(NULL));
    start();
    while (1)
    {

        draw();
        if (cnt / slow)
        {
            Sleep(slow);
            control();
            update();
            cleardevice();
            cnt = 0;
        }
        cnt++;
        if (cnt1 > 0)cnt1--; //加速时间减少

        if (cnt2 > 0)cnt2--;//体力减少
        else { draw(); getchar(); exit(0); }

        if (cnt3 > 0)cnt3--;                           //重播音乐
        else {
            mciSendString("close music.mp3", 0, 0, 0);
            mciSendString("open music.mp3", 0, 0, 0);
            mciSendString("play music.mp3", 0, 0, 0);
            cnt3 = 40000;
        }

        if (score >= 100)win = 1;                      //目标分数
    }
    getchar();
    return 0;

}
//找最大时,max不能放在循环里面
//a[y][x]  数组第一个是行,为y,第二个是列,才是x

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值