- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试想出一种将重力翻转机制实现到我的 C++ Win32 游戏中的有效方法。虽然我的语言不是很流利,但这就是我来这里问的原因。
我有:
我被困在如何实现重力翻转机制上。这是一款免费的跑酷游戏。玩家跑向窗口的右侧,轻按一个键后,玩家角色将在正常(向下)重力或反转(向上)重力之间切换。
我考虑的方法是使用 do-while。这样做的问题是用户必须等到 do-while 完成。也就是说,如果玩家没有到达屏幕顶部,则继续向上移动(反重力)。
我考虑过其他方法,但我认为它们不值得尝试。它只会导致一些其他的麻烦。
有什么建议吗?
提前致谢:)
更新
这是我一直在处理的代码,我必须解决的当前问题是,一旦玩家到达屏幕底部或顶部。我似乎无法改变重力。但它允许我在玩家接触顶部或底部窗口边缘之前这样做。这是为什么?
请随意批评我的代码并告诉我如何改进我。
main.cpp
#include "BaseWindow.h"
#include "GameWindow.h"
int APIENTRY WinMain ( HINSTANCE h_instance, HINSTANCE h_prev_instance, LPSTR lp_cmd_line, int n_cmd_show ) {
// Create the Game Window
GameWindow game_window ( h_instance, TEXT ( "GameWindow" ) );
game_window.Register ();
// Create the Base Window
BaseWindow base_window ( TEXT ( "BaseWindow" ), game_window.ClassName () );
base_window.Create ();
base_window.Show ();
// Pump Messages
MSG messages;
int status;
while ( ( status = GetMessage ( &messages, 0, 0, 0 ) ) != 0 ) {
if ( status == -1 ) {
// Break;
break;
}
TranslateMessage ( &messages );
DispatchMessage ( &messages );
}
return messages.wParam;
}
AbstractWindow.h
#pragma once
#ifndef __ABSTRACT_WINDOW_H__
#define __ABSTRACT_WINDOW_H__
#include <Windows.h>
class AbstractWindow {
public:
AbstractWindow ();
~AbstractWindow ();
virtual bool Create ();
static LRESULT CALLBACK MessageRouter ( HWND, UINT, WPARAM, LPARAM );
protected:
HWND hwnd_;
DWORD style_ex_;
LPCTSTR class_name_;
LPCTSTR window_name_;
DWORD style_;
int x_;
int y_;
int width_;
int height_;
HWND parent_;
HMENU menu_;
HINSTANCE h_instance_;
LPVOID param_;
// Bitmap Variables - Start
// Stucture for thw Window width and height
RECT rect_;
// Handle to Device Context
HDC hdc_;
// Handle to Device Context - Back Buffer
HDC back_buffer_;
// Bitmap - Front
HBITMAP bitmap_; // Bitmap
// Bitmap Variables - End
// Default Handlers
virtual bool OnCreate ( HWND ) = 0;
virtual bool OnCommand ( int, int ) = 0;
virtual bool OnDestroy () = 0;
virtual bool OnPaint ( HWND, WPARAM ) = 0;
// Player Handlers
virtual bool UpdatePlayerPosition( HWND, WPARAM ) = 0;
};
#endif // !__ABSTRACT_WINDOW_H__
AbstractWindow.cpp
#include "AbstractWindow.h"
AbstractWindow::AbstractWindow () {}
AbstractWindow::~AbstractWindow () {}
bool AbstractWindow::Create () {
hwnd_ = CreateWindowEx (
style_ex_,
class_name_,
window_name_,
style_,
x_,
y_,
width_,
height_,
parent_,
menu_,
h_instance_,
this // Pointer to this class instance
);
if ( hwnd_ ) {
OnCreate(hwnd_);
return true;
}
return false;
}
LRESULT CALLBACK AbstractWindow::MessageRouter ( HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param ) {
AbstractWindow* abstract_window = 0;
static bool move = false;
if ( message == WM_NCCREATE ) {
abstract_window = ( AbstractWindow* ) ( ( LPCREATESTRUCT ( l_param ) )->lpCreateParams );
SetWindowLong ( hwnd, GWL_USERDATA, long ( abstract_window ) );
return DefWindowProc ( hwnd, message, w_param, l_param );
}
else {
abstract_window = ( AbstractWindow* ) ( GetWindowLong ( hwnd, GWL_USERDATA ) );
if ( abstract_window ) {
switch ( message ) {
case WM_COMMAND:
return abstract_window->OnCommand ( LOWORD ( w_param ), HIWORD ( w_param ) );
case WM_DESTROY:
return abstract_window->OnDestroy ();
case WM_PAINT:
return abstract_window->OnPaint ( hwnd, w_param);
case WM_KEYDOWN:
return abstract_window->UpdatePlayerPosition( hwnd, w_param );
default:
return DefWindowProc ( hwnd, message, w_param, l_param );
}
}
else {
return DefWindowProc ( hwnd, message, w_param, l_param );
}
}
}
**BaseWindow.h**
#pragma once
#ifndef __BASE_WINDOW_H__
#define __BASE_WINDOW_H__
#include <Windows.h>
#include "AbstractWindow.h"
#include "Player.h"
class BaseWindow : public AbstractWindow {
public:
BaseWindow ();
~BaseWindow ();
BaseWindow ( const TCHAR*, const TCHAR* );
// Display the window onto the screen while updating the client area
void Show ();
private:
// Player Object
Player player;
// Handlers
virtual bool OnCreate ( HWND );
virtual bool OnCommand ( int, int );
virtual bool OnDestroy ();
virtual bool OnPaint( HWND, WPARAM );
// Player Handlers
virtual bool UpdatePlayerPosition( HWND, WPARAM );
};
#endif // !__BASE_WINDOW_H__
BaseWindow.cpp
#include "BaseWindow.h"
#pragma region Constructor and Destructor
BaseWindow::BaseWindow() {}
BaseWindow::~BaseWindow() {}
BaseWindow::BaseWindow( const TCHAR* window_name, const TCHAR* class_name ) : AbstractWindow() {
// Member variables are declaried in AbstractWindow as Protected
window_name_ = window_name;
class_name_ = class_name;
// Get the module handle for the window currently running
h_instance_ = GetModuleHandle( NULL );
style_ = ( WS_OVERLAPPED| WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX );
x_ = CW_USEDEFAULT;
y_ = CW_USEDEFAULT;
width_ = CW_USEDEFAULT;
height_ = CW_USEDEFAULT;
parent_ = NULL;
menu_ = NULL;
}
#pragma endregion
#pragma region Functions
void BaseWindow::Show() {
// hwnd_ is from AbstractWindow Class
ShowWindow( hwnd_, SW_SHOW );
UpdateWindow( hwnd_ );
}
#pragma endregion
#pragma region Basic Handlers
bool BaseWindow::OnCreate( HWND hwnd ) {
MessageBox( hwnd, TEXT( "[BaseWindow] Window has been successfully created!" ), TEXT( "SUCCESS" ), MB_OK );
GetClientRect( hwnd, &rect_ );
width_ = rect_.right;
height_ = rect_.bottom;
// Create the back buffer
back_buffer_ = CreateCompatibleDC( NULL );
// Get the Device Context
hdc_ = GetDC( hwnd );
// Create the Bitmap
bitmap_ = CreateCompatibleBitmap( hdc_, width_, height_ );
// Select the Bitmap
SelectObject( back_buffer_, bitmap_ );
// Release
ReleaseDC( hwnd, hdc_ );
return true;
}
bool BaseWindow::OnCommand( int ctrl_id, int notify_code ) {
// Lower 16 bits specifies the element the mouse was used on
// Upper 16 bits specifies the mouse properties
// ctrl_id = Lower
// notify_code = Upper
return true;
}
bool BaseWindow::OnDestroy() {
PostQuitMessage( 0 );
return true;
}
#pragma endregion
#pragma region Other Handlers
bool BaseWindow::OnPaint( HWND hwnd, WPARAM w_param ) {
PAINTSTRUCT paint_struct;
// Get the Device Context
hdc_ = BeginPaint( hwnd, &paint_struct );
BitBlt( back_buffer_, 0, 0, width_, height_, NULL, NULL, NULL, WHITENESS );
if ( player.WithinTopBounds() && player.WithinBottomBounds(height_) ) {
player.GravityMovement();
}
// Select the Rectangle Brush
SelectObject( back_buffer_, CreateSolidBrush( RGB( player.SpecializerRed(), player.SpecializerGreen(), player.SpecializerBlue() ) ) );
// Draw the Rectangle on the back buffer
Rectangle( back_buffer_,
player.X() - player.Width() / 2,
player.Y() - player.Height() / 2,
player.X() + player.Width() / 2,
player.Y() + player.Height() / 2);
// Display the back buffer
BitBlt( hdc_, 0, 0, width_, height_, back_buffer_, 0, 0, SRCCOPY );
// Repaint the screen
InvalidateRect( hwnd, NULL, false );
EndPaint( hwnd, &paint_struct );
return true;
}
#pragma region Player
bool BaseWindow::UpdatePlayerPosition(HWND hwnd, WPARAM w_param ) {
if ( w_param == VK_RIGHT ) {
if ( player.withinRightBounds( width_ ) ) {
// Movement to the right
player.RightMovement();
}
}
if ( w_param == VK_LEFT ) {
if ( player.WithinLeftBounds() ) {
// Movement to right left
player.LeftMovement();
}
}
if ( w_param == VK_UP ) {
// Movement upwards
if ( !player.WithinTopBounds() ) {
player.SetInverted( false );
}
player.SetInverted( true );
}
if ( w_param == VK_DOWN ) {
if ( !player.WithinBottomBounds( height_ ) ) {
player.SetInverted( true );
}
// Movement downwards
player.SetInverted( false );
}
return true;
}
#pragma endregion
#pragma endregions
GameWindow.h
#pragma once
#ifndef __GAME_WINDOW_H__
#define __GAME_WINDOW_H__
#include <Windows.h>
#include "AbstractWindow.h"
class GameWindow : protected WNDCLASSEX {
public:
GameWindow ();
~GameWindow ();
GameWindow ( HINSTANCE, const TCHAR* );
virtual bool Register ();
virtual const TCHAR* ClassName () const;
protected:
UINT cb_size_;
UINT style_;
WNDPROC lpfn_wnd_proc_;
int cb_cls_extra_;
int cb_wnd_extra_;
HINSTANCE h_instance_;
HICON h_icon_;
HCURSOR h_cursor_;
HBRUSH hbr_background_;
LPCTSTR lpsz_menu_name_;
LPCTSTR lpsz_class_name_;
HICON h_icon_sm_;
};
#endif // !__GAME_WINDOW_H__
**GameWindow.cpp**
#include "GameWindow.h"
GameWindow::GameWindow () {}
GameWindow::~GameWindow () {}
GameWindow::GameWindow ( HINSTANCE h_instance, const TCHAR* class_name ) {
// All messages belonging to this Window Class will get sent to MsgRouter
hInstance = h_instance;
lpszClassName = class_name;
lpfnWndProc = AbstractWindow::MessageRouter;
lpszMenuName = NULL;
cbSize = sizeof ( WNDCLASSEX );
cbClsExtra = NULL;
cbWndExtra = NULL;
// Prevent Window from Redrawing
style = 0;
hIcon = LoadIcon ( NULL, IDI_APPLICATION );
hIconSm = LoadIcon ( NULL, IDI_APPLICATION );
hCursor = LoadCursor ( NULL, IDC_HAND );
hbrBackground = CreateSolidBrush ( RGB ( 125, 255, 255 ) );
}
const TCHAR* GameWindow::ClassName () const {
return lpszClassName;
}
bool GameWindow::Register () {
return ( ( RegisterClassEx ( this ) ) ? true : false );
}
Player.h
#pragma once
#ifndef __PLAYER_H__
#define __PLAYER_H__
#include <Windows.h>
#include <algorithm>
#include <string>
using namespace std;
class Player {
public:
Player ();
~Player ();
// Mutator
void SetX ( float );
void SetY ( float );
void SetPlayerSpeed ( float );
void SetSpecializerState ( string );
void SetInverted( bool );
// Accessor
float X () const;
float Y () const;
int Width () const;
int Height () const;
int SpecializerRed () const;
int SpecializerGreen () const;
int SpecializerBlue () const;
string SpecializerState () const;
bool Inverted() const;
// Player Functionality
void RightMovement ();
void LeftMovement ();
void GravityMovement();
// Within Bounds Detection
bool WithinLeftBounds();
bool withinRightBounds(int);
bool WithinTopBounds();
bool WithinBottomBounds(int);
private:
// General Information
float x_;
float y_;
int width_;
int height_;
float speed_;
float gravity_;
bool inverted_;
// Player States
int specializer_state_;
// RGB
int specializer_red_;
int specializer_green_;
int specializer_blue_;
void SetSpecializerColor (int);
};
#endif // !__PLAYER_H__
Player.cpp
#include "Player.h"
#pragma region Constructor and Destructor
Player::Player () {
x_ = 200;
y_ = 200;
width_ = 50;
height_ = 50;
speed_ = 5.0f;
gravity_ = 0.2f;
inverted_ = false;
// Specializer
// Set to Default Specializer State
specializer_state_ = 0;
specializer_red_ = 0;
specializer_green_ = 0;
specializer_blue_ = 0;
SetSpecializerColor ( specializer_state_ );
}
Player::~Player () {}
#pragma endregion
#pragma region Mutator
#pragma region Position Properties
void Player::SetX ( float x ) {
x_ = x;
}
void Player::SetY ( float y ) {
y_ = y;
}
#pragma endregion
void Player::SetInverted( bool inverted ) {
inverted_ = inverted;
}
#pragma region Specializer Properties
void Player::SetSpecializerState ( string input_state ) {
if ( input_state == "NORMAL" ) {
specializer_state_ = 0;
}
else if ( input_state == "SHIELD" ) {
specializer_state_ = 1;
}
else if ( input_state == "SPEED" ) {
specializer_state_ = 2;
}
else if ( input_state == "PROJECTILE" ) {
specializer_state_ = 3;
}
else if ( input_state == "MULTIPLIER" ) {
specializer_state_ = 4;
}
SetSpecializerColor ( specializer_state_ );
}
void Player::SetSpecializerColor ( int state ) {
if ( specializer_state_ == 0 ) {
// NORMAL - White
specializer_red_ = 255;
specializer_green_ = 255;
specializer_blue_ = 255;
}
else if ( specializer_state_ == 1 ) {
// SHIELD - Green
specializer_red_ = 255;
specializer_green_ = 153;
specializer_blue_ = 76;
}
else if ( specializer_state_ == 2 ) {
// SPEED - Blue
specializer_red_ = 0;
specializer_green_ = 0;
specializer_blue_ = 102;
}
else if ( specializer_state_ == 3 ) {
// PROJECTILE - Yellow
specializer_red_ = 255;
specializer_green_ = 255;
specializer_blue_ = 0;
}
else if ( specializer_state_ == 4 ) {
// MULTIPLIER - Red
specializer_red_ = 204;
specializer_green_ = 0;
specializer_blue_ = 0;
}
}
#pragma endregion
#pragma endregion
#pragma region Accessor
#pragma region Position Properties
float Player::X () const {
return x_;
}
float Player::Y () const {
return y_;
}
int Player::Width () const {
return width_;
}
int Player::Height () const {
return height_;
}
bool Player::Inverted() const {
return inverted_;
}
#pragma endregion
#pragma region Specializer Properties
int Player::SpecializerRed () const {
return specializer_red_;
}
int Player::SpecializerGreen () const {
return specializer_green_;
}
int Player::SpecializerBlue () const {
return specializer_blue_;
}
string Player::SpecializerState () const {
string specializer_state = "";
if ( specializer_state_ == 0 ) {
specializer_state = "NORMAL";
}
else if ( specializer_state_ == 1 ) {
specializer_state = "SHIELD";
}
else if ( specializer_state_ == 2 ) {
specializer_state = "SPEED";
}
else if ( specializer_state_ == 3 ) {
specializer_state = "PROJECTILE";
}
else if ( specializer_state_ == 4 ) {
specializer_state = "MULTIPLIER";
}
return specializer_state;
}
#pragma endregion
#pragma endregion
#pragma region Functionality
#pragma region Movement
void Player::RightMovement() {
x_ += speed_;
}
void Player::LeftMovement() {
x_ -= speed_;
}
void Player::GravityMovement() {
if ( inverted_ ) {
// Upwards
if ( min( 0.0f, gravity_ ) == 0.0f ) {
gravity_ = -gravity_;
}
y_ += gravity_;
}
else {
// Downwards
if ( max( 0.0f, gravity_ ) == 0.0f ) {
gravity_ = -gravity_;
}
y_ += gravity_;
}
}
#pragma endregion
#pragma region Within Window Bounds
bool Player::WithinLeftBounds() {
if ( x_ - ( width_ / 2 ) > 0 ) {
return true;
}
return false;
}
bool Player::withinRightBounds( int width ) {
if ( x_ + (width_ / 2) < width ) {
return true;
}
return false;
}
bool Player::WithinTopBounds() {
if ( y_ - ( height_ / 2 ) > 0 ) {
return true;
}
return false;
}
bool Player::WithinBottomBounds( int height ) {
if ( y_ + ( height_ / 2 ) < height ) {
return true;
}
return false;
}
#pragma endregion
#pragma endregion
最佳答案
你的游戏应该有一个 game loop .
通常,(非常基本的)游戏循环看起来像:
while (playing) {
accept_input()
update_game_logic()
render()
}
在您的 update_game_logic()
函数中,您将有一个更新玩家位置的部分。玩家的位置更新步骤通常看起来像是以下的混合:
// 1. sum up forces on the player (i.e. running to the right)
// 2. add current_gravity (normal or inverted)
// 3. check for any collision and add an opposing force
// (if touching the ground add a force directly opposite the ground, equal to the force the object is pushing down with)
// 4. resolve position using some form of integration
// e.g. add force (* deltatime) to acceleration, add acceleration (* deltatime) to velocity, add velocity (* deltatime) to position.
因为您是在每次更新时根据力调整玩家的位置,所以您可以在更新游戏逻辑的同时继续接受输入和渲染屏幕。
关于c++ - 使用 C++ Win32 实现重力翻转机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687483/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!