我正在做一个小项目,我试图让我的2D视图中的玩家(实际上是一个圆圈)并让他从左到右移动,上下完美无瑕,我也希望能够按下2个键在那些,所以他侧身或类似的东西。
此外,我希望能够在项目的后期状态下与玩家一起向某个方向进行拍摄(从现在开始可能很重要)
我正在做这个项目,因为我想学习C ++ OOP的工作原理。 我脑海中的结构很简单:
Main > (Creates) > GameWindow
Main > (creates) > EntityManager > (creates) > Player
Player > (creates via) > EnitityManager > Gun
Gun > (Creates via) > EntityManager > Bullet
玩家可以: 向上/向下/向左/向右走 拍摄。
所以要获得代码,这就是我在FirstGame.cpp中的内容
#include "stdafx.h"
#include "GameWindow.h"
#include "EntityManager.h"
int main()
{
// Create an entity manager
EntityManager::Instance();
// Display the window
GameWindow::Instance().Run();
}
在我的GameWindow.h中我有:
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;
class GameWindow
{
public:
static GameWindow& Instance()
{
static GameWindow instance;
return instance;
}
RenderWindow& GetRenderWindow();
void Run();
private:
static const int DEF_WIDTH = 1366;
static const int DEF_HEIGHT = 720;
GameWindow();
GameWindow(const GameWindow&);
GameWindow& operator=(const GameWindow&);
~GameWindow();
string windowTitle;
int windowWidth;
int windowHeight;
void Initialize();
void DisplayWindow();
void CheckWindowEvent();
};
和我的GameWindow.cpp
#include "StdAfx.h"
#include "GameWindow.h"
#include "Player.h"
#include "SFML\Graphics.hpp"
RenderWindow renderWindow;
Event eventSF;
GameWindow::GameWindow()
{
Initialize();
}
GameWindow::~GameWindow()
{
}
void GameWindow::Initialize()
{
// Set width & height to default settings
windowWidth = DEF_WIDTH;
windowHeight = DEF_HEIGHT;
// Create the render window
renderWindow.create(VideoMode(windowWidth, windowHeight), windowTitle, Style::Titlebar | Style::Close | Style::Resize);
Cmd::WriteLine("GameWindow Initialized!");
}
RenderWindow& GameWindow::GetRenderWindow()
{
return renderWindow;
}
void GameWindow::Run()
{
// Loop until window has closed
while (renderWindow.isOpen())
{
// Check current window events
CheckWindowEvent();
// Display window
DisplayWindow();
}
}
void GameWindow::DisplayWindow()
{
// Display the render window
renderWindow.clear();
renderWindow.display();
}
void GameWindow::CheckWindowEvent()
{
Event _event;
while (renderWindow.pollEvent(_event))
{
// Request for closing the window
if (_event.type == Event::Closed)
renderWindow.close();
}
}
在我的EntityManager.h中我得到了:
#pragma once
#include "Entity.h"
#include "Player.h"
class EntityManager
{
public:
static EntityManager& Instance()
{
static EntityManager instance = EntityManager();
return instance;
}
private:
EntityManager();
~EntityManager();
void Initialize();
};
和我的EntityManager.cpp
#include "StdAfx.h"
#include "EntityManager.h"
#include "GameWindow.h"
#include "Player.h"
EntityManager::EntityManager()
{
Initialize();
}
EntityManager::~EntityManager()
{
}
void EntityManager::Initialize()
{
Player::Create();
}
现在是Player.h
#pragma once
#include <SFML/Graphics.hpp>
#include "Entity.h"
using namespace sf;
class Player: Entity
{
public:
Player();
~Player();
void GotDamage(int damage);
static void Create();
void Draw();
void Shoot();
void Move(float x, float y);
void Controls(Event _eventSF);
private:
string name;
int health;
Event eventSF;
CircleShape playerVisual;
protected:
void Initialize() override;
};
并持续播放Player.cpp
#include "StdAfx.h"
#include "Player.h"
#include "GameWindow.h"
#include <SFML/Graphics.hpp>
Player::Player()
{
}
Player::~Player()
{
}
void Player::Create()
{
Player player;
player.Initialize();
player.Draw();
player.Controls(player.eventSF);
}
void Player::Initialize()
{
CircleShape playerVisual(50);
playerVisual.setPosition(800, 450);
playerVisual.setFillColor(sf::Color(100, 250, 50));
Entity::Initialize();
}
void Player::Controls(sf::Event _eventSF)
{
while(GameWindow::Instance().GetRenderWindow().isOpen())
{
while(GameWindow::Instance().GetRenderWindow().pollEvent(_eventSF))
{
switch(_eventSF.type)
{
case sf::Event::KeyPressed:
if (_eventSF.key.code == sf::Keyboard::Up)
{
Move(0,-1);
}
if (_eventSF.key.code == sf::Keyboard::Down)
{
Move(0,1);
}
if (_eventSF.key.code == sf::Keyboard::Left)
{
Move(-1,0);
}
if (_eventSF.key.code == sf::Keyboard::Right)
{
Move(1,0);
}
if (_eventSF.key.code == sf::Keyboard::BackSpace)
{
GotDamage(20);
}
break;
}
}
}
cout << " Checking Controls " << endl;
}
void Player::Move(float _x, float _y)
{
cout << "Move Player " << endl;
playerVisual.move(_x, _y);
Draw();
}
void Player::GotDamage(int _damage)
{
//for some reason health is -858993460
cout << "Your current health is " << Player::health << " you received " << _damage << " damage now you have " << Player::health - _damage << " health left. " << endl;
health -= _damage;
}
void Player::Draw()
{
cout << "Draw Player" << endl;
CircleShape visual(50);
playerVisual.setPosition(800, 450);
playerVisual.setFillColor(sf::Color(100, 250, 50));
GameWindow::Instance().GetRenderWindow().draw(visual);
}
我知道这很多,但我希望有人可以帮助我。
重复我的目标: 我试图让我的播放器在屏幕上绘制,让他侧身移动,如果可能的话,对角线。
提前致谢!
答案 0 :(得分:1)
添加到KeyHeart的答案。
我稍微破解了你的代码并设法让它运行起来。
请记住变量的范围。 Player :: Initialize()中有CircleShape playerVisual(50)这是一个局部变量,但你已经在Player.h中有一个CircleShape playerVisual!所以前者是不必要的。同样在Player :: Create()中你可以创建一个本地玩家对象。
我没有完整的代码副本,因此我假设您计划做的是让EntityManager处理所有现有实体,例如播放器。因此,您应该在EntityManager的头文件中声明Player播放器。因此调用构造函数,我从Player :: Create()中获取了所有内容并将其放入Player :: Player()中。并且此播放器将在EntityManager的生命周期内存在。
您应该更新缓冲区的顺序是:
renderWindow.clear()
renderWindow.draw()
renderWindow.display()
你现在拥有它,你绘制()然后clear()有效地显示任何东西。 有很多方法,但最简单的方法是在GameWindow :: DisplayWindow()中添加对Player :: Draw()的调用。虽然GameWindow需要一个玩家对象来调用Draw()。
关于移动播放器。 Player :: Controls包含某种意义上的无限循环。 while(GameWindow :: Instance()。GetRenderWindow()。isOpen())只要窗口打开就会继续循环,因此可以阻止其他任何更新,例如绘图。
答案 1 :(得分:0)
你不应该在Player :: Draw()函数中绘制playerVisual而不是visual本地存在吗?
你也不应该在GameWindow :: Run()中调用Player :: Draw(),因为你调用了GameWindow :: DisplayWindow()来更新屏幕。在Player :: Move()中调用Player :: Draw()限制精灵的绘制仅限于它移动时。这样的结果将是仅在帧中存在的精灵,否则它将移动一个空画布。