Catch the bouncing smiley

Erel

Administrator
Staff member
Licensed User
Attached is a small program of a bouncing (and rotating) smiley.

As you can see in the image the background is not just a solid color.

smiley.png



The code for this program:
B4X:
'Activity module
Sub Process_Globals
    Dim x, y, vx, vy As Int
    Dim boxSize As Int
    Dim Rect As Rect
    Dim timer1 As Timer
    Dim SmileyBitmap As Bitmap
    Dim Degrees As Int
End Sub

Sub Globals
    Dim Canvas1 As Canvas
    Dim Bitmap1 As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Timer1.Initialize("Timer1", 30)
        vx = 10dip 'smiley speed
        vy = 10dip
        x = 50%x 'smiley first position (middle of the screen
        y = 50%y
        boxSize = 40dip 'smiley size
        Rect.Initialize(0, 0, 0, 0)
        SmileyBitmap.Initialize(File.DirAssets, "smiley.gif") 'load the smiley file
    End If
    Activity.LoadLayout("smiley.bal") 'load the designer layout (just the gradient color in this case).
    Canvas1.Initialize(Activity) 'Initialize the canvas (drawer) and makes it draw on the activity (form).
    Bitmap1.Initialize3(Canvas1.Bitmap) 'Save a copy of the current background. This bitmap will be used to erase the smiley each time.
End Sub
Sub Activity_Pause
    Timer1.Enabled = False
End Sub
Sub Activity_Resume
    Timer1.Enabled = True
End Sub
Sub Timer1_Tick
    If x  + boxSize > 99%x Then
        vx = -1 * Abs(vx)
    Else If x < 1%x Then
        vx = Abs(vx)
    End If
    If y  + boxSize > 99%y Then
        vy = -1 * Abs(vy)
    Else If y < 1%y Then
        vy = Abs(vy)
    End If
    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 + vy
    Rect.Top = y
    Rect.Left = x
    Rect.Bottom = y + boxSize
    Rect.Right = x + boxSize
    Degrees = (Degrees + 10) Mod 360
    Canvas1.DrawBitmapRotated(SmileyBitmap, Null, Rect, Degrees) 'Draw the new smiley
    Activity.Invalidate2(Rect)
End Sub
Sub Activity_Touch(Action As Int, tx As Float, ty As Float)
    If Not(Action = Activity.ACTION_DOWN) Then Return
    If tx > x - boxSize AND tx < x + boxSize AND _
        ty > y - boxSize AND ty < y + boxSize Then
        vx = -vx 'change the smiley direction if the user pressed on it
        vy = -vy
    End If
    Canvas1.DrawCircle(tx, ty, 5dip, Colors.Red, False, 2dip)
    Activity.Invalidate3(tx - 7dip, ty - 7dip, tx + 7dip, ty + 7dip)
End Sub
You are more than welcome to try to run it on your android device. :)
 

Attachments

  • Smiley.apk
    52.6 KB · Views: 481
  • Smiley-Source.zip
    8.4 KB · Views: 906

Cableguy

Expert
Licensed User
Is This the first App created by Our Basic4Android IDE????
Cool, I will try that...

WIll it be possible to have a note sheet in the IDE?, justo note things to do, goals, and specifics descriptions like variables purpose, and so on?
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
Is This the first App created by Our Basic4Android IDE????
There are several internal test applications. This is for sure the first one with a smiley.

WIll it be possible to have a note sheet in the IDE?, justo note things to do, goals, and specifics descriptions like variables purpose, and so on?
I guess that the regular comments do target most of these features...
 
Upvote 0

Cableguy

Expert
Licensed User
I guess that the regular comments do target most of these features...
Yes they do, but having to scroll a high numbered code line app, just to find a description, is tedious...and time consuming...
In Basic4ppc, I use a "module" and strip it of the Globals, and just place comments, but sometimes I forget to add the " ' ", so the app misfires...
A note sheet would be nice, but not essencial...
 
Upvote 0

corwin42

Expert
Licensed User
Attached is a small program of a bouncing (and rotating) smiley.

As you can see in the image the background is not just a solid color.

Works great on Vodafone 845 with 240x320 screen resolution.

Hope we will see a first beta soon. :)
 
Upvote 0

derez

Expert
Licensed User
I got my development environment set up and it runs nicely on "my" emulator :)

Pressing the mouse button while on the top bar of the emulator window freeze the smiley's motion !

Erel, keep on the good work, I'm ready for more :icon_clap:
 

Attachments

  • smiley.jpg
    smiley.jpg
    71.8 KB · Views: 326
Last edited:
Upvote 0

Brad

Active Member
Licensed User
I downloaded the zip file but had a problem extracting the smiley.b4a file. Couldn't find the file even though it's visible.
 
Upvote 0

Widget

Well-Known Member
Licensed User
Nice animation. I did notice if the bitmap is not perfectly round, then you need to increase the sides of the erasing square to be the length of the diagonal of the bitmap. Otherwise it will leave remnants of the previously rotated bitmap.

B4X:
    Rect.Top = y
    Rect.Left = x
    Rect.Bottom = y + Sqrt(boxSize*boxSize*2)   'Assuming bitmap is square
    Rect.Right = x + Sqrt(boxSize*boxSize*2)
    Canvas1.DrawBitmap(Bitmap1, Rect, Rect) 'Erase the previous smiley
    Activity.Invalidate2(Rect)

Widget
 

Attachments

  • smiley.gif
    smiley.gif
    3.1 KB · Views: 270
Upvote 0

jag

Member
Licensed User
Not sure if this is the right place, but... I took Erel's bounce code, combined with the transparent window from "SimpleDrawFunctions" in the Beginner's Guide to create a variation on the theme.
Additional info:
- random-color generator
- array of labels used as color objects
- view placement and sizing by % to fit various screens
- adjust ball (window) size according to landscape or portrait mode
- no layout, everything is created at runtime
- lots of comments

I hope other newbies find it useful.
John
 

Attachments

  • BBall_II.zip
    6.7 KB · Views: 288
Upvote 0
Top