Witam.
Staram się napisać coś w stylu Galaxoid. Póki co mam problem z poruszaniem się postaci. Tutaj kod:
main.cpp
#include <iostream>
#include <windows.h>
#include "Object.h"
#include "Enemy.h"
#include "Player.h"
#include <thread>
#include <conio.h>
enum direction{UP=0, LEFT=1, DOWN=3, RIGHT=4};
void wait(long ms){
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
bool init(){
system("cls");
HANDLE OutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
// GetConsoleCursorInfo(OutHandle, &CursorInfo);
// CursorInfo.bVisible = false;
// SetConsoleCursorInfo(OutHandle, &CursorInfo);
MoveWindow(GetConsoleWindow(), 0, 0, 800, 600, true);
}
void MoveObject(Object obj, direction dir){
obj.PrevPosition = obj.CurrentPosition;
if(dir==UP){
++obj.CurrentPosition.y;
}if(dir==LEFT){
--obj.CurrentPosition.x;
}if(dir==DOWN){
--obj.CurrentPosition.y;
}if(dir==RIGHT){
++obj.CurrentPosition.x;
}
obj.PrevPosition.GotoXY();
std::cout<<" ";
obj.CurrentPosition.GotoXY();
std::cout<<obj.look;
}
int main(){
init();
Player player;
player.CurrentPosition={5,5};
while(1){
if(kbhit()){
if(getch()=='a') MoveObject(player, LEFT);
if(getch()=='w') MoveObject(player, UP);
if(getch()=='s') MoveObject(player, DOWN);
if(getch()=='d') MoveObject(player, RIGHT);
}
wait(100);
}
return 0;
}
Object.h:
#ifndef OBJECT_H
#define OBJECT_H
#include "Coords.h"
class Object{
public:
char look;
Coords CurrentPosition={0,0};
Coords PrevPosition={0,0};
};
#endif
Player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include "Object.h"
#include "Bullet.h"
class Player : public Object{
public:
char look='#';
Bullet bullet;
Coords CurrentPosition={0,0};
Coords PrevPosition={0,0};
};
#endif
Coords.cpp | Coords.h:
#ifndef COORDS_H
#define COORDS_H
#include <windows.h>
#include <iostream>
class Coords{
public:
Coords() {}
Coords(unsigned int x, unsigned int y);
unsigned int x;
unsigned int y;
Coords& operator+ (const Coords& otherCoords);
bool operator== (const Coords& CoordsToCompare);
void GotoXY();
unsigned int GetX() {return this->x;}
unsigned int GetY() {return this->y;}
private:
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
};
#endif
---------------------------------
#include "Coords.h"
void Coords::GotoXY(){
COORD cd;
cd.X=this->x;
cd.Y=this->y;
SetConsoleCursorPosition(this->handle,cd);
}
Coords::Coords(unsigned int x, unsigned int y){
this->x=x;
this->y=y;
}
Coords& Coords::operator+(const Coords& otherCoords){
Coords TmpCoords (this->x+otherCoords.x,this->y+otherCoords.y);
return TmpCoords;
}
bool Coords::operator==(const Coords& CoordsToCompare){
return (this->x==CoordsToCompare.x)&&(this->y==CoordsToCompare.y);
}
Problem jest taki, że w zasadzie wszystko źle działa (filmik w załączniku).
Jakby kogoś nużyło takie czytanie, a chciałby pomóc to w załączniku jest zip z plikami, oraz filmikiem jak to wygląda.
Za wszelką pomoc z góry dziękuję.
Pozdrawiam.