windows游戏编程:球球大作战(吃鸡版)源码
源码介绍:https://blog.csdn.net/alzzw/article/details/100043938
#include "stdafx.h"是win32程序系统生成的
创建项目时选择win32程序项目
除了下面代码外,无其他改动
#include "stdafx.h"
#include <SDKDDKVer.h>
#include <graphics.h> //图形库界面 自己安装的库文件
#include <conio.h>
#include <stdio.h>
#include <tchar.h>
#include <time.h>
#include <math.h>#define WIDTH 1024 //屏幕的宽
#define HEIGHT 576 //屏幕高
#define MAPW (WIDTH*4)
#define MAPH (HEIGHT*4)
#define AINUM 200 //AI数量
#define FNUM 2000 //食物数量
#define DISTANCE(x1,y1,x2,y2) (sqrt((float)(x1-x2)*(float)(x1-x2)+(float)(y1-y2)*(float)(y1-y2))) //两点之间的距离公式 /*
结构体 自身去创建的一个类型
结构体成员 你所创建的类型里面所包含的属性
*//*画毒圈 会持续掉血的地图*/ struct FOOD
{bool eat; //是否被吃COLORREF color; //颜色int x, y; //坐标char type; //食物的类型(即形状)
};struct BALL //小球结构
{bool life; //生命COLORREF color; //颜色int x, y; //坐标float r; //半径
};FOOD food[FNUM]; //结构体数组 元素类型是所创建结构体的类型
BALL mover = { 1, RGB(0, 0, 0), 0, 0, 0 };
BALL ai[AINUM] = { 1, RGB(0, 0, 0), 0, 0, 0 };
DWORD *pBuffer; //显存指针
//int lx = -20, ly = MAPH + 20, rx = MAPW + 20, ry = -20;
int relx = -20, rely = MAPH + 20, rerx = MAPW + 20, rery = -20;
float asp = 1;
float Time = 0;
int eaten = 0; //玩家吃AI的数量
int ai_eaten = 0; //AI吃AI的数量/*
一款游戏最基本的要求是什么?
1.界面
2.数据初始化
3.更改其中的数据
4.判断数据是否达到一个临界点
5.退出游戏
*/void start();
void setall();
void move(BALL* ball); //如果实参传入的是地址 形参必定是一个指针变量
void draw();
void AI();int _tmain(int argc, _TCHAR* argv[])
{initgraph(WIDTH, HEIGHT);//游戏的初始化start();setall(); //初始化所有数据BeginBatchDraw();while (true){move(&mover);draw();AI();FlushBatchDraw();Sleep(10);}return 0;
}
//AI的位置是否固定 食物的位置又是否固定 随机出现在任何一个位置的
void setall()
{srand((unsigned int)time(NULL)); //随机函数种子mover.color = RGB(rand() % 256, rand() % 256, rand() % 256); //rand()%256 随机取值 0-255mover.life = 1;mover.x = int(WIDTH*0.5); //将玩家固定在屏幕中心处mover.y = int(HEIGHT*0.5);mover.r = 20;for (int i = 0; i < AINUM; i++){ai[i].color = RGB(rand() % 256, rand() % 256, rand() % 256); //rand()%256 随机取值 0-255ai[i].life= 1;ai[i].x = rand() % (MAPW - int(ai[i].r + 0.5)) + int(ai[i].r + 0.5); //AI产生的位置不会出现一个越界ai[i].y = rand() % (MAPH - int(ai[i].r + 0.5)) + int(ai[i].r + 0.5);ai[i].r = float(rand()%10+10);}for (int i = 0; i < FNUM; i++){food[i].eat = 1;food[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);food[i].x = rand() % MAPW;food[i].y = rand() % MAPH;food[i].type = rand() % 10 + 1;}//指针pBuffer = GetImageBuffer(NULL); //获取显存指针setbkcolor(WHITE);cleardevice();settextcolor(LIGHTRED);setbkmode(TRANSPARENT);settextstyle(16, 0, "宋体");
}void start()
{setbkcolor(WHITE);cleardevice();settextcolor(RED); //函数 设置文字颜色setbkmode(TRANSPARENT); //设置窗口透明settextstyle(128, 0, "宋体"); //字符集问题outtextxy(100, 40, "球球大作战");settextstyle(32, 0, "宋体");outtextxy(384, 500, "按任意键开始游戏");getch();
}void move(BALL* ball)
{if (ball->r <= 0) ball->life = false; //false 0 true 1if (ball->life == false){HWND hwnd = GetHWnd(); //获取窗口句柄MessageBox(hwnd, "你已经死亡", "游戏结束", MB_OK | MB_ICONEXCLAMATION);closegraph();exit(0);}if (ball->x > (MAPW - ball->r) || ball->x - ball->r < 0 || ball->y - ball->r<0 || ball->y>(MAPH - ball->r))ball->r -= 0.1f;//玩家吃AIfor (int i = 0; i < AINUM; i++){if (ball->r >= ai[i].r){if (ai[i].life == 0) continue;if (DISTANCE(ball->x, ball->y, ai[i].x, ai[i].y) < (4 / 5.0*(ball->r + ai[i].r))){ai[i].life = 0; //吃掉AIball->r += (ai[i].r*ai[i].r / 2) / ball->r;eaten++;}}}//食物被吃for (int n = 0; n < FNUM; n++){if (food[n].eat == 0) continue;if (DISTANCE((float)ball->x, (float)ball->y, (float)food[n].x, (float)food[n].y) < ball->r){ball->r += 4 / ball->r;food[n].eat = 0;}}static int mx = 0, my = 0; //记录偏移量if (GetAsyncKeyState(65) & 0x8000) { ball->x -= 3, mx += 3; } //获取键盘的值if (GetAsyncKeyState(87) & 0x8000) { ball->y -= 3, my += 3; }if (GetAsyncKeyState(83) & 0x8000) { ball->y += 3, my-= 3; }if (GetAsyncKeyState(68) & 0x8000) { ball->x += 3, mx -= 3; }/*awsd*/setorigin(mx, my); //坐标修正
}void draw()
{clearcliprgn();setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 20);setlinecolor(RGB(0, 100, 0));//左竖 上横 下横 右竖line(relx, rely, relx, rery);line(relx, rely, rerx, rely);line(relx, rery, rerx, rery);line(rerx, rery, rerx, rely);setfillcolor(GREEN);if (mover.x - 0.5*WIDTH / asp < relx) floodfill(relx - 11, mover.y, RGB(0, 100, 0));if (mover.x + 0.5*WIDTH / asp > rerx) floodfill(rerx + 11, mover.y, RGB(0, 100, 0));if (mover.y - 0.5*HEIGHT / asp < rery) floodfill(mover.x, rery-11, RGB(0, 100, 0));if (mover.y + 0.5*HEIGHT / asp > rely) floodfill(mover.x, rely + 11, RGB(0, 100, 0));setlinecolor(WHITE);setlinestyle(PS_NULL);for (int i = 0; i < FNUM; i++){if (food[i].eat == 0) continue;setfillcolor(food[i].color);switch (food[i].type) //食物的形状{case 1: solidellipse(food[i].x, food[i].y, food[i].x + 2, food[i].y + 4); break;case 2: solidellipse(food[i].x, food[i].y, food[i].x + 4, food[i].y + 2); break;case 3: solidrectangle(food[i].x, food[i].y, food[i].x + 4, food[i].y + 2); break;case 4: solidrectangle(food[i].x, food[i].y, food[i].x + 2, food[i].y + 4); break;case 5: solidroundrect(food[i].x, food[i].y, food[i].x + 2, food[i].y + 4, 2, 2); break;case 6: solidroundrect(food[i].x, food[i].y, food[i].x + 4, food[i].y + 2, 2, 2); break;case 7: solidroundrect(food[i].x, food[i].y, food[i].x + 4, food[i].y + 2, 4, 2); break;case 8: solidroundrect(food[i].x, food[i].y, food[i].x + 4, food[i].y + 2, 2, 4); break;case 9: solidroundrect(food[i].x, food[i].y, food[i].x + 4, food[i].y + 2, 1, 1); break;case 10: fillcircle(food[i].x, food[i].y, 4); break;}}//画出AIfor (int i = 0; i < AINUM; i++){if (ai[i].life == 0) continue;setfillcolor(ai[i].color);fillcircle(ai[i].x, ai[i].y, int(ai[i].r + 0.5)); //四舍五入}//画玩家setfillcolor(mover.color);fillcircle(mover.x, mover.y, int(mover.r + 0.5));IMAGE map(150, 100); //小地图SetWorkingImage(&map);setbkcolor(RGB(120, 165, 209));cleardevice();//相当于我们重新创建了一个图形界面for (int i = 0; i < AINUM; i++){if (ai[i].life == 0) continue;setfillcolor(ai[i].color);fillcircle(ai[i].x * 150 / WIDTH / 4, ai[i].y * 100 / HEIGHT / 4, int(ai[i].r / 28 + 0.5));}//画玩家setfillcolor(mover.color);fillcircle(mover.x * 150 / WIDTH / 4, mover.y * 100 / HEIGHT / 4, int(mover.r / 28 + 0.5));SetWorkingImage(); //恢复之前的绘图界面putimage(mover.x + int(0.5*WIDTH) - 150, mover.y - int(0.5*HEIGHT), 150, 100, &map, 0, 0);}void AI()
{for (int i = 0; i < AINUM; i++){//ai吃玩家if (ai[i].r>mover.r){if (DISTANCE(mover.x, mover.y, ai[i].x, ai[i].y) < 2 / 3.0*ai[i].r + mover.r){ai[i].r += (mover.r*mover.r) / ai[i].r;mover.life = 0;mover.r = 0;}}//AI吃AIfor (int j = 0; j < AINUM;j++){if (ai[i].r>ai[j].r){if (ai[j].life == 0) continue;if (DISTANCE(ai[i].x, ai[i].y, ai[j].x, ai[j].y) < 4 / 5.0*ai[i].r + ai[j].r){ai[i].r += (ai[j].r*ai[j].r) / ai[i].r;ai[j].life = 0;ai_eaten++;}}}double min_DISTANCE = 100000;int min = -1;//AI靠近AIfor (int k = 0; k < AINUM; k++){if (ai[i].r>ai[k].r&&ai[k].life == 1){if (DISTANCE(ai[i].x, ai[k].x, ai[i].y, ai[k].y) < min_DISTANCE){min_DISTANCE = DISTANCE(ai[i].x, ai[k].x, ai[i].y, ai[k].y);min = k;}}}if ((min != -1) && (rand() % 2 == 1)){if (rand() % 2){if (ai[i].x < ai[min].x) ai[i].x += 2;else ai[i].x-=2;}else{if (ai[i].y < ai[min].y) ai[i].y += 2;else ai[i].y -= 2;}}for (int n = 0; n < FNUM; n++){if (food[n].eat == 0) continue;if (DISTANCE(ai[i].x, ai[i].y, food[n].x, food[n].y) < ai[i].r){ai[i].r += 4 / ai[i].r;food[n].eat = 0;}}}
}
windows游戏编程:球球大作战(吃鸡版)源码相关推荐
- windows游戏编程:球球大作战吃鸡版(C语言游戏开发)
球球大作战: 前言: 本游戏用到了图形界面库graphics.h,图形界面库下载安装:https://blog.csdn.net/alzzw/article/details/100043681 下方有 ...
- 全网首发游戏陪玩系统 语音聊天系统11月商业版源码 附教程
源码简介: 此系统源码包含详细搭建教程及素材图,附搭建教程 源码仅用于学习使用(价值15980元) 此陪玩系统源码本人亲自测试搭建并且运营了一段时间,亲测有效,下方是我测试运营的 下面开始讲讲游戏陪玩 ...
- c语言编程球球大作战,C/C++项目源码——球球大作战
C/C++项目源码--球球大作战 这是一个球球大作战的小程序,能够运行,需要下载一个easyx库 初始产生一个小球,可以慢慢吃零食长大 游戏没有写完整,不能吃别的玩家(单机初始化产生的玩家) 有兴趣可 ...
- 年赚百万游戏主播,玩转Python后:几行代码轻松“吃鸡” 附源码
大吉大利,准备吃鸡! 你是否玩儿了好几个月的吃鸡,依旧是落地成盒? 是否常常不得知自己如何被打.莫名其妙的挂了? 还没有吃过鸡/(ㄒoㄒ)/~~总是不明不白的就被别的玩家杀了 !!!∑(゚Д゚ノ)ノ能 ...
- 年赚百万游戏主播!玩转Python后:几行代码轻松“吃鸡” 附源码
大吉大利,准备吃鸡! 你是否玩儿了好几个月的吃鸡,依旧是落地成盒? 是否常常不得知自己如何被打.莫名其妙的挂了? 还没有吃过鸡/(ㄒoㄒ)/~~总是不明不白的就被别的玩家杀了 !!!∑(゚Д゚ノ)ノ能 ...
- Windows游戏编程大师技巧(一)
第一章 无尽之旅 Windows编程是一场由来已久并还在进行着的战争.开始时,游戏程序拒绝Windows平 台,但正如Borg所言:"反对无效......",我也赞同这一观点.本章 ...
- 《Windows游戏编程大师技巧》(第二版)第1章(上)
第1章 学海无涯 "Oh, you want some too?!?" -Hudson, Aliens Windows 编程就像是一场由来已久并还在进行着的战争.尽管游戏程序员曾经 ...
- Windows游戏编程快速入门方法
Windows游戏编程快速入门方法 Easideao(简单思路) 序言: 从2001年到2005年,在不知不觉中我已经渡过了4年的职业游戏开发生涯.在这4年里经常会有些网友向我询问编程的入门有没有捷径 ...
- Windows游戏编程之从零开始d
Windows游戏编程之从零开始d I'm back~~恩,几个月不见,大家还好吗? 这段时间真的好多童鞋在博客里留言说或者发邮件说浅墨你回来继续更新博客吧. woxiangnifrr童鞋说每天都在来 ...
- 《Windows游戏编程大师技巧》(第二版)第2章
因此几乎是一夜间,Windows 95就改变了整个计算机行业.的确,目前还有一些公司仍然在使用Windows 3.1(你能相信吗?),但是Windows 95使得基于Intel的PC成为除游戏之外的所 ...
最新文章
- - -(我最近的开发..)
- VC中使用Unicode的一些列问题
- maven配置阿里云镜像后Eclipse不生效解决办法
- Monkey基本用法与常用参数
- 【机器学习基础】数学推导+纯Python实现机器学习算法7:神经网络
- 计算机二级必备快捷键知识,计算机二级考试中的一些注意事项️
- matlab求逆矩阵_MPU6050姿态解算2-欧拉角amp;旋转矩阵
- 3.8 Softmax 回归
- 最小的html5页面,第一个html5+响应式页面
- 不能使用sizeof计算的表达式
- javafx 与java,java桌面应用程序和javafx有什么区别?
- 基于Androidstudio的2048小游戏的设计与实现
- Linux动态库依赖其它动态库的问题
- 二叉树的后序遍历(递归和非递归)
- anaconda python3 安装库_痛点:Anaconda3 python第三方库批量安装
- 自定义NavigationBar的思路
- 学生选课管理系统(SQL Server+Java 解决选课冲突问题+附源码)
- JAVASE阶段测试试卷
- 郑莉版java第三章答案_java语言程序设计(郑莉)第七章课后习题答案.docx
- UML类图简介及类与类之间的关系