cipisi 2023-02-05 13:03 采纳率: 77.8%
浏览 46

c++贪吃蛇 蛇不吃食物

为什么蛇不吃食物

#include<iostream>
#include<graphics.h>
#include<time.h>
using namespace std;

int bodynu=3;
int i2 = 0;
int defen = 0;
IMAGE img_yuan;
typedef struct pointXY
{
    int x;
    int y;
}MYPOINT;
//定义蛇
class body
{
public:
    bool die;
    char postion;
    MYPOINT nu[1000];
};
body qwer[1000];
class food2
{
public:
    int x;
    int y;
    int nu;
};
//蛇移动
void bodymove(body* pthis)
{
    int i = 5;
    //键盘操作
    if (GetAsyncKeyState(VK_UP))
    {
        pthis->postion = 'u';
    }
    if (GetAsyncKeyState(VK_DOWN))
    {
        pthis->postion = 'd';
    }
    if (GetAsyncKeyState(VK_LEFT))
    {
        pthis->postion = 'l';
    }
    if (GetAsyncKeyState(VK_RIGHT))
    {
        pthis->postion = 'r';
    }
}
void initbody(body* pthis)
{
    pthis->nu[0].x = 550;
    pthis->nu[0].y = 350;

    pthis->nu[1].x = 540;
    pthis->nu[1].y = 400;

    pthis->nu[2].x = 530;
    pthis->nu[2].y = 450;

    pthis->postion = 'r';
}
//生成蛇
void drawbody(body*pthis)
{
    for(int i5=0;i5<bodynu;i5++)
    {
        putimage(pthis->nu[i5].x, pthis->nu[i5].y, &img_yuan);
    }
}
void bodymove2(body* pthis)
{
    for (int k = bodynu; k > 0; k--)
    {
        pthis->nu[k].x = pthis->nu[k - 1].x;
        pthis->nu[k].y = pthis->nu[k - 1].y;
    }
    switch (pthis->postion)
    {
    case 'u':
        pthis->nu[0].y -= 10;
        break;
    case 'd':
        pthis->nu[0].y += 10;
        break;
    case 'l':
        pthis->nu[0].x -= 10;
        break;
    case 'r':
        pthis->nu[0].x += 10;
        break;
    default:
        break;
    }
}
//背景
void wall()
{
    setlinecolor(RED);
    setlinestyle(PS_SOLID, 5);
    rectangle(0, 3, 1100, 600);
    HRGN rgn = CreateRectRgn(3, 5, 1096, 596);
    setcliprgn(rgn);
}
//生成食物
void initfood(food2* pthis)
{
    pthis->x = rand() % 1000;
    pthis->y = rand() % 500;
    pthis->nu++;
}
void food(food2*pthis)
{
    //生成食物
    setfillcolor(GREEN);
    solidrectangle(pthis->x, pthis->y, pthis->x + 5, pthis-> y+ 5);
}
void eatfood(food2* pthis,body* pthis2)
{
    if (pthis2->nu[0].x == pthis->x && pthis2->nu[0].y == pthis->y)
    {
        defen += 1;
        pthis->nu = 0;
    }
}
void init(food2*pthis)
{
    pthis->nu = 0;
    pthis->x = 0;
    pthis->y = 0;
}
int main()
{
    initgraph(1100, 600);
    loadimage(&img_yuan, _T("D:/c++/项目/贪吃蛇/yuan.jpg"),10,10);
    srand((unsigned int)time(NULL));
    body qwe;

    food2 p;
    qwe.die = false;
    wall();
    init(&p);
    initbody(&qwe);
    while (true)
    {
        BeginBatchDraw();
        clearcliprgn();
        if (p.nu == 0)
        {
            initfood(&p);
        }
        food(&p);
        eatfood(&p,&qwe);
        bodymove(&qwe);
        bodymove2(&qwe);
        drawbody(&qwe);
        
        EndBatchDraw();
        Sleep(100);
    }
    getchar();
    return 0;
}


  • 写回答

1条回答 默认 最新

  • Eliot5566 2023-02-05 18:08
    关注

    你代码中并没有食物的生成和吃食物的代码,因此蛇不会吃食物。
    通过在 initfood 函数和 food 函数中实现食物的生成,在 eatfood 函数中实现蛇吃食物的逻辑,来解决此问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 2月5日

悬赏问题

  • ¥15 请教合适的模型做回归
  • ¥15 stm32cubemx生成代码后利用printf输出浮点数卡死。
  • ¥15 关于#windows#的问题:我是想本地生成pdf的标签文件然后扔给其他的电脑打印,是修改pdf打印格式打印出来这个pdf给那边打印,还是说有其他办法可以生成文件给人家打印
  • ¥15 dy/dx-y/x=x^2sinx通解 解题过程 用一阶线性非齐次公式
  • ¥15 ERNIE大模型的系统记忆没有生效
  • ¥15 如何用C++在CAD二次开发中,对标注AcDbDimension文字的高度与宽度的获取方法?
  • ¥15 C++检查代码什么问题
  • ¥20 matlab写的上位机UDP速率不到1M
  • ¥100 tomcat应用从Termux上连接不到Linux上的MySQL服务器(相关搜索:服务器)
  • ¥15 flutter 错误RROR:flutter/runtime/dart_vm_initializer.cc(41)