#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;
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();
未选择文件