Bonkers Air Hockey

Started by bplus, November 05, 2019, 05:17:13

Previous topic - Next topic

bplus

* * *  Bonkers Air Hockey * * *
'Bonkers Air Hockey.bas for SmallBASIC 0.12.9 2017-04-22 (started) from
'bplus paddleball 2016-02-05 for SmallBASIC 0.12.2 [B+=MGA]
'and Bonkers Methods of tracking puck angle and collisions

const pr = 16                'puck radius
const pr2 = 2 * pr           'puck diameter = bumper width = radius of strikers
const tl = xmax              'table length
const tw = tl / 2            'table width
const tw13 = .3333 * tw \ 1  'goal end point
const tw23 = .6667 * tw \ 1  'goal end point
const speed = 40
const midc = (tl - 2 * pr2) \ 4    'mid point x of computer's field

computer = 0   'score
player = 0     'score
initball
pen on
while player < 21 and computer < 21
  cls
  updateScore
  drawTable
  drawComputerStriker
  drawPlayerStriker
  drawPuck
  showpage
  delay 10
wend
pen off
if computer > player then
  s = "Game Won by Computer."
else
  s = "Game Won by Player!"
end if
color rgb(200, 240, 140)
text (tl - txtw(s))/2, tw + 30, 26, s
showpage
delay 3000

sub initball
  px = tl/2 : py = tw/2 : pa = pi + rnd * pi/10
  rnddir = (rnd*2)\1 : if rnddir then pa = pi-pa
end

sub updateScore
  color rgb(40, 200, 255)
  s = "Computer: " + str(computer) + space(50) +"Player: " + str(player)
  text (tl - txtw(s))/2, tw + 30, 26, s
end

sub drawTable
  for i = 0 to pr2 step 4
    shade = 64 + i/pr2 * 100
    color rgb(shade, shade, shade)
    rect i, i, tl-i, tw-i filled
  next                                 
  rect pr2, pr2, tl - pr2, tw - pr2, rgb(190, 230, 255) filled 'field
  rect pr, tw13, pr2, tw23, rgb(60, 60, 60) filled                           'player goal
  rect tl - pr2, tw13, tl-pr, tw23, rgb(60, 60, 60) filled                   'computer goal
  rect tl \ 2 - 1, pr2, tl \ 2 + 1, tw- pr2, 8 filled          'center line
end

sub drawPlayerStriker
  psx = pen(4) : psy = pen(5)
  if psx - pr2 < tl/2     then psx = tl/2 + pr2
  if psx + pr2 > tl - pr2 then psx = tl - 2 * pr2
  if psy - pr2 < pr2      then psy = 2 * pr2
  if psy + pr2 > tw - pr2 then psy = tw - 2 * pr2
  striker psx, psy
end

sub drawComputerStriker
  c1 += pi/80
  csx = midc + pr2 + (midc-pr2) * sin(c1)
  if px > csx then csy = py + pr2 * 1.5 * sin(c1)
  if csy - pr2 < pr2 then csy = 2 * pr2
  if csy + pr2 > tw - pr2 then csy = tw - 2 * pr2
  striker csx, csy
end

sub drawPuck
  'update ball x, y and see if hit anything
  px = px + speed * cos(pa)
  py = py + speed * sin(pa)
 
  if px - pr < pr2 then
    if tw13 < py - pr and py + pr < tw23 then
      player += 1
      cls
      updateScore
      drawTable
      striker csx, csy
      striker psx, psy
      puck pr, py
      for i = 0 to pr step 4
        shade = 64 + i/pr2 * 100
        color rgb(shade, shade, shade)
        rect i, t13, pr, tw23 filled
      next 
      sound 1200, 200
      sound 2200, 300
      showpage
      initball
      delay 500
      exit sub
    else
      sound 2600, 8
      pa = pi - pa
      px = pr2 + pr
    fi
  fi
     
  if px + pr > tl - pr2 then
    if tw13 < py - pr and py + pr < tw23 then
      computer += 1
      cls
      updateScore
      drawTable
      striker csx, csy
      striker psx, psy
      puck tl-pr, py
      for i = 0 to pr step 4
        shade = 64 + i/pr2 * 100
        color rgb(shade, shade, shade)
        rect tl-pr, t13, tl-i, tw23 filled
      next
      sound 2200, 300
      sound 1200, 200
      showpage
      initball
      delay 500
      exit sub
    else
      sound 2600, 5
      pa = pi - pa
      px = tl - pr2 - pr
    fi
  fi
 
  if py - pr < pr2 then
    sound 2600, 8
    pa = -pa
    py = pr2 + pr
  fi

  if py + pr > tw - pr2 then
    sound 2600, 8
    pa = - pa
    py = tw - pr2 - pr
  end if
 
  if sqr((px-psx)^2 + (py-psy)^2) < (pr + pr2) then
    pa = atan2(py-psy, px-psx)
    sound 2200, 4
  fi
  if sqr((px-csx)^2 + (py-csy)^2) < (pr + pr2) then
    pa = atan2(py-csy, px-csx)
    sound 2200, 4
  fi       
  puck px, py
end

sub puck(x, y)
  color rgb(90, 90, 90)
  circle x, y, pr filled
  color rgb(190, 100, 0)
  circle x, y, pr - 4 filled
end

sub striker(x, y)
  local i
  for i = pr2 to pr step -1
    shade = 164 - 90 * sin((i)*2*pi/pr)
    color rgb(shade, shade, shade)
    circle x, y, i filled
  next
  for i = pr to 0 step -1
    shade = 185 + 70*(pr - i)/pr
    color rgb(shade, shade, shade)
    circle x, y, i filled
  next
end

sub text(x, y, size, s) ' a sub to make translating to SmallBASIC from SdlBasic easier
  'when this sub is used text size is altered for the rest of the run
  local l
  l.w = window() : l.w.setfont(size, "pt", 0, 0)
  at x, y : ? s
end
1 person likes this

Qube

He he, that's cool :) - Congrats on using the forums 60fps marquee scroller ;D
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

bplus

#2
Ha, there's a cute way to do fish swimming with it.

BTW, one might find the computer movements interesting in this game.
1 person likes this

johnno56

I've always liked this game... Cool...  :P
May your journey be free of incident.

Live long and prosper.

chrisws

Quote from: johnno56 on November 05, 2019, 19:51:46
I've always liked this game... Cool...  :P

Hey johnno56 is here! We've practically got the band back together :)

johnno56

I had almost forgotten that joined this new forum. I was working on an sdlbasic version of "Bonkers" and need clarification on how SB used RND... One search lead to another and 'boom', remembered 'syntax'... THIS time I have created a bookmark in my browser... lol

Band? Who's missing?
May your journey be free of incident.

Live long and prosper.

bplus

Hi Johnno,

RND = something between 0 and almost 1
Randomize Timer for new starting RND.

"Band? Who's missing?"
Glenn Miller
1 person likes this

johnno56

Oh, ha ha, very funny...  :P
May your journey be free of incident.

Live long and prosper.

kay63

The game is fun, hard to stop playing! :-)

bplus

Quote from: kay63 on March 13, 2022, 23:05:42
The game is fun, hard to stop playing! :-)

Ah! you just made my day! Thanks
1 person likes this