编辑代码

#include <iostream>
#include <string>
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>

// 定义一些常量
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int FPS = 60;

// 初始化 SDL
if (SDL_Init(SDL_INIT_EVERYTHING)!= 0) {
std::cerr << "SDL initialization failed." << std::endl;
return 1;
}

// 创建窗口
SDL_Window* window = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

// 创建渲染器
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

// 加载图像
SDL_Surface* playerImage = IMG_Load("player.png");
SDL_Surface* monsterImage = IMG_Load("monster.png");
SDL_Surface* cityImage = IMG_Load("city.png");
SDL_Surface* coinImage = IMG_Load("coin.png"); // 加载金币图像

// 加载音效
Mix_Chunk* coinDropSound = Mix_LoadWAV("coin_drop.wav"); // 加载金币掉落的音效

// 定义怪物类
class Monster {
public:
int x, y;
int speed;
int health; // 添加怪物血量

Monster(int x, int y) : x(x), y(y), speed(1), health(10) {}
};

// 定义游戏类
class Game {
public:
std::vector<SDL_Rect> map;
std::vector<Monster> monsters;
SDL_Point player; // 初始位置
int speed;
int health; // 添加人物血量
int weaponDamage; // 添加人物武器伤害

void run() {
while (true) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return;
}

if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_UP) {
player.y -= speed;
}
if (event.key.keysym.sym == SDLK_DOWN) {
player.y += speed;
}
if (event.key.keysym.sym == SDLK_LEFT) {
player.x -= speed;
}
if (event.key.keysym.sym == SDLK_RIGHT) {
player.x += speed;
}
}
}

SDL_RenderClear(renderer); // 清除屏幕

// 处理玩家移动
if (player.x < 0) {
player.x = 0;
}
if (player.x > SCREEN_WIDTH - 32) {
player.x = SCREEN_WIDTH - 32;
}
if (player.y < 0) {
player.y = 0;
}
if (player.y > SCREEN_HEIGHT - 32) {
player.y = SCREEN_HEIGHT - 32;
}

// 怪物移动
for (Monster& monster : monsters) {
monster.x += monster.speed;
}

// 碰撞检测
for (Monster& monster : monsters) {
if (SDL_HasIntersection(SDL_Rect(monster.x, monster.y, 32, 32), SDL_Rect(player.x, player.y, 32, 32))) {
player.health -= monster.speed; // 人物受到伤害
monster.health -= weaponDamage; // 怪物受到伤害
return;
}
}

// 金币掉落和拾取
if (random() % 100 == 0) {
SDL_Point coin = {random() % SCREEN_WIDTH, random() % SCREEN_HEIGHT};
coins.push_back(coin);
}

for (SDL_Point coin : coins) {
if (SDL_HasIntersection(SDL_Rect(coin.x, coin.y, 32, 32), SDL_Rect(player.x, player.y, 32, 32))) {
coins.erase(coins.begin() + &coin);
Mix_PlayChunk(coinDropSound); // 播放金币掉落的音效
}
}

// 绘制地图、怪物、玩家和城市
for (SDL_Rect& tile : map) {
SDL_RenderCopy(renderer, cityImage, NULL, &tile);
}

for (Monster& monster : monsters) {
SDL_RenderCopy(renderer, monsterImage, NULL, &monster);
}

SDL_RenderCopy(renderer, playerImage, NULL, &player);

for (SDL_Point coin : coins) {
SDL_RenderCopy(renderer, coinImage, NULL, &coin); // 显示怪物掉落的金币
}

SDL_RenderPresent(renderer); // 显示渲染结果
}
}
};

// 创建游戏对象
Game game;

// 创建地图
SDL_Rect mapTiles[6] = {
{0, 0, 32, 32},
{32, 0, 32, 32},
{64, 0, 32, 32},
{0, 32, 32, 32},
{32, 32, 32, 32},
{64, 32, 32, 32}
};

game.map = std::vector<SDL_Rect>(mapTiles, mapTiles + sizeof(mapTiles) / sizeof(SDL_Rect));

// 创建一些怪物
for (int _ = 0; _ < 10; ++_) {
Monster monster = {random() % SCREEN_WIDTH, random() % SCREEN_HEIGHT};
game.monsters.push_back(monster);
}

// 运行游戏
game.run();
未选择文件