C++ Ascii Game - Weiss nicht mehr weiter...
-
Hi an all hätte da ne frage wie könnte ich es am besten lösen einen "NPC" in mein
Game einzubauen?
Bin derzeit die ganze zeit nur auf fehler gestossen und weiss nicht wie ich das am besten machen sollte.Main.cpp:
#include <Windows.h> #include "include/Map.h" #include "include/Player.h" #include "include/Graphics.h" #include "include/GetKeystrokes.h" #include "include/Fiend.h" Graphics graphics; Player player; Map map; Fiend fiend; GetKeystrokes input; bool quit = false; bool first = false; bool redraw = true; void Update() { if(input.IsKeyPressed(VK_ESCAPE)) quit = true; if(input.IsKeyPressed(VK_RIGHT)) { if(!map.IsColliding(player.GetY(), player.GetX() + 1)) { player.MoveRight(); } redraw = true; } if(input.IsKeyPressed(VK_LEFT)) { if(!map.IsColliding(player.GetY(), player.GetX() - 1)) { player.MoveLeft(); } redraw = true; } if(input.IsKeyPressed(VK_UP)) { if(!map.IsColliding(player.GetY() - 1, player.GetX())) { player.MoveUp(); } redraw = true; } if(input.IsKeyPressed(VK_DOWN)) { if(!map.IsColliding(player.GetY() + 1, player.GetX())) { player.MoveDown(); } redraw = true; } } void Render() { graphics.ClearScreen(); map.Render(graphics); player.Render(graphics); fiend.Spawnfiend(graphics); } void main() { graphics.Initialize(24, 79, L"ASCII GAME"); map.Load(".\\data\\maps\\map00.txt"); player.Initialize(35,20); fiend.Spawnposition(31,70); while(!quit) { Update(); if(redraw) { Render(); redraw = false; } } }Graphics.cpp:
#include "include/Graphics.h" void Graphics::Initialize(short height, short width, wchar_t* title) { handle = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SMALL_RECT size; COORD b_size; size.Left = 0; size.Top = 0; size.Right = width; size.Bottom = height; b_size.X = width+1; b_size.Y = height+1; SetConsoleWindowInfo(hCon, true, &size); SetConsoleScreenBufferSize(hCon, b_size); CONSOLE_CURSOR_INFO inf; inf.bVisible = false; inf.dwSize = 1; SetConsoleCursorInfo(handle, &inf); SetConsoleTitle(title); } void Graphics::ClearScreen() { system("cls"); } void Graphics::DrawAscii(int c, int x, int y, WORD color) { COORD coord = {x,y}; SetConsoleCursorPosition (handle, coord); SetConsoleTextAttribute (handle, color); cout << (char)c; } void Graphics::DrawFiend(int f, int x2, int y2, WORD color) { COORD coord = {x2,y2}; SetConsoleCursorPosition (handle, coord); SetConsoleTextAttribute (handle, color); cout << (char)f; }Fiend.cpp:
#include "include/Fiend.h" void Fiend::Spawnfiend(Graphics& graphics) { graphics.DrawFiend(15, this->x2 +5, this->y2 +5, FOREGROUND_RED | FOREGROUND_INTENSITY); } void Fiend::Spawnposition(int PosX2, int PosY2) { this->x2 = PosX2; this->y2 = PosY2; }GetKeystrokes.cpp:
#include "include\GetKeystrokes.h" bool GetKeystrokes::IsKeyPressed(int key) { if(GetAsyncKeyState(key) & 0x1) return true; return false; }Map.cpp:
#include "include/Map.h" void Map::Load(char* path) { FILE *fp; // Open file fp = fopen(path, "r"); // Perform file operations char c = getc(fp); int zeile = 0; int spalte = 0; while(c != EOF) { background[zeile][spalte] = c - 48; c = getc(fp); if(spalte < 80) spalte += 1; else { spalte = 0; zeile += 1; } } // Close file fclose(fp); } void Map::Render(Graphics& graphics) { for(int zeile = 0; zeile < 22; zeile++) { for(int spalte = 0; spalte < 80; spalte++) { int wert = background[zeile][spalte]; if(wert == 1) graphics.DrawAscii(205, spalte, zeile, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); if(wert == 2) graphics.DrawAscii(201, spalte, zeile, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); if(wert == 3) graphics.DrawAscii(187, spalte, zeile, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); if(wert == 4) graphics.DrawAscii(200, spalte, zeile, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); if(wert == 5) graphics.DrawAscii(188, spalte, zeile, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); if(wert == 6) graphics.DrawAscii(186, spalte, zeile, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); if(wert == 7) graphics.DrawAscii(5, spalte, zeile, FOREGROUND_GREEN | FOREGROUND_INTENSITY); if(wert == 8) graphics.DrawAscii(6, spalte, zeile, FOREGROUND_GREEN); if(wert == 9) graphics.DrawAscii(176, spalte, zeile, FOREGROUND_BLUE | FOREGROUND_INTENSITY); } } } bool Map::IsColliding(int y, int x) { if(background[y][x] == 0) return false; return true; }Player.cpp:
#include "include/Player.h" void Player::Initialize(int posX, int posY) { this->x = posX; this->y = posY; } void Player::Render(Graphics& graphics) { graphics.DrawAscii(2, this->x, this->y, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); } int Player::GetX() { return this->x; } int Player::GetY() { return this->y; } void Player::MoveDown() { this->y+=1; } void Player::MoveUp() { this->y-=1; } void Player::MoveLeft() { this->x-=1; } void Player::MoveRight() { this->x+=1; }Fiend.h:
#pragma once #include "include/Map.h" #include "include/Player.h" #include "include/Graphics.h" #include <Windows.h> class Fiend { public: void Spawnfiend(Graphics& graphics); void Spawnposition(int PosX2, int PosY2); private: int x2; int y2; };GetKeystrokes.h:
#pragma once #include "include/Player.h" #include "include/Map.h" #include "include/Graphics.h" class GetKeystrokes { public: bool IsKeyPressed(int key); private: };Graphics.h:
#pragma once #include <iostream> #include <stdio.h> #include <Windows.h> using namespace std; class Graphics { public: void Initialize(short height, short width, wchar_t* title); void ClearScreen(); void DrawAscii(int c, int x, int y, WORD color); void DrawFiend(int f, int x2, int y2, WORD color); private: HANDLE handle; };Map.h:
#pragma once #include <stdio.h> #include <Windows.h> #include "include/Graphics.h" class Map { public: void Load(char* path); void Render(Graphics& graphics); bool IsColliding(int y, int x); private: int background[22][80]; };Player.h:
#pragma once #include "include/Graphics.h" #include <Windows.h> class Player { public: void Initialize(int posX, int posY); void Render(Graphics& graphics); int GetX(); int GetY(); void MoveUp(); void MoveDown(); void MoveLeft(); void MoveRight(); private: int x; int y; };Fiend.cpp und Fiend.h sollen zuständig sein für das spawnen und für die orientierung der NPC's jedoch wie gesagt derzeit ende im gelände...
Schonma im voraus thx für alle die helfen können / wollen ^^
-
Ich fürchte deine Frage ist auf allen Ebenen viel zu vage gestellt, als dass sie irgendjemand beantworten kann.
Was soll einen NPC auszeichen? Wie sieht dein bisheriges Design aus (nicht der Code, sondern was du dir dabei gedacht hast)? Womit hast du Schwierigkeiten?
-
Pwnerer1993 schrieb:
...
Glaubst du wirklich, das jemand sich den gesamten Sourcecode ansieht, ganz davon abgesehen das dieser kein gültiges C++ ist, und auch noch diverse Probleme unnötig einbaut (Warum z.B. die vielen globalen Variablen, "void main" war noch nie gültiges C++...).
Bitte lies dir Wichtig: Du brauchst Hilfe? durch. Konkret unter anderem "Stell deine Fragen präzise", "Reduziere Codebeispiele auf das Wesentliche"...
-
Was mir beim flüchtigen Schauen aufgefallen ist:
GetKeystrokes.cpp:
#include "include\GetKeystrokes.h" bool GetKeystrokes::IsKeyPressed(int key) { if(GetAsyncKeyState(key) & 0x1) return true; // das wird immer von der folgenden Zeile überschrieben return false; }MfG f.-th.
-
f.-th. schrieb:
Was mir beim flüchtigen Schauen aufgefallen ist:
bool GetKeystrokes::IsKeyPressed(int key) { if(GetAsyncKeyState(key) & 0x1) return true; // das wird immer von der folgenden Zeile überschrieben return false; }Nicht wirklich - durch das return kommst du gar nicht zur "folgenden Zeile".
-
f.-th., CStoll: Beide falsch :p Die Funktion arbeitet korrekt, auch wenn sie übermäßig kompliziert geschrieben ist.
-
ok also ich möchte derzeit nicht das der "NPC" überhaupt was tut (nachher soll er halt den player verfolgen und ihm wehtun etc.
derzeit möchte ich es jedoch nur so hinbekommen das der npc die x und y coordinaten des players abliest und in einem bestimmten radius darum herum "spawnt" also gezeichnet wird, jedoch hänge ich genau da
@asc - sorry werd das nächste ma drauf achten. war n bisschen verwirrt als ich hier ankam... (koffein überdosis und schlafentzug) hoffe du nimmst mir dat nicht krumm. und bezüglich der "überkomplizierten" programmierweise... ich progge erst seit nem Monat deswegenm fehlt mir in diesem sinne halt die "Erfahrung"
Ps: in der lage ihn zu zeichnen bin ich ja, nur wenn ich es schaffe ihn zu zeichnen übernimmt er andauernd die posX und posY des Players und klebt somit immer ein Feld neben ihm?! also kein plan wieso er dat tut
-
naja wenn du es schaffst ihn zu zeichnen und ihm eine position zu geben, dann hasts dus doch fast geschafft
du gibst ihm seine startposition nur einmal mit (das ist eine vorraussetzung ansosnten hast du schon was falsch gemacht - nur so als bedingung, falls das bei dir nicht der fall ist)
und da kannst du jetzt was zu addieren oder abziehen, sprich die position verändernnach dem prinzip:
int random(int low, int high) { // srand(time(0)); nur einmal am anfang des programms !! return (random() % (high-low)) + low; } // ... vorheriges programm int npc_x = player.getX(); int npc_y = player.getY(); NPC npc1(npc_x + random(-5, 5), npc_y + random(-5, 5)); // weiteres programm z.b. zeichnenwobei die random funktion nicht sehr gut ist, ist nich für spezialfälle gerüstet sondern nur wenn high > 0 && high > low
-
ok das mit dem random scheint zwar ne lösung zu sein jedoch hat mich das auf die idee gebracht das mit der playerposition erstma wegzulassen und einen (wert == x)
einzufügen um seine position zu bestimmen wobei x gleich ein wert auf meiner map ist.thx