1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| #include<stdio.h>//C语言标准头文件 #include<graphics.h>//图形库头文件 #include<conio.h>//按键处理 #include<time.h>//随机函数 #include<mmstream.h>//多媒体库 #pragma comment(lib,"winmm.lib")
IMAGE background; IMAGE bigBird[2]; IMAGE endImg[2]; IMAGE up[2]; IMAGE down[2]; HWND hwnd;
struct bird { int x; int y; int speed; };
struct bird myBird = { 124,304,100 };
void loadResource() { loadimage(&background, "./images/background.bmp"); loadimage(&bigBird[0], "./images/birdy.bmp",48,48); loadimage(&bigBird[1], "./images/bird.bmp",48,48); loadimage(&endImg[0], "./images/endy.bmp"); loadimage(&endImg[1], "./images/end.bmp");
loadimage(&down[0], "./images/downy.bmp"); loadimage(&down[1], "./images/down.bmp");
loadimage(&up[0], "./images/upy.bmp"); loadimage(&up[1], "./images/up.bmp"); }
void drawBigbird(int x ,int y) { putimage(x, y, &bigBird[0], SRCAND); putimage(x, y, &bigBird[1], SRCPAINT); }
DWORD WINAPI playMusic(LPVOID pVoid) { mciSendString("open jump.mp3", 0, 0, 0); mciSendString("play jump.mp3 wait", 0, 0, 0); mciSendString("clos jump.mp3", 0, 0, 0); return 0; }
struct pillar { int x ; int y ; int h ; }; struct pillar zhuzi[3];
void initPillar(struct pillar zhuzi[], int i) { zhuzi[i].h = rand() % 100 + 160; zhuzi[i].y = 0; zhuzi[i].x = 288; }
void drawPillar(struct pillar zhuzi) { putimage(zhuzi.x, 0, 52, zhuzi.h,&down[0],0,320 - zhuzi.h,SRCAND); putimage(zhuzi.x, 0, 52, zhuzi.h, &down[1], 0, 320 - zhuzi.h, SRCPAINT); putimage(zhuzi.x, 512-(320-zhuzi.h), 52, 320-zhuzi.h, &up[0], 0,0, SRCAND); putimage(zhuzi.x, 512 - (320 - zhuzi.h), 52, 320 - zhuzi.h, &up[1], 0, 0, SRCPAINT);
}
void keyDown() { char userKey = '\0'; userKey = _getch(); if (userKey == ' ') { while (_getch() != ' '); } switch (userKey) { case 'w': case 'W': case 72: myBird.y -= myBird.speed; CreateThread(NULL,NULL, playMusic, NULL, NULL, NULL); break; default: break; } }
int hitFloor() { if (myBird.y <= 0 || myBird.y >= (512 - 48)) { return 1; } return 0; }
void gameOverAction() { int x = 60; int y = 608; while (y >= 240) { putimage(0, 0, &background); putimage(x, y, &endImg[0], SRCAND); putimage(x, y, &endImg[1], SRCPAINT); y -= 50; Sleep(50); } MessageBox(hwnd,"GameOver You Die!","提示",MB_OK); }
int main(void) { srand((unsigned int)time(NULL)); loadResource(); initgraph(288, 608); for (int i = 0; i < 3; i++) { initPillar(zhuzi, i); zhuzi[i].x = 288 + i * 150; } while (1) { putimage(0, 0, &background); drawBigbird(myBird.x, myBird.y); for (int i = 0; i < 3; i++) { zhuzi[i].x -= 10; if(zhuzi[i].x < (-52 - 150)) { initPillar(zhuzi, i); } } for (int i = 0; i < 3; i++) { drawPillar(zhuzi[i]); } if (hitFloor()) { gameOverAction(); break; } myBird.y += 10; if (_kbhit()) { keyDown(); } Sleep(50); } _getch(); closegraph(); return 0; }
|