b+ Beginners Corner
#18
OK I have "Terryfied"  enRitchied the code. 
Update: I suppose I should refrain from word play with newbies but I wanted to honor Terry Ritchie's work and very notable commenting of code in a humorous way. Terry has been building a great beginner resource here:
https://www.qb64tutorial.com/games
Code: (Select All)
Option _Explicit '                          Get into this habit and save yourself grief from Typos

_Title "Missile Command Terryfied" '                                     another b+ mod 2023-06-23
'                                   I probably picked up this game at the JB forum some years ago.

'    Get Constants, Shared Variables and Arrays() declared. These Will Start with Capital Letters.
'        Get Main module variables and arrays declared with starting lower case letters for local.
'         This is what Option _Explicit helps, by forcing us to at least declare these before use.
'       While declaring, telling QB64 the Type we want to use, we can also give brief description.


Const ScreenWidth = 800, ScreenHeight = 600 '                     for our custom screen dimensions
Dim As Integer bombX, bombY '                          incoming bomb screen position to shoot down
Dim As Single bombDX, bombDY '                  DX and DY mean change in X position and Y position
Dim As Integer missileX, missileY '                                               missile position
Dim As Single missileDX, missileDY '                            change X and Y of Missile position
Dim As Integer hits, misses '                                                score hits and misses
Dim As Integer mouseDistanceX, mouseDistanceY '       for calculations of missile DX, DY direction
Dim As Single distance '                                                                     ditto
Dim As Integer radius '                                      drawing hits with target like circles
Dim As Integer boolean '                         to shorten the code line with a bunch of OR tests

Screen _NewImage(ScreenWidth, ScreenHeight, 32) ' sets up a graphics screen with custom dimensions
'                                          the 32 is for _RGB32(red, green, blue, alpha) coloring.
'
_ScreenMove 250, 60 '             this centers screen in my laptop, you may need different numbers

InitializeForRound: '                             reset game and start a round with a bomb falling
Cls
bombX = Rnd * ScreenWidth '                                starts bomb somewhere across the screen
bombY = 0 '                                                           starts bomb at top of screen
bombDX = Rnd * 6 - 3 '                                  pick rnd dx = change in x between -3 and 3
bombDY = Rnd * 3 + 3 '                 pick rnd dy = change in y between 3 and 6,  > 0 for falling
missileX = ScreenWidth / 2 '                                  missile base at middle across screen
missileY = ScreenHeight - 4 '   missile launch point at missile base is nearly at bottom of screen
missileDX = 0 '                           missile is not moving awaiting mouse click for direction
missileDY = 0 '                                                                              ditto
distance = 0 '                                             distance of mouse click to missile base

Do
    'what's the score?
    _Title "Click mouse to intersect incoming   Hits:" + Str$(hits) + ", misses:" + Str$(misses)
    _PrintString (400, 594), "^" '                                 draw missle base = launch point
    While _MouseInput: Wend '                                             poll mouse to get update
    If _MouseButton(1) Then '               the mouse was clicked calc the angle from missile base
        mouseDistanceX = _MouseX - missileX
        mouseDistanceY = _MouseY - missileY
        distance = (mouseDistanceX ^ 2 + mouseDistanceY ^ 2) ^ .5
        missileDX = 5 * mouseDistanceX / distance
        missileDY = 5 * mouseDistanceY / distance
    End If

    missileX = missileX + missileDX '                                      update missile position
    missileY = missileY + missileDY '                                                        ditto
    bombX = bombX + bombDX '                                                  update bomb position
    bombY = bombY + bombDY '                                                                 ditto

    '                     I am about to use a boolean variable to shorten a very long IF code line
    '                                 boolean is either 0 or -1 when next 2 statements are execued
    '                                            -1/0 or True/False is everything still in screen?
    boolean = missileX < 0 Or missileY < 0 Or bombX < 0 Or bombY < 0
    boolean = boolean Or missileX > ScreenWidth Or bombX > ScreenWidth Or bombY > ScreenHeight
    If boolean Then ' done with this boolean
        '   reuse boolean to shorten another long code line checking if bomb and missile in screen
        boolean = bombY > ScreenHeight Or missileX < 0 Or missileY < 0 Or missileX > ScreenWidth
        If boolean Then misses = misses + 1
        GoTo InitializeForRound
    End If
    '     if the distance between missle and bomb < 20 pixels then the missile got the bomb, a hit
    If ((missileX - bombX) ^ 2 + (missileY - bombY) ^ 2) ^ .5 < 20 Then '  show a strike as target
        For radius = 1 To 20 Step 4 '                        draw concetric circles to show strike
            Circle ((missileX + bombX) / 2, (missileY + bombY) / 2), radius
            _Limit 60
        Next
        hits = hits + 1 '                                                    add hit to hits score
        GoTo InitializeForRound
    Else
        PSet (missileX, missileY), &HFFFFFF00 '                            draw your missle yellow
        PSet (bombX, bombY), &HFF0000FF '                                           draw bomb blue
    End If
    _Limit 20
Loop

BTW you can click the mouse more than once, in fact you can turn the mouse and missile into a heat seeker and curve around back if you over shoot, pretty cool!

Look at the nice clean edges I had in the IDE, can't repeat in any code tag??? wait now old code tag looks good and it pastes correctly aligned back in IDE yea!
   
b = b + ...
Reply


Messages In This Thread
b+ Beginners Corner - by bplus - 05-20-2023, 06:34 PM
RE: b+ Beginners Corner - by vince - 05-20-2023, 06:47 PM
RE: b+ Beginners Corner - by bplus - 05-20-2023, 07:11 PM
RE: b+ Beginners Corner - by PhilOfPerth - 05-26-2023, 12:12 AM
RE: b+ Beginners Corner - by bplus - 05-26-2023, 04:11 PM
RE: b+ Beginners Corner - by PhilOfPerth - 05-26-2023, 11:18 PM
RE: b+ Beginners Corner - by mnrvovrfc - 05-27-2023, 12:15 AM
RE: b+ Beginners Corner - by PhilOfPerth - 05-27-2023, 02:27 AM
RE: b+ Beginners Corner - by bplus - 05-29-2023, 12:07 AM
RE: b+ Beginners Corner - by bplus - 05-29-2023, 01:37 AM
RE: b+ Beginners Corner - by mnrvovrfc - 05-29-2023, 02:29 AM
RE: b+ Beginners Corner - by bplus - 05-30-2023, 04:17 PM
RE: b+ Beginners Corner - by bplus - 06-15-2023, 03:06 PM
RE: b+ Beginners Corner - by GareBear - 06-15-2023, 07:50 PM
RE: b+ Beginners Corner - by bplus - 06-15-2023, 10:42 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 02:46 PM
RE: b+ Beginners Corner - by CharlieJV - 06-23-2023, 03:26 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 08:28 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-23-2023, 09:45 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 09:56 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-24-2023, 02:47 AM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 10:02 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 02:35 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 02:52 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 07:48 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-24-2023, 08:02 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 08:40 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-24-2023, 10:07 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 09:08 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 09:12 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-24-2023, 11:44 PM
RE: b+ Beginners Corner - by bplus - 06-25-2023, 02:27 PM
RE: b+ Beginners Corner - by OldMoses - 06-25-2023, 05:49 PM
RE: b+ Beginners Corner - by bplus - 06-25-2023, 06:40 PM
RE: b+ Beginners Corner - by OldMoses - 06-25-2023, 08:03 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 01:14 AM
RE: b+ Beginners Corner - by mnrvovrfc - 06-26-2023, 02:26 AM
RE: b+ Beginners Corner - by Ultraman - 06-26-2023, 11:29 AM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 12:17 PM
RE: b+ Beginners Corner - by Ultraman - 06-26-2023, 12:21 PM
RE: b+ Beginners Corner - by Dimster - 06-26-2023, 02:38 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 03:32 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 04:48 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 01:29 AM
RE: b+ Beginners Corner - by OldMoses - 06-27-2023, 11:49 AM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 12:40 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-27-2023, 02:12 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 03:22 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-27-2023, 05:21 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 05:48 PM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 03:20 AM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 02:54 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-28-2023, 07:07 PM
RE: b+ Beginners Corner - by Dimster - 06-28-2023, 09:50 PM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 10:27 PM



Users browsing this thread: 17 Guest(s)