Microsoft Small Basic

Program Listing:
Embed this in your website
'Ping Pong by Professional of Small Basic (POSB)'
'There is a problem with resetting the entire game :('

' program no:

Sub initialize
  GraphicsWindow.Width=640
  GraphicsWindow.Height=480
  GraphicsWindow.BackgroundColor="black"
  GraphicsWindow.fillRectangle(10,40,620,5)
  GraphicsWindow.fillRectangle(10,460,620,5)
  GraphicsWindow.FillRectangle(320,60,10,50)
  GraphicsWindow.FillRectangle(320,130,10,50)
  GraphicsWindow.FillRectangle(320,200,10,50)
  GraphicsWindow.FillRectangle(320,270,10,50)
  GraphicsWindow.FillRectangle(320,340,10,50)
  GraphicsWindow.FillRectangle(320,410,10,50)
  GraphicsWindow.BrushColor="black"
  GraphicsWindow.FillRectangle(260,10,60,50)
  GraphicsWindow.FillRectangle(330,10,60,50)
  GraphicsWindow.KeyDown=KeyDown
  GraphicsWindow.KeyUp=KeyUp
  IsPlaying="True"
  score1=8
  score2=0
  speed=1
EndSub

Sub numbers
  GraphicsWindow.BrushColor="white"
  GraphicsWindow.FontBold="True"
  GraphicsWindow.FontName="FixedSys"
  GraphicsWindow.FontSize=60
  For i=0 To 9
    number[1][i]=Shapes.AddText(i)
    Shapes.Move(number[1][i],0,-60)
  EndFor
  For j=0 To 9
    number[2][j]=Shapes.AddText(j)
    Shapes.Move(number[2][j],0,-60)
  EndFor
EndSub

Sub players
  player1=Shapes.AddRectangle(15,75)
  player2=Shapes.AddRectangle(15,75)
  xplayer1=10
  xplayer2=615
  yplayer1=195
  yplayer2=195
EndSub

Sub KeyDown
  lastKey = GraphicsWindow.LastKey
  If lastKey = "W" Then
    y1=-5
  EndIf
  If lastKey = "S" Then
    y1=5
  EndIf
  If lastKey="Up" Then
    y2=-5
  EndIf
  If lastKey = "Down" Then
    y2=5
  EndIf
  If lastKey = "Y" Then
    ResetGame()
  EndIf
  If lastKey = "N" Then
    Program.End()
  EndIf
EndSub

Sub KeyUp
  lastKey=GraphicsWindow.LastKey
  If lastKey="W" Or lastKey="S" Then
    y1=0
  EndIf
  If lastKey="Up" Or lastKey="Down" Then
    y2=0
  EndIf
EndSub

Sub playermovement
  If yplayer1>45 Then
    yplayer1=yplayer1+y1
  Else
    yplayer1=yplayer1+5
  EndIf
  If yplayer1<385 Then
    yplayer1=yplayer1+y1
  Else
    yplayer1=yplayer1-5
  EndIf
  If yplayer2>45 Then
    yplayer2=yplayer2+y2
  Else
    yplayer2=yplayer2+5
  EndIf
  If yplayer2<385 Then
    yplayer2=yplayer2+y2
  Else
    yplayer2=yplayer2-5
  EndIf
  Shapes.Move(player1,xplayer1,yplayer1)
  Shapes.Move(player2,xplayer2,yplayer2)
EndSub

Sub ball
  GraphicsWindow.PenColor="white"
  GraphicsWindow.PenWidth=1
  GraphicsWindow.BrushColor="white"
  ball=Shapes.AddRectangle(15,15)
  ball_x=317
  ball_y=230
  move_x=1
  random=Math.GetRandomNumber(2)
  move_y=random
  If move_y=1 Then
    move_x=-move_x
  EndIf
EndSub

Sub ballmovement
  If ball_y>47 Then
    ball_y=ball_y+move_y
  Else
    Sound.PlayClick()
    move_y=random
  EndIf
  If ball_y<443 Then
    ball_y=ball_y+move_y
  Else
    Sound.PlayClick()
    move_y=-random
  EndIf
  If ball_x=xplayer1+15 And ball_y>yplayer1 And ball_y<yplayer1+75 Then
    Sound.PlayClick()
    move_x=-move_x
  EndIf
  If ball_x+15=xplayer2 And ball_y>yplayer2 And ball_y<yplayer2+75 Then
    Sound.PlayClick()
    move_x=-move_x
  EndIf
  ball_x=ball_x+move_x
  ball_y=ball_y+move_y
  Shapes.Move(ball,ball_x,ball_y)
  Program.Delay(10-speed)
EndSub

Sub score
  If ball_x>640 And ball_x<642 Then
    Sound.PlayChimes()
    score1=score1+1
    resetround()
    speed=speed+0.5
  EndIf
  If ball_x<0 And ball_x>-2 Then
    Sound.PlayChimes()
    score2=score2+1
    resetround()
    speed=speed+0.5
  EndIf
  If score1=9 Or score2=9 Then
    gameover()
    resetgame()
  EndIf
  Shapes.Move(number[1][score1-1],275,-60)
  Shapes.Move(number[2][score2-1],345,-60)
  Shapes.Move(number[1][score1],275,10)
  Shapes.Move(number[2][score2],345,10)
EndSub

Sub resetround
  ball_x=317
  ball_y=230
  move_x=1
  move_y=Math.GetRandomNumber(2)
  If move_y=1 Then
    move_x=-move_x
  EndIf
  xplayer1=10
  xplayer2=615
  yplayer1=195
  yplayer2=195
  Shapes.Move(ball,ball_x,ball_y)
  Shapes.Move(player1,xplayer1,yplayer1)
  Shapes.Move(player2,xplayer2,yplayer2)
EndSub

Sub ResetGame
  GraphicsWindow.Clear()
  Shapes.Remove(ball)
  Shapes.Remove(player1)
  Shapes.Remove(player2)
  For i=0 To 9
    shapes.Remove(number[1][i])
    Shapes.Remove(number[2][i])
  EndFor
  numbers()
  ball()
  players()
  initialize()
  resetround()
  score1=0
  score2=0
  IsPlaying = "True"
  play()
EndSub

Sub gameover
  IsPlaying="False"
  Sound.PlayChime()
  Shapes.Move(ball,100,-50)
  GraphicsWindow.FillRectangle(120,190,450,150)
  GraphicsWindow.BrushColor="Orange"
  GraphicsWindow.DrawText(140,190,"GAME OVER")
  If score1=9 Then
    GraphicsWindow.DrawText(120,240,"Player 1 WON!")
  Else
    GraphicsWindow.DrawText(120,240,"Player 2 WON!")
  EndIf
  GraphicsWindow.FontSize=25
  GraphicsWindow.DrawText(150,300,"Do you want to play again?(Y/N)")
EndSub

Sub play
  While (IsPlaying)
    playermovement()
    ballmovement()
    score()
    Program.Delay(10)
  EndWhile
EndSub
numbers()
ball()
players()
initialize()
play()
Copyright (c) Microsoft Corporation. All rights reserved.