#include <SDL.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <SDL2/SDL.h>
#include <vector>
#include <cstdlib>
#include <ctime>
const int TILE_SIZE = 32; // Rozmiar kafelka w pikselach
const int WIDTH = 20; // Liczba kafelków w poziomie
const int HEIGHT = 10; // Liczba kafelków w pionie
struct Player {
int x, y;
int hp;
int gold;
};
void initGame(Player &p, std::vector<std::string> &mapa) {
p.x = 1;
p.y = 1;
p.hp = 100;
p.gold = 0;
mapa = {
"####################",
"#P...G....#........#",
"#.###.#####.########",
"#.#.......#.#......#",
"#.#.#####.#.#.####.#",
"#...#...G.#...#..#.#",
"#####.#####.###.##.#",
"#.....M.....#...M..#",
"#.#########.########",
"####################"
};
}
int main(int argc, char* argv[]) {
// Inicjalizacja SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "Błąd inicjalizacji SDL: " << SDL_GetError() << std::endl;
return 1;
}
// Tworzenie okna
SDL_Window* window = SDL_CreateWindow("Dungeon Crawler SDL2",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE,
SDL_WINDOW_SHOWN);
if (!window) {
std::cout << "Błąd tworzenia okna: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
// Tworzenie renderera
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cout << "Błąd tworzenia renderera: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
srand(time(0));
Player p;
std::vector<std::string> mapa;
initGame(p, mapa);
bool running = true;
SDL_Event e;
while (running) {
// Obsługa zdarzeń (klawiatura, zamknięcie okna)
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
running = false;
} else if (e.type == SDL_KEYDOWN) {
int newX = p.x;
int newY = p.y;
switch (e.key.keysym.sym) {
case SDLK_w:
case SDLK_UP:
newY--; break;
case SDLK_s:
case SDLK_DOWN:
newY++; break;
case SDLK_a:
case SDLK_LEFT:
newX--; break;
case SDLK_d:
case SDLK_RIGHT:
newX++; break;
case SDLK_q:
case SDLK_ESCAPE:
running = false; break;
}
// Kolizje i mechanika gry
if (mapa[newY][newX] != '#') {
char target = mapa[newY][newX];
if (target == 'G') {
p.gold += 10;
std::cout << "Znalazłeś złoto! Złoto: " << p.gold << std::endl;
mapa[newY][newX] = '.'; // Usuń złoto
} else if (target == 'M') {
int damage = rand() % 20 + 10;
p.hp -= damage;
std::cout << "Potwór zaatakował! Pozostałe HP: " << p.hp << std::endl;
if (p.hp <= 0) {
std::cout << "=== KONIEC GRY (GAME OVER) ===" << std::endl;
running = false;
}
mapa[newY][newX] = '.'; // Pokonaj potwora
}
p.x = newX;
p.y = newY;
}
}
}
// --- RENDEROWANIE GRAFIKI ---
// Czyszczenie ekranu (ciemnoszare tło)
SDL_SetRenderDrawColor(renderer, 30, 30, 30, 255);
SDL_RenderClear(renderer);
// Rysowanie mapy
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
SDL_Rect rect = { x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
char tile = mapa[y][x];
if (tile == '#') {
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255); // Ściana (szara)
} else if (tile == 'G') {
SDL_SetRenderDrawColor(renderer, 255, 215, 0, 255); // Złoto (żółte)
} else if (tile == 'M') {
SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255); // Potwór (czerwony)
} else {
SDL_SetRenderDrawColor(renderer, 20, 20, 20, 255); // Podłoga (ciemna)
}
SDL_RenderFillRect(renderer, &rect);
}
}
// Rysowanie gracza (niebieski kwadrat)
SDL_Rect playerRect = { p.x * TILE_SIZE, p.y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
SDL_SetRenderDrawColor(renderer, 0, 120, 255, 255);
SDL_RenderFillRect(renderer, &playerRect);
// Wyświetlenie klatki na ekranie
SDL_RenderPresent(renderer);
// Krótkie opóźnienie dla stabilizacji (ok. 60 FPS)
SDL_Delay(16);
}
// Sprzątanie po SDL
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <SDL2/SDL.h>
#include <vector>
#include <cstdlib>
#include <ctime>
const int TILE_SIZE = 32; // Rozmiar kafelka w pikselach
const int WIDTH = 20; // Liczba kafelków w poziomie
const int HEIGHT = 10; // Liczba kafelków w pionie
struct Player {
int x, y;
int hp;
int gold;
};
void initGame(Player &p, std::vector<std::string> &mapa) {
p.x = 1;
p.y = 1;
p.hp = 100;
p.gold = 0;
mapa = {
"####################",
"#P...G....#........#",
"#.###.#####.########",
"#.#.......#.#......#",
"#.#.#####.#.#.####.#",
"#...#...G.#...#..#.#",
"#####.#####.###.##.#",
"#.....M.....#...M..#",
"#.#########.########",
"####################"
};
}
int main(int argc, char* argv[]) {
// Inicjalizacja SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "Błąd inicjalizacji SDL: " << SDL_GetError() << std::endl;
return 1;
}
// Tworzenie okna
SDL_Window* window = SDL_CreateWindow("Dungeon Crawler SDL2",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE,
SDL_WINDOW_SHOWN);
if (!window) {
std::cout << "Błąd tworzenia okna: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
// Tworzenie renderera
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cout << "Błąd tworzenia renderera: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
srand(time(0));
Player p;
std::vector<std::string> mapa;
initGame(p, mapa);
bool running = true;
SDL_Event e;
while (running) {
// Obsługa zdarzeń (klawiatura, zamknięcie okna)
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
running = false;
} else if (e.type == SDL_KEYDOWN) {
int newX = p.x;
int newY = p.y;
switch (e.key.keysym.sym) {
case SDLK_w:
case SDLK_UP:
newY--; break;
case SDLK_s:
case SDLK_DOWN:
newY++; break;
case SDLK_a:
case SDLK_LEFT:
newX--; break;
case SDLK_d:
case SDLK_RIGHT:
newX++; break;
case SDLK_q:
case SDLK_ESCAPE:
running = false; break;
}
// Kolizje i mechanika gry
if (mapa[newY][newX] != '#') {
char target = mapa[newY][newX];
if (target == 'G') {
p.gold += 10;
std::cout << "Znalazłeś złoto! Złoto: " << p.gold << std::endl;
mapa[newY][newX] = '.'; // Usuń złoto
} else if (target == 'M') {
int damage = rand() % 20 + 10;
p.hp -= damage;
std::cout << "Potwór zaatakował! Pozostałe HP: " << p.hp << std::endl;
if (p.hp <= 0) {
std::cout << "=== KONIEC GRY (GAME OVER) ===" << std::endl;
running = false;
}
mapa[newY][newX] = '.'; // Pokonaj potwora
}
p.x = newX;
p.y = newY;
}
}
}
// --- RENDEROWANIE GRAFIKI ---
// Czyszczenie ekranu (ciemnoszare tło)
SDL_SetRenderDrawColor(renderer, 30, 30, 30, 255);
SDL_RenderClear(renderer);
// Rysowanie mapy
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
SDL_Rect rect = { x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
char tile = mapa[y][x];
if (tile == '#') {
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255); // Ściana (szara)
} else if (tile == 'G') {
SDL_SetRenderDrawColor(renderer, 255, 215, 0, 255); // Złoto (żółte)
} else if (tile == 'M') {
SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255); // Potwór (czerwony)
} else {
SDL_SetRenderDrawColor(renderer, 20, 20, 20, 255); // Podłoga (ciemna)
}
SDL_RenderFillRect(renderer, &rect);
}
}
// Rysowanie gracza (niebieski kwadrat)
SDL_Rect playerRect = { p.x * TILE_SIZE, p.y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
SDL_SetRenderDrawColor(renderer, 0, 120, 255, 255);
SDL_RenderFillRect(renderer, &playerRect);
// Wyświetlenie klatki na ekranie
SDL_RenderPresent(renderer);
// Krótkie opóźnienie dla stabilizacji (ok. 60 FPS)
SDL_Delay(16);
}
// Sprzątanie po SDL
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
No comments:
Post a Comment