本程序使用VS2022编写并打包,预先安装好easyx图形库
tip:记得设置字符集使用多字节字符集还有设置静态编译(发给别人玩)
在b站上学习并自己加以修改
在网络上下载了一些声音素材和图片素材
源码程序与素材下载
链接: https://pan.baidu.com/s/17ddiff2SjVT_ueQY_VpZtw?pwd=cdzy 提取码: cdzy
有打包好的游戏文件可以下载体验
原来想设置鼠标点击的,但是感觉没有键盘灵敏,经常没反应,不知道啥原因,所以还是用键盘踏实;
操作方式:
Q小跳 E大跳 T加速 Y减速 空格开始、继续、暂停
代码如下
#include <graphics.h>
#include <stdio.h>
#include <easyx.h>
#include <time.h>
#include <mmsystem.h>
#include<conio.h>
#pragma comment(lib,"winmm.lib")
#define WIDTH 288
#define HIGHT 512
int score = 0;
int flag = 1;
int speed = 20;
unsigned long t1, t2;
IMAGE backimg;
IMAGE start[2];
IMAGE gameover[2];
struct Land
{
int x, y;
int vx;
IMAGE img;
}land;
struct Bird
{
int x, y;
int vy;
int g;
int frame;
IMAGE img[7][2];
}bird;
struct Pipe
{
int x1, y1;
int x2, y2;
int vx;
IMAGE img[2];
}pipe;
void GameInit()
{
mciSendString("open src/bgm.mp3",0,0,0);
mciSendString("play src/bgm.mp3 repeat",0, 0, 0);
srand(time(0));
initgraph(WIDTH, HIGHT);
SetWindowText(GetHWnd(), "飞翔的Bird by 话说");
loadimage(&backimg,"src/background.bmp");
loadimage(&land.img, "src/land.bmp");
land.x = 0;
land.y = 446;
land.vx = 2;
t1 = GetTickCount();
t2 = GetTickCount();
loadimage(&start[0], "src/start.bmp");
loadimage(&start[1], "src/starty.bmp");
loadimage(&bird.img[0][0], "src/1.bmp");
loadimage(&bird.img[0][1], "src/01.bmp");
loadimage(&bird.img[1][0], "src/2.bmp");
loadimage(&bird.img[1][1], "src/02.bmp");
loadimage(&bird.img[2][0], "src/3.bmp");
loadimage(&bird.img[2][1], "src/03.bmp");
loadimage(&bird.img[3][0], "src/4.bmp");
loadimage(&bird.img[3][1], "src/04.bmp");
loadimage(&bird.img[4][0], "src/5.bmp");
loadimage(&bird.img[4][1], "src/05.bmp");
loadimage(&bird.img[5][0], "src/6.bmp");
loadimage(&bird.img[5][1], "src/06.bmp");
loadimage(&bird.img[6][0], "src/7.bmp");
loadimage(&bird.img[6][1], "src/07.bmp");
loadimage(&gameover[0], "src/end.bmp");
loadimage(&gameover[1], "src/endy.bmp");
bird.x = 20;
bird.y = 100;
bird.vy = 0;
bird.g = 1;
bird.frame = 0;
pipe.x1 = WIDTH;
pipe.y1 =rand()%256;
pipe.x2 = WIDTH+220;
pipe.y2 = rand() % 256;
pipe.vx = -2;
flag = 1;
speed = 20;
loadimage(&pipe.img[0], "src/down.bmp");
loadimage(&pipe.img[1], "src/up.bmp");
}
void GameDraw()
{
BeginBatchDraw();
putimage(0, 0, &backimg);
putimage(pipe.x1, pipe.y1 + 160, &pipe.img[1]);
putimage(pipe.x2, pipe.y2 + 160, &pipe.img[1]);
putimage(land.x, land.y, &land.img);
putimage(287+land.x, land.y, &land.img);
putimage(pipe.x1, pipe.y1 - 300, &pipe.img[0]);
putimage(pipe.x2, pipe.y2 - 300, &pipe.img[0]);
putimage(bird.x, bird.y, &bird.img[bird.frame][1], SRCAND);
putimage(bird.x, bird.y, &bird.img[bird.frame][0], SRCINVERT);
TCHAR s[30];
TCHAR sp[30];
_stprintf_s(s, _T("%d"), score);
_stprintf_s(sp, _T("%d"), 30-speed);
setbkmode(TRANSPARENT);
settextcolor(RGB(30, 30, 30));
settextstyle(15, 0, "黑体");
outtextxy(5, 485, "得分:");
outtextxy(45, 485, s);
outtextxy(60, 485, "难度:");
outtextxy(105, 485, sp);
Sleep(speed);
EndBatchDraw();
}
void GameUpdate()
{
t2 = GetTickCount();
if (t2 - t1 > 30)
{
land.x -= land.vx;
t1 = t2;
}
if (land.x < -288)
{
land.x = 0;
}
bird.vy += bird.g;
bird.y += bird.vy;
pipe.x1 += pipe.vx;
pipe.x2 += pipe.vx;
if (pipe.x1<=-52)
{
pipe.x1=pipe.x2+220;
}
if (pipe.x2 <= -52)
{
pipe.x2 = pipe.x1 + 220;
}
if (bird.x+28==pipe.x1||bird.x+28==pipe.x2)
{
score++; mciSendString("play src/get1.mp3", 0, 0, 0);
}
}
void click()
{
char key;
if (_kbhit())
{
key = _getch();
if (key == 'q'|| key == 'Q')
{
bird.vy = -10; bird.frame++;
}
if (key == 'e' || key == 'E')
{
bird.vy = -20; bird.frame++;
mciSendString("play src/jump.mp3", 0, 0, 0);
}
if (key==' ')
{
while (1)
{
if (_getch() == ' ')
return;
}
}
if (key=='T'||key=='t')
{
if (speed >=1)speed -= 1;
if (speed < 1 ) speed = 1;
}
if (key == 'Y' || key == 'y')
{
if (speed <=30)speed += 1;
if (speed > 30) speed = 30;
}
}
if (bird.frame >= 6)
{
bird.frame = 0;
}
}
void Gameover()
{
if (pipe.x1 + 2 < bird.x +28&& pipe.x1 + 50 > bird.x+28)
{
if ((pipe.y1 - 300 < bird.y + 24 && pipe.y1 + 20 > bird.y + 24) || (pipe.y1 + 160 < bird.y + 24 && pipe.y1 + 480 > bird.y + 24))
{
mciSendString("close src/bgm.mp3", 0, 0, 0);
mciSendString("play src/gameover.mp3", 0, 0, 0);
putimage(42, 202, &gameover[1], SRCAND);
putimage(42, 202, &gameover[0], SRCINVERT);
while (1)
{
if (_getch() == ' ')
{
score = 0;
GameInit();
return;
}
}
}
}
if (pipe.x2 + 2 < bird.x + 28 && pipe.x2 + 50 > bird.x + 28)
{
if ((pipe.y2 - 300 < bird.y + 24 && pipe.y2 + 20 > bird.y + 24) || (pipe.y2 + 160 < bird.y + 24 && pipe.y2 + 480 > bird.y + 24))
{
mciSendString("close src/bgm.mp3", 0, 0, 0);
mciSendString("play src/gameover.mp3", 0, 0, 0);
putimage(42, 202, &gameover[1], SRCAND);
putimage(42, 202, &gameover[0], SRCINVERT);
while (1)
{
if (_getch() == ' ')
{
score = 0;
GameInit();
return;
}
}
}
}
if ((bird.y + 24 > land.y))
{
mciSendString("close src/bgm.mp3", 0, 0, 0);
mciSendString("play src/gameover.mp3", 0, 0, 0);
putimage(42, 202, &gameover[1], SRCAND);
putimage(42, 202, &gameover[0], SRCINVERT);
while (1)
{
if (_getch() == ' ')
{
score = 0;
GameInit();
return;
}
}
}
}
void Gamestart()
{
while (1)
{
putimage(0, 0, &start[1], SRCAND);
putimage(0, 0, &start[0], SRCINVERT);
if (_getch() == ' ')
{
score = 0; flag = 0;
return;
}
}
}
int main()
{
GameInit();
while (1)
{
GameDraw();
if (flag)
Gamestart();
GameUpdate();
click();
Gameover();
}
return 0;
}