Discuz! Board

 找回密码
 立即注册
查看: 436|回复: 0

54

[复制链接]

51

主题

54

帖子

8724

积分

论坛元老

Rank: 8Rank: 8

积分
8724

大牙狸 Lv:12
发表于 2023-5-4 19:38:37 | 显示全部楼层 |阅读模式
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<bits/stdc++.h>
using namespace std;

key_msg keyMsg;

//图片文件位置
PIMAGE pimg_tree[6];
PIMAGE pimg_dino[5];
PIMAGE pimg_bird[2];
PIMAGE pimg_ground[6];
PIMAGE pimg_cloud;
PIMAGE pimg_gameover;

int cnt;

struct dinosaur
{
        int state;//恐龙状态  0-空中 1-站立  2-下蹲
        int act;//恐龙动作 0-不动  12-站立左右脚        34-下蹲左右脚
        float vy;//y方向速度
        float g;//加速度
        float x,y;//位置坐标
        float lx,ly,rx,ry;//恐龙左上坐标,右下坐标
}dino;

struct ground
{
        int  type;//路的类型
        float x,y;//坐标
}gro[4];

struct cloud
{
        float vx;//飘动速度
        float x,y;//坐标
}clo[3];


struct obstacle
{
        int type;//仙人掌的类型 0-2小  3-5大   6-8 h鸟/m鸟/l鸟
        float x,y;//位置坐标
        float lx,ly,rx,ry;//障碍左上坐标,右下坐标
}obs[9];

float vx;//水平速度
int bird_act;//翼龙动作

const char* imgFiles[21] =
{
        "resource\\tree_s1.png",
        "resource\\tree_s2.png",
        "resource\\tree_s3.png",
        "resource\\tree_b1.png",
        "resource\\tree_b2.png",
        "resource\\tree_b3.png",

        "resource\\dinosaur00.png",
        "resource\\dinosaur01.png",
        "resource\\dinosaur02.png",
        "resource\\dinosaur03.png",
        "resource\\dinosaur04.png",
       
        "resource\\bird01.png",
        "resource\\bird02.png",
       
        "resource\\ground_01.png",
        "resource\\ground_02.png",
        "resource\\ground_03.png",
        "resource\\ground_04.png",
        "resource\\ground_05.png",
        "resource\\ground_06.png",
       
         "resource\\cloud.png",
         "resource\\gameover.png"
};



void loadImage()//图片载入
{
        //图 初始化
        for (int i = 0; i < 6; i++)
        {
                pimg_tree[i] = newimage();
                getimage(pimg_tree[i], imgFiles[i]);
        }
        for (int i = 0; i < 5; i++)
        {
                pimg_dino[i] = newimage();
                getimage(pimg_dino[i], imgFiles[i+6]);
        }
        for (int i = 0; i < 2; i++)
        {
                pimg_bird[i] = newimage();
                getimage(pimg_bird[i], imgFiles[i+11]);
        }
        for (int i = 0; i < 6; i++)
        {
                pimg_ground[i] = newimage();
                getimage(pimg_ground[i], imgFiles[i+13]);
        }
       
        pimg_cloud = newimage();
        getimage(pimg_cloud, imgFiles[19]);
        pimg_gameover = newimage();
        getimage(pimg_gameover, imgFiles[20]);
}


void start()//初始化
{
//关闭窗口不强制退出程序,以便进行游戏保存工作
//        setinitmode(INIT_RENDERMANUAL | INIT_NOFORCEEXIT, 100, 50);
       
        for(int i=0;i<4;i++)
        {
                gro[i].type=rand()%6;
                gro[i].x=0+i*300;
                gro[i].y=250;
        }
        dino.y=220;
        dino.state=1;
        initgraph(900,300);//图形初始化
        setbkcolor(WHITE);
        srand(time(0));//随机数初始化
        loadImage();//图片载入
       
        setcolor(DARKGRAY);
        setfont(25,0,"幼圆");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,幼圆字体
        setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
        setcaption("小恐龙历险记");//设置标题
}



void draw()//图形绘制
{
        for(int i=0;i<4;i++)//路面绘制
        {
                putimage_withalpha(NULL, pimg_ground[gro[i].type],gro[i].x, gro[i].y);//首个路面号循环递增
        }
        for(int i=0;i<3;i++)//云绘制
        {
                putimage_withalpha(NULL, pimg_cloud, clo[i].x, clo[i].y);
        }
        for(int i=0;i<3;i++)//障碍物
        {
                if(obs[i].type<6)        putimage_withalpha(NULL, pimg_tree[obs[i].type], obs[i].x, obs[i].y);
                else if(obs[i].type<9)        putimage_withalpha(NULL, pimg_bird[bird_act], obs[i].x, obs[i].y);
        }
        putimage_withalpha(NULL, pimg_dino[dino.act], 20, dino.y);//绘制恐龙 ,最后绘制防遮挡
        //游戏分数
}


void dino_act()//恐龙状态
{
}


void update()//数据更新
{
        if(dino.state==1)
        {
                if(cnt%10<5)
                {
                        dino.act=1;
                }
                else
                        dino.act=2;
        }
       
}


int main()
{       
a:        start();//初始化
        draw();//图形绘制
        while(1)
        {
                while(kbmsg())
                {
                        keyMsg = getkey();
                        if (((keyMsg.msg == key_msg_down)||(keyMsg.msg == key_msg_up))&&((keyMsg.key==key_up)||
                        (keyMsg.key==key_down)||(keyMsg.key=='Q')))
                        {
                                //mergeMusic.Play(0);
                                dino_act();       
                                if(keyMsg.key=='Q')       
                                {
                                        cnt=0;
                                        goto a;
                                }       
                        }
                        //gameSave();
                }
                update();
                cleardevice();
                draw();
                //
               
                delay_fps(60);
                cnt++;
               
        }
       
        //gameSave();
        putimage_withalpha(NULL, pimg_gameover,350,125 );//首个路面号循环递增
        getch();
        cnt=0;
        goto a;
        return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-5-30 12:27 , Processed in 0.052276 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

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