### Author Topic: Bonkers Air Hockey  (Read 105 times)

#### B+

• Sr. Member
• Posts: 419
##### Bonkers Air Hockey
« on: 23. April 2017, 18:38:17 »
Mouse controls Player's striker on right. Computer dumbed down, when puck is behind computer striker, striker only moves along x and can knock puck into player's goal. When puck is again in front of the computer striker, it will jump into action. Had to do something so humans have a chance to win.
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 collisionsconst pr = 16                'puck radiusconst pr2 = 2 * pr           'puck diameter = bumper width = radius of strikersconst tl = xmax              'table lengthconst tw = tl / 2            'table widthconst tw13 = .3333 * tw \ 1  'goal end pointconst tw23 = .6667 * tw \ 1  'goal end pointconst speed = 40const midc = (tl - 2 * pr2) \ 4    'mid point x of computer's fieldcomputer = 0   'score player = 0     'scoreinitballpen onwhile player < 21 and computer < 21  cls  updateScore  drawTable  drawComputerStriker  drawPlayerStriker  drawPuck  showpage  delay 10wendpen offif computer > player then  s = "Game Won by Computer."else  s = "Game Won by Player!"end ifcolor rgb(200, 240, 140)text (tl - txtw(s))/2, tw + 30, 26, sshowpagedelay 3000sub initball  px = tl/2 : py = tw/2 : pa = pi + rnd * pi/10  rnddir = (rnd*2)\1 : if rnddir then pa = pi-paendsub updateScore  color rgb(40, 200, 255)  s = "Computer: " + str(computer) + space(50) +"Player: " + str(player)  text (tl - txtw(s))/2, tw + 30, 26, sendsub 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 lineendsub 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, psyendsub 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, csyendsub 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, pyend sub puck(x, y)  color rgb(90, 90, 90)  circle x, y, pr filled  color rgb(190, 100, 0)  circle x, y, pr - 4 filledendsub 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  nextendsub 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 : ? send`
« Last Edit: 23. April 2017, 18:45:10 by B+ »