I started a blog to learn FreeBASIC through videogames

New to FreeBASIC? Post your questions here.
redroosterbcn
Posts: 1
Joined: Dec 16, 2019 10:18

I started a blog to learn FreeBASIC through videogames

Post by redroosterbcn »

Hi everybody!

Yesterday I started a blog to learn FreeBASIC through videogames.

In this blog we can learn to program in FreeBASIC through videogames, step by step, from zero. Come on and join the goal!

https://learningfreebasic.blogspot.com

I hope you like it, See you!
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: I started a blog to learn FreeBASIC through videogames

Post by badidea »

Nice, but blogs often die early. I tried it myself once, after 2 weeks I forgot about it. I hope your effort will last longer.
Anyway, for inspiration, there is also: http://games.freebasic.net/basicgaming.php
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Re: I started a blog to learn FreeBASIC through videogames

Post by thebigh »

Sorry to necro this, but that ezine is inspiring. A while ago I was teaching myself openGL in the hope of making a 3D game of some sort, but came to a crashing halt when I just couldn't get my collision detection routines to work.

Now I'm going to dive back in, find that nasty little bug, and make my game!
BasicCoder2
Posts: 3908
Joined: Jan 01, 2009 7:03
Location: Australia

Re: I started a blog to learn FreeBASIC through videogames

Post by BasicCoder2 »

Doing a blog of your OpenGL learning journey to share the issues and solutions you find along the way may be a useful resource for others. There is always the issue that people have different starting points. Learning a programming language is not the same as learning how to use the language to obtain some outcome such as writing a computer game although you might combine the two for a beginner. I bought books on writing c++ computer games back in my MSDOS days not so much to enable me to write a game but because they provided tutorials on how to use graphics and sound in a program. The last book I bought was "Tricks of the WINDOWS Game programming gurus" 2002 by André LaMothe. An interesting read and useful resource.
https://en.wikipedia.org/wiki/Andr%C3%A9_LaMothe
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Re: I started a blog to learn FreeBASIC through videogames

Post by thebigh »

I probably won't write a blog, but if I ever do get this *%#§)% collision algorithm working, I might do a writeup of it here on the forum.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: I started a blog to learn FreeBASIC through videogames

Post by badidea »

thebigh wrote:*%#§)%
Yes, coding is 90% frustration and 10% satisfaction. Still we do it.
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Re: I started a blog to learn FreeBASIC through videogames

Post by thebigh »

badidea wrote:
thebigh wrote:*%#§)%
90% frustration
Like ripping up and rewriting the algorithm three or four times, certain that the reason I was skidding up walls and falling through the floor was an arcane mathematical subtlety... and then going back to my basic linear algebra functions to find I've done this stupid stupid stupid stupid stupid thing:

Code: Select all

function dotprod( v1 as point3d, v2 as point3d ) as single
    return v1.x*v2.x + v1.y+v2.y + v1.z*v2.z
end function
stupid stupid stupid stupid
paul doe
Moderator
Posts: 1735
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: I started a blog to learn FreeBASIC through videogames

Post by paul doe »

Looks like a perfectly fine dot product to me... =/

EDIT: No, I just saw the '+' in there. For what is worth, that's why I would write it like this:

Code: Select all

function dotprod( v1 as point3d, v2 as point3d ) as single
  return( v1.x * v2.x + v1.y + v2.y + v1.z * v2.z )
end function
It helps with readability (now that plus out of place stands out like a sore thumb). My eyesight isn't what it used to be *sigh*
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: I started a blog to learn FreeBASIC through videogames

Post by Tourist Trap »

redroosterbcn wrote: I hope you like it, See you!
Yes, it's a nice start. I hope you will make this all grow up well and big. Good work so far.
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Re: I started a blog to learn FreeBASIC through videogames

Post by thebigh »

My style has always been to put spaces around +, -, and = but no spaces around multiplication and division. I personally find that easier to read, but of course nothing much can help against typos.
Ghegs
Posts: 6
Joined: Apr 18, 2020 17:03

Re: I started a blog to learn FreeBASIC through videogames

Post by Ghegs »

Even though the blog died I wanted to say thanks for it, it worked as a starting point to FreeBasic for me.

I developed the example into a small minigame of my own, it gave me a motivation to figure out things that weren't covered in the blog's guide, though they probably would have if the blog had continued.

Code: Select all

'Meteor Dodge
'
'2020-04 by Ghegs

Const as Integer ScreenWidth=480, ScreenHeight=640
Const as Integer sc_esc=1, sc_up=72,sc_right=77,sc_down=80,sc_left=75, sc_shift=42
Const as Integer JoystickSlowButton = 1
Const as Integer PointsForMeteorPass = 10, PointsForSkinDancing = 100, SkinDanceDistance = -10

Dim Shared as Single DPadX, DPadY
Dim Shared as Integer JoystickID, JoystickButtons, JoystickResult

Type Cube
    x as Integer 'x position on screen
    y as Integer 'y position on screen
    c as Integer 'color
    size as Integer
    speed as Integer
    Declare sub Draw
    Declare Sub Sparkle
End Type

Sub Cube.Draw
    Line (x,y)-Step(size,size),c,bf
End Sub

Sub Cube.Sparkle
    Line (x-7,y-7)-Step(2,2),c 'Upper left
    Line (x-7,y+size/2)-Step(2,0),c 'Middle left
    Line (x-7,y+size+5)-Step(2,-2),c 'Lower left
    Line (x+size+5,y-5)-Step(2,-2),c 'Upper right
    Line (x+size+5,y+size/2)-Step(2,0),c 'Middle right
    Line (x+size+5,y+size+3)-Step(2,2),c 'Lower right
End Sub

Declare Function CollisionCheck(Player as Cube, Enemy as Cube) As Boolean
Declare Function InitializeMeteor() as Cube
Declare Function FindJoystick() as Integer 
Declare Sub AddToArray (Array() as Cube, NewEntry as Cube)
Declare Sub Title(Score as Integer = 0)
Declare Sub GameLoop()
Declare Sub GameOver(Score as Integer)

Function RNG (first As Double, last As Double) As Double
    Randomize
    Return Int (Rnd * (last - first) + first)
End Function

Function DistanceCheck (Player as Cube, Enemy as Cube) As Integer
    Dim Result as Integer
    Dim as Integer XDistance, XDistanceLeft, XDistanceRight, YDistance, YDistanceUp, YDistanceDown
    
    XDistanceLeft = Player.x + Player.size - Enemy.x
    XDistanceRight = Enemy.x + Enemy.Size - Player.x
    YDistanceUp = Player.y + Player.size - Enemy.y
    YDistanceDown = Enemy.y + Enemy.Size - Player.y
    
    If XDistanceLeft <= XDistanceRight Then XDistance = XDistanceLeft Else XDistance = XDistanceRight
    If YDistanceUp <= YDistanceDown Then YDistance = YDistanceUp Else YDistance = YDistanceDown
    If XDistance <= YDistance Then Result = XDistance Else Result = YDistance
    
    Return Result
End Function

Function InitializeMeteor() As Cube
    Dim as Integer MinSize = 5, MaxSize = 30
    Dim as Integer MinSpeed = 1, MaxSpeed = 10
    Dim NewMeteor As Cube
    NewMeteor.size = RNG(MinSize,MaxSize)
    NewMeteor.x = RNG(0,ScreenWidth-NewMeteor.size)
    NewMeteor.y = -1 - NewMeteor.size
    NewMeteor.c = RNG(1,15)
    NewMeteor.speed = RNG(MinSpeed,MaxSpeed)
    Return NewMeteor
End Function

Sub AddToArray (Array() as Cube, NewEntry as Cube)
    ReDim Preserve Array(UBound(Array)+1)
    Array(UBound(Array)) = NewEntry
End Sub

Function FindJoystick() as Integer
    Dim ID as Integer
    For ID = 0 to 15
        If GetJoystick(ID) = 0 Then Return ID
    Next ID
End Function
JoystickID = FindJoystick()

Screenres ScreenWidth, ScreenHeight, 8

Sub Title(Score as Integer = 0)
    Static as Integer HighScore = 0
    Dim as Integer LogoX = ScreenWidth/3
    Dim as Integer LogoY = ScreenHeight/4
    
    If Score > HighScore Then HighScore = Score
    
    Do
        GetJoystick(JoystickID, JoystickButtons, , , , , , , DPadX, DPadY)
        
        If DPadX = -1 And JoystickButtons = JoystickSlowButton Then LogoX -= 1
        If DPadX = 1 And JoystickButtons = JoystickSlowButton Then LogoX += 1  
        If DPadY = -1 And JoystickButtons = JoystickSlowButton Then LogoY -= 1
        If DPadY = 1 And JoystickButtons = JoystickSlowButton Then LogoY += 1
        
        Screensync
        Screenlock
        CLS
        Draw string (ScreenWidth/3,20),"High score: " + Str(HighScore)
        Draw string (LogoX,LogoY),"Meteor Dodge"
        
        Draw string (ScreenWidth/4,ScreenHeight/1.5),"Arrows/D-Pad to move"
        Draw string Step (0,20),"Hold Shift/Button 1 to slow down"
        Draw string Step (0,20),"Press Shift/Button 1 to start or ESC to exit"
        Draw string (ScreenWidth/4,ScreenHeight-20),"2020-04 by Ghegs"
        Screenunlock
        
        Sleep 1,1
    
    Loop Until Multikey(sc_esc) = -1 Or Multikey(sc_shift) = -1 Or JoystickButtons = JoystickSlowButton
    If Multikey(sc_esc)=-1 Then 
        End
    ElseIf Multikey(sc_shift) = -1 Or JoystickButtons = JoystickSlowButton Then 
        GameLoop()
    Else
        End
    End If
    
End Sub

Sub GameLoop()
    
    Dim as string DebugMessage
    Dim as Integer GameTimer = 0, Score = 0, PlayerNormalSpeed = 5, PlayerSlowSpeed = 2, MaxEnemiesOnScreen = 40, EnemyDistanceToPlayer = 0
    Dim as boolean Finish = False, Debug = False
    Dim Player as Cube
    Dim Meteor as Cube
    Dim EnemyArray() as Cube
    
    Player.size = 16
    Player.x = ScreenWidth/2
    Player.y = ScreenHeight - Player.size - 10
    Player.c = 15
    Player.speed = PlayerNormalSpeed
    
    Do
        'INPUT
        GetJoystick(JoystickID, JoystickButtons, , , , , , , DPadX, DPadY)
        If multikey(sc_esc)=-1 Then End
        If multikey(sc_shift)=-1 Or JoystickButtons = JoystickSlowButton Then Player.Speed = PlayerSlowSpeed Else Player.Speed = PlayerNormalSpeed
        If (multikey(sc_up)=-1 Or DPadY = -1) And Player.y > 0 Then Player.y -= Player.speed
        If (multikey(sc_right)=-1 Or DPadX = 1) And Player.x < ScreenWidth - Player.size Then Player.x += Player.speed
        If (multikey(sc_down)=-1 Or DPadY = 1) And Player.y < ScreenHeight - Player.size Then Player.y += Player.speed
        If (multikey(sc_left)=-1 Or DPadX = -1) And Player.x > 0 Then Player.x -= Player.speed
        
        'UPDATE
        GameTimer += 1
        DebugMessage = ""
             
        'Update meteor locations, check if a meteor goes outside the screen, and check for collisions
        For Enemy As Integer = LBound(EnemyArray) To UBound(EnemyArray)
            'Location on screen
            EnemyArray(Enemy).y += EnemyArray(Enemy).speed
            
            'Respawn the meteor if it goes outside the screen and increase player's score
            If EnemyArray(Enemy).y > ScreenHeight Then 
                Score += PointsForMeteorPass
                EnemyArray(Enemy) = InitializeMeteor
            End If
            
            'Check the distance between player and the meteor
            EnemyDistanceToPlayer = DistanceCheck (Player, EnemyArray(Enemy))
            If EnemyDistanceToPlayer > 0 Then
                DebugMessage = "Collision!"
                Finish = True
            ElseIf EnemyDistanceToPlayer <= 0 and EnemyDistanceToPlayer >= SkinDanceDistance Then
                Score += PointsForSkinDancing
                Player.Sparkle
                DebugMessage = "Skindance!"
            End If
            
        Next Enemy
        
        'Create a new enemy metor
        If Frac(GameTimer/10) = 0 And UBound(EnemyArray) < MaxEnemiesOnScreen Then AddToArray(EnemyArray(), InitializeMeteor)
        
        'Allow the creation of a new enemy meteor every second
        If Frac(GameTimer/60) = 0 Then
            MaxEnemiesOnScreen += 1
        End If
        
        'DRAW
        Screensync
        Screenlock
        CLS
        
        'Draw enemy meteors
        For Enemy As Integer = LBound(EnemyArray) To UBound(EnemyArray)
            EnemyArray(Enemy).Draw
        Next Enemy
    
        'Draw player
        Player.Draw
        
        'Draw score
        Draw string (5,5),"Score: " + Str(Score)
        
        'Draw debug information
        If Debug = True Then
            Dim as Integer DebugLineCounter = 0
            Draw string (ScreenWidth-90,DebugLineCounter),"Timer: " + Str(GameTimer):DebugLineCounter += 10
            Draw string (ScreenWidth-90,DebugLineCounter),DebugMessage:DebugLineCounter += 10
            Draw string (ScreenWidth-90,DebugLineCounter),"x: " + Str(Player.x):DebugLineCounter += 10
            Draw string (ScreenWidth-90,DebugLineCounter),"y: " + Str(Player.y):DebugLineCounter += 10
            Draw string (ScreenWidth-90,DebugLineCounter),"Enemies: " + Str(UBound(EnemyArray)):DebugLineCounter += 10
            For Enemy As Integer = LBound(EnemyArray) To UBound(EnemyArray)
                Draw string (ScreenWidth-90,DebugLineCounter)," " + Str(EnemyArray(Enemy).x) + " - " + Str(EnemyArray(Enemy).y):DebugLineCounter += 10
            Next Enemy
        End If
        
        Screenunlock
        Sleep 1,1

    Loop Until Finish = True
    GameOver(Score)
    
End Sub

Sub GameOver(Score as Integer)

    Sleep 1500,1
    
    Screensync
    Screenlock
    CLS
    
    Draw string (ScreenWidth/2.9,ScreenHeight/2/2),"Game Over!"
    Draw string Step(0,240),"Final Score: " + Str(Score)
    
    Screenunlock
    GetKey
    Sleep 5000
    Title (Score)
    
End Sub

Title()

End
BasicCoder2
Posts: 3908
Joined: Jan 01, 2009 7:03
Location: Australia

Re: I started a blog to learn FreeBASIC through videogames

Post by BasicCoder2 »

A nice reflex game. Unfortunately I can't concentrate for long as my thoughts wander to other things to think about. I was hooked on Tetris for a couple of weeks because it was a thinking game not a simple reflex game.

Maybe a Rectangle type not a Cube type or even call it a Sprite type for when you use images?
http://virtualink.wikidot.com/fbtut:fboop
Ghegs
Posts: 6
Joined: Apr 18, 2020 17:03

Re: I started a blog to learn FreeBASIC through videogames

Post by Ghegs »

Oh, for sure. I'd also want to make a proper configuration menu for controllers to assure any controller works with it. And saving that configuration to an external file, along with the high score. But that's for the next project, this was just to get my feet wet.

There is a little bit more to it than just reflex-dodging, though. You get 10 points for every cube/meteor/rectangle that passes the screen, but for every frame you're in close proximity to one (less than 10 pixels) you get a 100 points. And since more and more will appear, it becomes a race to gather as much points as possible before being overwhelmed, and you'll need to play in a risky way for that. So it's a (very) short-loop score attack game, shouldn't take more than 30 seconds to complete at the most.
paul doe
Moderator
Posts: 1735
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: I started a blog to learn FreeBASIC through videogames

Post by paul doe »

Ghegs wrote:Even though the blog died I wanted to say thanks for it, it worked as a starting point to FreeBasic for me.
...
Well, it has served its purpose, then. My highest score so far is 86860.
BasicCoder2
Posts: 3908
Joined: Jan 01, 2009 7:03
Location: Australia

Re: I started a blog to learn FreeBASIC through videogames

Post by BasicCoder2 »

I had enough trouble trying not to get hit let alone worrying about how close to get 100 points! My best was 3960.
Now you have got your feet wet it will be interesting to see what you come up with next.
Your code seems too good for the Beginners section you could probably post to the Game Dev section?
Post Reply