打气球小游戏C语言代码实现

发布时间:2024年01月15日

前言

期末结束了,寒假想学一点点和小游戏有关的代码,这当然要用图形库,所以打算接着之前的继续学easyx图形库,毕竟这个对新手比较友好,以后学的和easyx有关的内容就直接放在一个专栏里了,感兴趣的朋友可以看看哦,那么,启动!摆脱高数线代英语的折磨,寒假第一天的C语言快乐学习。

头文件及结构体

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<stdbool.h>
#define WIDTH 800
#define HEIGHT 600
#define MAX_NUM 5
#define RADIUS 30
typedef struct ballon
{
?? ?int x, y, ?r, v;
?? ?COLORREF color;
}ballon;

函数实现

void initballon(ballon* ballons, int num)
{
?? ?for (int i = 0; i < num; i++)
?? ?{
?? ??? ?ballons[i].x = rand() % 651 + 30;
?? ??? ?ballons[i].y = HEIGHT;
?? ??? ?ballons[i].r = RADIUS;
?? ??? ?ballons[i].v = rand() % 3 + 1;
?? ??? ?ballons[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
?? ?}
}
void paintballon(ballon* ballons, int num)
{
?? ?for (int i = 0; i < num; i++)
?? ?{
?? ??? ?setfillcolor(ballons[i].color);
?? ??? ?solidcircle(ballons[i].x, ballons[i].y,ballons[i].r);
?? ?}
}
void moveballon(ballon* ballons, int num)
{
?? ?for (int i = 0; i < num; i++)
?? ?{
?? ??? ?ballons[i].y -= ballons[i].v;
?? ?}
}
void judgeballon(int* current, ballon* ballons)
{
?? ?int i = 0;
?? ?int num = *current;
?? ??? ?for (i; i <*current;)
?? ??? ?{
?? ??? ??? ?if (ballons[i].y < -RADIUS)
?? ??? ??? ?{
?? ??? ??? ??? ?for (int j = i; j < *current - 1; j++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?ballons[j] = ballons[j + 1];

?? ??? ??? ??? ?}
?? ??? ??? ??? ?*current -= 1;
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ??? ?i++;
?? ??? ?}
}
void isadd(int* current, ballon* ballons)
{
?? ?while (*current < MAX_NUM)
?? ?{
?? ??? ?ballons[*current].x = rand() % 601 + 100;
?? ??? ?ballons[*current].y = HEIGHT;
?? ??? ?ballons[*current].r = RADIUS;
?? ??? ?ballons[*current].v = rand() % 3 + 1;
?? ??? ?ballons[*current].color = RGB(rand() % 256, rand() % 256, rand() % 256);
?? ??? ?(*current)++;
?? ?}
}

主函数

int main()
{
?? ?srand((unsigned)time(NULL));
?? ?initgraph(WIDTH, HEIGHT, 0);
?? ?setbkcolor(WHITE);
?? ?cleardevice();
?? ?timeBeginPeriod(1);
?? ?LARGE_INTEGER start, end, f;
?? ?QueryPerformanceFrequency(&f);
?? ?BeginBatchDraw();
?? ?ballon ballons[MAX_NUM];
?? ?initballon(ballons, MAX_NUM);
?? ?int current = MAX_NUM;
?? ?int mousex = 0, mousey = 0;
?? ?while (1)
?? ?{
?? ??? ?QueryPerformanceCounter(&start);
?? ??? ?cleardevice();
?? ??? ?paintballon(ballons, MAX_NUM);
?? ??? ?moveballon(ballons, MAX_NUM);
?? ??? ?QueryPerformanceCounter(&end);
?? ??? ?judgeballon(&current, ballons);
?? ??? ?isadd(&current, ballons);
?? ??? ?setlinecolor(RGB(237, 178, 29));
?? ??? ?setlinestyle(PS_SOLID, 3);
?? ??? ?circle(mousex, mousey, 20);
?? ??? ?line(mousex - 20, mousey, mousex + 20, mousey);
?? ??? ?line(mousex, mousey + 20, mousex, mousey - 20);
?? ??? ?while ((end.QuadPart - start.QuadPart) * 1000000 / f.QuadPart < 1000000 / 60)
?? ??? ?{
?? ??? ??? ?Sleep(1);
?? ??? ??? ?QueryPerformanceCounter(&end);
?? ??? ??? ?ExMessage msg;
?? ??? ??? ?bool is = peekmessage(&msg, EX_MOUSE);
?? ??? ??? ?if (is==true)
?? ??? ??? ?{
?? ??? ??? ??? ?if (msg.message==WM_MOUSEMOVE)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?mousex = msg.x;
?? ??? ??? ??? ??? ?mousey = msg.y;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if (msg.message==WM_LBUTTONDOWN)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?int i = 0;
?? ??? ??? ??? ??? ?while (i < current)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?int distance = (int)sqrt((ballons[i].x - msg.x) * (ballons[i].x - msg.x) + (ballons[i].y - msg.y) * (ballons[i].y - msg.y));
?? ??? ??? ??? ??? ??? ?if (distance <RADIUS)
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?for (int j = i; j < current - 1; j++)
?? ??? ??? ??? ??? ??? ??? ??? ?ballons[j] = ballons[j + 1];
?? ??? ??? ??? ??? ??? ??? ?current -= 1;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ??? ??? ??? ?i++;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?FlushBatchDraw();
?? ?}
?? ?EndBatchDraw();
?? ?timeEndPeriod(1);
?? ?closegraph();
?? ?return 0;
}

视频

运行视频我上传到视频那一栏了

结语

今天的学习就到这里了,希望大家在寒假可以学习自己想学的东西,不被各种无奈的原因限制,

一起加油!

文章来源:https://blog.csdn.net/2301_79551553/article/details/135602760
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。