TGC Codebase Backup



Two by Trek

6th Apr 2005 21:21
Summary

How to transmit a variable telling another computer what arrowkeys your pressing and input a variable to see what arrowkeys their pressing.



Description

This is a code snippet I wrote for the most basic transmission needed for most 3D games. In almost any game, the programmer needs to somehow let the computer know what the gamer wants to do. This is usually done by checking the status of the upkey(), downkey(), rightkey(), and leftkey() variables. However, when you want to play two player you only have one set of arrow keys. An easy way to solve this is to make a network game where someone else can play from another computer.

In this program, I demonstrate how two variables can be used to tell the corresponding computer what arrow keys are being pressed on the given system. If the variable is set to 0 nothing is being pressed. If the variable has a 1 in the tens place the upkey is being pressed and if it has a 2 in the tens place the downkey is being pressed. If a 1 is in the ones place a left arrow key is being pressed and if a 2 is in the ones place the right arrow key is being pressed. These can be combined to make various combinations such as the following:

12 = Up Arrow and Right Arrow keys are pressed

20 = Down arrow is being pressed

1 = Left arrow key is being pressed

I am using this concept right now in a fully 3D networked game called Ice Hockey II which is due to be released later this summer. To see a sneak peak and screenshot of it, check out my software website at http://roborangers.home.comcast.net/Trek.html and head over to the blog section.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    remstart
.........................................................
.			 Two-Way Network Transmission (TNT) 			  .
.						 	     By						           .
.			     	      Trek Software				           .
.    http://roborangers.home.comcast.net/Trek.html      .
.........................................................
remend

`Setup screen and show loading text

hide mouse
sync on : sync rate 50

set text font "Lucida Console"
set text size 10

Text 240,240,"Initializing Variables..."

wait 10
sync
wait 10

lightblue=rgb(0,50,255)
ink lightblue,0
set text size 12

` Find the TCP/IP connection number

TcpIpNumber = FindTCPIPConnectionNumber()

CLS

` Ask for the players name

Get_Name:

text 50,20,"Welcome!! This is a two-way network variable input and transmission program."
sync
wait 500

MyName$=""
letter=0

while returnkey()=0
	if inkey$()<>"" and letter<10 then MyName$=MyName$+inkey$() : letter=letter+1 : wait 100
	text 230,50,"Please Enter Your Name - " + MyName$
	sync
endwhile

IF MyName$ = ""
	 red=rgb(255,0,0)
	 ink red,0
    text 240,200,"You need to enter a name!!"
    WAIT 1000
	 ink lightblue,0
	 CLS
    Goto Get_Name
ENDIF

` Show Host/Client options.

CLS

green=rgb(0,255,0)
ink green,0

text 240,180, "[1] I'm the Host"
text 240,230, "[2] I'm the Client"

sync

A$ = ""
Answer = 0

WHILE Answer = 0
	 sync
    A$ = INKEY$()
    IF A$ = "1" THEN Answer = 1
    IF A$ = "2" THEN Answer = 2
ENDWHILE

wait 200

` If "host" is selected...

CLS

white=rgb(255,255,255)
ink white,0

IF Answer = 1
    text 230,240,"Creating net session..."
	 wait 100
	 sync
    SET NET CONNECTION TcpIpNumber," "
    CREATE NET GAME "Sample Net session",MyName$,16,1
ENDIF

` If "client" is selected...

CLS

IF Answer = 2
	 green=rgb(0,255,0)
	 ink green,0
    Input "Please enter the Hosts IP Address or press enter to search for host: ",AddressData$
    text 170,140,"Connecting to net session...  Please wait..."
	 sync
	 wait 100
    SET NET CONNECTION TcpIpNumber, AddressData$
    PERFORM CHECKLIST FOR NET SESSIONS
    NumberOfGames =CHECKLIST QUANTITY()
    IF NumberOfGames = 0
			red=rgb(255,0,0)
			ink red,0
         text 250,200,"No session found."
         wait 1500
			CLS
         End
    ENDIF
    JOIN NET GAME 1,MyName$
ENDIF

NumberOfPlayers = 0
	 
slate=rgb(150,150,170)
ink slate,0

CLS

if Answer=1 then goto Host_Loop
if Answer=2 then goto Client_Loop

Host_Loop:

left=0
joined=0
keyvariable=0
DIM Pressed$(100)
Pressed$(0)="Nothing is being pressed"
Pressed$(1)="The Left Arrow Key is being pressed"
Pressed$(2)="The Right Arrow Key is being pressed"
Pressed$(10)="The Up Arrow Key is being pressed"
Pressed$(11)="The Up Arrow Key and Left Arrow Key are being pressed"
Pressed$(12)="The Up Arrow Key and Right Arrow Key are being pressed"
Pressed$(20)="The Down Arrow Key is being pressed"
Pressed$(21)="The Down Arrow Key and Left Arrow Key are being pressed"
Pressed$(22)="The Down Arrow Key and Right Arrow Key are being pressed"


WHILE ESCAPEKEY()=0

	 oldkey=keyvariable

	 text 220,10,"Two-Way Network Transmission (TNT) "
	 text 220,20,"   ..........................      "
    OldNOP=NumberOfPlayers
	 text 10,450,"Host - " + Pressed$(keyvariable) 
	 text 10,400,"Client - " + Pressed$(keyvariable2)

    PERFORM CHECKLIST FOR NET PLAYERS
    NumberOfPlayers = CHECKLIST QUANTITY()

	 if OldNOP>NumberOfPlayers then tdisplay=100 : left=1
	 if OldNOP<NumberOfPlayers then tdisplay=100 : joined=1
	 if tdisplay>0 and left=1 then text 10,55,"A player has left the session" : tdisplay=tdisplay-1 : if tdisplay=0 then left=0 : cls
	 if tdisplay>0 and joined=1 then text 10,55,"A player has joined the session" : tdisplay=tdisplay-1 : if tdisplay=0 then joined=0 : cls
	 
	 if upkey()=1 and downkey()=0 and leftkey()=0 and rightkey()=0 then keyvariable=10
	 if upkey()=1 and downkey()=0 and leftkey()=1 and rightkey()=0 then keyvariable=11
	 if upkey()=1 and downkey()=0 and leftkey()=0 and rightkey()=1 then keyvariable=12
	 if downkey()=1 and upkey()=0 and leftkey()=0 and rightkey()=0 then keyvariable=20
	 if downkey()=1 and upkey()=0 and leftkey()=1 and rightkey()=0 then keyvariable=21
	 if downkey()=1 and upkey()=0 and leftkey()=0 and rightkey()=1 then keyvariable=22
	 if upkey()=0 and downkey()=0 and leftkey()=1 and rightkey()=0 then keyvariable=1
	 if upkey()=0 and downkey()=0 and leftkey()=0 and rightkey()=1 then keyvariable=2
	 if upkey()=0 and downkey()=0 and leftkey()=0 and rightkey()=0 then keyvariable=0	

	 if oldkey<>keyvariable then Send Net Message Integer 0,keyvariable : CLS
	
	 Get Net Message
	
	 If Net Message Exists()<>0
	 	keyvariable2=Net Message Integer()
		CLS
	 endif

	 sync

ENDWHILE

END

Client_Loop:

left=0
joined=0
keyvariable=0
DIM Pressed$(100)
Pressed$(0)="Nothing is currently being pressed"
Pressed$(1)="The Left Arrow Key is being pressed"
Pressed$(2)="The Right Arrow Key is being pressed"
Pressed$(10)="The Up Arrow Key is being pressed"
Pressed$(11)="The Up Arrow Key and Left Arrow Key are being pressed"
Pressed$(12)="The Up Arrow Key and Right Arrow Key are being pressed"
Pressed$(20)="The Down Arrow Key is being pressed"
Pressed$(21)="The Down Arrow Key and Left Arrow Key are being pressed"
Pressed$(22)="The Down Arrow Key and Right Arrow Key are being pressed"

While EscapeKey()=0

	 oldkey2=keyvariable2

	 text 220,10,"Two-Way Network Transmission (TNT) "
	 text 220,20,"   ..........................      "
	 text 10,450,"Host - " + Pressed$(keyvariable) 
	 text 10,400,"Client - " + Pressed$(keyvariable2)

    OldNOP=NumberOfPlayers

    PERFORM CHECKLIST FOR NET PLAYERS
    NumberOfPlayers = CHECKLIST QUANTITY()

	 if OldNOP>NumberOfPlayers then tdisplay=100 : left=1
	 if OldNOP<NumberOfPlayers then tdisplay=100 : joined=1
	 if tdisplay>0 and left=1 then text 10,55,"A player has left the session" : tdisplay=tdisplay-1 : if tdisplay=0 then left=0 : cls
	 if tdisplay>0 and joined=1 then text 10,55,"A player has joined the session" : tdisplay=tdisplay-1 : if tdisplay=0 then joined=0 : cls

	 Get Net Message

	 If Net Message Exists()<>0
	 	keyvariable=Net Message Integer()
		CLS
	 endif

	 if upkey()=1 and downkey()=0 and leftkey()=0 and rightkey()=0 then keyvariable2=10
	 if upkey()=1 and downkey()=0 and leftkey()=1 and rightkey()=0 then keyvariable2=11
	 if upkey()=1 and downkey()=0 and leftkey()=0 and rightkey()=1 then keyvariable2=12
	 if downkey()=1 and upkey()=0 and leftkey()=0 and rightkey()=0 then keyvariable2=20
	 if downkey()=1 and upkey()=0 and leftkey()=1 and rightkey()=0 then keyvariable2=21
	 if downkey()=1 and upkey()=0 and leftkey()=0 and rightkey()=1 then keyvariable2=22
	 if upkey()=0 and downkey()=0 and leftkey()=1 and rightkey()=0 then keyvariable2=1
	 if upkey()=0 and downkey()=0 and leftkey()=0 and rightkey()=1 then keyvariable2=2
	 if upkey()=0 and downkey()=0 and leftkey()=0 and rightkey()=0 then keyvariable2=0	

	 if oldkey2<>keyvariable2 then Send Net Message Integer 0,keyvariable2 : CLS

	 sync

EndWhile


` Ths function will determine which NET CONNECTION number is TCP/IP.

FUNCTION FindTCPIPConnectionNumber()
    Flag = 0
    PERFORM CHECKLIST FOR NET CONNECTIONS
    FOR x = 1 TO CHECKLIST QUANTITY()
        Service$ = CHECKLIST STRING$(x)
        IF LEFT$(Service$,15)="Internet TCP/IP"
            Flag = x
        ENDIF
    NEXT x
ENDFUNCTION Flag