#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