How to create an online game ? (total beginner...)

Just starting out? Need help? Post your questions and find answers here.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

How to create an online game ? (total beginner...)

Post by Joris »

Hi all,

How do you make an online game (like a card or chess program)?
I mainly mean how do you send / receive data between two (or more computers)?
(The program is ment to run on it's own, not in a browser.)
What is needed for making contact, what is needed for security?
How do you detect when data has been received / sent?
What and how to secure the connection ?


I am a beginner and quit unfamiliar with all the terminology (client server I understand a bit).
So...

Thank you for helping me out.
(or keep me away from this ;-).
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 341
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: How to create an online game ? (total beginner...)

Post by Mindphazer »

I guess you have to use SpiderBasic for that...
MacBook Pro 14" M1 Pro - 16 Gb - MacOS 14 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to create an online game ? (total beginner...)

Post by infratec »

You don't need SpiderBasich for this :wink:

I gave this as work for a trainee:

1. Programm '4 gewinnt' I think it is known as 'Connect 4' in foreign countries
2. Make it work between 2 PCs via network.

It was not a big deal, but not secured and not via internet.
Only a few commands were needed to send via network.
If I remember correct, we used SendNetworkString() to keep it simple.

So it depends on your game.

(I will search for the code, if it is not deleted I will publish it)
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to create an online game ? (total beginner...)

Post by infratec »

Found it.

But it sends the 'whole' board via UDP.
The code is not from me, it is from a former trainee.
And it was only to show how it works.

Code: Select all

EnableExplicit


Enumeration
    #playground
EndEnumeration

Enumeration #PB_Event_FirstCustomValue
    #Network_DataReceived
EndEnumeration


Structure ThreadParameter
    Thread.i
    Exit.i
    Port.i
    Semaphore.i
    Active.i
    *RxDBuffer
EndStructure


Procedure.i dropStone(xstart,playerNumber,playground,Start)
    
    Static y
    Protected x, Result, playerColor 
    
    
    If playerNumber = 0
        playerColor = #Yellow
    Else
        playerColor = #Red
    EndIf
    
    
    If StartDrawing(CanvasOutput(#playground))
        
        x = ((xstart - 50) / 100 + 1) * 100
        If Start
            y = 100
            If Point(xstart, y) <> #White
                Result = 2
            EndIf
        EndIf
        
        If Not Result
            
            If Point(x, y) = #White
                FillArea(x, y, -1, playerColor)
                
                If Y > 100
                    FillArea(x, y - 100, -1, #White)
                EndIf
                
                y + 100
                
                If y = 700 Or Point(x, y) <> #White
                    Result = 1
                EndIf
                
            EndIf
            
        EndIf
        
        StopDrawing()              
        
    EndIf
    
    ;Debug "Drop Y: " + Str(y)
    
    ProcedureReturn Result
    
EndProcedure


Procedure checkForWin(Array PlayGroundArr.a(2))
    
    Protected Result, x, y, counter, countingColor = #White, stoneColor = #White, i
    
    
    If StartDrawing(CanvasOutput(#playground))
        ; check lines
        For y = 1 To 6
            For x = 1 To 7
                stoneColor = Point(100 * x, 100 * y) 
                If stoneColor = #Red Or stoneColor = #Yellow
                    If countingColor = stoneColor
                        counter + 1                
                    Else
                        countingColor = stoneColor
                        counter = 1
                    EndIf                
                EndIf  
                
                If counter = 4
                    For i = x - 3 To x
                        FillArea(i * 100, y * 100, -1, #Green)
                    Next i
                    
                    Result = #True
                    Break 2
                EndIf
                
            Next x
        Next y
                
        If Not Result
            counter = 0
            
            ; check columnes
            For x = 1 To 7
                For y = 1 To 6
                    stoneColor = Point(100 * x, 100 * y) 
                    If stoneColor = #Red Or stoneColor = #Yellow
                        If countingColor = stoneColor
                            counter + 1                
                        Else
                            countingColor = stoneColor
                            counter = 1
                        EndIf                
                    EndIf  
                    
                    If counter = 4
                        
                        For i = y - 3 To y
                            FillArea(x * 100, i * 100, -1, #Green)
                        Next i
                        
                        Result = #True
                        Break 2
                    EndIf
                    
                Next y
            Next x
            
        EndIf
        ; checkDiagonal
        ;     For y = 1 To 7
        ;         For x = 1 To 6
        ;             If Point(100 * x, 100 * y) = #White
        ;                 
        ;             EndIf            
        ;         Next x
        ;     Next y    
        
        
        
        For y = 1 To 6
            For x = 1 To 7
                stoneColor = Point(100 * x, 100 * y) 
                Select stoneColor
                    Case #White : stoneColor = 0
                    Case #Red : stoneColor = 1
                    Case #Yellow : stoneColor = 2
                    Case #Green : stoneColor = 3
                EndSelect
                PlayGroundArr(x - 1, y - 1) = stoneColor
            Next x
        Next y
        
        
        StopDrawing()
        
    EndIf
    
    ProcedureReturn Result
    
EndProcedure


Procedure UDPServer(*Parameter.ThreadParameter)
    
    Protected RxDLen, Client
    
    
    If CreateNetworkServer(0, *Parameter\Port, #PB_Network_UDP)
        
        *Parameter\RxDBuffer = AllocateMemory(100)
        If *Parameter\RxDBuffer
            
            Repeat
                
                Select NetworkServerEvent()
                    Case #PB_NetworkEvent_None
                        Delay(10)
                        
                    Case #PB_NetworkEvent_Data
                        Client = EventClient()
                        RxDLen = ReceiveNetworkData(Client, *Parameter\RxDBuffer, MemorySize(*Parameter\RxDBuffer))
                        If RxDLen = 42
                            ;MessageRequester("Info", "bin da")
                            PostEvent(#Network_DataReceived)
                            WaitSemaphore(*Parameter\Semaphore)
                        EndIf
                        
                EndSelect
                
            Until *Parameter\Exit
            
            FreeMemory(*Parameter\RxDBuffer)
            
        EndIf
        
    EndIf
    
EndProcedure



;Main

Define playerOneTurn, Timer, DropActive, DropStart, DropResult, Index, i, Byte, con
Define exit, event, Col, x, y
Define PartnerIP$
Define Parameter.ThreadParameter
Dim PlayGroundArr.a(6, 5)

InitNetwork()

PartnerIP$ = InputRequester("Partner IP", "Bitte geben sie die IP Adresse des Partners ein.", "")


Parameter\Port = 4444
Parameter\Semaphore = CreateSemaphore()
Parameter\Thread = CreateThread(@UDPServer(), @Parameter)



OpenWindow(0,0,0,820,720,"Vier Gewinnt", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

CanvasGadget(#playground, 10, 10, 800, 700)


If StartDrawing(CanvasOutput(#playground))
    Box(0, 0, 800, 700, $FF0000)
    For x = 1 To 7
        For y = 1 To 6
            Circle(100 * x, 100 * y, 40, #White)
        Next y
    Next x
    StopDrawing()
EndIf





Repeat
    event = WaitWindowEvent()
    
    Select Event
        Case #Network_DataReceived
            CopyMemory(Parameter\RxDBuffer, @PlayGroundArr(0, 0), 42)
            If StartDrawing(CanvasOutput(#playground))
                For y = 1 To 6
                    For x = 1 To 7
                        Byte = PlayGroundArr(x - 1, y - 1)
                        ;Debug Str(x) + "/" + Str(y) + ": " + Str(byte)
                        Select Byte
                            Case 0 : FillArea(100 * x, 100 * y, -1, #White)
                            Case 1 : FillArea(100 * x, 100 * y, -1, #Red)
                            Case 2 : FillArea(100 * x, 100 * y, -1, #Yellow)
                            Case 3 : FillArea(100 * x, 100 * y, -1, #Green)
                        EndSelect
                    Next x
                Next y
                StopDrawing()
            EndIf
            Parameter\Active = #True
            DropActive = #False
           
            SignalSemaphore(Parameter\Semaphore)
            
        Case #PB_Event_Timer
            If EventTimer() = 1
                
                If PartnerIP$ <> "" And Not Parameter\Active And DropStart
                    playerOneTurn = ~playerOneTurn
                    Parameter\Active = #True
                EndIf
                
                Debug playerOneTurn
                
                DropResult = dropStone(x,playerOneTurn,#playground,DropStart)
                If DropResult > 0
                    RemoveWindowTimer(0, 1)
                    DropActive = #False
                    
                    If DropResult = 1
                        
                        If checkForWin(PlayGroundArr())
                            ;MessageRequester("Gewonnen", "Gewonnen!")
                        EndIf
                        
                        If PartnerIP$ <> ""
                            con = OpenNetworkConnection(PartnerIP$, 4444, #PB_Network_UDP)
                            If con
                                SendNetworkData(Con, @PlayGroundArr(0, 0), 42)
                                CloseNetworkConnection(con)
                            EndIf
                        EndIf
                        
                        If Parameter\Active
                            DropActive = #True
                        Else
                            playerOneTurn = ~playerOneTurn
                            DropActive = #False
                        EndIf
                    EndIf
                    
                Else
                    DropActive = #True
                EndIf
                
                DropStart = #False
                
                
            EndIf
            
        Case #PB_Event_Gadget
            Select EventGadget()
                Case #playground
                    If EventType() = #PB_EventType_LeftClick
                        If Not DropActive
                            x = GetGadgetAttribute(#playground, #PB_Canvas_MouseX)
                            DropStart = #True
                            AddWindowTimer(0, 1, 100)
                        EndIf      
                    EndIf
            EndSelect
            
        Case #PB_Event_CloseWindow
            exit = #True
            
    EndSelect
    
Until exit


If IsThread(Parameter\Thread)
    Parameter\Exit = #True
    If WaitThread(Parameter\Thread, 3000) = 0
        KillThread(Parameter\Thread)
    EndIf
EndIf

FreeSemaphore(Parameter\Semaphore)
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: How to create an online game ? (total beginner...)

Post by Joris »

Ok infratec thanks.

Small question (I hope/think) :
To test such online stuff, can I do this at home over my DLan setup ?
If so, what is needed exactly to get that started ?
I have a XP, iMac and Ubuntu computer all on the DLan...
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: How to create an online game ? (total beginner...)

Post by Joris »

Joris wrote:Ok infratec thanks.

Small question (I hope/think) :
To test such online stuff, can I do this at home over my DLan setup ?
If so, what is needed exactly to get that started ?
I have a XP, iMac and Ubuntu computer all on the DLan...
I mean : can I set it up, so I have a 'client' and a 'server' on my DLan (all in one place my home) ?
How do you test this otherwhise (needing a 'client' and a 'server' as computer) ?
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to create an online game ? (total beginner...)

Post by infratec »

This was only a small example that it is possible without SpiderBasic :wink:

It has nothing todo with a game server.

This example connects directly 2 PCs.
Best for testing is to use 2 PCs in your LAN.

If you want to create a multiclient server game, you need:
1. good knowledge of networks and network protocolls.
2. a well planned data exchange.
3. good knowledge of PB threaded networkserver stuff.
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: How to create an online game ? (total beginner...)

Post by Bitblazer »

Joris wrote:
Joris wrote:How do you test this otherwhise (needing a 'client' and a 'server' as computer) ?
You could just develop a regular client software where you give one client the ability to act as "game server". Based on the network client/server examples Here. Extend them so the server handles the state data of your game (for example the number, position and value of items or cards) and the client just sends a command to the server like "i insert a coin in slot 13" or "i will take another card and raise my stake to 5$".
That way you could develop the software on one machine and in one version. Once you have that working, you could use a virtualbox machine and see if it works to play with the software on the host and the client. Or you go and use two computers right away ;)

If you do it right, you can even do network games without using any network commands - by using a network drive to exchange data with ordinary files. Or you learn the network commands and implement a client/server protocol including automatic LAN detection. Networks are a fundamental technology and quite fun.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: How to create an online game ? (total beginner...)

Post by Joris »

I suppose I have a wrong idea about what is client/server use. At least I expressed myself a bit limited (as my knowledge about this, is like that).
I just wont to be able to send/recieve (a small block) data between two computers, in both directions.
To me the one who is sending is the server the other then the client (and vice versa).
In fact in my case they both don't have to do anything more.

Thanks infratec and Bitblazer (clear explanation and very close to my intended use).

I'm gonna try to get the basics send/recieve setup between my iMac and XP computer over the DLan.
If that works I'll should have been started.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: How to create an online game ? (total beginner...)

Post by Bitblazer »

Joris wrote:To me the one who is sending is the server the other then the client (and vice versa).
In fact in my case they both don't have to do anything more.
Hmmm now consider a ftp client and a ftp server. Imagine a user who uploads the file 'A' to the server and downloads the file 'B' :)

No matter if client, server or a mix (p2p) - they all use a simple method to send some data and another to receive some data. The concrete use and combination of send and receive is just a convention, the 'protocol'.
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to create an online game ? (total beginner...)

Post by infratec »

So you did not tried my example code :?:
This sends data in both directions.

You can eliminate the need for enter the IP address, if you send broadcasts before.
But that's not possible with PB, you need API stuff for this.
Or you have to send something to all IP addresses of the subnet and wait fr an answer.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: How to create an online game ? (total beginner...)

Post by Joris »

Bitblazer thanks.

Yet, you do remind me to an earlier idea I wonted to get programmed.
Using my webspace for : uploads the file 'A' to the server and downloads the file 'B'.
I use Filezilla for this, but it would be very uselfull too, to have this functionality in some of my programs.
But for that, I need at least the necessary security knowledge... (not for now unless you think it's not that dificult)

[EDIT]infratec, in your code I saw IP adresses are needed. Now just went looking how to get those IP adresses. I thought they never changed, as I wrote one down earlier. Yeah you see... not used to this stuff, yet.
P.s. Besides I'm still busy with writing the manual for a free PB-tool I'll release here one of these. [/EDIT]i
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: How to create an online game ? (total beginner...)

Post by Joris »

infratec thanks.
I played that game here for the first time (my planned vacation is suddenly canceled because of covid, so... yeah realy sad).
The game works fine over my DLan. Now I do understand a bit more about what I need to do.
Getting my own IP adress was quit easy. But now, transfering IP-adresses from one pc to the other is at least the next step. As I begin to understand, this is commonly done by use of a webspace (probably the so called server-part). Yet, using my webspace as place to register and transfer other adresses, that will become a more difficult part to explore ?
A lot to study and figuring out.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Post Reply