March 29, 2020, 12:16:08 AM

Author Topic: Bonkers Air Hockey  (Read 799 times)

Offline bplus

  • Full Member
  • ***
  • Posts: 120
Bonkers Air Hockey
« on: November 05, 2019, 05:17:13 AM »
* * *  Bonkers Air Hockey * * *
Code: [Select]
'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

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2343
Re: Bonkers Air Hockey
« Reply #1 on: November 05, 2019, 05:40:33 AM »
He he, that's cool :) - Congrats on using the forums 60fps marquee scroller ;D
Until the next time...

Offline bplus

  • Full Member
  • ***
  • Posts: 120
Re: Bonkers Air Hockey
« Reply #2 on: November 05, 2019, 06:16:32 AM »
Ha, there's a cute way to do fish swimming with it.

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

Offline johnno56

  • Jr. Member
  • **
  • Posts: 26
  • Logic is the beginning of Wisdom.
Re: Bonkers Air Hockey
« Reply #3 on: November 05, 2019, 07:51:46 PM »
I've always liked this game... Cool...  :P
May your journey be free of incident.

Live long and prosper.

Offline chrisws

  • Jr. Member
  • **
  • Posts: 30
Re: Bonkers Air Hockey
« Reply #4 on: November 05, 2019, 09:54:48 PM »
I've always liked this game... Cool...  :P

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

Offline johnno56

  • Jr. Member
  • **
  • Posts: 26
  • Logic is the beginning of Wisdom.
Re: Bonkers Air Hockey
« Reply #5 on: November 24, 2019, 04:21:40 AM »
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.

Offline bplus

  • Full Member
  • ***
  • Posts: 120
Re: Bonkers Air Hockey
« Reply #6 on: November 24, 2019, 05:36:59 AM »
Hi Johnno,

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

"Band? Who's missing?"
Glenn Miller

Offline johnno56

  • Jr. Member
  • **
  • Posts: 26
  • Logic is the beginning of Wisdom.
Re: Bonkers Air Hockey
« Reply #7 on: November 24, 2019, 07:43:42 AM »
Oh, ha ha, very funny...  :P
May your journey be free of incident.

Live long and prosper.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal