#include"stdafx.h"
#include<graphics.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#define BOMB_NUMBER 2
#define CAKE_NUMBER 4
typedef struct BOMB //炸弹
{
int x;
int y;
int speed;
}Bomb;
typedef struct PLAYER //玩家
{
int x;
int y;
int score;
char name[10];
}player;
typedef struct CAKE//蛋糕
{
int x;
int y;
int speed;
}Cake;
void createme(int x, int y);
void downbomb(Bomb*bomb);
void downcake(Cake*cake);
int touchbomb(Bomb*bomb, int x, int y);
void eatcake(Cake*cake, player*p, int x, int y);
void randombomb(Bomb*bomb, int i);
void randomcake(Cake*cake, int i);
void setName(player*p);
void printScore(player*p);
void drawinterface();
void downbomb(Bomb*bomb);
void clickBegin(MOUSEMSG m);
//int clickEnd(MOUSEMSG m);
IMAGE BOMB;
IMAGE CAKE;
IMAGE PLAYER;
IMAGE BOOM;
void main()
{
loadimage(&BOMB, _T("d:\\炸弹.jpg"), 40, 40);
loadimage(&CAKE, _T("d:\\蛋糕.jpg"), 40, 40);
loadimage(&PLAYER, _T("d:\\玩家.jpg"),50, 50);
loadimage(&BOOM, _T("d:\\爆炸.jpg"), 600, 500);
Bomb bomb[BOMB_NUMBER];
Cake cake[CAKE_NUMBER];
player*p = NULL;
p = (player*)malloc(sizeof(player));
setName(p);
p->score = 0;
drawinterface();
srand((unsigned)(time(NULL)));
char k = 0;
int x = 275, y = 440;
int i = 0;
for (i = 0; i < BOMB_NUMBER; i++) {
randombomb(bomb, i);
randomcake(cake, i);
}
MOUSEMSG m = GetMouseMsg();
clickBegin(m);
while (true)//主循环
{
downbomb(bomb);
downcake(cake);
createme(x, y);
char k = 0;
if (_kbhit()) {
k = _getch();
switch (k) {
case'w': {
if (y < 10) {
y = 0;
}
else {
createme(x, y);
y -= 10;
createme(x, y);
}
break;
}
case's': {
createme(x, y);
if (y > 430) {
y = 440;
}
else {
y += 10;
createme(x, y);
}
break;
}
case'a': {
if (x < 10) {
x = 0;
}
else {
createme(x, y);
x -= 10;
createme(x, y);
}
break;
}
case'd': {
if (x > 540) {
x = 550;
}
else {
createme(x, y);
x += 10;
createme(x, y);
}
break;
}
}
}
if (touchbomb(bomb, x, y) == 0)
break;
eatcake(cake, p, x, y);
printScore(p);
}
cleardevice();
setbkcolor(WHITE);
putimage(0, 0, &BOOM);
Sleep(2000);
closegraph();
}
void createme(int x, int y)
{
fillrectangle(x-7 ,y-6, x + 58, y +58);
putimage(x,y,&PLAYER);
}
void downbomb(Bomb*bomb) {
int i = 0;
BeginBatchDraw();
for (i = 0; i < BOMB_NUMBER; i++)
{
if (bomb[i].y<470)
{
setfillcolor(BLACK);
fillrectangle(bomb[i].x-22,bomb[i]. y-22,bomb[i]. x +22,bomb[i].y +22);
bomb[i].y += 5;
putimage(bomb[i].x-20, bomb[i].y-20, &BOMB);
Sleep(10);
FlushBatchDraw();
}
else
{
setfillcolor(BLACK);
fillrectangle(bomb[i].x - 21, bomb[i].y - 21, bomb[i].x + 21, bomb[i].y + 21);
randombomb(bomb, i);
}
}
}
void downcake(Cake*cake) {
int i = 0;
BeginBatchDraw();
for (i = 0; i < CAKE_NUMBER; i++)
{
if (cake[i].y<470)
{
setfillcolor(BLACK);
setlinecolor(BLACK);
fillrectangle(cake[i].x - 21, cake[i].y - 21, cake[i].x + 21, cake[i].y + 21);
cake[i].y += 5;
putimage(cake[i].x - 20, cake[i].y - 20, &CAKE);
Sleep(1);
FlushBatchDraw();
}
else
{
setfillcolor(BLACK);
fillrectangle(cake[i].x - 21, cake[i].y - 21, cake[i].x + 21, cake[i].y + 21);
randomcake(cake, i);
}
}
}
int touchbomb(Bomb*bomb, int x, int y) {
int i;
for (i = 0; i < BOMB_NUMBER; i++) {
if (y<bomb[i].y + 20 && y>bomb[i].y&&bomb[i].x > x - 20 && bomb[i].x < x +70)
return 0;
else if (x<bomb[i].x + 20 && x>bomb[i].x&&bomb[i].y > y - 20 && bomb[i].y < y + 70)
return 0;
else if (x + 50 > bomb[i].x - 20 && x + 50 < bomb[i].x&&bomb[i].y > y - 20 && bomb[i].y < y + 70)
return 0;
else if (y + 50 > bomb[i].y - 20 && y + 50 < bomb[i].y&&bomb[i].x > x - 20 && bomb[i].x < x + 70)
return 0;
}
}
void eatcake(Cake*cake, player*p, int x, int y) {
int i;
for (i = 0; i<BOMB_NUMBER; i++) {
if (y<cake[i].y + 20 && y>cake[i].y&&cake[i].x > x - 20 && cake[i].x < x + 70) {
setfillcolor(BLACK);
fillcircle(cake[i].x, cake[i].y, 20);
p->score += 1;
randomcake(cake, i);
}
if (x<cake[i].x + 20 && x>cake[i].x&&cake[i].y > y - 20 && cake[i].y < y + 70) {
setfillcolor(BLACK);
fillcircle(cake[i].x, cake[i].y, 20);
p->score += 1;
randomcake(cake, i);
}
if (x + 50 >cake[i].x - 20 && x + 50 <cake[i].x&&cake[i].y > y - 20 && cake[i].y < y + 70) {
setfillcolor(BLACK);
fillcircle(cake[i].x, cake[i].y, 20);
p->score += 1;
randomcake(cake, i);
}
if (y + 50 > cake[i].y - 20 && y + 50 < cake[i].y&&cake[i].x > x - 20 && cake[i].x < x + 70) {
setfillcolor(BLACK);
fillcircle(cake[i].x, cake[i].y, 20);
p->score += 1;
randomcake(cake, i);
}
}
}
void randombomb(Bomb*bomb, int i) {
bomb[i].x = (rand() % 11) * 50 + 25;
bomb[i].y = (rand() % 20 + 1)*(-12);
}
void randomcake(Cake*cake, int i) {
cake[i].x = (rand() % 12) * 50 + 25;
cake[i].y = (rand() % 20 + 1)*(-10);
}
void setName(player*p) {
printf("请输入用户名:");
scanf("%s", p->name);
initgraph(600, 600);
char buf[20];
sprintf(buf, "玩家姓名:%s", p->name);
outtextxy(5, 505, buf);
}
void printScore(player*p)
{
char buf[20];
sprintf(buf, "得分:%d", p->score);
outtextxy(150, 505, buf);
}
void drawinterface() {
setbkcolor(BLACK);
line(0, 500, 600, 500);
setfillcolor(BLUE);
bar(10, 540, 125, 580);
outtextxy(48, 552, "开始");
bar(165, 540, 280, 580);
outtextxy(203, 552, "结束");
bar(320, 540, 435, 580);
bar(475, 540, 590, 580);
}
void clickBegin(MOUSEMSG m) {
while (1) {
if (MouseHit())
m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONDOWN&&m.x > 10 && m.x < 125 && m.y>540 && m.y < 580) {
setfillcolor(WHITE);
bar(10, 540, 125, 580);
Sleep(50);
setfillcolor(BLUE);
bar(10, 540, 125, 580);
outtextxy(48, 552, "开始");
break;
}
}
}
//int clickEnd(MOUSEMSG m) {
// if (MouseHit())
// m = GetMouseMsg();
// if (m.uMsg == WM_LBUTTONDOWN&&m.x > 165 && m.x < 280 && m.y>540 && m.y < 580) {
// setfillcolor(WHITE);
// bar(165, 540, 280, 580);
// Sleep(50);
// setfillcolor(BLUE);
// bar(165, 540, 280, 580);
// outtextxy(203, 552, "结束");
// return 0;
// }
//}