Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Networking question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
staticlike




PostPosted: Mon Apr 11, 2005 12:09 pm   Post subject: 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...
code:
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 <= 0 then
        put "Unable to connect to ", netAddress
        return
    end if
end if

procedure drawboard
    cls
    locate (1, 1)
    put "                       ", scorep1, "                             ", scorep2
    drawfillbox (324, 0, 326, maxy, white)
    drawfillbox (10, paddley1, 15, paddley1 + 100, white)
    drawfillbox (maxx - 10, paddley2, maxx - 15, paddley2 + 100, white)
    drawfilloval (ballx, bally, 5, 5, white)
    View.Update
end drawboard

procedure balldirection
    randint (randomness, 1, 4)
    if randomness = 1 then
        up := true
        up2 := false
        left := true
        down := false
        down2 := false
        right := false
    end if
    if randomness = 2 then
        up := true
        up2 := false
        left := false
        down := false
        down2 := false
        right := true
    end if
    if randomness = 3 then
        up := false
        up2 := false
        down2 := false
        left := true
        down := true
        right := false
    end if
    if randomness = 4 then
        up := false
        up2 := false
        down2 := false
        left := false
        down := true
        left := true
    end if
end balldirection

procedure startupball
    if ballin = false then
        balldirection
        ballx := 325
        randint (bally, 5, maxy - 5)
        ballin := true
    end if
end startupball


procedure boundaries
    if paddley1 + 100 > 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 + 25 then
        left := false
        up2 := false
        down2 := true
        right := true
        down := false
        up := false
    end if
    if ballx - 5 < 10 and bally >= paddley1 + 26 and bally <= paddley1 + 50 then
        left := false
        up2 := false
        down2 := false
        right := true
        down := true
        up := false
    end if
    if ballx - 5 < 10 and bally >= paddley1 + 51 and bally <= paddley1 + 75 then
        left := false
        up2 := false
        down2 := false
        right := true
        down := false
        up := true
    end if
    if ballx - 5 < 10 and bally >= paddley1 + 76 and bally <= paddley1 + 100 then
        left := false
        up2 := true
        down2 := false
        right := true
        down := false
        up := false
    end if
    if ballx + 5 > maxx - 10 and bally >= paddley2 and bally <= paddley2 + 25 then
        left := true
        right := false
        up2 := false
        down2 := true
        down := false
        up := false
    end if
    if ballx + 5 > maxx - 10 and bally >= paddley2 + 26 and bally <= paddley2 + 50 then
        left := true
        right := false
        up2 := false
        down2 := false
        down := true
        up := false
    end if
    if ballx + 5 > maxx - 10 and bally >= paddley2 + 51 and bally <= paddley2 + 75 then
        left := true
        right := false
        up2 := false
        down2 := false
        down := false
        up := true
    end if
    if ballx + 5 > maxx - 10 and bally >= paddley2 + 76 and bally <= paddley2 + 100 then
        left := true
        right := false
        up2 := true
        down2 := false
        down := false
        up := false
    end if
    if 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?
Sponsor
Sponsor
Sponsor
sponsor
StarGateSG-1




PostPosted: Wed Apr 13, 2005 10:25 pm   Post subject: (No subject)

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




PostPosted: Thu Apr 14, 2005 6:03 am   Post subject: (No subject)

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




PostPosted: Thu Apr 14, 2005 6:14 am   Post subject: (No subject)

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
code:

        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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: