用C语言写一个切水果的小游戏
时间: 2023-10-07 17:04:05 浏览: 91
好的,我可以帮您提供一个基础的思路,您可以根据需求进行修改和完善。
思路:
1. 首先,需要随机生成水果的种类和下落的位置。
2. 使用图形库(如graphics.h)绘制水果和刀的图形。
3. 通过鼠标或键盘控制刀的移动,实现切水果的操作。
4. 判断水果是否被切开,如果被切开则得分,否则减少生命值。
5. 限定游戏时间或生命值,当时间到或生命值为0时,游戏结束,显示得分。
代码示例:
```c
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 640
#define HEIGHT 480
#define FRUIT_NUM 3
typedef struct {
int x;
int y;
int type;
int state; // 0 - 未切开,1 - 切开
} Fruit;
typedef struct {
int x;
int y;
int state; // 0 - 静止,1 - 移动
} Knife;
int score = 0; // 得分
int life = 3; // 生命值
void draw_fruit(Fruit f);
void draw_knife(Knife k);
void draw_score();
void draw_life();
int check_fruit(Fruit f, Knife k);
void update_fruit(Fruit *f);
void update_knife(Knife *k);
void game_over();
int main()
{
initgraph(WIDTH, HEIGHT);
srand((unsigned int)time(NULL));
Fruit fruits[FRUIT_NUM];
Knife knife = {WIDTH / 2, HEIGHT - 50, 0};
setbkcolor(COLOR(255, 255, 255));
while (1) {
cleardevice();
for (int i = 0; i < FRUIT_NUM; i++) {
if (fruits[i].y > HEIGHT) {
update_fruit(&fruits[i]);
}
else {
fruits[i].y += 2;
draw_fruit(fruits[i]);
if (check_fruit(fruits[i], knife)) {
score += 10;
fruits[i].state = 1;
}
else if (fruits[i].y > HEIGHT - 30) {
life--;
update_fruit(&fruits[i]);
}
}
}
if (life <= 0) {
game_over();
break;
}
if (kbhit()) {
char ch = getch();
if (ch == 'a') {
knife.state = 1;
knife.x -= 10;
}
else if (ch == 'd') {
knife.state = 1;
knife.x += 10;
}
}
if (knife.x < 0) {
knife.x = 0;
}
else if (knife.x > WIDTH) {
knife.x = WIDTH;
}
draw_knife(knife);
draw_score();
draw_life();
delay(10);
}
getch();
closegraph();
return 0;
}
void draw_fruit(Fruit f)
{
switch (f.type) {
case 1:
setfillcolor(COLOR(255, 0, 0));
break;
case 2:
setfillcolor(COLOR(0, 255, 0));
break;
case 3:
setfillcolor(COLOR(0, 0, 255));
break;
}
if (f.state == 0) {
fillcircle(f.x, f.y, 20);
setfillcolor(COLOR(255, 255, 255));
fillcircle(f.x, f.y, 15);
}
}
void draw_knife(Knife k)
{
setfillcolor(COLOR(0, 0, 0));
fillrectangle(k.x - 10, k.y - 15, k.x + 10, k.y + 15);
if (k.state == 1) {
setfillcolor(COLOR(255, 255, 255));
fillrectangle(k.x - 10, k.y - 15, k.x + 10, k.y + 15);
k.state = 0;
}
}
void draw_score()
{
settextcolor(COLOR(0, 0, 255));
settextstyle(20, 0, _T("Arial"));
char buf[16];
sprintf(buf, "Score: %d", score);
outtextxy(10, 10, buf);
}
void draw_life()
{
settextcolor(COLOR(255, 0, 0));
settextstyle(20, 0, _T("Arial"));
char buf[16];
sprintf(buf, "Life: %d", life);
outtextxy(WIDTH - 100, 10, buf);
}
int check_fruit(Fruit f, Knife k)
{
if (f.state == 1) {
return 0;
}
if (k.y - f.y > 20 || f.y - k.y > 15 || abs(k.x - f.x) > 20) {
return 0;
}
else {
return 1;
}
}
void update_fruit(Fruit *f)
{
f->type = rand() % 3 + 1;
f->x = rand() % (WIDTH - 40) + 20;
f->y = rand() % 100 - 200;
f->state = 0;
}
void game_over()
{
cleardevice();
settextcolor(COLOR(255, 0, 0));
settextstyle(50, 0, _T("Arial"));
outtextxy(150, 200, _T("Game Over"));
settextcolor(COLOR(0, 0, 255));
settextstyle(20, 0, _T("Arial"));
char buf[16];
sprintf(buf, "Score: %d", score);
outtextxy(270, 250, buf);
}
```
这是一个基础的切水果小游戏,您可以根据自己的需求进行修改和完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![torrent](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)