使用问题

0
#include <stdio.h>
#include <easyx.h>
#include <time.h>
#include <list>
#include <string>
#include <cmath>
#include <graphics.h>

using namespace std;

// 位置结构体
struct FVector
{
    float x, y;
    FVector operator*(float s)
    {
        return {x * s, y * s};
    }
    FVector operator+(const FVector& Vector)
    {
        return {x + Vector.x, y + Vector.y};
    }
    FVector operator-(const FVector& Vector)
    {
        return {x - Vector.x, y - Vector.y};
    }
    float length()
    {
        return sqrt(x * x + y * y);
    }
    FVector normalization()
    {
        float xx = sqrt(x * x + y * y);
        return {x / xx, y / xx};
    }
};

// 定义一个玩家球的类型
struct FPlayerSphere
{
    FVector Position;
    float radius;
    float speed;
    COLORREF color;
    wstring name;
};

// 定义一个食物的类型
struct FFood
{
    FVector Position;
    float radius;
    COLORREF color;
};

FPlayerSphere MyPlayerSphere;
list<FPlayerSphere> SphereList;
list<FFood> FoodList;

void InitGame()
{
    setbkcolor(WHITE);
    cleardevice();

    // 初始化自己的球
    MyPlayerSphere.Position = {500, 200};
    MyPlayerSphere.radius = 50;
    MyPlayerSphere.speed = 0.5f;
    MyPlayerSphere.color = RGB(145, 233, 251);
    MyPlayerSphere.name = L"小小呀";

    // 初始化食物
    for (int i = 0; i < 1000; i++)
    {
        FFood food;
        food.Position = {(float)(rand() % 960), (float)(rand() % 540)};
        food.radius = (rand() % 3) + 1;
        food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
        FoodList.push_back(food);
    }
}

void DrawGame()
{
    cleardevice();
    setlinecolor(BLACK);

    // 绘制网格线
    for (int i = 0; i < 550; i += 30)
        line(0, i, 960, i);
    for (int i = 0; i < 970; i += 30)
        line(i, 0, i, 540);

    // 显示食物
    for (auto food : FoodList)
    {
        setfillcolor(food.color);
        solidcircle(food.Position.x, food.Position.y, food.radius);
    }

    // 绘制自己的球
    setfillcolor(MyPlayerSphere.color);
    solidcircle(MyPlayerSphere.Position.x, MyPlayerSphere.Position.y, MyPlayerSphere.radius);

// 绘制自己的名字
wstring wideName = MyPlayerSphere.name;
char narrowName[50];
wcstombs(narrowName, wideName.c_str(), sizeof(narrowName));
setbkmode(TRANSPARENT);
settextcolor(BLACK);
outtextxy((int)(MyPlayerSphere.Position.x), (int)(MyPlayerSphere.Position.y), narrowName);


    // 绘制其他球
    for (auto sphere : SphereList)
    {
        setfillcolor(sphere.color);
        solidcircle(sphere.Position.x, sphere.Position.y, sphere.radius);
    }
}

void UpdateGame()
{
    // 更新游戏数据
    FVector move_vector = {0, 0};

    // 处理玩家球的移动
    if (GetAsyncKeyState('W'))
    {
        MyPlayerSphere.Position.y -= MyPlayerSphere.speed;
        move_vector.y -= 1;
    }
    if (GetAsyncKeyState('S'))
    {
        MyPlayerSphere.Position.y += MyPlayerSphere.speed;
        move_vector.y += 1;
    }
    if (GetAsyncKeyState('A'))
    {
        MyPlayerSphere.Position.x -= MyPlayerSphere.speed;
        move_vector.x -= 1;
    }
    if (GetAsyncKeyState('D'))
    {
        MyPlayerSphere.Position.x += MyPlayerSphere.speed;
        move_vector.x += 1;
    }
    if (GetAsyncKeyState(VK_SPACE))
    {
        if (!(move_vector.x == 0 && move_vector.y == 0))
            move_vector = move_vector.normalization();
        FVector endPoint = MyPlayerSphere.Position + move_vector * 100;
        line(MyPlayerSphere.Position.x, MyPlayerSphere.Position.y, endPoint.x, endPoint.y);
        FPlayerSphere Sphere;
        Sphere.Position = endPoint;
        Sphere.color = MyPlayerSphere.color;
        if (MyPlayerSphere.radius > 20)
        {
            Sphere.radius = 5;
            MyPlayerSphere.radius -= 5;
        }
        printf("%f\n", MyPlayerSphere.radius);
        SphereList.push_back(Sphere);
    }
    
    // 球的碰撞判断
    auto Iterator = FoodList.begin();
    while (Iterator != FoodList.end())
    {
        float length = (Iterator->Position - MyPlayerSphere.Position).length();
        if (length < MyPlayerSphere.radius - Iterator->radius)
        {
            // 食物被吃掉了
            Iterator = FoodList.erase(Iterator);
            MyPlayerSphere.radius += 0.02;
        }
        else
        {
            ++Iterator;
        }
    }

    // 如果食物只剩下200个的时候重新生成500个
    if (FoodList.size() < 200)
    {
        for (int i = 0; i < 500; i++)
        {
            FFood food;
            food.Position = {(float)(rand() % 960), (float)(rand() % 540)};
            food.radius = (rand() % 3) + 1;
            food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
            FoodList.push_back(food);
        }
    }
}

int main()
{
    // 初始化随机数种子
    srand(time(NULL));
    
    // 初始化一个绘图窗口
    initgraph(960, 540);
    
    InitGame();
    
    while (true)
    {
        BeginBatchDraw();
        DrawGame();
        UpdateGame();
        EndBatchDraw();
    }

    closegraph();
    return 0;
}

安装easyx库后并且配置了一遍,然后运行时报错setbkcolor(WHITE);这种函数一直报错[Error] 'setlinecolor' was not declared in this scope类似这种,查询gpt也没解决

0

你创建的代码是 .c 吗?

如果是,请改为 .cpp 扩展名。

如果不是,请详细描述你的操作过程,以及具体的错误提示。

ava
慢羊羊

2024-3-18

技术讨论社区
相关提问