Microsoft Small Basic

Program Listing: rzd792
' Challenge of the month March 2013 Like pong gme . by NaochanON
' If Player (or PC) get 5 Points , You ( or PC) wins.


GraphicsWindow.MouseMove=Playermoving
Shapes_Init()
Shapes_Add()
InitData()

While Gameover<>"true"
Moving="true"
RNDBallPosition()
While Moving="true"
NN=NN+0.005
defence_moving()
PCMoving()
ball_moving()
If NN>15 then
NN=15
endif
Program.Delay(20-NN) ' speed Up
endwhile
Program.Delay(1000)
NN=0
endwhile
endmessage() ' Game Over

Sub PCMoving
PCSX=shapes.GetLeft(SHP[9]["obj"]) ' PC slider
Distance=(BL+BW/2)-(GLX+GLW/2)
chaseSpeed=Distance*((PLpoint+1)/( PLpoint+5.5)) ' chasing speed changes related to PLPoint
If BYY<0 and math.Abs(Distance)<3*GLW/4 Then
Shapes.Move(SHP[9]["obj"],GLX+GLW/4+chaseSpeed,Shapes.GetTop(SHP[9]["obj"]))
EndIf
endsub

Sub ball_moving
If Moving="true" Then
BL=Shapes.GetLeft(SHP[8]["obj"]) ' ball left position
BT=Shapes.Gettop(SHP[8]["obj"]) ' ball top position
PCSX=shapes.GetLeft(SHP[9]["obj"]) ' PC slider position
PLSX=shapes.GetLeft(SHP[10]["obj"]) ' Player slider position
DFX=Shapes.GetLeft(SHP[7]["obj"]) ' defence X position
DFY=shapes.GetTop(SHP[7]["obj"]) ' defence Y position
If (GPCY-GLH)< BT and (BT+BW)<(GPPY+GLH) And BL<=WL Then ' left wall
BXX=BXY["X"]
ElseIf (GPCY-GLH) BXX=-BXY["X"]
ElseIf ((GPCY-GLH) BYY=BXY["Y"]+1
ElseIf ((GPCY-GLH) BYY=BXY["Y"]+1
elseIf (GPPY<=(BT+BW) and (BT+BW)<(GPPY+GLH) and BL+BW/2<=GLX ) Then ' player Lower wall Left
BYY=-(BXY["Y"]+1)
ElseIf (GPPY<=(BT+BW) and (BT+BW)<(GPPY+GLH) and GLX2<=(BL+BW/2)) then ' player Lower wall Right
BYY=-(BXY["Y"]+1)
ElseIf BYY>0 and (DFX-BW<=BL and BL<=(DFX+DFW)) and (DFY<=(BT+BW) and (BT+BW)<(DFY+DFH/2) ) then ' at defence from PC
BYY=-(BXY["Y"]+1)
ElseIf BYY<0 and (DFX-BW<=BL and BL<=(DFX+DFW)) and ((DFY+DFH/2) BYY=BXY["Y"]+1
elseif BYY<0 and (PCSX-BW<=BL and BL<(PCSX+PCW)) and (PCT+PCH/2 BYY=BXY["Y"]+1
elseif BYY>0 and (PLSX-BW<=BL and BL<(PLSX+PLW)) and (PLT BYY=-(BXY["Y"]+1)
elseif BT>500 or BT<0 then ' if ball run throgh Player's Goal !!! PC Wins
WhichWins()
EndIf
Shapes.Move(SHP[8]["obj"],BL+BXX,BT+BYY)
EndIf
EndSub

Sub endmessage
If PCpoint>PLpoint Then
winmsg="You loose !!!! "
Sound.PlayBellRing()
Else
winmsg="You Win !!!! "
Sound.PlayChime()
EndIf
wmsg=Shapes.AddText(winmsg)
Shapes.Move(wmsg,130,225)
EndSub

Sub WhichWins
Moving="false"
If BT<0 Then
PLpoint=PLpoint+1
elseif BT>500 Then
PCpoint=PCpoint+1
endif
Shapes.SetText(smsg," PC : "+PCpoint+" Player : "+PLpoint)
If PLPoint=5 Or PCPoint=5 Then ' If You (PC) get 5 points
GameOver="true"
EndIf
EndSub

Sub defence_moving
DFX=Shapes.GetLeft(SHP[7]["obj"]) ' defence X position
DFY=shapes.GetTop(SHP[7]["obj"]) ' defence Y position
If DFX dPM=-dPM
ElseIf WR dPM=-dPM
EndIf
Shapes.Move(SHP[7]["obj"],DFX-dPM,DFY)
EndSub

sub PlayerMoving
MX=graphicswindow.MouseX
PLT= Shapes.GetTop(SHP[10]["obj"])
If MX MX=WL+3
ElseIf MX+PLW/2>WR then
MX=WR-3-PLW/2
EndIf
Shapes.Move(SHP[10]["obj"],MX-PLW/2,PLT)
EndSub

Sub Shapes_Init
X0 = 100 ' x offset
Y0 = 100 ' y offset
SHP[1] = "func=rect;x=1;y=1;width=70;height=12;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' Upper left
SHP[2] = "func=rect;x=198;y=0;width=70;height=12;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' Upper right
SHP[3] = "func=rect;x=1;y=10;width=12;height=351;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' left wall
SHP[4] = "func=rect;x=1;y=361;width=70;height=12;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' lower left
SHP[5] = "func=rect;x=198;y=361;width=70;height=12;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' lower right
SHP[6] = "func=rect;x=256;y=11;width=12;height=351;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' Right wall
SHP[7] = "func=rect;x=94;y=185;width=50;height=12;bc=#EE0515;pc=#EE0515;pw=2;" ' center defence
SHP[8] = "func=ell;x=140;y=30;width=16;height=16;bc=#209F09;pc=#209F09;pw=2;" ' ball
SHP[9] = "func=rect;x=94;y=36;width=60;height=12;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' PC's slider
SHP[10] = "func=rect;x=110;y=326;width=60;height=12;bc=#FFFFFF;pc=#FFFFFF;pw=2;" ' your slider
EndSub

Sub SHaPes_Add
Mouse.HideCursor()
GraphicsWindow.Title=" Get 5 points to Win to PC !!! "
GraphicsWindow.Width=500
GraphicsWindow.Height=500
GraphicsWindow.BackgroundColor="Black"
s=1
For i = 1 To Array.GetItemCount(SHP)
GraphicsWindow.PenWidth = SHP[i]["pw"]*s
GraphicsWindow.PenColor = SHP[i]["pc"]
GraphicsWindow.BrushColor = SHP[i]["bc"]
If SHP[i]["func"] = "rect" Then
SHP[i]["obj"] = Shapes.AddRectangle(SHP[i]["width"]*s, SHP[i]["height"]*s)
ElseIf SHP[i]["func"] = "ell" Then
SHP[i]["obj"] = Shapes.AddEllipse(SHP[i]["width"]*s, SHP[i]["height"]*s)
ElseIf SHP[i]["func"] = "tri" Then
SHP[i]["obj"] = Shapes.AddTriangle(SHP[i]["x1"]*s, SHP[i]["y1"]*s, SHP[i]["x2"]*s, SHP[i]["y2"]*s, SHP[i]["x3"]*s, SHP[i]["y3"]*s)
ElseIf SHP[i]["func"] = "line" Then
SHP[i]["obj"] = Shapes.AddLine(SHP[i]["x1"]*s, SHP[i]["y1"]*s, SHP[i]["x2"]*s, SHP[i]["y2"]*s)
EndIf
Shapes.Move(SHP[i]["obj"], X0 + SHP[i]["x"]*s, Y0 + SHP[i]["y"]*s)
Shapes.Rotate(SHP[i]["obj"], SHP[i]["angle"])
EndFor
EndSub

Sub InitData
'----------------------------------------------------------------------------------------------------
dPM=1 ' defence moving
'----------------------------------------------------------------------------------------------------
WL=Shapes.GetLeft(SHP[3]["obj"])+SHP[3]["width"]*s ' Wall left position
WR=Shapes.GetLeft(SHP[6]["obj"]) ' wall right position
BW=SHP[8]["width"]*s ' ball width & height
GPCY=Shapes.Gettop(SHP[1]["obj"])+SHP[1]["height"]*s ' Goal PC position Y
GLH=SHP[1]["height"]*s ' Goal wall height
DFW=SHP[7]["width"]*s ' defence width
DFH=SHP[7]["height"]*s ' defence height
GPPY=Shapes.Gettop(SHP[4]["obj"]) ' Goal Player position Y
GLX=Shapes.Getleft(SHP[1]["obj"])+SHP[1]["width"]*s ' goal position X //PC & Player same value
GLX2=Shapes.Getleft(SHP[2]["obj"]) ' goal position X2 // PC & Player same value
GLW=GLX2-GLX ' goal width
PCT=Shapes.Gettop(SHP[9]["obj"]) ' PC's slider Top
PCW=SHP[9]["width"]*s ' PC's slider width
PCH=SHP[9]["height"]*s ' PC's slider Height
PLW=SHP[10]["width"]*s ' Player's slider width
PLH=SHP[10]["height"]*s ' Player's slider Height
GraphicsWindow.FontSize=30
GraphicsWindow.BrushColor="White"
smsg= Shapes.AddText(" PC : 0 Player : 0 ")
Shapes.Move(smsg,50,25)
Moving="true" ' Initial Moving = true
EndSub

Sub RNDBallPosition
BXY="X=1;Y=4" ' ball speed
BYY=BXY["Y"]
BXX=BXY["X"]
Shapes.Move(SHP[8]["obj"],WL+Math.GetRandomNumber(200),GPCY+50+Math.GetRandomNumber(100))
EndSub