大鱼吃小鱼代码c++
时间: 2023-12-10 10:32:36 浏览: 287
这是一个使用C++编写的大鱼吃小鱼游戏源代码程序。该程序使用了EasyX库,并且可以在CodeBlocks IDE中运行。该程序是作者在大一时期完成的funcode课设,也是作者进入大学本科后完成的第一个比较完整的程序。作者在编写这个程序时遇到了一些困难,主要是因为刚接触完C++的基础语法,就开始去编写这样一个对当时的作者来说“十分不可思议”的“大程序”。包括要去学习一个新的库的用法,程序的组织等等。如果你对这个程序感兴趣,可以通过引用中提供的链接下载工程文件并进行学习和实践。
相关问题
大鱼吃小鱼游戏C++代码
下面是一份大鱼吃小鱼游戏的C语言代码,供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10 // 游戏地图大小
#define FISH_NUM 20 // 初始小鱼数量
#define BIG_FISH 1 // 大鱼标记
#define SMALL_FISH 2 // 小鱼标记
int map[SIZE][SIZE]; // 游戏地图
int fish_num = FISH_NUM; // 剩余小鱼数量
int score = 0; // 得分
// 在地图上随机生成指定数量的小鱼
void init_fish() {
int i, x, y;
for (i = 0; i < FISH_NUM; i++) {
x = rand() % SIZE;
y = rand() % SIZE;
if (map[x][y] == 0) {
map[x][y] = SMALL_FISH;
} else {
i--;
}
}
}
// 在地图上随机生成一个大鱼
void init_big_fish() {
int x, y;
while (1) {
x = rand() % SIZE;
y = rand() % SIZE;
if (map[x][y] == 0) {
map[x][y] = BIG_FISH;
break;
}
}
}
// 打印游戏地图
void print_map() {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
switch (map[i][j]) {
case 0: printf(". "); break;
case BIG_FISH: printf("B "); break;
case SMALL_FISH: printf("s "); break;
}
}
printf("\n");
}
}
// 判断大鱼是否能吃掉小鱼
int can_eat(int big_x, int big_y, int small_x, int small_y) {
if (big_x == small_x && abs(big_y - small_y) == 1) {
return 1;
}
if (big_y == small_y && abs(big_x - small_x) == 1) {
return 1;
}
return 0;
}
// 大鱼吃小鱼
void eat(int big_x, int big_y, int small_x, int small_y) {
map[small_x][small_y] = 0;
fish_num--;
score++;
map[big_x][big_y] = BIG_FISH;
}
int main() {
srand((unsigned int)time(NULL)); // 设置随机种子
init_fish(); // 初始化小鱼
init_big_fish(); // 初始化大鱼
while (fish_num > 0) {
print_map(); // 打印游戏地图
int big_x, big_y, small_x, small_y;
printf("请输入大鱼的坐标x,y:");
scanf("%d,%d", &big_x, &big_y);
if (big_x < 0 || big_x >= SIZE || big_y < 0 || big_y >= SIZE) {
printf("坐标不合法!\n");
continue;
}
if (map[big_x][big_y] != BIG_FISH) {
printf("该位置没有大鱼!\n");
continue;
}
printf("请输入小鱼的坐标x,y:");
scanf("%d,%d", &small_x, &small_y);
if (small_x < 0 || small_x >= SIZE || small_y < 0 || small_y >= SIZE) {
printf("坐标不合法!\n");
continue;
}
if (map[small_x][small_y] != SMALL_FISH) {
printf("该位置没有小鱼!\n");
continue;
}
if (!can_eat(big_x, big_y, small_x, small_y)) {
printf("大鱼不能吃小鱼!\n");
continue;
}
eat(big_x, big_y, small_x, small_y); // 大鱼吃小鱼
}
printf("恭喜您获得了%d分!\n", score);
return 0;
}
```
在该代码中,我们使用二维数组来表示游戏地图。地图大小为10x10,初始小鱼数量为20。大鱼和小鱼的标记分别为1和2。游戏开始时,我们在地图上随机生成指定数量的小鱼和一个大鱼。
在每个回合中,我们先打印游戏地图,然后要求玩家输入大鱼和小鱼的坐标。如果输入的坐标不合法或者该位置没有相应的鱼,则要求玩家重新输入。如果大鱼不能吃小鱼,则提示玩家重新输入,否则大鱼将吃掉小鱼,得分加一。
当所有小鱼都被吃掉后,游戏结束,显示得分。
大鱼吃小鱼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;
}
--相关问题--:
阅读全文
相关推荐
















