Jump to content
  • Advertisement
Sign in to follow this  

Basic Side-Scrolling Game Tips

This topic is 4720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am writing a video game (or trying to) in .NET C++, and I have run into multiple problems and require any assistance. First, I would like to make it possible for two keys to be pressed as once. For example, if SPACE is the jump and RIGHT-ARROW is to move..well right, then when the right arrow is pressed and held, then space is pressed, the character will continue moving right, however the player stops moving until I let go of the key, then press it again. Also, contrary to what may have been implied up there, I can't get the jumping down. Since I said space is used for jump; the jumping looks fidgety. Also, I know that for Java, and C# (and maybe others, I haven't tried) you can play sound, is it possible to play sound in .NET C++? Any assistance would be greatly appreciated. I am using .NET C++. Mike

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I'm not familiar with .Net, but I am with the Windows API. For reading a series of keys, you could either make successive calls to GetAyncKeyState() (one for each key your game uses) or call GetKeyboardState(). A lot of folks here seem to like using DirectInput for handling game input, so you may wish to take that route.

The Windows API provides PlaySound() for playing sounds in a simple manner. One of the major problems with it is that you can only play one sound at a time. The same API provides functions with the prefix "wave" which grant you full control over sound playback. Because these functions are low-level, however, you'll need to read music files and/or resources own your own. I addition, you'll need to write routines for implementing special effects yourself. If you desire to accomplish playback in an easy way, you might look into DirectSound ,FMOD ,or OpenAL which are free libraries. Be warned, however, that apps utilizing the latter two libraries are subject to certain liscenses.

I should also note that there is another free library called SDL that handles input and sound, among other things.

Hope I've been of some assistance.

Share this post


Link to post
Share on other sites
Quote:
Original post by christianboypcx
Hello, I am writing a video game (or trying to) in .NET C++, and I have run into multiple problems and require any assistance.


I'd be delighted to offer what I can to assist!

Quote:
First, I would like to make it possible for two keys to be pressed as once. For example, if SPACE is the jump and RIGHT-ARROW is to move..well right, then when the right arrow is pressed and held, then space is pressed, the character will continue moving right, however the player stops moving until I let go of the key, then press it again.


Well, what method are you using right now? The Form class sports an event called KeyDown, and a curious sister-method entitled KeyUp. My suggestion would be to keep an array of boolean values, and store the corresponding key to TRUE when the KeyDown event is called, and FALSE when KeyUp is called. Then, in your gameloop you simply need to check if the keys are down and act upon them appropriately.

Quote:
Also, contrary to what may have been implied up there, I can't get the jumping down. Since I said space is used for jump; the jumping looks fidgety.


Fidgety is a broad word used by a broad number of people. Can you supply us with a diagram of sorts or somesort of example of what you mean exactly? I'd love a full-length motion picture, but I'm reasonable.

Quote:
Also, I know that for Java, and C# (and maybe others, I haven't tried) you can play sound, is it possible to play sound in .NET C++? Any assistance would be greatly appreciated. I am using .NET C++.


It indeed is possible. Unfortunately I haven't managed (a-ha, a pun!) to figure out the usage of native DLLs in C++.NET. Normally one would use the Win32 function PlaySound, located in winmm.dll, and accessed through windows.h. Unfortunately this method -- as you can see, I'm more inclined towards vanilla C++ -- doesn't work for me. But, when in doubt, import the native DLL the old fashioned way!


namespace AnotherNamespace
{
using namespace System::Runtime::InteropServices;

public class Whichever
{

[DllImport("winmm.dll", EntryPoint="PlaySound")]
bool PlaySound(LPSTR pszSound, HMODULE hmod, DWORD fdwSound);

void ReletivelyUnimportantFunction()
{
PlaySound("mysound.wav", 0, 0);
}

};
}





Share this post


Link to post
Share on other sites
Quote:
Well, what method are you using right now? The Form class sports an event called KeyDown, and a curious sister-method entitled KeyUp. My suggestion would be to keep an array of boolean values, and store the corresponding key to TRUE when the KeyDown event is called, and FALSE when KeyUp is called. Then, in your gameloop you simply need to check if the keys are down and act upon them appropriately.


Well right now, I am using the KeyDown event. I have a function called CheckInput( System::Windows::Forms::Keys ); and in the KeyDown event, I pass in e->KeyCode, and handle it appropriately. Also, it was drilled into my head that .NET C++ is event based, so I don't know where I would add a gameloop except for in the Form_Load event.

Quote:
Fidgety is a broad word used by a broad number of people. Can you supply us with a diagram of sorts or somesort of example of what you mean exactly? I'd love a full-length motion picture, but I'm reasonable.


Instead of trying to say what it does NOW, I can explain what I'd like it to do. When the space bar is pushed, the character should jump and it should look like an actual jump, (you know a smooth acceleration upwards, then a slow stop due to gravity, then an increasing fall), however it just moves the character a certain amount (say 20 pixels) every 1 ms (or some other arbitrary amount) and it doesn't look right. Also, other key inputs aren't handled and when a key is held down, the falling stops completely and the character moves in that direction. Thank you very much for the help with .NET C++ so far.

Mike

Share this post


Link to post
Share on other sites
Quote:
It indeed is possible. Unfortunately I haven't managed (a-ha, a pun!) to figure out the usage of native DLLs in C++.NET. Normally one would use the Win32 function PlaySound, located in winmm.dll, and accessed through windows.h. Unfortunately this method -- as you can see, I'm more inclined towards vanilla C++ -- doesn't work for me. But, when in doubt, import the native DLL the old fashioned way!


I just tried the code "snippet" mentioned after the quote, and it generated a lot of errors. Am I forgetting any includes at all? Just for the sake of, I'm going to include the errors :-)

A lot of these -> error C2061: syntax error : identifier 'LPSTR'
A WHOLE lot of these -> error C2872: 'FILETIME' : ambiguous symbol

Mike

Share this post


Link to post
Share on other sites
I can't help you with your input problems as I use DirectInput, but your jumping problem sounds easy enough to fix.

It sounds like you're representing your entity with a "position" variable of sorts, and just updating that every frame. You need to add a velocity (2D) vector variable and use that to apply movement to the position variable. Hopefully you're familiar with vectors from your mathematics classes. If not, a Google for 2D vectors should help you out. They're used to represent magnitude (speed) and direction.

On input, set the vector's X component to a positive value (let's say 20) if you're heading to the right, or a negative value if you're heading to the left. Whenever you need to jump, apply a force to the vector's Y component (let's say 20 again). Every update, apply a gravity force against the vector's Y component (let's say -10).

Your movement updates will then become something like this:


// Move Character
PositionX += VelocityX * DeltaTime;
PositionY += VelocityY * DeltaTime;

// Decrease Upwards Velocity
VelocityY += GravityY * DeltaTime;

// Check for collisions and compensate here.


If you don't have a timing solution yet, feel free to ignore the DeltaTime variable.

Now let's run through a few frames to give you a clearer idea of what I mean:

[Beginning]
PositionX = 0
PositionY = 0

VelocityX = 0
VelocityY = 0

Player presses the right arrow key.
VelocityX = 20

[Frame1]
PositionX = 20 (See their position move as it's increased by the VelocityX variable)
PositionY = 0

VelocityX = 20
VelocityY = 0

[Frame2]
Player continues on their merry way to the right (X increasing)

PositionX = 40 (notice it's increasing by VelocityX)
PositionY = 0

VelocityX = 20
VelocityY = 0

[Frame3]
Player presses the jump button, applying an upward force.

PositionX = 60 (still going to the right)
PositionY = 20 (up we go)

VelocityX = 20
VelocityY = 20

Now apply the gravity to the player's velocity:
VelocityY = 10

[Frame4]
Player is still heading upwards while jumping.

PositionX = 80
PositionY = 30

VelocityX = 20
VelocityY = 10

Apply the gravity again:
VelocityY = 0

[Frame5]
The player is no longer ascending as gravity has caught up to them.

PositionX = 100
PositionY = 30

VelocityX = 20
VelocityY = 0

Apply the gravity again:
VelocityY = -10

[Frame5]
The player now has a negative Y velocity, causing them to fall after their arcing-jump.

PositionX = 120
PositionY = 20

VelocityX = 20
VelocityY = -10

Apply the gravity again:
VelocityY = -20

[Frame6]
The player's feet have touched the ground again after the jump, so remove the negative Y velocity (so they don't end up going through the floor).

PositionX = 140
PositionY = 0

VelocityX = 20
VelocityY = 0

Share this post


Link to post
Share on other sites
Quote:
Original post by christianboypcx
I just tried the code "snippet" mentioned after the quote, and it generated a lot of errors. Am I forgetting any includes at all? Just for the sake of, I'm going to include the errors :-)

A lot of these -> error C2061: syntax error : identifier 'LPSTR'
A WHOLE lot of these -> error C2872: 'FILETIME' : ambiguous symbol


Are you including windows.h at the top of the source file? Mind showing me in what context you're using the code? We'll get to the bottom of this one way or the other!

Share this post


Link to post
Share on other sites
Quote:
Original post by darenking
Is .NET suitable for games? Do people ever write games in .NET? I thought it was just for applications.


Define "suitable". I have tried other languages, and yes they have their advantages over .NET, some MANY advantages over .NET, but I am a novice programmer, and so far, .NET is something I understand more than anything. Plus, so far, it is capable of doing everything I need.

Quote:
Are you including windows.h at the top of the source file? Mind showing me in what context you're using the code? We'll get to the bottom of this one way or the other!


Here you go:


#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Runtime::InteropServices;

#include <cstdlib>
#include <windows.h>

public __gc class Player
{
public:
Player( Image* );
~Player(void);

void SetImg( Image* );
void SetX( float );
void SetY( float );
void SetVelocityX( float );
void SetVelocityY( float );

Image* GetImg();
float GetX();
float GetY();
float GetVelocityX();
float GetVelocityY();

[DllImport("winmm.dll", EntryPoint="PlaySound")]
bool PlaySound(LPSTR pszSound, HMODULE hmod, DWORD fdwSound);

private:
Image* MyImg;

float x;
float y;

float dx;
float dy;

public:
int CUR_IMG;
};



Quote:

I can't help you with your input problems as I use DirectInput, but your jumping problem sounds easy enough to fix.....


Thank you very much for that. I understand vectors (I am going to school to be a software engineer) and I have a timing solution, and I also think that the solution you presented may help my input problem: with some booleans and a little more abstraction.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!