Share My Creation Zombies in the Swamp

Swombies (Zombies in the Swamp) v4.0

Hi All,

Here is the updated, and probably final, release of a simple 2D game that I have been writing over the last three weeks.

My main goal was to learn more about B4A programming, so I have done everything in code - not used the Designer at all.

All screen objects now position/size/scale themselves correctly (I believe) based on the device that it is running on, whether it be a large tablet or a small phone.

If you play it let me know what you think.


Regards,
PenguinHero.

Edit: Hopefully final version that corrected the issue of button text on small screens. All seems correct now. If not, please tell me.
 

Attachments

  • Swombies.apk
    383.2 KB · Views: 251
  • Swombies.jpg
    Swombies.jpg
    88.6 KB · Views: 3,675
  • Swombies_Splash.jpg
    Swombies_Splash.jpg
    88.5 KB · Views: 247
Last edited:

PenguinHero

Member
Licensed User
Longtime User
Thanks for the feedback and screenshots.

I expected it would look Ok on a Nexus 7.
I wonder why some items are placed correctly, and others are way out? It seems to be the difference between using DrawText and placing labels on the activity. I used a mixture of both.

I think I need to do a bit of research.

The splash screen looked correct on the Galaxy, did it play correctly on that?
And how did you do the screen captures?
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
If you use the designer scripts it will look ok on any device. They are out of place because perhaps you are not using DIP.

I captured the screens using the Android tools (Monitor).
 

PenguinHero

Member
Licensed User
Longtime User
Yes, I noticed a few of my coordinates were missing an explicit "dip", and have changed them.

Where I use a calculated variable to determine the value, how do I attach the dip in the parameter?
 

klaus

Expert
Licensed User
Longtime User
Where I use a calculated variable to determine the value, how do I attach the dip in the parameter?
It depends on where the values you use to calculate come from.
If the valies are %x or %y values or Left, Top, Width or Top properties no need.
To convert absolute values you can use DipToCurrent(x) like:
Button1.Width = DipToCurrent(100)
this is the same as
Button1.Width = 100dip

Best regards.
 

PenguinHero

Member
Licensed User
Longtime User
Thanks klaus,

But I was thinking more of calculated coordinates.
For example, in my game I need to move the man and zombies around, so I calculate the coordinates for each DrawBitmap rectangle, and then do the initialise like this: rect1.Initialize(Left,Top,Right,Bottom)

The parameters (Left, etc.) are all Int variables. Should I just be sticking a dip after each one? And if so, how? Or have I missed something here?
 

klaus

Expert
Licensed User
Longtime User
in my game I need to move the man and zombies around, so I calculate the coordinates for each DrawBitmap rectangle
How are your man and zombies defined ?
How are the moving values defined ?
With bitmaps it depends on how you loaded them.

Best regards.
 
Last edited:

PenguinHero

Member
Licensed User
Longtime User
Hi klaus,

Thanks for your help on this - I'll use a few code examples to try and answer your questions. Perhaps you can tell me of better ways of doing things.
I've just shown the lines of code that matter, cut from various places in my program, with comments added to show where I do a calculation (that's a bit big to show here).

How are your man and zombies defined ?

B4X:
Dim bmpZombie As Bitmap
bmpZombie.Initialize(File.DirAssets,"Zombie2.bmp")
' Do calc to move a zombie towards the man, storing the results in Int variables (Left, Top, etc.).
rect1.Initialize(Left,Top,Right,Bottom)
cvsBoard.DrawBitmap(bmpMan,Null,rect1)

How are the moving values defined ?
I calculate them based on current integer coordinates stored for my man and zombie. I store everything that is on the game board in a 2 dimensional array, and use the array index positions to convert back to actual coordinates for my rect1 parameters.
To speed things up I also keep two Int variables that hold the board X and Y coordinates of the man.

With bitmaps it depends on how you loaded them.
Okay, what are the options and differences?

I notice that I haven't usually been putting "dip" on the width and height parameters for panels and alike, so I'll fix that too.
The big issue, as I see it, is the one I've already raised - how to add the "dip" to a Int variable when it is being used as parameter, like my rect1 above.

Edit: This has been fixed with the wrapping each variable in the Rectangle initialise with a DipToCurrent. That appears to have fixed the issue, but I don't have another device to test it on, just my Kindle Fire which always looked correct.


Thanks,
PH.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
I notice that I haven't usually been putting "dip" on the width and height parameters for panels and alike, so I'll fix that too.
If the panel withds and heights are defined or modified in the code with absolute values you must add dip or use the DipToCurrent(x) function.

If you want you could send me the project and I'll have a look at it.

Best regards.
 

PenguinHero

Member
Licensed User
Longtime User
Thanks Klaus,

I'm not sure if you have seen the edits I made to the posts above, but I seem to have everything working as expected now - subject to someone running it on a device other than a kindle fire, that has a large enough screen (it still wont scale down).
It looks "good" using the android emulator, which I hadn't tried until I had the problems that NJDude found. When I first ran it on the emulator it looked a lot like NJDude's screen images. Now it looks like my kindle shows it.

Adding the missing DIPs and wrapping all the variable coordinate parameters in the DipToCurrent function appears to have fixed everything. It was this final bit, the way of getting the DIP associated with a parameter variable, rather than just suffixing a literal number with "dip", that was the sticking point.
 

PenguinHero

Member
Licensed User
Longtime User
Thanks NJDude.

The Galaxy looks almost exactly as expected, but I'm still surprised that the Nexus is truncating the bottom of the screen a little.
I have no idea why that would be.
 

PenguinHero

Member
Licensed User
Longtime User
Hi NJDude,

I was mainly trying to learn more about the programming side of B4A.

But I also struggle with the concept of having all the screen objects declared in the designer - all those zombies and pits would need to be ImageViews that would need to be placed in the designer, I presume?

I know I can move/hide them programactically, but it looks like it would be very messy to set up. I might have a play with doing something simple to try it out...

And from a playability perspective, I doubt there is enough screen space on a phone to see the board properly anyway. It is only just big enough on my kindle fire as it is.

But thanks for your help with this.
Maybe I need to see the source for an equivalent 2D graphics game where the designer was used, and learn how that was done.
 

NJDude

Expert
Licensed User
Longtime User
I agree, that game will look better on tabs only, but you could make it fix perfectly on any size tab using a different way.

You've done a fine job so far, as you gain experience you'll improve your code, just matter of time.
 

JesseW

Active Member
Licensed User
Longtime User
With a little work, you could convert your current pixel coordination system to work on any tablet of any resolution. I've had issues using the dip operator, because it relies on the screenscale parameter, which is set by the manufacturer, and isn't always correct for pixel mapping. Here's what I would do in your circumstance

Added in Sub Globals
B4X:
   Dim ww_, wh_ As Int      'window width and height
   Dim ws_ As Boolean      'widescreen
   Dim dx_, dy_ As Double   'density modifiers

Added in Sub Activity_Create
B4X:
   ww_ = Activity.Width
   wh_ = Activity.Height
   If ww_ > wh_ Then
      ws_ = True
      dx_ = ww_ / 1024
      dy_ = wh_ / 600
   Else
      ws_ = False
      dx_ = ww_ / 600
      dy_ = wh_ / 1024
   End If

Then modify your pixel coordination system like this: An example line that might be in your code

B4X:
' Do calc to move a zombie towards the man, storing the results in Int variables (Left, Top, etc.).
rect1.Initialize(Left,Top,Right,Bottom)
cvsBoard.DrawBitmap(bmpMan,Null,rect1)

Change it to this...

B4X:
' Do calc to move a zombie towards the man, storing the results in Int variables (Left, Top, etc.).
rect1.Initialize(Left*dx_,Top*dy_,Right*dx_,Bottom*dy_)
cvsBoard.DrawBitmap(bmpMan,Null,rect1)

It will add a little overhead in coordinate processing, but if all of your coordinate processing is enhanced this way, it'll work on any tablet. If your bitmap sizing is also skewed in this fashion, it'll work on any android device.

One issue tho... If the device orientation changes to portrait, the sizes and/or positions of the bitmaps may be stretched. Use the ws_ (widescreen) variable to detect when this happens, and adjust accordingly. Perhaps consider setting the dy_ variable to GetDeviceLayoutValues.Scale instead of computing it based on the screen height.

I use this method of calculating screen coordinates when I code in B4Script, because the dip and %x / %y operators are not available. Hope this helps...
 

PenguinHero

Member
Licensed User
Longtime User
Thanks for your feedback on this JesseW.

I had been thinking along similar lines, but stopped when I considered the issues with rounding/truncation to get to the integer values required.
But perhaps a small difference between each item being displayed wouldn't matter too much.

I'll have a play with it and see how it looks.

I presume that using the B4A project option of Portrait Only locks that in, so I don't need to worry about landscape mode?

I'll also update the APK file in the first post (yet again) now that I have found out that the B4A projects have two properties to use Full Screen (I wondered how to "minimise" the menu bar an the bottom of my Kindle Fire) and the option to hide the Title.
This frees up a bit more screen space, so it might not truncate on a Nexus 7 any more.
 

PenguinHero

Member
Licensed User
Longtime User
JesseW, I followed you suggestions, and found that it worked correctly once I included the devices scale as a divisor for the calculated dx_ & dy_

I had to apply the dx_ to button text sizes as well, but on the whole it looks like it has worked - I need people with other devices to try it out. I've only tested on my KF and the default android emulator as it has a different scale.

PH.
 
Top