首页 > 其他分享 >C语言 ---杂乱代码的记录

C语言 ---杂乱代码的记录

时间:2023-09-27 21:32:17浏览次数:36  
标签:ball color object paddle C语言 --- 杂乱 SDL renderer

#include <SDL2/SDL.h>

void drawHeart(SDL_Renderer* renderer, int x, int y, int size) {
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    
    // 绘制爱心形状
    for (int angle = 0; angle <= 360; angle++) {
        float radians = angle * (3.14159 / 180);
        int heartX = x + size * (16 * pow(sin(radians), 3));
        int heartY = y + size * (13 * cos(radians) - 5 * cos(2 * radians) - 2 * cos(3 * radians) - cos(4 * radians));
        SDL_RenderDrawPoint(renderer, heartX, heartY);
    }
}

int main() {
    // 初始化SDL
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("爱心图案", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    SDL_RenderClear(renderer);
    
    // 绘制爱心图案
    drawHeart(renderer, 250, 250, 100);
    
    SDL_RenderPresent(renderer);
    
    // 等待退出事件
    SDL_Event event;
    int quit = 0;
    while (!quit) {
        if (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = 1;
            }
        }
    }
    
    // 清理资源
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
}
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <Box2D/Box2D.h>

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define PADDLE_WIDTH 100
#define PADDLE_HEIGHT 20
#define BALL_RADIUS 10

// 用于存储游戏中的物体
typedef struct GameObject {
    float x;
    float y;
    float width;
    float height;
    SDL_Color color;
    b2Body* body;
} GameObject;

SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
b2World* world = NULL;

GameObject paddle;
GameObject ball;

void initSDL() {
    // 初始化SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL初始化失败:%s\n", SDL_GetError());
        exit(1);
    }

    // 创建窗口和渲染器
    window = SDL_CreateWindow("高端游戏示例", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        printf("窗口创建失败:%s\n", SDL_GetError());
        exit(1);
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL) {
        printf("渲染器创建失败:%s\n", SDL_GetError());
        exit(1);
    }
}

void initBox2D() {
    // 创建Box2D世界
    b2Vec2 gravity(0.0f, 9.8f);
    world = new b2World(gravity);

    // 创建边界
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0.0f, WINDOW_HEIGHT);
    b2Body* groundBody = world->CreateBody(&groundBodyDef);
    b2PolygonShape groundBox;
    groundBox.SetAsBox(WINDOW_WIDTH, 10.0f);
    groundBody->CreateFixture(&groundBox, 0.0f);
}

void createObject(GameObject* object, float x, float y, float width, float height, SDL_Color color, bool isDynamic) {
    object->x = x;
    object->y = y;
    object->width = width;
    object->height = height;
    object->color = color;

    // 创建物体在物理世界中的刚体
    b2BodyDef bodyDef;
    bodyDef.type = isDynamic ? b2_dynamicBody : b2_staticBody;
    bodyDef.position.Set(object->x, object->y);
    object->body = world->CreateBody(&bodyDef);

    // 创建物体的形状
    b2PolygonShape shape;
    shape.SetAsBox(object->width / 2, object->height / 2);

    // 创建物体的夹具
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    object->body->CreateFixture(&fixtureDef);
}

void handleInput() {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            exit(0);
        }
        else if (event.type == SDL_KEYDOWN) {
            switch (event.key.keysym.sym) {
                case SDLK_LEFT:
                    paddle.body->SetLinearVelocity(b2Vec2(-10.0f, 0.0f));
                    break;
                case SDLK_RIGHT:
                    paddle.body->SetLinearVelocity(b2Vec2(10.0f, 0.0f));
                    break;
            }
        }
        else if (event.type == SDL_KEYUP) {
            paddle.body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
        }
    }
}

void update() {
    // 更新物理世界
    world->Step(1.0f / 60.0f, 8, 3);

    // 更新物体位置
    paddle.x = paddle.body->GetPosition().x - paddle.width / 2;
    paddle.y = paddle.body->GetPosition().y - paddle.height / 2;

    ball.x = ball.body->GetPosition().x - ball.width / 2;
    ball.y = ball.body->GetPosition().y - ball.height / 2;
}

void render() {
    // 清空渲染器
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);

    // 绘制挡板
    SDL_SetRenderDrawColor(renderer, paddle.color.r, paddle.color.g, paddle.color.b, paddle.color.a);
    SDL_Rect paddleRect = { paddle.x, paddle.y, paddle.width, paddle.height };
    SDL_RenderFillRect(renderer, &paddleRect);

    // 绘制球体
    SDL_SetRenderDrawColor(renderer, ball.color.r, ball.color.g, ball.color.b, ball.color.a);
    SDL_Rect ballRect = { ball.x, ball.y, ball.width, ball.height };
    SDL_RenderFillRect(renderer, &ballRect);

    // 刷新渲染器显示
    SDL_RenderPresent(renderer);
}

int main() {
    initSDL();
    initBox2D();

    // 创建挡板和球体
    createObject(&paddle, WINDOW_WIDTH / 2, WINDOW_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT, { 255, 0, 0, 255 }, true);
    createObject(&ball, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, BALL_RADIUS, BALL_RADIUS, { 0, 255, 0, 255 }, true);

    while (true) {
        handleInput();
        update();
        render();
    }

    return 0;
}

标签:ball,color,object,paddle,C语言,---,杂乱,SDL,renderer
From: https://blog.51cto.com/u_16251486/7629249

相关文章

  • 无涯教程-JavaScript - VARA函数
    描述VARA函数根据样本估算方差。除数字外,计算中还包括文本和逻辑值,如TRUE和FALSE。语法VARA(value1,[value2]...)争论Argument描述Required/Optionalvalue11to255valueargumentscorrespondingtoasampleofapopulation.RequiredValue2,...OptionalN......
  • E---句子中的there 与 here
    译文:CallstodisassemblealltelescopesonMaunaKeaortobanfuturedevelopmentthere ignoretherealitythatastronomyandHawaiianculturebothseektoanswerbigquestionsaboutwhoweare,wherewecomefromandwherewearegoing.翻译:拆除莫纳克亚......
  • 【垫底模拟】CSP-46
    考场解题T1染色(color):结论+构造结论:\(1,2,3,4\)循环节染色一定合法。证明:对于\(j-i=\)奇数质数:因为:\[\text{偶数+奇数=奇数}\\\text{奇数+奇数=偶数}\]奇偶不同色,所以可以满足所有的奇数质数。对于\(j-i=\)偶数质数\(2\):\[\text{奇数+2=偶数}\\\text{偶数+......
  • Codeforces Round 900 (Div. 3) - A B C D E
    目录A.HowMuchDoesDaytonaCost?B.AleksaandStackA.HowMuchDoesDaytonaCost?判断数k包不包含在数组里面即可B.AleksaandStack选定初始数为2,3,后面的遍历法二:全奇数即可,因为奇数+奇数=偶数,奇数x3=奇数,......
  • 软工日报23-9-27
    学习时长:3h解决问题:今天学了反爬内容,学习了基础的字体反爬以及selenium库代码量:100行fromseleniumimportwebdriverimporttimebrowser=webdriver.Firefox()browser.get("https://www.baidu.com")print(browser.page_source)print("------------------------......
  • E---介词提前的定语从句
    一、问题“Carryabookwithyouatalltimes”canactuallywork,too—providingyoudipinoftenenough,sothatreadingbecomesthedefaultstatefromwhichyoutemporarilysurfacetotakecareofbusiness,beforedroppingbackdown.重点关注黄色部分,不仔细......
  • 日常记录--day10--2023-9月26日--周二
    日程:今天只有上午有课,7点起床,吃了个早饭去上课,早上第一节数据结构,学习了队列,还讲了相关应用。中午午休一个小时,下午起来干了点别的,完善了之前的代码,晚上7-9点听了下代码随想路,学了会javaweb。学了什么:可恶的Javaweb,复习了数据结构。PS:不想学习,想要成为月饼盒;......
  • 【Python进阶-PyQt5】00搭建PyQt5环境
    1.创建独立开发虚拟环境1.1虚拟环境简介我们编写的程序,有时用到的Python库是不一样的,比如说开发桌面应用程序我们主要用到PyQt5相关的Python库、开发Web应用程序我们主要用到Django相关的Python库等等。假设我们在开发桌面应用程序的时候除了PyQt5相关的Python库外,还附加了其他......
  • 日常记录--day11--2023-9月27日--周三
    日程:今天只有上午有节英语课,睡过头了,9点20才起床,怕赶不上就没有吃早饭。中午小睡半个小时,下午没课,起来学习了一下Javaweb,预习了数据结构,锻炼了一下,晚上7-9点继续javaweb。学了什么:可恶的Javaweb,继续学习Javaweb。PS:不想学习,想要成为餐巾纸;......
  • dcgm-exporter:Collect Switch Metrics和Collect Link Metrics
    在DCGM(DataCenterGPUManager)中,"CollectSwitchMetrics"和"CollectLinkMetrics"是两个功能选项,用于收集关于GPU交换机和连接的指标数据。它们的含义如下:CollectSwitchMetrics(收集交换机指标)在GPU集群中,GPU交换机是用于处理GPU设备之间通信和数据传输的关键组......