2D: General Question Thread.

Advanced game related topics
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

2D: General Question Thread.

Post by Zach »

Hi guys,

I think that very soon, I am going to be attempting to make a game with graphics. Nothing fancy though, just some 2D Stuff. I've already strolled around the net collecting some free resources I can hopefully use directly, or as a base to modify. I also have an idea for the kind of game I'd like to do, its been kicking around in my head for about a week and I think it would be really cool if I ever completed it. It'll borrow lots of elements from different genres (Shooters, Action/Platformers, RPG/skill systems, and of course it'll have a Story).

I'm just trying to collect some information that will help me out as I'm not sure of the best way to approach certain things, or how to do some things at all.
So if there are any threads people know of that might address a solution to a question, that would be great. Or if anyone has general links that might help, or overall just some advice. All will be appreciated.

I'm going to start off with a single question for now, but will definitely ask more as I go.

I think the biggest question on my mind right now (even though I am a long way off from tackling it) is how to control the movement speed of different sprites on the screen at the same time.. i.e Let's say you've got your space ship zooming along at X speed, and then you fire the lasers/guns/missiles whatever.. So you've got a laser bolt / bullet / missile etc that obviously goes very fast across the screen relative to the speed of the ship that fired it.. You also probably have the background scrolling along to simulate the movement in the first place, and enemies coming at you..

It seems like a really complex thing to do, but I just know it can't be? There has to be a simple basis at the heart of it all. But I really don't have a clue to how that kind of stuff works.. Is there a timer at the base of it all (or multiple timers for different objects)? How can you get smooth movement across the X/Y coordinates without it appearing to skip or jump the faster you want it to go? etc..

Having this answered would help me to understand a lot, I think, and help me to better design the game from the start.
Image
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 2D: General Question Thread.

Post by IdeasVacuum »

If you can get your hands on this book about programming games in PB, it may hold a lot of the answers to the questions you have in mind:
Programming 2D scrolling Games
They are as rare as rocking horse droppings but the publisher may still have one or perhaps a forum member would be happy to part company with one (actually, I'm sure someone said they had two of them).

The Book's Table of Contents: http://content.gpwiki.org/index.php/Pro ... ling_games

Book Review on this forum: viewtopic.php?p=97300

Game download (based on book): http://www.krylarskreations.com/migz.html
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: 2D: General Question Thread.

Post by Zach »

Yeah I heard about that book.. I haven't been able to find a copy of it anywhere
Image
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: 2D: General Question Thread.

Post by Zach »

Also,

Just curious if I want to be looking into SDL or something? Has anyone wrapped SDL, or does PB support it natively on Windows/Linux? (I remember something about a Library Subsystem option).
I heard MP3D can do 2D as well, but the file download gives me a CRC error when I try to extract the files.
Image
Nituvious
Addict
Addict
Posts: 1000
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: 2D: General Question Thread.

Post by Nituvious »

I think the Linux version of PB's graphic library uses SDL.
▓▓▓▓▓▒▒▒▒▒░░░░░
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: 2D: General Question Thread.

Post by Zach »

Just going to ask a dumb question here anyway..

Would PB's internal commands be sufficient for building a tile based renderer? (speed, parallax scrolling, multiple layers, applying any special effects, etc) and would it be better to use the Sprite3D functions over the more traditional 2D functions? and by that I mean without having to make API calls to anything.
Image
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: 2D: General Question Thread.

Post by kenmo »

Zach, I don't really have any proof to back this up, but I would suggest going with Sprite3D.

As much as I like to hold on to good old pixel-perfect 2D graphics functions, the hardware-accelerated Sprite3D functions seem to be just as good, or better, in every oddball test program I've tried. They seem to run faster, make scaling and effects much simpler and nicer looking, and should be cross-platform compatible.

My next game project in PureBasic will probably use Sprite3D. My current project, which I hope to finish this summer, uses all 2D drawing and a few Sprite functions, which makes a lot of effects difficult and actually seems to run slower on the newer computers that I've tested! (Windows 7 versus Windows XP, for example). But I'm definitely too far along to convert it all over now.
Zach wrote:I think the biggest question on my mind right now (even though I am a long way off from tackling it) is how to control the movement speed of different sprites on the screen at the same time.. i.e Let's say you've got your space ship zooming along at X speed, and then you fire the lasers/guns/missiles whatever.. So you've got a laser bolt / bullet / missile etc that obviously goes very fast across the screen relative to the speed of the ship that fired it.. You also probably have the background scrolling along to simulate the movement in the first place, and enemies coming at you..
Brush up on 2D vector math, for sure. Especially if it's some sort of overhead game or it has projectiles or physics or..... almost anything really. Vector addition, scaling, dot products, gravity, all that fun geometric stuff.
Zach wrote:It seems like a really complex thing to do, but I just know it can't be? There has to be a simple basis at the heart of it all. But I really don't have a clue to how that kind of stuff works.. Is there a timer at the base of it all (or multiple timers for different objects)? How can you get smooth movement across the X/Y coordinates without it appearing to skip or jump the faster you want it to go? etc..
Not sure what you mean here... there's usually one "timer" that all game objects run on. Smoothness is highly dependent on framerate (go with 30 or 60!) and as a side note, I suggest using all floating points for object positions and velocities and such, and only round them to integers at the final step in your screen updating procedure. Using integers for physics and objects will sometimes lead to subtle choppy motion... unless you plan on a grid-based game (like an RPG or something).

One more thing... early in your design you need to decide if you want a strict frame-based time system or a framerate-independent time system....... For example, in some games when a lot of special effects occur, the framerate drops and everything seems to move slower..... other games try to compensate for this by monitoring the framerate and increases the game motion as needed so it appears more constant. Both have their pros and cons. For simple 2D games, I think the strict frame-based system is better and more reliable, even if once in a while it might slow down for a second -- framerate-compensating gameplay can really screw with your physics or collision routines.
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: 2D: General Question Thread.

Post by Zach »

Hi, to clarify what I meant about using multiple timers. I suppose that relates to what kind of rendering I would go with, which you touched on next..

When I originally posed the question, I assumed I would be attempting to build an engine that decouples the graphics from the main loop, thus requiring "Timer based movement". I was postulating something along the lines of, "Well I'd have 1 timer for the player vessel, one for projectiles, and another for whatever else" as a means to control how fast the objects were moving and the distance they were covering.. My understanding being that with a decoupled graphics rendering and Timers, you can be sure if something is supposed to move "5 feet" across 2 seconds, it is only ever going to move "5 feet" even if the frame rate catastrophically drops to a stop-motion-animation slideshow.

But thinking about it, I suppose in the end one timer is all you would need, because you could just bounce different calculations off that timer..

But your comments on framebased rendering and the slow-down effect we often see from old Arcade and 8 - 16 bit console games is actually something that might be fun to have. If nothing but for a taste of retro-gaming, which is the whole style I am going for anyway. There is something "neat" about a screen full of bullets suddenly slowing down your FPS in this context.


Just for the record, I really am horrible at math :( So I don't know how I will fare in that regard.

Also, just to expand more on my previous question you didn't understand.. I was saying I really don't know what a fundamental formula would be, to control different movement speeds of multiple objects on the screen. All of this is brand new to me and I really don't even know where to start, or what to research, to begin developing a 2D, or any simple graphics based game really.

It always seems like the kind of teaching examples / tutorials I crave, simply do not exist. I would love to find a tutorial (or series of tutorials) that dissects early games (Space Invaders, Galaga, Pac Man, Pitfall, and more complex early games or styles of games (Shooters, Platformers, Action Adventures, Flight/Space Sims) ) breaks them down to their core parts, and explains how they work, what a piece of code controls, and WHY/HOW it works, especially any of the math involved.

Unfortunately I am the kind of person who sorely needs the benefit of a Teacher/Student/Classroom environment, I need to be held by the hand on a lot of stuff and I rarely possess the inspiration to experiment and come up with abstract solutions on my own. At least not until I get the core knowledge down. In school I was poor at moving from one subject to another in Math, but once I got the subject down and was confident, I could sit there and solve problems of that type all day long.... Until the next series of different problems came along and I had to start all over again. Complicating matters is there are lots of tools (Frameworks and other game-oriented utilities) I would love to use with PB, but writing wrappers isn't something I can really "do".. And I keep waffling between picking up my C++ books again to try and learn something so I can understand and properly port things over with wrappers.. ( But then I ask why all this time in PB if I'm just going to learn C++ anyway, and then I can use it natively? argh...)


I don't know how long it will take me (I'm already over a year into PB and my Text RPG, though I admit I don't spend a lot of time each day on learning/programming), but eventually I would love to be able to make this game idea I have in my head.. 2D retro-style graphics, sound, music and all that.
Image
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: 2D: General Question Thread.

Post by kenmo »

Hmmm, I assumed there were tutorials out there for simple 2D game engines, topdown shooters and platformers and whatnot, (not necessarily for PureBasic of course). I don't know specific ones, but I will keep an eye out.

Decoupling the graphics from the game logic is absolutely a good idea. In fact, I would start playing with some movement code and only use basic 2D shapes to start, colored squares and circles. I usually keep the control and physics/collision and rendering as separate chunks of code. Later on you can start adding sprites, or Sprite3D if you want easier scaling, rotation, transparency, all that fun stuff. Don't worry about background graphics right away either...

Depending on how simple or complex you want your control scheme to be, you might not need any complex math. Just basic stuff like newX = previousX + xVelocity * timeStep (in this case, timeStep would be a scalar that compensates for framerate or allows "bullet time" effects or whatever you want; it's not required). Assuming you want non-orthogonal movement, you should brush up on basic trig too, like yVelocity = speed * sin(angle). Of course you also need to decide early on your game's coordinate system.... which doesn't need to match the screen's pixel coordinates.... is up your positive-y direction, or is down? Does a player angle of zero point to the right, or up? And so on. (I'm sort of basing all this on the assumption your game idea is a top-down view... platformers and other types have their own design choices...)

Audio is usually pretty straightforward, as in sound effects and background music. I really appreciate PureBasic adding streaming/looping Ogg Vorbis playback a couple years ago.
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: 2D: General Question Thread.

Post by Zach »

There probably are some tutorials out there, I just need to look for them.. But then again most tutorials I find tend to be "Here's the C/C++ source code with comments"... :|

I actually envision the game as a side-view scroller. It will be a Horizontal Shooter / Action type Platform game. So it will have elements of

Horizontal Shooters (side view, left to right scrolling)
Platformers (side view, left and right movement / screen scrolling with jumping, falling, and simulated gravity)
Exploration / Combat action
RPG Elements (Statistics, Items, Skills)
A Variety of Puzzles based on different environments (obstacles, traps, dead ends, locked areas)

That's the idea any way.. Kind of a cross between something like Air Fortress (NES) and CastleVania: Symphony of The Night (PSX)
Image
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: 2D: General Question Thread.

Post by J. Baker »

Simple multiple (2) timer example. The movement repeats every 2 seconds.

Code: Select all

InitSprite()

InitKeyboard()

OpenWindow(0, 0, 0,320, 240, "Timer", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 320, 240, 0, 0, 0)

   CreateSprite(1, 32, 32)
    StartDrawing(SpriteOutput(1))
     DrawingMode(#PB_2DDrawing_Default)
      Box(0, 0, 32, 32, RGB(127, 127, 127))
    StopDrawing()
    
   CreateSprite(2, 32, 32)
    StartDrawing(SpriteOutput(2))
     DrawingMode(#PB_2DDrawing_Default)
      Box(0, 0, 32, 32, RGB(220, 0, 0))
    StopDrawing()
    
Quit = #False
   
X = 10
Y = 10
XX = 280
YY = 200
Go = 1

;SetFrameRate(60)

Repeat
  
  ExamineKeyboard()
  
  Repeat
   Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
        Quit = #True
    EndIf
  Until Event = 0
 
    FlipBuffers()
    ClearScreen(0)
    
  If Go = 1
     StartTime = ElapsedMilliseconds()
     Go = 2
  EndIf
  
  ElapsedTime = ElapsedMilliseconds() - StartTime
  
  If Go = 2 And ElapsedTime >= 15 ; 60fps roughly. On Mac a value of 16 stutters some times, so I made it 15.
     X + 2
     XX - 4 ;red box (sprite) moves twice as fast as the grey box at 60fps
     Go = 1
  EndIf
  
  DisplaySprite(1, X, Y)
  DisplaySprite(2, XX, YY)
  
  If X = 200
     Go = 0
     X = 10 ;_______ reset our positions of the sprites
     XX = 280 ;__|
     WaitTime = ElapsedMilliseconds()
  EndIf
  
  WaitElapsedTime = ElapsedMilliseconds() - WaitTime
  
  If Go = 0 And WaitElapsedTime >= 2000; wait for 2 seconds roughly before repeating
     Go = 1
  EndIf
  
Until Quit Or KeyboardPushed(#PB_Key_Escape)
Of course you can have multiple timers for various sprites and so forth. Multiple sprites can even use the same timer too. It all depends on what you are trying to do. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: 2D: General Question Thread.

Post by xorc1zt »

high resolution timers with callback for purebasic

edit:
fixed timer with random frame rates:

Code: Select all

#PB_TIME_MILLISECOND    = 4
#MOVE_OFFSET            = 5 ; move 5 each second

Define frameTime.f = GetTimeDelta(#PB_TIME_MILLISECOND)
Define totalTime.f
Define randDelay.i
Define totalMove.f


Delay(5) ; avoid division by 0

Repeat
    randDelay = Random(500) ;use less for better accuracy
    
    frameTime = GetTimeDelta(#PB_TIME_MILLISECOND)
    
    totalTime + frameTime
    
    totalMove + ( #MOVE_OFFSET * (frameTime/1000) )
    
    If (totalTime > 1000)
        Debug "*** one second passed ***"
        Debug "you moved "+Str(totalMove)
        totalTime = 0.0
        totalMove = 0.0
    EndIf
   
    Debug "delay +"+Str(randDelay)
    Delay( randDelay )
ForEver
Zach
Addict
Addict
Posts: 1656
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: 2D: General Question Thread.

Post by Zach »

Thanks for those examples. They will undoubtedly help 8)
Image
Post Reply