#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<mmsystem.h>
#include<stdlib.h>
#include<time.h>
#pragma comment(lib,"winmm.lib")
void GameDraw();
void BirdMove();
DWORD WINAPI MusicPlay(LPVOID lpparamer);
int HitFloor();
int HitPilliar(int i);
void InitPilliar();
void LoadPilliar(int i);
void PilliarMove();
IMAGE bkground;
IMAGE bird[2];
IMAGE uppilliar[2];
IMAGE downpilliar[2];
IMAGE end[2];
struct bird
{
int x;
int y;
int speed;
int g;
};
struct pilliar
{
int x;
int y;
int height;
};
struct bird flybird = { 144,10,0,10 };
struct pilliar pilliars[3];
void GameDraw()
{
loadimage(&bkground, _T("background.bmp"));
putimage(0, 0, &bkground);
loadimage(bird, _T("birdy.bmp"));
loadimage(bird+1, _T("bird.bmp"));
putimage(flybird.x,flybird.y , bird , SRCAND);
putimage(flybird.x, flybird.y, bird+1, SRCPAINT);
loadimage(downpilliar, _T("downy.bmp"));
loadimage(downpilliar+1, _T("down.bmp"));
loadimage(uppilliar, _T("upy.bmp"));
loadimage(uppilliar + 1, _T("up.bmp"));
for (int i = 0; i < 3; i++)
{
LoadPilliar(i);
}
loadimage(end, _T("endy.bmp"));
loadimage(end + 1, _T("end.bmp"));
for (int i = 0; i < 3; i++)
{
if (HitPilliar(i))
goto again;
}
if (HitFloor())
{
again:
cleardevice();
int x, y;
x = 50, y = 240;
putimage(0, 0, &bkground);
putimage(x, y, end, SRCAND);
putimage(x, y, end + 1, SRCPAINT);
if (_kbhit())
{
char ch = _getch();
if (ch == 27)
{
exit(1);
}
if (ch == 32)
{
flybird.x = 144;
flybird.y = 10;
InitPilliar();
}
}
}
}
void BirdMove()
{
flybird.y += flybird.speed;
flybird.speed += flybird.g;
if (GetAsyncKeyState(VK_SPACE))
{
flybird.speed = -40;
CreateThread(NULL, NULL, MusicPlay, NULL, NULL, NULL);
}
}
DWORD WINAPI MusicPlay(LPVOID lpparamer)
{
mciSendString(_T("open jump.mp3 alias jump "), 0, 0, 0);
mciSendString(_T("play jump wait", ), 0, 0, 0);
mciSendString(_T("close jump"), 0, 0, 0);
return 0;
}
int HitFloor()
{
if (flybird.y <= 0 || flybird.y>=512)
return 1;
else
return 0;
}
int HitPilliar(int i)
{
if (flybird.x >= pilliars[i].x && flybird.x <= pilliars[i].x + 52 && flybird.y >= 512 - (320 - pilliars[i].height))
return 1;
if (flybird.x >= pilliars[i].x && flybird.x <= pilliars[i].x + 52 && flybird.y <= pilliars[i].height)
return 1;
return 0;
}
void InitPilliar()
{
for (int i = 0; i < 3; i++)
{
pilliars[i].x = 288 + i * 150;
pilliars[i].height = rand() % 100 + 150;
}
}
void PilliarMove()
{
int i;
for (i = 0; i < 3; i++)
{
pilliars[i].x -= 10;
if (pilliars[i].x <= 20)
{
pilliars[i].x = 288+150;
pilliars[i].height = rand() % 100 + 150;
}
}
}
void LoadPilliar(int i)
{
putimage(pilliars[i].x, 0, 52, pilliars[i].height, downpilliar, 0, 320 - pilliars[i].height, SRCAND);
putimage(pilliars[i].x, 0, 52, pilliars[i].height, downpilliar+1, 0, 320 - pilliars[i].height, SRCPAINT);
putimage(pilliars[i].x, 512 - (320 - pilliars[i].height), 52, 320 - pilliars[i].height, uppilliar, 0, 0, SRCAND);
putimage(pilliars[i].x, 512 - (320 - pilliars[i].height), 52, 320 - pilliars[i].height, uppilliar+1, 0, 0, SRCPAINT);
}
int main()
{
initgraph(288, 608);
srand((unsigned)time(NULL));
BeginBatchDraw();
InitPilliar();
while (1)
{
GameDraw();
BirdMove();
PilliarMove();
FlushBatchDraw();
Sleep(100);
}
closegraph();
}
|