Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
b+ Beginners Corner
#1
I hope to post fun little bits of code and then try to comment them as fully as I can in attempts to get people new to QB64 up and running faster. Feel free to ask questions to get clarification, I may have screwed up.

This is something I posted at Friends after messing around with a dice roll thing at Aurels place:
BTW that's here: https://friends-of-basic.freeforums.net
and here: http://basic4us.epizy.com/forum/index.php

Here is original post:
Code: (Select All)
_Title "Playing for Charity: 100 dice" ' b+ 2023-05-20
' A game that no one loses!
Randomize Timer
Dim As Long roll, Charity, me, r
For roll = 1 To 100
    r = Int(Rnd * 6) + 1
    Print "  Roll number:"; roll; "is"; r
    If r < 5 Then Charity = Charity + r Else me = me + r
    Print: Print "         I get 5's and 6's:"; me
    Print " Charity gets 1, 2, 3, 4's:"; Charity
    Print: Print "     zzz... press any to continue"
    Print
    Sleep
Next
Print: Print " Treat ";
If me > Charity Then Print "yourself"; Else Print "Others";
Print " specially well today."

Then I decided if I really wanted to do something Friendly I should comment more fully for Beginners.
So here is same resulting output with lots of comments:
Code: (Select All)
' The following not in original code but a very good habit to get into, it will save you
' from your typos. 3P = Proper Programming Practice :)
Option _Explicit ' This forces you to Dim (or ReDim) your variables before you use them.
' !!! EXCEPTION !!! should you do something like DEFINT A-Z or DEFLNG A-Z then everything
' is automatically. :O

' If the following is in the first lines of your program and you go to save it, QB64 IDE
' will suggest your _Title string for your .bas file name when starting from File > New.
_Title "Playing for Charity: 100 dice" ' b+ 2023-05-20   < 3P author and date with _Title.
' A game that no one loses! < 3P a quick summary or comments. I usually list versions here.

Randomize Timer ' this is here so I don't play the same game over on each run.

' Dim As Long roll, Charity, me, r  < 3P originally I had this, but really strong 3P is
' declaring all your variables first 2nd part of Option _Explicit team.
Dim As Long roll '            this tracks the For loop index eg roll number
Dim As Long r '               this is for random to be used to store the dice roll value.
Dim As Long Charity '         variable to store running total points for Charity
Dim As Long me '              variable to store running total points for me.
' I am using Long because I want Integers and Long is easier to type and doesn't take more
' time than Integer Type. So I get a range of over 32,000 with 0 cost.

For roll = 1 To 100 '         the main loop does 100 rolls

    r = Int(Rnd * 6) + 1 '    Rnd = 0 to .9999 Multiply by 6 and have 0 to 5.9999
    '                         Take Int(0 to 5.99999) get 0,1,2,3,4,5
    '                         Add 1 for 1 to 6
    '
    ' Print something; < with semi colon ending keeps the print head right where it stops.
    ' Print something, < with comma tabs the print head right to set column widths
    ' Print something  < nothing after literals or variables moves print head to next line.
    ' Print will automatically start on next line if can't finish print job on current one.
    ' Print:           < finishes last print line or starts new one by inserting blank line.
    Print "  Roll number:"; roll; "is"; r ' this first line reporting roll number and roll.

    If r < 5 Then Charity = Charity + r Else me = me + r ' this decides if me or Charity
    ' gets the rolled points

    ' This reports the running scores and tells Sleep is activated so we have to press a key
    ' to continue game.
    Print: Print "         I get 5's and 6's:"; me
    Print " Charity gets 1, 2, 3, 4's:"; Charity
    Print: Print "     zzz... press any to continue"
    Print
    Sleep
Next ' end of 100 rolls loop onto summary result.

Print: Print " Treat ";
If me > Charity Then Print "yourself"; Else Print "Others";
Print " specially well today."
' the End  < 3P if you had GoSubs after this End put an End statement here.

I decided to post the 2nd one here first because A) this is my favorite forum and B) it is likely to be seen more here.
C) I thought it an excellent first post for this thread.
b = b + ...
Reply
#2
now these are mods, very nice B+
Reply
#3
Thanks vince for all your support.

I see reading over my comments a potential point of confusion.
Colons, these :  are used to separate 2 statements on one line of code.
"Print:" is not a command it is Print statement with another following right after the colon on the same line above.

Hey what do you think the following will do?
Code: (Select All)
For i = 1 To 100
    Print ;
Next
Print "Hello"
Smile
b = b + ...
Reply
#4
A great idea , will be very useful to newbys to this forum. I'll be back!!!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#5
(05-26-2023, 12:12 AM)PhilOfPerth Wrote: A great idea , will be very useful to newbys to this forum. I'll be back!!!

Hi Phil, do you have any requests you'd like to see? I doubt you'd be the only one.
b = b + ...
Reply
#6
I do have one thing I'm struggling with, and suspect some others may also:
How do I make my prog use a Monospace font (eg Courier)? 
I think I'm supposed to add Monospace to the font name somewhere, but can't find a way to do this. I just get Invalid Handle.
Maybe a demo of how to assign some common fonts and font-types (Monospace, sans and serif etc.), and the main features of those types?
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#7
In think on Windows the font is called:
Code: (Select All)
C:\Windows\Fonts\cour.ttf

and to load it the statement is like:
Code: (Select All)
fonthandle = _LOADFONT("C:\Windows\Fonts\cour.ttf", 16, "MONOSPACE")

But the "MONOSPACE" as last parameter might not be necessary.

You have to use the filename of the font. Open File Explorer into "C:\Windows\Fonts".

Lucida Console is called "lucon.ttf" for a long time. A nice one I miss on Linux. I like the girl's name too LOL.
Reply
#8
(05-27-2023, 12:15 AM)mnrvovrfc Wrote: In think on Windows the font is called:
Code: (Select All)
C:\Windows\Fonts\cour.ttf

and to load it the statement is like:
Code: (Select All)
fonthandle = _LOADFONT("C:\Windows\Fonts\cour.ttf", 16, "MONOSPACE")

But the "MONOSPACE" as last parameter might not be necessary.

You have to use the filename of the font. Open File Explorer into "C:\Windows\Fonts".

Lucida Console is called "lucon.ttf" for a long time. A nice one I miss on Linux. I like the girl's name too LOL.

Thanks mnrvovrfc. That works, and I didn't need the "MONOSPACE"! I ended up using cour.ttf (courier). Good job!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#9
Here is a cute one from Ron77 I fixed up a little:
Code: (Select All)
_Title "Mod Ron77 Color Walker, arrows to move, c to cycle color change" 'b+ 2023-05-28
' kinda cute! nice one Ron77

' Notice I put the instructions to the app in the _Title

' We will be using the default Screen 0, Shout out to Pete! :)
' This uses 16 colors 0 to 15

' We will be using _KeyHit for user input because it's real easy to get
' the arrow key numbers in IDE. Just put cursor where you need the number
' Select Tools > Insert Quick Keycode (or shortcut Ctrl+K)
' then press the direction arrow key, BAM! there's your number!

' Notice at Locate under update screen comment, its y, x.
' That's because Locate does Row, Column not x, y like all graphics commands
' eg Pset (x,y), Color
' Row Column are how Print Locations work with Row (Vertical) listed first
' followed by the Column (Horizontal) position.
' That use to mix the hell up in me back with GW BASIC years ago.

DefLng A-Z ' all variables are long unless otherwise spec'd
x = 20
y = 20
c = 12
Color c ' Ron77 started the color at 0 and ha! Aurel is complaining nothing
'         is happening, nice joke on Aurel Ron :)
'
Do
    'update screen
    Locate y, x: Print "@";

    kh = _KeyHit ' see if user hit a key and if so which?
    If kh = 18432 Then ' arrow up
        If y - 1 > 0 Then y = y - 1 Else Beep
    End If
    If kh = 19200 Then ' arrow left
        If x - 1 > 0 Then x = x - 1 Else Beep
    End If
    If kh = 19712 Then ' arrow right
        If x + 1 <= _Width Then x = x + 1 Else Beep
    End If
    If kh = 20480 Then ' arrow down
        If y + 1 <= _Height Then y = y + 1 Else Beep
    End If

    'I did color changes different than Ron, I only updated color after color change
    'and I never set the color at 0 the black background, so maybe Aurel will enjoy
    'my version better ;-))

    If kh = 99 Then ' key c is for color change
        c = c + 1
        If c > 15 Then c = 1 ' if c exceeds 15 the last color at top range set it to 1, not 0
        Color c
        Locate y, x: Print "@"; ' update our @ guy immediately with color change so Aurel knows
    End If
Loop Until kh = 27 ' standard escape clause that johnno always insisted on

It's like a color Etch-A-Sketch:
   

BTW unlike Ron's if you try to go out of bounds of the screen, you are going to hear about it. Beep can be loud enough to make you jump out of your seat if you have volume up, so now consider yourself warned Smile
b = b + ...
Reply
#10
To me, it's smarter to change color when change direction so Mod 2:
Code: (Select All)
_Title "Mod 2 Ron77 Color Walker, arrows to move, change color when change direction" 'b+ 2023-05-28
' kinda cute! nice one Ron77

' Notice I put the instructions to the app in the _Title

' We will be using the default Screen 0, Shout out to Pete! :)
' This uses 16 colors 0 to 15

' We will be using _KeyHit for user input because it's real easy to get
' the arrow key numbers in IDE. Just put cursor where you need the number
' Select Tools > Insert Quick Keycode (or shortcut Ctrl+K)
' then press the direction arrow key, BAM! there's your number!

' Notice at Locate under update screen comment, its y, x.
' That's because Locate does Row, Column not x, y like all graphics commands
' eg Pset (x,y), Color
' Row Column are how Print Locations work with Row (Vertical) listed first
' followed by the Column (Horizontal) position.
' That use to mix the hell up in me back with GW BASIC years ago.

' Mod 2 Say let's change color only when we change direction.

DefLng A-Z ' all variables are long unless otherwise spec'd
x = 20
y = 20
c = 12
OldD = 1 ' OldD is 1 = North, 2 = East, 3 = South, 4 = West
'       pretend the last direction was North to get things started

Color c ' Ron77 started the color at 0 and ha! Aurel is complaining nothing
'         is happening, nice joke on Aurel Ron :)
'
Do
    'update screen
    Locate y, x: Print "@";

    kh = _KeyHit ' see if user hit a key and if so which?
    If kh = 18432 Then ' arrow up
        If y - 1 > 0 Then
            y = y - 1: d = 1: GoSub ChangeColor
        Else
            Beep
        End If
    End If
    If kh = 19200 Then ' arrow left
        If x - 1 > 0 Then
            x = x - 1: d = 4: GoSub ChangeColor
        Else
            Beep
        End If
    End If
    If kh = 19712 Then ' arrow right
        If x + 1 <= _Width Then
            x = x + 1: d = 2: GoSub ChangeColor
        Else
            Beep
        End If
    End If
    If kh = 20480 Then ' arrow down
        If y + 1 <= _Height Then
            y = y + 1: d = 3: GoSub ChangeColor
        Else
            Beep
        End If
    End If
Loop Until kh = 27 ' standard escape clause that johnno always insisted on
End

ChangeColor:
'  Mod 2 Say let's change color only when we change direction.    changeColor:
If d <> OldD Then
    c = c + 1
    If c > 15 Then c = 1 ' if c exceeds 15 the last color at top range set it to 1, not 0
    Color c
    Locate y, x: Print "@"; ' update our @ guy immediately with color change so Aurel knows
    OldD = d
End If
Return

   

Not much different from the way I was using the first Mod.
b = b + ...
Reply




Users browsing this thread: 2 Guest(s)