Author Topic: Retro Game  (Read 2057 times)

B+

  • Guest
Retro Game
« on: February 04, 2016, 05:51:39 AM »
This might bring back memories:

Code: [Select]
'bplus paddleball 2016-02-05 for SmallBASIC 0.12.2 [B+=MGA]
'2016-02-06 sound added

rb=10
computer=0
player=0
initball

pen on
while player<21 and computer <21
  cls
  updatescore
  drawtable
  drawpaddle
  drawball
  showpage
  delay 10
wend
pen off
if computer>player then
  color 9,15
  s="  Game goes to Computer.  "
  at (xmax-txtw(s))/2+100,ymax/2-txth(s)/2:?s
else
  color 12,15
  s="  Game goes to Player!!!  "
  at (xmax-txtw(s))/2+100,ymax/2-txth(s)/2:?s
end if
showpage
delay 3000

sub initball
  xball=rnd*.4*xmax
  yball=ymax/2-.25*ymax+rnd*.5*ymax
  dx=rnd*6+17
  r=int(rnd*2)
  if r then dx*=-1
  dy=rnd*3+6
  r=int(rnd*2)
  if r then dy*=-1
end

sub updatescore
  color 12,0
  s="Player"
  at (100-txtw(s))/2,10:?s
  s=str(player)
  at (100-txtw(s))/2,40:?s
  color 9,0
  s="Computer"
  at (100-txtw(s))/2,100:?s
  s=str(computer)
  at (100-txtw(s))/2,140:?s
end

sub drawtable
  color rgb(230,190,230),0
  rect 100,0,xmax,ymax,rgb(0,128,80) filled
  rect 100,0,xmax,10 filled
  rect 100,ymax-10,xmax,ymax filled
  rect 100,0,110,ymax filled
  rect 100,50,110,50+10*rb,12 filled
  rect 100,ymax-50-10*rb,110,ymax-50,12 filled
  rect xmax-20,10,xmax,ymax-10,9 filled
end

sub drawpaddle
  my=pen(5)
  topy=my-5*rb
  boty=my+5*rb
  rect xmax-20,topy,xmax-10,boty,rgb(255,124,0) filled
  rect xmax-10,topy,xmax,boty,rgb(120,60,30) filled
end

sub drawball
  xball+=dx
  if xball>xmax-20 then
    if yball<topy or yball>boty then 'paddle miss
      computer+=1
      sound 100,100
      delay 500
      initball
    else 'paddle hit
      sound 660,20
      dx=dx*-1
      xball=xmax-20-rb
    end if
  end if
  if xball<110 then
    if (yball>50 and yball<50+10*rb) or (yball>ymax-50-10*rb and yball<ymax-50) then
      player+=1
      sound 200,100
      delay 500
      initball
    else
      sound 990,20
      dx=dx*-1
      xball=110+rb
    end if 
  end if
  yball+=dy
  if yball<10+rb then
    sound 990,20
    dy=dy*-1
    yball=10+rb
  end if
  if yball>ymax-10-rb then
    sound 990,20
    dy=dy*-1
    yball=ymax-10-rb
  end if
  circle xball,yball,rb,1,14 filled
end
« Last Edit: February 07, 2016, 05:06:49 PM by B+ »

B+

  • Guest
Re: Retro Game
« Reply #1 on: February 04, 2016, 05:56:01 AM »
Here is sdlBasic version with literal twist added to it:

Code: [Select]
    'Bplus paddleball.sdlbas 2016-02-02 [B+=MGA] translated from
    ' pong maybe.bas 2016-02-02 for SmallBASIC 0.12.2 [B+=MGA]
    'twist added 2016-02-04 with mousebutton altering course of ball

    common xmax,ymax,rb,computer,player,pady,xball,yball,dx,dy,topy,boty
    xmax=800
    ymax=600

    option qbasic
    setdisplay(xmax,ymax,32,1)
    setcaption("Bplus Paddleball for sdlBasic 2016-02-02")
    autoback(-2)

    rb=10 'ball radius
    computer=0 'score
    player=0
    initball()

    while player<21 and computer <21
      cls
  if mousebutton=1 then: dx=dx+1:end if
  if mousebutton=3 then: dx=dx-1:end if
      updatescore()
      drawtable()
      drawpaddle()
      drawball()
      screenswap
      wait(10)
    wend
    if computer>player then
        s="Computer."
    else
        s="Player !!!"
    end if
    text(260,ymax/2-20,32,"Game goes to: "+s)
    screenswap
    wait(4000)


    sub initball()
        xball=rnd(.5*xmax)
        yball=ymax/2-.25*ymax+rnd(.5*ymax)
        dx=rnd(6)+18
        r=rnd(3)-1
        if r then
            dx=-1*dx
        end if
        dy=rnd(3)+6
        r=rnd(3)-1
        if r then
            dy*=-1
        end if
        wait(500)
    end sub

    sub updatescore
        ink(0xff0000)
        text(30,10,16, "Player")
        text( 40,40,16,str(player))
        ink(0x0000ff)
        text(10,100,16, "Computer")
        text(40,140,16,str(computer))
    end sub

    sub drawtable
        ink(0x009641)
        bar(100,0,xmax,ymax)
        ink(0xeeeecc)
        bar(100,0,xmax,10)
        bar(100,ymax-10,xmax,ymax)
        bar(100,0,110,ymax)
        ink(0xff0000)
        bar(100,50,110,50+10*rb)
        bar(100,ymax-50-10*rb,110,ymax-50)
        ink(0x0000ff)
        bar(xmax-20,10,xmax,ymax-10)
    end sub

    sub drawpaddle
        my=MouseY
        topy=my-5*rb
        boty=my+5*rb
        ink(0xff8000)
        bar(xmax-20,topy,xmax-10,boty)
        ink(0x874637)
        bar(xmax-10,topy,xmax,boty)
    end sub

    sub drawball
        xball=xball+dx
        if xball>xmax-20-rb then
            if yball<topy or yball>boty then 'paddle miss
                computer+=1
                initball
            else 'paddle hit
                dx=dx*-1
                xball=xmax-20-rb
            end if
        end if
        if xball<110+rb then
            if (yball>50 and yball<50+10*rb) or (yball>ymax-50-10*rb and yball<ymax-50) then
                player+=1
                initball
            else
                dx=dx*-1
                xball=110+rb
            end if
        end if
        yball+=dy
        if yball<10+rb then
            dy=dy*-1
            yball=10+rb
        end if
        if yball>ymax-10-rb then
            dy=dy*-1
            yball=ymax-10-rb
        end if
        ink(0xffff00)
        fillcircle(xball,yball,rb)
    end sub

Yes, that's right! You can beat the computer using only the right mouse button! (without moving the paddle once)
« Last Edit: February 04, 2016, 06:13:49 AM by B+ »

B+

  • Guest
Re: Retro Game
« Reply #2 on: February 07, 2016, 05:08:17 PM »
I updated the SmallBASIC code in the original post with SOUND, so much better! B+