大鱼吃小鱼c++代码
时间: 2023-12-06 16:37:55 浏览: 182
大鱼吃小鱼源代码C++
以下是一个使用EasyX库实现的大鱼吃小鱼游戏的C++代码示例:
```cpp
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_FISH 20
#define MAX_SHARK 3
IMAGE bg, fish[4], shark[4];
int fish_x[MAX_FISH], fish_y[MAX_FISH], fish_kind[MAX_FISH], fish_speed[MAX_FISH];
int shark_x[MAX_SHARK], shark_y[MAX_SHARK], shark_kind[MAX_SHARK], shark_speed[MAX_SHARK];
int score = 0;
void init();
void show();
void updateWithoutInput();
void updateWithInput();
bool isCollide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
int main()
{
init();
while (true)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
void init()
{
srand((unsigned)time(NULL));
initgraph(640, 480);
loadimage(&bg, _T("bg.jpg"));
loadimage(&fish[0], _T("fish1.png"));
loadimage(&fish[1], _T("fish2.png"));
loadimage(&fish[2], _T("fish3.png"));
loadimage(&fish[3], _T("fish4.png"));
loadimage(&shark[0], _T("shark1.png"));
loadimage(&shark[1], _T("shark2.png"));
loadimage(&shark[2], _T("shark3.png"));
loadimage(&shark[3], _T("shark4.png"));
for (int i = 0; i < MAX_FISH; i++)
{
fish_x[i] = rand() % 540 + 50;
fish_y[i] = rand() % 380 + 50;
fish_kind[i] = rand() % 4;
fish_speed[i] = rand() % 4 + 1;
}
for (int i = 0; i < MAX_SHARK; i++)
{
shark_x[i] = rand() % 540 + 50;
shark_y[i] = rand() % 380 + 50;
shark_kind[i] = rand() % 4;
shark_speed[i] = rand() % 4 + 1;
}
}
void show()
{
putimage(0, 0, &bg);
for (int i = 0; i < MAX_FISH; i++)
{
putimage(fish_x[i], fish_y[i], &fish[fish_kind[i]]);
}
for (int i = 0; i < MAX_SHARK; i++)
{
putimage(shark_x[i], shark_y[i], &shark[shark_kind[i]]);
}
settextcolor(WHITE);
settextstyle(20, 0, _T("宋体"));
TCHAR s[100];
_stprintf_s(s, _T("得分:%d"), score);
outtextxy(10, 10, s);
}
void updateWithoutInput()
{
for (int i = 0; i < MAX_FISH; i++)
{
fish_x[i] += fish_speed[i];
if (fish_x[i] > 640)
{
fish_x[i] = -fish[i].getwidth();
fish_y[i] = rand() % 380 + 50;
fish_kind[i] = rand() % 4;
fish_speed[i] = rand() % 4 + 1;
}
}
for (int i = 0; i < MAX_SHARK; i++)
{
shark_x[i] += shark_speed[i];
if (shark_x[i] > 640)
{
shark_x[i] = -shark[i].getwidth();
shark_y[i] = rand() % 380 + 50;
shark_kind[i] = rand() % 4;
shark_speed[i] = rand() % 4 + 1;
}
}
}
void updateWithInput()
{
if (_kbhit())
{
char ch = _getch();
if (ch == ' ')
{
for (int i = 0; i < MAX_FISH; i++)
{
if (isCollide(fish_x[i], fish_y[i], fish[fish_kind[i]].getwidth(), fish[fish_kind[i]].getheight(),
320, 240, shark[shark_kind[0]].getwidth(), shark[shark_kind[0]].getheight()))
{
score++;
fish_x[i] = -fish[fish_kind[i]].getwidth();
fish_y[i] = rand() % 380 + 50;
fish_kind[i] = rand() % 4;
fish_speed[i] = rand() % 4 + 1;
}
}
}
}
for (int i = 0; i < MAX_SHARK; i++)
{
if (isCollide(shark_x[i], shark_y[i], shark[shark_kind[i]].getwidth(), shark[shark_kind[i]].getheight(),
320, 240, shark[shark_kind[0]].getwidth(), shark[shark_kind[0]].getheight()))
{
score = 0;
break;
}
}
}
bool isCollide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
if (x1 + w1 < x2 || x1 > x2 + w2 || y1 + h1 < y2 || y1 > y2 + h2)
{
return false;
}
return true;
}
--相关问题--:
阅读全文