#include <SDL2/SDL.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
const int TILE_SIZE = 32;
const int WIDTH = 20; // Szerokość mapy w kafelkach (20 * 32 = 640 px)
const int HEIGHT = 10; // Wysokość mapy w kafelkach (10 * 32 = 320 px)
const int UI_HEIGHT = 120; // Wysokość dolnego panelu na przyciski
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..#",
"#.#########.########",
"####################"
};
}
// Funkcja obsługująca ruch gracza (wspólna dla klawiatury i przycisków dotykowych)
void movePlayer(Player &p, std::vector<std::string> &mapa, int dx, int dy, bool &running) {
int newX = p.x + dx;
int newY = p.y + dy;
if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT) {
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] = '.';
} 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] = '.';
}
p.x = newX;
p.y = newY;
}
}
}
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "Błąd inicjalizacji SDL: " << SDL_GetError() << std::endl;
return 1;
}
// Okno szersze i wyższe, żeby zmieścić przyciski na dole
SDL_Window* window = SDL_CreateWindow("Dungeon Crawler Mobile",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH * TILE_SIZE, (HEIGHT * TILE_SIZE) + UI_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
std::cout << "Błąd tworzenia okna: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
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;
// Definicje obszarów przycisków na dolnym panelu (X, Y, Szerokość, Wysokość)
SDL_Rect btnUp = { 110, 340, 60, 45 };
SDL_Rect btnLeft = { 40, 390, 60, 45 };
SDL_Rect btnDown = { 110, 390, 60, 45 };
SDL_Rect btnRight = { 180, 390, 60, 45 };
SDL_Rect btnQuit = { 520, 365, 80, 60 };
while (running) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
running = false;
}
// Obsługa klawiatury (jeśli ktoś ma podłączoną klawiaturo-myszkę)
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_w: case SDLK_UP: movePlayer(p, mapa, 0, -1, running); break;
case SDLK_s: case SDLK_DOWN: movePlayer(p, mapa, 0, 1, running); break;
case SDLK_a: case SDLK_LEFT: movePlayer(p, mapa, -1, 0, running); break;
case SDLK_d: case SDLK_RIGHT: movePlayer(p, mapa, 1, 0, running); break;
case SDLK_q: case SDLK_ESCAPE: running = false; break;
}
}
// Obsługa dotknięcia ekranu / kliknięcia myszką
else if (e.type == SDL_MOUSEBUTTONDOWN) {
int mx = e.button.x;
int my = e.button.y;
// Sprawdzamy, w który przycisk kliknął użytkownik
if (mx >= btnUp.x && mx <= btnUp.x + btnUp.w && my >= btnUp.y && my <= btnUp.y + btnUp.h) {
movePlayer(p, mapa, 0, -1, running);
} else if (mx >= btnDown.x && mx <= btnDown.x + btnDown.w && my >= btnDown.y && my <= btnDown.y + btnDown.h) {
movePlayer(p, mapa, 0, 1, running);
} else if (mx >= btnLeft.x && mx <= btnLeft.x + btnLeft.w && my >= btnLeft.y && my <= btnLeft.y + btnLeft.h) {
movePlayer(p, mapa, -1, 0, running);
} else if (mx >= btnRight.x && mx <= btnRight.x + btnRight.w && my >= btnRight.y && my <= btnRight.y + btnRight.h) {
movePlayer(p, mapa, 1, 0, running);
} else if (mx >= btnQuit.x && mx <= btnQuit.x + btnQuit.w && my >= btnQuit.y && my <= btnQuit.y + btnQuit.h) {
running = false;
}
}
}
// --- RENDEROWANIE ---
SDL_SetRenderDrawColor(renderer, 20, 20, 20, 255);
SDL_RenderClear(renderer);
// 1. Rysowanie mapy gry
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
} else if (tile == 'G') {
SDL_SetRenderDrawColor(renderer, 255, 215, 0, 255); // Złoto
} else if (tile == 'M') {
SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255); // Potwór
} else {
SDL_SetRenderDrawColor(renderer, 15, 15, 15, 255); // Podłoga
}
SDL_RenderFillRect(renderer, &rect);
}
}
// Rysowanie gracza
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);
// 2. Rysowanie dolnego panelu sterowania (tło panelu)
SDL_Rect uiPanel = { 0, HEIGHT * TILE_SIZE, WIDTH * TILE_SIZE, UI_HEIGHT };
SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
SDL_RenderFillRect(renderer, &uiPanel);
// Rysowanie przycisków kierunkowych (Pad)
SDL_SetRenderDrawColor(renderer, 80, 80, 120, 255);
SDL_RenderFillRect(renderer, &btnUp);
SDL_RenderFillRect(renderer, &btnLeft);
SDL_RenderFillRect(renderer, &btnDown);
SDL_RenderFillRect(renderer, &btnRight);
// Przycisk wyjścia
SDL_SetRenderDrawColor(renderer, 150, 40, 40, 255);
SDL_RenderFillRect(renderer, &btnQuit);
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
const int TILE_SIZE = 32;
const int WIDTH = 20; // Szerokość mapy w kafelkach (20 * 32 = 640 px)
const int HEIGHT = 10; // Wysokość mapy w kafelkach (10 * 32 = 320 px)
const int UI_HEIGHT = 120; // Wysokość dolnego panelu na przyciski
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..#",
"#.#########.########",
"####################"
};
}
// Funkcja obsługująca ruch gracza (wspólna dla klawiatury i przycisków dotykowych)
void movePlayer(Player &p, std::vector<std::string> &mapa, int dx, int dy, bool &running) {
int newX = p.x + dx;
int newY = p.y + dy;
if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT) {
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] = '.';
} 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] = '.';
}
p.x = newX;
p.y = newY;
}
}
}
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "Błąd inicjalizacji SDL: " << SDL_GetError() << std::endl;
return 1;
}
// Okno szersze i wyższe, żeby zmieścić przyciski na dole
SDL_Window* window = SDL_CreateWindow("Dungeon Crawler Mobile",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH * TILE_SIZE, (HEIGHT * TILE_SIZE) + UI_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
std::cout << "Błąd tworzenia okna: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
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;
// Definicje obszarów przycisków na dolnym panelu (X, Y, Szerokość, Wysokość)
SDL_Rect btnUp = { 110, 340, 60, 45 };
SDL_Rect btnLeft = { 40, 390, 60, 45 };
SDL_Rect btnDown = { 110, 390, 60, 45 };
SDL_Rect btnRight = { 180, 390, 60, 45 };
SDL_Rect btnQuit = { 520, 365, 80, 60 };
while (running) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
running = false;
}
// Obsługa klawiatury (jeśli ktoś ma podłączoną klawiaturo-myszkę)
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_w: case SDLK_UP: movePlayer(p, mapa, 0, -1, running); break;
case SDLK_s: case SDLK_DOWN: movePlayer(p, mapa, 0, 1, running); break;
case SDLK_a: case SDLK_LEFT: movePlayer(p, mapa, -1, 0, running); break;
case SDLK_d: case SDLK_RIGHT: movePlayer(p, mapa, 1, 0, running); break;
case SDLK_q: case SDLK_ESCAPE: running = false; break;
}
}
// Obsługa dotknięcia ekranu / kliknięcia myszką
else if (e.type == SDL_MOUSEBUTTONDOWN) {
int mx = e.button.x;
int my = e.button.y;
// Sprawdzamy, w który przycisk kliknął użytkownik
if (mx >= btnUp.x && mx <= btnUp.x + btnUp.w && my >= btnUp.y && my <= btnUp.y + btnUp.h) {
movePlayer(p, mapa, 0, -1, running);
} else if (mx >= btnDown.x && mx <= btnDown.x + btnDown.w && my >= btnDown.y && my <= btnDown.y + btnDown.h) {
movePlayer(p, mapa, 0, 1, running);
} else if (mx >= btnLeft.x && mx <= btnLeft.x + btnLeft.w && my >= btnLeft.y && my <= btnLeft.y + btnLeft.h) {
movePlayer(p, mapa, -1, 0, running);
} else if (mx >= btnRight.x && mx <= btnRight.x + btnRight.w && my >= btnRight.y && my <= btnRight.y + btnRight.h) {
movePlayer(p, mapa, 1, 0, running);
} else if (mx >= btnQuit.x && mx <= btnQuit.x + btnQuit.w && my >= btnQuit.y && my <= btnQuit.y + btnQuit.h) {
running = false;
}
}
}
// --- RENDEROWANIE ---
SDL_SetRenderDrawColor(renderer, 20, 20, 20, 255);
SDL_RenderClear(renderer);
// 1. Rysowanie mapy gry
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
} else if (tile == 'G') {
SDL_SetRenderDrawColor(renderer, 255, 215, 0, 255); // Złoto
} else if (tile == 'M') {
SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255); // Potwór
} else {
SDL_SetRenderDrawColor(renderer, 15, 15, 15, 255); // Podłoga
}
SDL_RenderFillRect(renderer, &rect);
}
}
// Rysowanie gracza
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);
// 2. Rysowanie dolnego panelu sterowania (tło panelu)
SDL_Rect uiPanel = { 0, HEIGHT * TILE_SIZE, WIDTH * TILE_SIZE, UI_HEIGHT };
SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
SDL_RenderFillRect(renderer, &uiPanel);
// Rysowanie przycisków kierunkowych (Pad)
SDL_SetRenderDrawColor(renderer, 80, 80, 120, 255);
SDL_RenderFillRect(renderer, &btnUp);
SDL_RenderFillRect(renderer, &btnLeft);
SDL_RenderFillRect(renderer, &btnDown);
SDL_RenderFillRect(renderer, &btnRight);
// Przycisk wyjścia
SDL_SetRenderDrawColor(renderer, 150, 40, 40, 255);
SDL_RenderFillRect(renderer, &btnQuit);
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
No comments:
Post a Comment