In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The content of this article mainly revolves around the C language game project ball big battle how to carry on the narration, the article content is clear and easy to understand, the organization is clear, is very suitable for the novice to study, is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Project code
Let's go straight to the code phase and watch carefully.
1. Structure struct Ball / / small ball structure, player, food, artificial mental retardation {int x; int y; int / radius DWORD color;// color bool flag;// exists}; int score = 0; struct Ball foot [food _ NUM]; struct Ball player;// player struct Ball ai [AI _ NUM] 2. Initialize void GameInit () {/ / play background music mciSendString ("open. / BallBGM.mp3 alias BGM", 0,0,0); / / mciSendString ("play BGM repeat", 0,0,0); / / set random number seed time is constantly changing srand ((unsigned) time (NULL)); / / find each element for (int I = 0; I)
< FOOD_NUM; i++) { food[i].x = rand() % WIDTH; food[i].y = rand() % HEIGHT; food[i].flag = true;//刚开始食物是存在的,没有被吃掉 food[i].r = rand() % 6 + 1; food[i].color = RGB(rand() % 256, rand() % 256, rand() % 256); } //初始化玩家数据 player.x = rand() % WIDTH; player.y = rand() % HEIGHT; player.r = 18;//只要比最大的食物大就可以了 player.flag = true; player.color= RGB(rand() % 256, rand() % 256, rand() % 256); //初始化ai for (int i = 0; i < AI_NUM; i++) { ai[i].x = rand() % WIDTH; ai[i].y = rand() % HEIGHT; ai[i].r = rand() % 10 + 5; ai[i].flag = true; ai[i].color = RGB(rand() % 256, rand() % 256, rand() % 256); }}3、绘制函数void GameDraw(){ //防止闪屏 BeginBatchDraw(); setbkcolor(WHITE); cleardevice();//清屏 for (int i = 0; i < FOOD_NUM; i++) { if (food[i].flag) { setfillcolor(food[i].color);//设置填充颜色 solidcircle(food[i].x, food[i].y, food[i].r);//画一个填充圆 } else { food[i].x = rand() % WIDTH; food[i].y = rand() % HEIGHT; food[i].flag = true;//刚开始食物是存在的,没有被吃掉 food[i].r = rand() % 6 + 1; food[i].color = RGB(rand() % 256, rand() % 256, rand() % 256); } } //绘制玩家 setfillcolor(player.color);//设置填充颜色 solidcircle(player.x, player.y, player.r); settextcolor(GREEN); settextstyle(30, 0, "黑体"); setbkmode(0);//设置文字背景透明 outtextxy(player.x-50, player.y, "我是最亮的崽"); //输出分数 settextcolor(BLACK); char str[20]; sprintf(str,"分数:%d",score);//格式化字符串 outtextxy(20, 20, str); //绘制ai for (int i = 0; i < AI_NUM; i++) { if (ai[i].flag) { setfillcolor(ai[i].color);//设置填充颜色 solidcircle(ai[i].x, ai[i].y, ai[i].r);//画一个填充圆 } } EndBatchDraw();}4、玩家控制函数void keyControl(int speed){ //获取键盘消息,按得是哪一个键 _getch(); GetAsyncKeyState() windows系统函数 if ((GetAsyncKeyState('W') || GetAsyncKeyState(VK_UP)) && player.y >= 0) / / pressed the upper key y cannot be less than 0 {player.y-= speed;} if ((GetAsyncKeyState ('S') | | GetAsyncKeyState (VK_DOWN)) & & player.y = 0) / / pressed the left key {player.x-= speed;} if ((GetAsyncKeyState ('D') | | GetAsyncKeyState (VK_RIGHT)) & & player.x
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.