Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed regarding Lesson 17
#22
Quote:I would gladly welcome help to correct this lesson.

OK @TerryRitchie we'll see about that Smile
Code: (Select All)
_Title "Ship Demo for lesson 17, Degrees Only" ' bplus overhaul 2024-10-09

Type XYPAIR '            x,y value definition
    X As Single '        x value
    Y As Single '        y value
End Type

Type SHIP_Type '         ship definition First 2 needed for DrawShip
    Loc As XYPAIR '      where the ship center is on screen
    Heading As Single '  Degree angle the ship is pointed towards
    V As XYPAIR '        Normal vector: V.X = CosD(Ship.Heading) V.Y = SinD(Ship.Heading)
    Velocity As Single ' mult. Normal Vector by Velocity add to old Loc for new Location
End Type

Dim ship As SHIP_Type '         get our ship started!
Dim angle '                     degrees for the compass
Dim showCompass As Integer '    show Compass toggle flag
showCompass = -1 '              turn the toggle on
ship.Loc.X = 400 '              ship's x coordinate on screen
ship.Loc.Y = 300 '              ship's y coordinate on screen
ship.Heading = 270 '            let's head North for starters
ship.Velocity = 5 '             ship's speed

Screen _NewImage(800, 600, 32) ' graphics screen
_ScreenMove 250, 60 '            centered 0n laptop screen

Do '                             begin main loop
    Cls '                        clear screen
    Print '                      here are instructions to user  cp = Center Print
    cp "Right/Left Arrow keys to turn, Up Arrow key to move forward"
    cp "Spacebar to toggle Compass Off/On"
    DrawShip ship.Loc, ship.Heading
    If showCompass Then ' I like your idea Terry to show turn ticks, here is a nice Compass for you.
        For angle = 0 To 359 Step 15
            PSet (ship.Loc.X + 62 * CosD(angle), ship.Loc.Y + 62 * SinD(angle)) ' draw pixel at 10 degree point
            If angle = 0 Then
                _PrintString (ship.Loc.X + 62 * CosD(angle) + 3, ship.Loc.Y + 62 * SinD(angle) - 7), "E"
            ElseIf angle = 90 Then
                _PrintString (ship.Loc.X + 62 * CosD(angle) - 2, ship.Loc.Y + 62 * SinD(angle) + 1), "S"
            ElseIf angle = 180 Then
                _PrintString (ship.Loc.X + 62 * CosD(angle) - 11, ship.Loc.Y + 62 * SinD(angle) - 7), "W"
            ElseIf angle = 270 Then
                _PrintString (ship.Loc.X + 62 * CosD(angle) - 2, ship.Loc.Y + 62 * SinD(angle) - 16), "N"
            End If
        Next
    End If
    If _KeyDown(18432) Then '                                up arrow key pressed? move it!
        ship.V.X = CosD(ship.Heading) ' very simple formula for normal x component of vector
        ship.V.Y = SinD(ship.Heading) ' very simple formula for normal y component of vector
        ship.Loc.X = ship.Loc.X + ship.V.X * ship.Velocity ' new X location for ship
        ship.Loc.Y = ship.Loc.Y + ship.V.Y * ship.Velocity ' new Y location for ship
        If ship.Loc.X < 0 Then ShipLoc.x = 0 '               keep ship on screen  just stop!
        If ship.Loc.X > _Width Then ship.Loc.X = _Width '    when at border
        If ship.Loc.Y < 0 Then ship.Loc.Y = 0 '              of the screen
        If ship.Loc.Y > _Height Then ShipLoc.y = _Height
    End If
    If _KeyDown(19200) Then ship.Heading = ship.Heading - 1 ' nose heading counter-clockwise to left
    If _KeyDown(19712) Then ship.Heading = ship.Heading + 1 ' nose heading clockwise to right
    If _KeyHit = 32 Then showCompass = Not showCompass '      toggle flag when space bar pressed
    _Display '                                                update screen with changes
    _Limit 60 ' gots to have pause for _keydown release or ship will spin like mad!
Loop Until _KeyDown(27) '                                     leave when ESC pressed
System

Sub DrawShip (place As XYPAIR, DegreeHeading As Single) ' hey we can use this design in another app!!!
    Dim As Single radius
    radius = 60 ' nose and wing tips touch radius around central location = placeXY
    Dim pts(1 To 3) As XYPAIR 'the 3 outer points for triangle ship with 2 wings fit on radius
    pts(1).X = place.X + radius * CosD(0 + DegreeHeading) '   nose at 0 degrees
    pts(1).Y = place.Y + radius * SinD(0 + DegreeHeading)
    pts(2).X = place.X + radius * CosD(135 + DegreeHeading) ' 180 - 45 SW wing tip
    pts(2).Y = place.Y + radius * SinD(135 + DegreeHeading)
    pts(3).X = place.X + radius * CosD(225 + DegreeHeading) ' 180 + 45 NW wing tip
    pts(3).Y = place.Y + radius * SinD(225 + DegreeHeading)
    Line (pts(1).X, pts(1).Y)-(pts(2).X, pts(2).Y)
    Line (pts(2).X, pts(2).Y)-(place.X, place.Y)
    Line (pts(3).X, pts(3).Y)-(place.X, place.Y)
    Line (pts(3).X, pts(3).Y)-(pts(1).X, pts(1).Y)
End Sub

' use angles in degrees units instead of radians (converted inside sub)
Function CosD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    CosD = Cos(_D2R(degrees))
End Function

' use angles in degrees units instead of radians (converted inside sub)
Function SinD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    SinD = Sin(_D2R(degrees))
End Function

Sub cp (s$) ' cp = Center Print string s$ assuming 8 pixel width font on graphics screen
    Print Space$(((_Width / 8) - Len(s$)) / 2); s$
End Sub

   

Here is same exact project in Radians Only for comparison: (I remembered this code was suppose to be a Radian Demo)
Code: (Select All)
_Title "Ship Demo for lesson 17 Radians Only" ' bplus overhaul 2024-10-09

Type XYPAIR '            x,y value definition
    X As Single '        x value
    Y As Single '        y value
End Type

Type SHIP_Type '         ship definition First 2 needed for DrawShip
    Loc As XYPAIR '      where the ship center is on screen
    Heading As Single '  Degree angle the ship is pointed towards
    V As XYPAIR '        Normal vector: V.X = cos(Ship.Heading) V.Y = sin(Ship.Heading)
    Velocity As Single ' mult. Normal Vector by Velocity add to old Loc for new Location
End Type

Dim ship As SHIP_Type '         get our ship started!
Dim angle '                     degrees for the compass
Dim showCompass As Integer '    show Compass toggle flag
showCompass = -1 '              turn the toggle on
ship.Loc.X = 400 '              ship's x coordinate on screen
ship.Loc.Y = 300 '              ship's y coordinate on screen
ship.Heading = _Pi(3 / 2) '     let's head North for starters
ship.Velocity = 5 '             ship's speed

Screen _NewImage(800, 600, 32) ' graphics screen
_ScreenMove 250, 60 '            centered 0n laptop screen

Do '                             begin main loop
    Cls '                        clear screen
    Print '                      here are instructions to user  cp = Center Print
    cp "Right/Left Arrow keys to turn, Up Arrow key to move forward"
    cp "Spacebar to toggle Compass Off/On"
    DrawShip ship.Loc, ship.Heading
    If showCompass Then ' I like your idea Terry to show turn ticks, here is a nice Compass for you.
        For angle = 0 To _Pi(1.99) Step _Pi(2 / 24)
            PSet (ship.Loc.X + 62 * Cos(angle), ship.Loc.Y + 62 * Sin(angle)) ' draw pixel at 10 degree point
            If angle = 0 Then
                _PrintString (ship.Loc.X + 62 * Cos(angle) + 3, ship.Loc.Y + 62 * Sin(angle) - 7), "E"
            ElseIf Abs(angle - _Pi(.5)) < .0001 Then
                _PrintString (ship.Loc.X + 62 * Cos(angle) - 2, ship.Loc.Y + 62 * Sin(angle) + 1), "S"
            ElseIf Abs(angle - _Pi) < .0001 Then
                _PrintString (ship.Loc.X + 62 * Cos(angle) - 11, ship.Loc.Y + 62 * Sin(angle) - 7), "W"
            ElseIf Abs(angle - _Pi(3 / 2)) < .0001 Then
                _PrintString (ship.Loc.X + 62 * Cos(angle) - 2, ship.Loc.Y + 62 * Sin(angle) - 16), "N"
            End If
        Next
    End If
    If _KeyDown(18432) Then '                                up arrow key pressed? move it!
        ship.V.X = Cos(ship.Heading) ' very simple formula for normal x component of vector
        ship.V.Y = Sin(ship.Heading) ' very simple formula for normal y component of vector
        ship.Loc.X = ship.Loc.X + ship.V.X * ship.Velocity ' new X location for ship
        ship.Loc.Y = ship.Loc.Y + ship.V.Y * ship.Velocity ' new Y location for ship
        If ship.Loc.X < 0 Then ShipLoc.x = 0 '               keep ship on screen  just stop!
        If ship.Loc.X > _Width Then ship.Loc.X = _Width '    when at border
        If ship.Loc.Y < 0 Then ship.Loc.Y = 0 '              of the screen
        If ship.Loc.Y > _Height Then ShipLoc.y = _Height
    End If
    If _KeyDown(19200) Then ship.Heading = ship.Heading - _Pi(1 / 180) ' nose counter-clockwise to left
    If _KeyDown(19712) Then ship.Heading = ship.Heading + _Pi(1 / 180) ' nose heading clockwise to right
    If _KeyHit = 32 Then showCompass = Not showCompass '      toggle flag when space bar pressed
    _Display '                                                update screen with changes
    _Limit 60 ' gots to have pause for _keydown release or ship will spin like mad!
Loop Until _KeyDown(27) '                                     leave when ESC pressed
System

Sub DrawShip (place As XYPAIR, DegreeHeading As Single) ' hey we can use this design in another app!!!
    Dim As Single radius
    radius = 60 ' nose and wing tips touch radius around central location = placeXY
    Dim pts(1 To 3) As XYPAIR 'the 3 outer points for triangle ship with 2 wings fit on radius
    pts(1).X = place.X + radius * Cos(0 + DegreeHeading) '   nose at 0 degrees
    pts(1).Y = place.Y + radius * Sin(0 + DegreeHeading)
    pts(2).X = place.X + radius * Cos(_Pi(3 / 4) + DegreeHeading) ' 180 - 45 SW wing tip
    pts(2).Y = place.Y + radius * Sin(_Pi(3 / 4) + DegreeHeading)
    pts(3).X = place.X + radius * Cos(_Pi(5 / 4) + DegreeHeading) ' 180 + 45 NW wing tip
    pts(3).Y = place.Y + radius * Sin(_Pi(5 / 4) + DegreeHeading)
    Line (pts(1).X, pts(1).Y)-(pts(2).X, pts(2).Y)
    Line (pts(2).X, pts(2).Y)-(place.X, place.Y)
    Line (pts(3).X, pts(3).Y)-(place.X, place.Y)
    Line (pts(3).X, pts(3).Y)-(pts(1).X, pts(1).Y)
End Sub

Sub cp (s$) ' cp = Center Print string s$ assuming 8 pixel width font on graphics screen
    Print Space$(((_Width / 8) - Len(s$)) / 2); s$
End Sub
b = b + ...
Reply


Messages In This Thread
Help needed regarding Lesson 17 - by RhoSigma - 10-08-2024, 10:39 AM
RE: Help needed regarding Lesson 17 - by bplus - 10-08-2024, 05:24 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-08-2024, 05:46 PM
RE: Help needed regarding Lesson 17 - by SMcNeill - 10-08-2024, 06:32 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-08-2024, 06:59 PM
RE: Help needed regarding Lesson 17 - by SMcNeill - 10-08-2024, 07:04 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-08-2024, 07:17 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-08-2024, 07:10 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-08-2024, 07:22 PM
RE: Help needed regarding Lesson 17 - by SMcNeill - 10-08-2024, 07:44 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-08-2024, 08:16 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-09-2024, 01:25 PM
RE: Help needed regarding Lesson 17 - by SMcNeill - 10-09-2024, 04:19 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-09-2024, 03:17 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-09-2024, 04:00 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-09-2024, 04:11 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-09-2024, 10:43 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-10-2024, 10:33 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-11-2024, 03:01 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-11-2024, 06:36 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-11-2024, 07:08 PM
RE: Help needed regarding Lesson 17 - by bplus - 10-11-2024, 08:51 PM



Users browsing this thread: 2 Guest(s)