
-----------------------------------
staticlike
Mon Apr 11, 2005 12:09 pm

Networking question
-----------------------------------
I finished a basic pong game, and i would like to take it to the next level. I've decided to make a version that lets you connect up to another person's computer and play against them in pong. I started looking up commands for networking, and put them in place, but it doesn't seem to work. I did some basic network testing, and i tried out the example that is in the turing example. only the example worked. I took pieces out of the program and added them to the pong program, but still nothing.

guess i'm trying to say: why won't my program communicate with another computer?

the source code of what i have is here...
const chatPort : int := 5055
var choice : int
var paddley1, paddley2, ballx, bally : int
var left, right, up, down, up2, down2, ballin : boolean := false
setscreen ("graphics:651;551, nobuttonbar, offscreenonly")
var scorep1, scorep2 : int
var randomness : int


loop
    put "Enter 1 to run server"
    put "Enter 2 to connect to server"
    put "Choice: " ..
    View.Update
    get choice
    exit when choice = 1 or choice = 2
end loop


var netStream : int
var netAddress : string

if choice = 1 then
    put "This machine is '", Net.LocalName, "' at net addresss ", Net.LocalAddress
    put "Waiting for a connection... "
    View.Update
    netStream := Net.WaitForConnection (chatPort, netAddress)
else
    put "Enter the address to connect to: " ..
    get netAddress
    netStream := Net.OpenConnection (netAddress, chatPort)
    if netStream  maxy then
        paddley1 := maxy - 100
    end if
    if paddley2 + 100 > maxy then
        paddley2 := maxy - 100
    end if
    if paddley1 < 0 then
        paddley1 := 0
    end if
    if paddley2 < 0 then
        paddley2 := 0
    end if
    if ballx - 5 < 10 and bally >= paddley1 and bally = paddley1 + 26 and bally = paddley1 + 51 and bally = paddley1 + 76 and bally  maxx - 10 and bally >= paddley2 and bally  maxx - 10 and bally >= paddley2 + 26 and bally  maxx - 10 and bally >= paddley2 + 51 and bally  maxx - 10 and bally >= paddley2 + 76 and bally  maxy and up2 = true then
        up2 := false
        down2 := true
    end if
    if bally > maxy and up = true then
        up := false
        down := true
    end if
    if bally < 0 and down2 = true then
        up2 := true
        down2 := false
    end if
    if bally < 0 and down = true then
        up := true
        down := false
    end if
    if ballx < 0 then
        scorep2 := scorep2 + 1
        ballin := false
        startupball
    end if
    if ballx > maxx then
        scorep1 := scorep1 + 1
        ballin := false
        startupball
    end if

end boundaries

procedure ballmove
    if left = true then
        ballx := ballx - 2
    end if
    if right = true then
        ballx := ballx + 2
    end if
    if up = true then
        bally := bally + 2
    end if
    if down = true then
        bally := bally - 2
    end if
    if up2 = true then
        bally := bally + 4
    end if
    if down2 = true then
        bally := bally - 4
    end if
    if left = right then
        randint (randomness, 1, 2)
        if randomness = 1 then
            right := false
        else
            left := false
        end if
    end if
    if up = down then
        randint (randomness, 1, 2)
        if randomness = 1 then
            up := false
        else
            down := false
        end if
    end if

end ballmove




Text.Cls
var ch : char
View.Set ("noecho")
startupball
drawboard
loop
    % If there is a character waiting ..
    if hasch then
        % Read the character from the keyboard.
        ch := getchar
        if choice = 1 then
            if ch = 'w' then
                paddley1 := paddley1 + 2
            end if
            if ch = 's' then
                paddley1 := paddley1 - 2
            end if
        end if
        if choice = 2 then
            if ch = 'i' then
                paddley2 := paddley2 + 2
            end if
            if ch = 'k' then
                paddley2 := paddley2 - 2
            end if
        end if



        exit when Error.Last not= 0
    end if

    if Net.CharAvailable (netStream) then
        % Read the character from the net stream
        get : netStream, ch
        if choice = 2 then
            if ch = 'w' then
                paddley1 := paddley1 + 2
            end if
            if ch = 's' then
                paddley1 := paddley1 - 2
            end if
        end if
        if choice = 1 then
            if ch = 'i' then
                paddley2 := paddley2 + 2
            end if
            if ch = 'k' then
                paddley2 := paddley2 - 2
            end if
        end if

    end if
    ballmove
    drawboard
    boundaries
    delay (5)
    exit when scorep1 = 15
    exit when scorep2 = 15
    exit when ch = '\'
    exit when Error.Last not= 0
end loop
Net.CloseConnection (netStream)


any help is appreciated!

EDIT: Oh, almost forgot another question: how do you save and load information for a game?

-----------------------------------
StarGateSG-1
Wed Apr 13, 2005 10:25 pm


-----------------------------------
Lol, for start you ahve the chocie for server trying to connect to somewere, and the else opening the server, That is you first problem.

-----------------------------------
Martin
Thu Apr 14, 2005 6:03 am


-----------------------------------
I'll look at your network code after. It could be a firewall problem.

As for the save/load feature, all that you have to do is record all of the information that you need to know (location, score, etc.) into a file. To load a game, simply load that information back and set the appropriate variables.

-----------------------------------
Token
Thu Apr 14, 2005 6:14 am


-----------------------------------
And also you have to make it so that when the player moves it sends the message thru to the other computer to move, so with your code would look somthing like this

        if choice = 1 then 
            if ch = 'w' then 
                paddley1 := paddley1 + 2 
                put :netstream, "moveright"
            end if 

 and then you would need to make it so that when the other computer gets this message it adds 2 to remotepaddley  hope this helps, if not keep on askin questions.
