|
楼主 |
发表于 2023-12-21 22:35:19
|
显示全部楼层
这个代码问题再哪里,运行不出来
#include <tchar.h>
#include <graphics.h> //图形库头文件
#include <stdlib.h> //Sleep
#include <conio.h> //_getch()按键盘
#include <stdio.h> //C语言头文件
#include <time.h> //随机函数使用
#include <mmsystem.h> //音乐播放的多媒体库
#pragma comment(lib,"winmm.lib") //链接winmm.lib这个库
//起图片名字
IMAGE background;
IMAGE mybird[2]; //鸟有两张:mybird[0]、mybird[1]
IMAGE overImg[2]; //游戏结束动画两张
IMAGE downPillar[2]; //朝下的柱子两张
IMAGE upPillar[2]; //朝上的柱子两张
struct Bird //鸟的属性
{
int x; //初始坐标
int y;
int speed; //上升速度
};
struct Pillar
{
int x; //上面柱子起始坐标
int y;
int height; //上面柱子显示高度
};
struct Bird flyBird={124,304,50}; //初始化。在窗口正中间
void loadResource(){ //加载资源。相同的操作归类处理
loadimage(&background,L"background.bmp");
loadimage(&mybird[0],L"birdy.bmp",48,48); //先是掩码图y
loadimage(&mybird[1],L"bird.bmp",48,48);
loadimage(&overImg[0],L"endy.bmp");
loadimage(&overImg[1],L"end.bmp");
loadimage(&downPillar[0],L"downy.bmp");
loadimage(&downPillar[1],L"down.bmp");
loadimage(&upPillar[0],L"upy.bmp");
loadimage(&upPillar[1],L"up.bmp");
}
void drawBird(int x,int y){ //画鸟---->透明贴图技术(白加黑去掉背景,就是裸鸟了)
//SRCAND方式贴掩码图
putimage(x,y,&mybird[0],SRCAND);
//SRCPAINT方式贴背景图
putimage(x,y,&mybird[1],SRCPAINT);
}
DWORD WINAPI playMusic(LPVOID lpParamer){ //多线程
mciSendString(L"open jump.mp3",0,0,0); //需要加L
mciSendString(L"play jump.mp3 wait",0,0,0);
mciSendString(L"close jump.mp3",0,0,0);
return 0;
}
DWORD WINAPI playMusic1(LPVOID lpParamer){ //多线程
mciSendString(L"open hit.mp3",0,0,0); //需要加L
mciSendString(L"play hit.mp3 wait",0,0,0);
mciSendString(L"close hit.mp3",0,0,0);
return 0;
}
DWORD WINAPI playMusic2(LPVOID lpParamer){ //多线程
mciSendString(L"open gameover.mp3",0,0,0); //需要加L
mciSendString(L"play gameover.mp3 wait",0,0,0);
mciSendString(L"close gameover.mp3",0,0,0);
return 0;
}
void keyDown(){ //鸟的按键处理
char userkey=_getch(); //_getch();用来接收用户按键
switch (userkey)
{
case ' ':flyBird.y-=flyBird.speed;
CreateThread(NULL,NULL,playMusic,NULL,NULL,NULL);
break;
default:break;
}
}
int hitFloor(){ //鸟撞天花板和地板
if (flyBird.y<=0||flyBird.y>=512){
CreateThread(NULL,NULL,playMusic1,NULL,NULL,NULL);
return 1;
}
else return 0;
}
int hitPillar(struct Pillar *myPillar){ //撞到柱子
for(int i=0;i<3;++i){
if (flyBird.x >= myPillar[i].x && flyBird.x <= myPillar[i].x + 52)
{
if (flyBird.y <= myPillar[i].height || flyBird.y >= (512 - 320 + myPillar[i].height))
{
CreateThread(NULL,NULL,playMusic1,NULL,NULL,NULL);
return 1;
}
}
}
return 0;
}
void actionGameOver(){
//结束图片的初始位置
int x=50;
int y=608;
while (y>=240)
{
putimage(0,0,&background);
putimage(x,y,&overImg[0],SRCAND);
putimage(x,y,&overImg[1],SRCPAINT);
y-=50;
Sleep(50);
}
CreateThread(NULL,NULL,playMusic2,NULL,NULL,NULL);
Sleep(5000); //5秒后关闭窗口
}
void initPillar(struct Pillar myPillar[],int i){ //建立柱子
myPillar[i].height=rand()%100+160; //随机数范围160~259
myPillar[i].x=228;
myPillar[i].y=0;
}
void drawPillar(struct Pillar myPillar){ //画柱子。每根柱子高度是320
//上面的柱子
putimage(myPillar.x,0,52,myPillar.height,&downPillar[0],0,320-myPillar.height,SRCAND); //掩码图
putimage(myPillar.x,0,52,myPillar.height,&downPillar[1],0,320-myPillar.height,SRCPAINT);
//下面的柱子
putimage(myPillar.x,512-(320-myPillar.height),52,320-myPillar.height,&upPillar[0],0,0,SRCAND);
putimage(myPillar.x,512-(320-myPillar.height),52,320-myPillar.height,&upPillar[1],0,0,SRCPAINT);
}
int main(){
//背景尺寸:288*608
srand((unsigned int)time(NULL)); //随机函数种子
initgraph(288,608); //创建窗口
struct Pillar myPillar[3];
for(int i=0;i<3;++i){
initPillar(myPillar,i);
//为了让柱子之间有差距
myPillar[i].x=288+i*150;
}
loadResource();
while (true)
{
putimage(0,0,&background);
drawBird(flyBird.x,flyBird.y);
flyBird.y+=5;
for(int i=0;i<3;++i){
myPillar[i].x-=5; //柱子向左移动
if(myPillar[i].x<(-52-150)){
initPillar(myPillar,i);
}
}
for(int i=0;i<3;++i){ //画柱子
drawPillar(myPillar[i]);
}
if(_kbhit()){ //一定要先判断按键是否按下
keyDown();
}
if(hitFloor()||hitPillar(myPillar)){
actionGameOver();
break;
}
Sleep(50); //休眠50毫秒
}
closegraph(); //关闭窗口
return 0;
} |
|