#include<stdio.h>
#include<graphics.h>//包含easyx图形库头文件,绘制窗口之类
#include<Windows.h>
#pragma comment(lib,"Winmm.lib")//音乐,导入winmm库
#define WIDTH 640
#define HEIGHT 480
#define MAX 1
struct Watermelon//西瓜结构
{
int x;
int y;
int r;
}watermelon[MAX];
struct Apple//苹果结构
{
int x;
int y;
int r;
}apple[MAX];
struct Bomb//炸弹结构
{
int x;
int y;
int r;
}bomb[MAX];
struct Box//篮子结构
{
int x;
int y;
int score;
}box;
IMAGE img20;//西瓜定义变量
IMAGE img21;//西瓜掩码图定义变量
IMAGE img30;//苹果定义变量
IMAGE img31;//苹果掩码图定义变量
IMAGE img40;//炸弹定义变量
IMAGE img41;//炸弹掩码图定义变量
IMAGE img50;//炸弹定义变量
IMAGE img51;//炸弹掩码图定义变量
IMAGE imgbeijing1;//背景
void Gameinit()//初始化
{
for (int i = 0; i < MAX; i++)
{
//初始化西瓜
watermelon[i].x = rand() % WIDTH;
watermelon[i].y = -rand() % 400;//有一定的高度
watermelon[i].r = 20;
}
for (int i = 0; i < MAX; i++)
{
//初始化苹果
apple[i].x = rand() % WIDTH;
apple[i].y = -rand() % 700;//有一定的高度
apple[i].r = 20;
}
for (int i = 0; i < MAX; i++)
{
//初始化炸弹
bomb[i].x = rand() % WIDTH;
bomb[i].y = -rand() % 1000;//有一定的高度
bomb[i].r = 20;
}
//初始化篮子
box.x = WIDTH / 2 - 50;
box.y = HEIGHT - 80;
box.score = 0;
loadimage(&imgbeijing1, "./背景1.jpg", 640, 480);
loadimage(&img20, "20.png",40,40);//西瓜加载图片
loadimage(&img21, "21.png",40,40);//西瓜加载图片
loadimage(&img30, "30.png", 40, 40);//苹果加载图片
loadimage(&img31, "31.png", 40, 40);//苹果加载图片
loadimage(&img40, "40.png", 40, 40);//炸弹加载图片
loadimage(&img41, "41.png", 40, 40);//炸弹加载图片
loadimage(&img50, "50.png", 80, 80);//篮子加载图片
loadimage(&img51, "51.png", 80, 80);//篮子加载图片
}
void GameDraw()//画
{
cleardevice();//清屏
//setfillcolor(RED);//画圆
for (int i = 0; i < MAX; i++)//画西瓜,苹果,炸弹
{
//putimage(berry[i].x, berry[i].y, &img, SRCAND);//输出图片 SRCAND背景去掉
putimage(0, 0, &imgbeijing1);
putimage(watermelon[i].x, watermelon[i].y, &img21, SRCAND);//输出苹果,利用掩码图使图片背景透明,通过OR操作符将源和目标矩形区域的颜色合并。
putimage(watermelon[i].x, watermelon[i].y, &img20,SRCPAINT);
putimage(apple[i].x, apple[i].y, &img31, SRCAND);
putimage(apple[i].x, apple[i].y, &img30, SRCPAINT);
putimage(bomb[i].x, bomb[i].y, &img41, SRCAND);
putimage(bomb[i].x, bomb[i].y, &img40, SRCPAINT);
putimage(box.x, box.y, &img51, SRCAND);
putimage(box.x, box.y, &img50, SRCPAINT);
}
//setfillcolor(BLUE);//画盒子
//fillrectangle(box.x, box.y, box.x + 100, box.y + 20);
setbkmode(TRANSPARENT);//将分数背景去掉
settextcolor(RED);
char arr[20] = "";//分数定义
sprintf_s(arr, "分数:%d", box.score);//注意使用多字符集
outtextxy(50, 50, arr);//画分数位置,使用需要在项目,属性里面,高级,将unicode改成多字节字符集
}
void Gamemove()//移动
{
if (GetAsyncKeyState(VK_LEFT))//盒子左移,windows系统函数,获取键盘输入,判断
{
if (box.x >= 0)
{
box.x -= 40;
}
}
if (GetAsyncKeyState(VK_RIGHT))//盒子左移
{
if (box.x + 100 <= WIDTH)
{
box.x += 40;
}
}
//让西瓜左右摇摆
for (int i = 0; i < MAX; i++)
{
if (rand() % 2 == 0)
{
watermelon[i].x += 10;
if (watermelon[i].x>WIDTH-20)
{
watermelon[i].x = WIDTH-20;
}
}
else
{
watermelon[i].x -= 10;
if (watermelon[i].x < 20)
{
watermelon[i].x = 20;
}
}
watermelon[i].y += 10;
}
//苹果左右摇摆
for (int i = 0; i < MAX; i++)
{
if (rand() % 2 == 0)
{
apple[i].x += 10;
if (apple[i].x > WIDTH-20)
{
apple[i].x = WIDTH-20;
}
}
else
{
apple[i].x -= 10;
if (apple[i].x < 20)
{
apple[i].x = 20;
}
}
apple[i].y += 10;
}
//炸弹左右摇摆
for (int i = 0; i < MAX; i++)
{
if (rand() % 2 == 0)
{
bomb[i].x += 10;
if (bomb[i].x > WIDTH-20)
{
bomb[i].x = WIDTH-20;
}
}
else
{
bomb[i].x -= 10;
if (bomb[i].x < 20)
{
bomb[i].x = 20;
}
}
bomb[i].y += 10;
}
}
void Gamejude()//判断西瓜,苹果,炸弹与篮子是否接触,计分
{
for (int i = 0; i < MAX; i++)
{
if (watermelon[i].x > box.x && watermelon[i].x<box.x + 100 && watermelon[i].y + watermelon[i].r>box.y)
{
box.score += 10;
watermelon[i].y = 0;
watermelon[i].x = rand() % WIDTH;
}
else if (watermelon[i].y > HEIGHT)
{
box.score -= 10;
watermelon[i].y = 0;
watermelon[i].x = rand() % WIDTH;
}
}
for (int i = 0; i < MAX; i++)
{
if (apple[i].x > box.x && apple[i].x<box.x + 100 && apple[i].y + apple[i].r>box.y)
{
box.score += 20;
apple[i].y = 0;
apple[i].x = rand() % WIDTH;
}
else if (apple[i].y > HEIGHT)
{
box.score -= 10;
apple[i].y = 0;
apple[i].x = rand() % WIDTH;
}
}
for (int i = 0; i < MAX; i++)
{
if (bomb[i].x > box.x && bomb[i].x<box.x + 100 && bomb[i].y + bomb[i].r>box.y)
{
box.score -= 30;
bomb[i].y = 0;
bomb[i].x = rand() % WIDTH;
}
else if (bomb[i].y > HEIGHT)
{
//box.score -= 10;
bomb[i].y = 0;
bomb[i].x = rand() % WIDTH;
}
}
}
void music()//音乐循环播放
{
PlaySound("张璐 - 江南.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//音乐播放
mciSendString("play bkmusic repeat", NULL, 0, NULL); //循环播放音乐
}
int main()
{
initgraph(WIDTH, HEIGHT);//初始化窗口 initgraph(WIDTH, HEIGHT,1);控制台窗口也显示
//setbkcolor(GREEN);//背景颜色 或(RGB(179,192,204))
cleardevice();//清屏
srand(GetTickCount());//随机数种子函数
Gameinit();
music();
while (1)
{
Gamemove();
GameDraw();
Sleep(100);
Gamejude();
}
return 0;
}
图片