[Question] Platform Game

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hello everyone,

I wonder how u guys will make action platform games like mario, kirby, etc games. I am quite confident with programming itself, but I have never made a game with basic yet. so how would you guys:

- Control the views in the game (let the screen follow the character)
- Tile your game (with tilesets making the background)
- character movement (left, right, jump, etc)

I mostly programmed with Game Maker, but I left it because it was to easy.
for example, for moving you would do: obj_Character.x = -2 on left arrow button. So how would you do that using Basic4Android then?


Thanks

XverhelstX
 

ssg

Well-Known Member
Licensed User
Longtime User
Ok, and how would you follow the view with the character?

Here is a little idea, instead of looking it the way of "following the view with the character", try to imagine that you are moving the background and all other objects on the screen to the left from right (for example in mario), while your character is always at the middle of the screen.

I'm really interested in how this thread will evolve, we really need some good game examples :)

all the best!
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
I think it will be programmatically easier to 'drop' the stuff and tile the level instead of letting it scroll.

I have a bunch of sprites, gfx, animations and more and I would like to create a platformer. If anyone is interested in making the game, feel free to pm me. :)

XverhelstX
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hey guys!

I have managed to create the beginning of my platform game with the basic movement.
It's made for the Xperia PLAY, so you control it with the arrow keys and combo buttons.

1) Now I wonder how you guys would use gravity in a platform game as I am not so familair with games. Any code snippet would be really much appreciated. I will post a basic tutorial on how to make platform games with basic4android later on then.

2) Also a side-question: what would be a better button to perform a jump on the PLAY? the Cross Button (X) or Up Arrow? Because the game will contain a lot of combo's.

here is how it will look like on android:

YouTube - ‪Stick-Men's Redemption I: The Notebook‬‏

A Preview of the gameplay of my game on windows.
So on the Xperia Play, you control the character with the arrow and buttons, on another device, you could walk with the character with the arrows on the left side and make combo's with the drawing pad at the right bottom.

Thanks!
XverhelstX
 
Last edited:
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Ok, moving on... this is how i usually do gravity in my games:

B4X:
Sub Process_Globals
   Dim t As Timer
End Sub

Sub Globals
   Dim p As Panel
   Dim g As Double
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then t.Initialize("t", 30)
   p.Initialize("")
   Activity.AddView(p, 20dip, 0dip, 20%x, 20%x)
   p.Color = Colors.Blue
   g = 0
   t.Enabled = True
End Sub

Sub t_Tick
   g = g + 0.5
   p.Top = p.Top + g
   If (p.Top + p.Height) > 100%y Then
      p.Top = 100%y - p.Height
      g = g * -1
   End If
End Sub

Basically i have a gravity acceleration variable (g in the code above), and every loop (whether timer or via gameloop), i add a certain amount of value to the acceleration.

This would hopefully give some kind of drop feel rather than straight forward static speed drop.

As to how to bounce it to a halt, like how a ball would drop and bounce till it stops, change the tick code to following:

B4X:
Sub t_Tick
   g = g + 0.5
   p.Top = p.Top + g
   If (p.Top + p.Height) > 100%y Then
      p.Top = 100%y - p.Height
      g = (g * -1) + 2
   End If
End Sub

the +2 acts as a resistance force which will make your bounce lower and lower as you move. To get a right amount for your game, tweak it a little and see whats best fit.

Hope we get to move on further in this great thread :D

Cheers you all!
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Wow, thanks ssg. Seems intresting.
and thanks for the link.

This is how I move my character (with xperia PLAY buttons):

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
'Right arrow
If Keycode = KeyCodes.KEYCODE_DPAD_RIGHT Then
   blnFacing = True 'sets the sprite to look right.
   CharMoveRight
End If
'Left arrow
If Keycode = KeyCodes.KEYCODE_DPAD_LEFT Then
   blnFacing = False 'sets the sprite to look left.
   CharMoveLeft
End If

'Cross button
If Keycode = 23 Then
   isjumping = False
   vely = 100
   CharJump
   
End If


'Circle button
'If Keycode = 4 Then
'   Return True
'End If


End Sub
Sub Activity_Pause (UserClosed As Boolean)
   
End Sub
Sub Activity_Resume
   
   
End Sub
Sub CharMoveRight

   If blnFacing = True Then
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmap(Bitmap1, Rect, Rect) 'Erase the previous character
      Activity.Invalidate2(Rect)
      
      x = x + vx
      y = y 
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmap(btmChar, Null, Rect) 'Draw the new character
      Activity.Invalidate2(Rect)
      blnFacing = False
      
   Else
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmap(Bitmap1, Rect, Rect) 'Erase the previous character
      Activity.Invalidate2(Rect)
      
      x = x + vx
      y = y 
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmapFlipped(btmChar, Null, Rect,False,True) 'Draw the new character
      Activity.Invalidate2(Rect)
      blnFacing = False
   End If
   
End Sub
Sub CharMoveLeft
   
   If blnFacing = False Then
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmap(Bitmap1, Rect, Rect) 'Erase the previous smiley
      Activity.Invalidate2(Rect)
      
      x = x - vx
      y = y 
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmapFlipped(btmChar, Null, Rect,False,True) 'Draw the new smiley
      
      Activity.Invalidate2(Rect)
      blnFacing = True
   Else
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmap(Bitmap1, Rect, Rect) 'Erase the previous smiley
      Activity.Invalidate2(Rect)
      
      x = x - vx
      y = y 
      Rect.Top = y
      Rect.Left = x
      Rect.Bottom = y + boxSize
      Rect.Right = x + boxSize
      Canvas1.DrawBitmap(btmChar, Null, Rect) 'Draw the new smiley
      Activity.Invalidate2(Rect)
      blnFacing = True
   End If
End Sub

Does anyone have a better idea?

XverhelstX
 
Last edited:
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
allritey... coding time...

here is a simple way of moving your character on downpress of a button.

i do not have a mobile which has physical buttons, so i have added 2 on screen buttons... the code (added on to the gravity piece previously):

B4X:
Sub Process_Globals
   Dim t As Timer
End Sub

Sub Globals
   Dim p As Panel
   Dim g As Double
   Dim lb As Button
   Dim rb As Button
   
   Dim boolmove As Boolean
   Dim velx As Double
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then t.Initialize("t", 30)
   p.Initialize("")
   Activity.AddView(p, 20dip, 0dip, 20%x, 20%x)
   
   lb.Initialize("b")
   Activity.AddView(lb, 0, 0, 50%x, 20%x)
   lb.Text = "left"
   lb.Tag = "left"
   
   rb.Initialize("b")
   Activity.AddView(rb, 50%x, 0, 50%x, 20%x)
   rb.Text = "right"
   rb.Tag = "right"

   p.Color = Colors.Blue
   g = 0
   t.Enabled = True
End Sub

Sub b_Down
   'get the sender and assign the right acceleration to left or right
   Dim s As Button
   s = Sender
   
   If s.Tag = "left" Then
      velx = -3
   Else
      velx = 3
   End If
   
   boolmove = True 
   
End Sub

Sub b_Up
   velx = 0 'no more speed
   boolmove = False 'stop moving, though this might be redundant as speed is already 0
End Sub

Sub t_Tick
   'add gravity and bounce
   g = g + 0.5
   p.Top = p.Top + g
   If (p.Top + p.Height) > 100%y Then
      p.Top = 100%y - p.Height
      g = (g * -1) + 2
   End If
   
   'move the panel based on horizontal velocity
   If boolmove Then
      p.Left = p.Left + velx
   End If
End Sub

hope the above is useful somehow...

as for your code XverhelstX, I am not judging you or anything, but i really think you do not need 2 subs for the move left and right, setting the velocity to positive or negative should provide the desired result from a single sub.

it will be easier to maintain in future... just my 2 cents! (yes yes.. please take it! hahaha)

cheers!
 
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
Xna

I know it's not B4A but Visual Studio Express + XNA has a ton of these kinds of tutorials. The graphics engine / directx code is gonna be way different, but there's usually a very useful section, the game's main loop. There's usually a sub that contains all kinds of code that deals with keystrokes, that's the part you'd want to look at. From what i've ran in the past, I can tell you they have pretty easy to understand / translate code that just relates to physics, levels, game objects, bounding boxes etc... along with lots of comments.

Here's an example:
Starter Kit: Platformer
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
thank you for the link kanaida.

yes i guess the game logic should be the same more or less with xna or any other game development.

i am wondering though, when using canvas to draw and a game loop... when i put a finger on the screen, the game slows down very badly, how do you guys overcome that?

it is not very noticeable if i use a timer to update the game and the game runs smoothly enough.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
allritey... coding time...

here is a simple way of moving your character on downpress of a button.

i do not have a mobile which has physical buttons, so i have added 2 on screen buttons... the code (added on to the gravity piece previously):

B4X:
Sub Process_Globals
   Dim t As Timer
End Sub

Sub Globals
   Dim p As Panel
   Dim g As Double
   Dim lb As Button
   Dim rb As Button
   
   Dim boolmove As Boolean
   Dim velx As Double
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then t.Initialize("t", 30)
   p.Initialize("")
   Activity.AddView(p, 20dip, 0dip, 20%x, 20%x)
   
   lb.Initialize("b")
   Activity.AddView(lb, 0, 0, 50%x, 20%x)
   lb.Text = "left"
   lb.Tag = "left"
   
   rb.Initialize("b")
   Activity.AddView(rb, 50%x, 0, 50%x, 20%x)
   rb.Text = "right"
   rb.Tag = "right"

   p.Color = Colors.Blue
   g = 0
   t.Enabled = True
End Sub

Sub b_Down
   'get the sender and assign the right acceleration to left or right
   Dim s As Button
   s = Sender
   
   If s.Tag = "left" Then
      velx = -3
   Else
      velx = 3
   End If
   
   boolmove = True 
   
End Sub

Sub b_Up
   velx = 0 'no more speed
   boolmove = False 'stop moving, though this might be redundant as speed is already 0
End Sub

Sub t_Tick
   'add gravity and bounce
   g = g + 0.5
   p.Top = p.Top + g
   If (p.Top + p.Height) > 100%y Then
      p.Top = 100%y - p.Height
      g = (g * -1) + 2
   End If
   
   'move the panel based on horizontal velocity
   If boolmove Then
      p.Left = p.Left + velx
   End If
End Sub
hope the above is useful somehow...

Very usefull! Thanks.
So does this work with an imageview to move the character around and let it jump?


as for your code XverhelstX, I am not judging you or anything, but i really think you do not need 2 subs for the move left and right, setting the velocity to positive or negative should provide the desired result from a single sub.

it will be easier to maintain in future... just my 2 cents! (yes yes.. please take it! hahaha)

cheers!

Well yeah, I'm quite new at game programming. I just adapted some things from the Smiley example

I know it's not B4A but Visual Studio Express + XNA has a ton of these kinds of tutorials. The graphics engine / directx code is gonna be way different, but there's usually a very useful section, the game's main loop. There's usually a sub that contains all kinds of code that deals with keystrokes, that's the part you'd want to look at. From what i've ran in the past, I can tell you they have pretty easy to understand / translate code that just relates to physics, levels, game objects, bounding boxes etc... along with lots of comments.

Here's an example:
Starter Kit: Platformer

Thanks kanaida, very intresting.
I will update the first page of this topic into Basic4Android Game Making or something for people who are intrested in the B4A Gaming skills.

Sincerely,
XverhelstX
 
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
OpenGL

I forgot to mention, if you have any experience with OpenGL, that can solve most of not 100% of performance problems. Because they have accelerated ways to do the same stuff you do in regular code. For example "transforms" as they call them in directx. These things take a matrix of vertxes and do something to them, like rotation, scaling, movement and if I remember right collision detection. Except they use 3d cubes and simple boolean operations as it's too expensive usually to check the object directly because it has too many vertecies to check as opposed to 4 like a dice.

Generic

A vertex = a little point. like a corner of a 3d object.
Vertex (geometry) - Wikipedia, the free encyclopedia

A Surface, just like it sounds. not to get too complicated the smallest surface is a triangle made up of 3 vertex.

A 2D texture is what you draw on a surface. A bitmap. You can draw these on surfaces, usually squares (like a little transparent bitmap holding mario as the texture). When placed in front of another plane with a background image texture, you now have a level almost.
Programming with DirectX : Shading and Surfaces - Types of Textures - Tutorials,Articles,Algorithms,Tips,Examples about Multimedia

Here's a similar project to B4A with some tutorials, except instead of android for OpenGL.
Basic4GL

from what I remember they always went something like this:

Do while Running
ReadInputs()
UpdateGameValues()
HandleEventslikeCollisions() 'Here you set game object properties like .Alive = false etc..
Draw() 'Draw everything that's alive
Next

It's a little intimidating at first seeing all those little numbers until you realize it's because they're making objects by hand point by point instead of reading them from a file. But essentially it's the same as 2D for the most part, except way faster and smoother. Think MiniSquadron. I'll bet my mother they use OpenGL. But it's a relatively simple 2D concept.

This Wiki also helped out with common math formulas etc... it's for game programming in all languages.
http://www.gpwiki.org/index.php/Main_Page
 
Last edited:
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi kanaida,

Thank you for the link to the Basic4GL. This would be fun to learn.

Hopefully another thread for an opengl game tutorial would surface once we get some experts made for B4A for opengl :D
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi XverhelstX,

Yes, this logic would work with imageviews too. In each update you could assign a different bitmap/fram to the imageview to display which could show your character running.

As for moving and jumping at the same time, that is something you'd need to work out on how you'd like to handle it.

One option is to cater for multi touch, another is using tilt to move your character and tap screen to jump. There should be other ways too, like your character is constantly running to the right, and you can only jump.

Cheers!
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I just noticed this topic so I like to share this. As a hobby project, I'm writing a library that will be able to make platform games or fight games in b4a very easily.

Some of it's features I want to build in:
1. fully animated sprites (for the main hero as well for the villains)
2. fully customizable on screen game pad (multi touch)
3. background audio and action sounds
4. different layers (background, level map, characters, ...)
5. each layer can scroll on it's own (like a background layer that scrolls slower than the level layer so you get a fake 3D effect)
6. Events to b4a when something happens (hit a wall, hit an enemy, etc...)

All this will be done by the library. All you would need to write is the game logic and make the graphics.

Currently, step 1, 2 and 4 are almost done. If you have suggestions for this engine, please do.

This is for example a screenshot of what I have now. The code is no more than 40 lines of code in b4a (mainly adding the sprites). All sprites are animated, the main character can be controlled by the game pad.

snapshot.png
 
Upvote 0
Top