Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fun with TCP/IP
#1
Hi,

A funny little application of using TCP/IP: I’ll let you discover it. Note: if you already have a localhost service running on port 8080, you should change the value of FREE_LOCAL_PORT to assign it an available port number.

Code: (Select All)
type ball_type
    x as single
    y as single
    vx as single
    vy as single
    radius as single
end type

const TAU = 8 * atn(1)

randomize timer

const FREE_LOCAL_PORT = "8080"

me& = _openhost("TCP/IP:"+FREE_LOCAL_PORT)

if me& then
    _title "SERVER (LEFT WINDOW)"
    shell _dontwait "./ping"
    sleep 1
    you& = _openconnection(me&)
else
    _title "CLIENT (RIGHT WINDOW)"
    you& = _openclient("TCP/IP:"+FREE_LOCAL_PORT+":localhost")
    synchronize% = 0
end if

const VIEWPORT_WIDTH = 320
const VIEWPORT_HEIGHT = 240

const HALF_WIDTH = VIEWPORT_WIDTH / 2
const HALF_HEIGHT = VIEWPORT_HEIGHT / 2

viewport& = _newimage(VIEWPORT_WIDTH,240,32)
_printmode _keepbackground, viewport&

canvas& = _newimage(VIEWPORT_WIDTH,240,32)
layer& = _newimage(VIEWPORT_WIDTH,240,32)
background& = _newimage(VIEWPORT_WIDTH,240,32)

screen viewport&

_dest background&
cls , &HFF009DFF
for i% = 0 to 255
    color _rgb(128,i%,255-i%), &H00000000
    if me& then
        _printstring ((i% mod 16 + 1) * 18 + HALF_WIDTH,(i% \ 16 + 1)*12), chr$(i%)
    else
        _printstring ((i% mod 16 + 1) * 18 - HALF_WIDTH,(i% \ 16 + 1)*12), chr$(i%)
    end if
next i%

dim ball as ball_type

ball.radius = 16

xmin! = ball.radius
ymin! = ball.radius
xmax! = _width(canvas&) - ball.radius
ymax! = _height(canvas&) - ball.radius

ball.x = _width(canvas&) / 2
ball.y = _height(canvas&) / 2
a! = rnd*(TAU/4) - TAU/8
s! = 5.5

ball.vx = cos(a!) * s!
ball.vy = sin(a!) * s!

t = timer(.001)
fps% = 0
frames% = 0

do
    _limit 60

    frames% = frames% + 1
    if timer(.001)>=t+1 then
    fps% = frames%
    frames% = 0
    t = timer(.001)
    end if

    if not me& then get #you&, , ball

    _dest layer&
    cls , &H00000000
    if me& then
        circle (ball.x,ball.y), ball.radius, &HFFFF0000
    else
        circle (ball.x-320,ball.y), ball.radius, &HFFFF0000
    end if
    paint step (0,0), &HC7FF0000, &HFFFF0000

    color &HFFFFFFFF, &H00000000
    _printstring (1,1), "fps:"+right$("000"+_tostr$(fps%),3)

    _putimage , background&, canvas&
    _putimage , layer&, canvas&
    _putimage , canvas&, viewport&

    key$ = inkey$
    if key$ = chr$(27) then system

    ball.x = ball.x + ball.vx
    ball.y = ball.y + ball.vy

    if me& then
        if ball.x < xmin! _orelse ball.x > (xmax! + VIEWPORT_WIDTH) then ball.vx = - ball.vx
    else
        if ball.x < (xmin! - VIEWPORT_WIDTH) _orelse ball.x > xmax! then ball.vx = - ball.vx
    end if
    if ball.y < ymin! _orelse ball.y > ymax! then ball.vy = - ball.vy

    if me& then put #you&,, ball

loop

[Image: 2025-09-30-22-30.png]
Reply
#2
GIF of the running program
[Image: ping.gif]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Tongue Some fun Steffan-68 7 1,821 04-12-2025, 05:50 AM
Last Post: Pete
  a little TCP/IP network in LocalHost TempodiBasic 5 1,630 12-03-2024, 01:09 AM
Last Post: Keybone
  A little bit of spark fun today TerryRitchie 2 834 05-18-2024, 12:59 AM
Last Post: bobalooie

Forum Jump:


Users browsing this thread: 1 Guest(s)