2021新年快乐呀,两天都没出去玩终于写完这个小游戏。
之前上一篇博文说过要写跑酷来着,但因为我实在太菜了就中途放弃了,换成了公路障碍赛车。。。
界面如图,感觉是个比较劣质的游戏。。。但是它还是一个有碰撞判断,有bgm,有加速功能的比较健全的小游戏的(小声bbbbbb…) 当然也会有bug。。。不管怎么说,这是我学C++三个月以来第一次自己写完四百行代码,还是值得记录一下下的。
老师布置作业时,也在网上搜了很多教程,但。。emmmm一言难尽,首先是贪吃蛇和碰撞小球的比较多,跑酷和赛车的easyx案例是真的少,要么就是太难懂(因为我太菜了),会用到我们还没学过的复杂的函数和库还有链表什么的 (我只学到指针呜呜呜,这些是下学期的内容了)。
如果你跟我一样是个绝对的新手,那这个代码就很友好了~~
一般我拿到一个别人写的好长的代码我就不想看了。。所以贴心的 我准备了超级完备的压缩包!!素材源码一应俱全!!
附上链接:https://download.csdn.net/download/m0_51357657/14014675
(免费的资源它不可吗?)
而且!!PPT 中有非常非常详细的教程!!一看就会!!(因为我本人水平就很低。。。)
不想下载压缩包的。。只能看看这个了。。
/*1.创立背景2.线2.1 画线2.2 移动线3.车4.控制玩家车的移动5.随机生成障碍车6.判断碰撞7.得分栏设置8.开始界面9.终点线10.是否结束游戏
*/#include<graphics.h>
#include<time.h>
#include<conio.h>
#include<iostream>
#include<stdlib.h>
#include <mmsystem.h> //导入声音头文件库
#pragma comment(lib,"winmm.lib")//导入声音的链接库 using namespace std;IMAGE bk;
IMAGE red;
IMAGE yellow;
IMAGE white;
IMAGE white2;
IMAGE truck;
IMAGE terminal;
char ID;
char color;
int points = 25;
int speed1 = 12;
int speed2 = 18;//没用到的计时器
int Timer(time_t num, int id)
{static time_t start[5] = { clock(),clock(), clock(), clock(),clock() };time_t end = clock();if (end - start[id] > num){start[id] = end;return 1;}return 0;
}void setpng()
{loadimage(&bk, "赛车背景.png", 480, 750);loadimage(&red, "红.png", 60, 80);loadimage(&yellow, "黄.png", 60, 80);loadimage(&white, "白1.png", 60, 80);loadimage(&white2, "白2.png", 60, 80);loadimage(&truck, "卡车.png", 63, 130);
}//描述线段的结构体struct line{int f_x, f_y;int r_x, r_y;int h;};struct line ll[1500];//初始画线void initline(int i){ll[i].f_x = 155;ll[i].r_x = 295;ll[i].f_y = i * 56 - 25000;ll[i].r_y = i * 56 - 25000;ll[i].h = 23;}//画线void drawline(int i){setlinecolor(WHITE);setlinestyle(PS_SOLID, 8);int wide1 = ll[i].f_y + ll[i].h;int wide2 = ll[i].r_y + ll[i].h;line(ll[i].f_x, ll[i].f_y, ll[i].f_x, wide1);line(ll[i].r_x, ll[i].r_y, ll[i].r_x, wide2);}//移动白线void movline(int i){ll[i].f_y += speed1;ll[i].r_y += speed1;}//擦线void eraseline(int i){setlinecolor(RGB(154,217,233));int wide1 = ll[i].f_y + ll[i].h;int wide2 = ll[i].r_y + ll[i].h;line(ll[i].f_x, ll[i].f_y, ll[i].f_x, wide1);line(ll[i].r_x, ll[i].r_y, ll[i].r_x, wide2);}//重新生成线段void reline(int i){ll[i].f_x = 155;ll[i].r_x = 295;ll[i].f_y = i * 56;ll[i].r_y = i * 56;ll[i].h = 23;}//描述车辆的结构体struct Car{int _x;int _y;int id;};struct Car car[100];void initcar(){//2号:玩家车car[2]._x = 200;car[2]._y = 500;car[70]._y = -1500;car[71]._y = -1540;for (int i = 3; i < 30; i++){car[i]._x = 150;car[i]._y = rand() % 100 * -220;}for (int i = 30; i < 60; i++){car[i]._x = 250;car[i]._y = rand() % 80 * -420;}}void showcar(){putimage(car[2]._x, car[2]._y, &white2);for (int a = 3; a < 20; a++){putimage(car[a]._x, car[a]._y, &red);}for (int b = 20; b < 36; b++){putimage(car[b]._x, car[b]._y, &yellow);}for (int c = 36; c < 45; c++){putimage(car[c]._x, car[c]._y, &truck);}for (int d = 45; d < 60; d++){putimage(car[d]._x, car[d]._y, &white);}}void gocar(int i){car[i]._y += speed2; }//键盘控制车辆void movcar(int i){if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT)){car[i]._x -= 10;}if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT)){car[i]._x += 10;}if (GetAsyncKeyState('W') || GetAsyncKeyState(VK_UP)){speed1 += 5;speed2 += 5;settextcolor(RED);settextstyle(40, 40, "Impact");outtextxy(70, 300, "SPEED UP");Sleep(36);car[71]._y += 10; //终点线也要配合地向前挪挪}if (GetAsyncKeyState('S') || GetAsyncKeyState(VK_DOWN)){speed1 -= 5;speed2 -= 5;}}
//没用到的键盘获取:void movvcar(int i){char c='a';if (_kbhit()){c = _getch();if (c == 'W'){car[i]._y += 10;}if (c == 'S'){car[i]._y -= 10;}if (c == 'A'){car[i]._x -= 10;}if (c == 'D'){car[i]._x += 10;}}}//判断是否撞车void crash(){for (int i = 3; i < 30; i++){if (abs(car[2]._x - car[i]._x)< 45 && abs(car[2]._y - car[i]._y) < 45){settextcolor(RED);settextstyle(50, 50, "Impact");outtextxy(70, 300, "CRASH!!");points--;Sleep(100);}}for (int i = 30; i < 60; i++){if (abs(car[i]._x - car[2]._x) < 45 &&abs( car[2]._y - car[i]._y) < 45){settextcolor(RED);settextstyle(50, 50, "Impact");outtextxy(70, 300, "CRASH!!");points--;Sleep(100);}}}
//得分栏void score(){char get[10] = " ";sprintf_s(get, "分数:%d", points);settextcolor(WHITE);settextstyle(25, 10, "宋体");outtextxy(1, 1, get);Sleep(10);}//开始界面void Start(){setbkcolor(BLACK);cleardevice();settextcolor(WHITE);settextstyle(30, 30, "Impact");outtextxy(12, 300, "Do you want to play ?");settextcolor(WHITE);settextstyle(38, 20, "宋体");outtextxy(120, 200, "公路障碍赛");Sleep(20);settextcolor(WHITE);settextstyle(20, 10, "宋体");outtextxy(120, 400, "按空格键开始");while (true){FlushBatchDraw();Sleep(10);if (_kbhit()){if (_getch() == ' '){break;}}}}//绘制终点线void finishline(){ settextcolor(RED);settextstyle(70, 80, "Impact");outtextxy(50, car[71]._y, "FINISH");car[71]._y+=2;}//是否到达终点void success(){if (car[2]._y - car[71]._y < 30){settextcolor(RED);settextstyle(70, 80, "Impact");outtextxy(95, 220, "WIN!");settextcolor(WHITE);settextstyle(20, 10, "宋体");outtextxy(120, 400, "按空格键结束游戏");Sleep(2000);while (true){FlushBatchDraw();Sleep(10);if (_kbhit()){if (_getch() == ' '){closegraph();}}}}}//是否失败void failure(){if (points <= 0){settextcolor(RED);settextstyle(50, 60, "Impact");outtextxy(65, 300, "YOU LOSE!");settextcolor(WHITE);settextstyle(20, 10, "宋体");outtextxy(120, 400, "按空格键结束游戏");Sleep(2000);while (true){FlushBatchDraw();Sleep(10);if (_kbhit()){if (_getch() == ' '){closegraph();}}}}}int main()
{initgraph(480, 750); //创建窗口setbkcolor(GREEN);cleardevice();mciSendString("open Reset.mp3", NULL, 0, NULL);mciSendString("play Reset.mp3", NULL, 0, NULL);setpng(); //初始图片srand((unsigned int)time(NULL));Start(); //初始画线for (int n = 0; n < 1500; n++){initline(n);}initcar(); //初始车while (1){BeginBatchDraw(); clearrectangle(0, 0, 480, 750); putimage(1, 1, &bk);for (int n = 0; n < 1500; n++){drawline(n);}Sleep(50);for (int n = 1500; n >= 0; n--){movline(n);}showcar();movcar(2);for (int k = 3; k < 60; k++){gocar(k);}finishline();crash();score();failure();success();EndBatchDraw();}closegraph();_getch();return 0;
}
//代码报错的伙伴考虑一下项目属性里要设置成多字符集哦!
//PS:这里没给素材,压缩包里有素材,也可以用自己的素材哈
如果有错的地方还请谅解啊。。。