Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GraphicsWindow.BackgroundColor = "Black"
  2. GraphicsWindow.Width = 565
  3. GraphicsWindow.Height = 500
  4. GraphicsWindow.Title = "Breakout"
  5. GraphicsWindow.PenColor = "White"
  6. numberOfRows = 3
  7. score = 0
  8. totalBricks = 0
  9.  
  10. InitializeComponents()
  11.    
  12. deltaX = 1
  13. deltaY = -2
  14.  
  15. While "True"
  16.   x = x + deltaX
  17.   y = y + deltaY
  18.  
  19.   ' If the wall is hit, change the direction
  20.   If x >= GraphicsWindow.Width - 10 Or x <= 0 Then
  21.     deltaX = -deltaX
  22.   EndIf
  23.  
  24.   If y <= 0 Then
  25.     deltaY = -deltaY
  26.   EndIf
  27.    
  28.   deltaX = deltaX + 0.001 * (x - Shapes.GetLeft(ball)) / 30 'Put some diversion
  29.   Shapes.Move(ball, x, y)
  30.   Program.Delay(5)
  31.  
  32.   CheckIfMainBlockIsHitted()
  33.   CheckIfBrickIsHitted()
  34.  
  35.   If totalBricks = 0 Then
  36.     GraphicsWindow.Clear()
  37.     GraphicsWindow.FontSize = 30
  38.     GraphicsWindow.BrushColor = "Green"
  39.     GraphicsWindow.DrawText(200, 185, "You Won!")
  40.     GraphicsWindow.FontSize = 25
  41.     GraphicsWindow.DrawText(185, 235, "Your score is: " +  score)
  42.   EndIf
  43. EndWhile
  44.  
  45. Sub CheckIfMainBlockIsHitted
  46.   ' If the main block is hit
  47.   If y > GraphicsWindow.Height - 29 And x >= Shapes.GetLeft(mainBlock) And x <= Shapes.GetLeft(mainBlock) + 100 Then
  48.     deltaY = -deltaY
  49.     mainBlockMiddleX = Shapes.GetLeft(mainBlock) + 50
  50.     If Shapes.GetLeft(ball) + 5 <= mainBlockMiddleX Then
  51.       If deltaX > 0 Then
  52.         deltaX = -deltaX
  53.       Else
  54.         deltaX = deltaX - 0.1
  55.       EndIf
  56.     ElseIf Shapes.GetLeft(ball) + 5 > mainBlockMiddleX Then
  57.       If deltaX > 0 Then
  58.         deltaX = deltaX + 0.1
  59.       Else
  60.         deltaX = -deltaX
  61.       EndIf
  62.     EndIf
  63.   ElseIf Shapes.GetTop(ball) > GraphicsWindow.Height - 12 Then
  64.     EndGame()
  65.   EndIf
  66. EndSub
  67.  
  68.  
  69. Sub CheckIfBrickIsHitted
  70.   For row = 1 To numberOfRows
  71.     For col = 1 To 5
  72.       xCoord = 50 + (col - 1) * 100
  73.       yCoord = 25 * row
  74.       arrayName = "Row" + row
  75.       If (y > yCoord - 10 And y < yCoord + 12) And (x >= xCoord And x <= xCoord + 100) Then
  76.         If Array.GetValue(arrayName, col) = 1 Then
  77.           Array.SetValue(arrayName, col, 0)
  78.           Sound.PlayClick()
  79.          
  80.           GraphicsWindow.PenColor = "Black"
  81.           GraphicsWindow.BrushColor = "Black"
  82.           GraphicsWindow.FillRectangle(xCoord, yCoord, 65, 12)
  83.           GraphicsWindow.DrawRectangle(xCoord, yCoord, 65, 12)
  84.           deltaY = -deltaY
  85.           totalBricks = totalBricks - 1
  86.           score = score + 1
  87.           PrintScore()
  88.         EndIf
  89.       EndIf  
  90.     EndFor
  91.   EndFor
  92. EndSub
  93.  
  94.  
  95. Sub InitializeComponents
  96.   'Bricks
  97.   For row = 1 To numberOfRows
  98.     For col = 1 To 5
  99.       xCoord = 50 + (col - 1) * 100
  100.       yCoord = 25 * row
  101.       GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
  102.       GraphicsWindow.FillRectangle(xCoord, yCoord, 65, 12)
  103.      
  104.       arrayName = "Row" + row
  105.       Array.SetValue(arrayName, col, 1)
  106.      
  107.       totalBricks = totalBricks + 1
  108.     EndFor
  109.   EndFor
  110.    
  111.   'Main Block
  112.   mainBlockXCoord = 240
  113.   mainBlock = Shapes.AddRectangle(100, 15)
  114.   Shapes.Move(mainBlock, mainBlockXCoord, 480)
  115.   Mouse.ShowCursor()
  116.   GraphicsWindow.MouseMove = OnMouseMove
  117.   PrintScore()
  118.  
  119.   'Ball
  120.   ball = Shapes.AddEllipse(10, 10)
  121.   x = 275.5
  122.   y = GraphicsWindow.Height - 50
  123.   Shapes.Move(ball, 275.5, 425)
  124.    
  125.   GraphicsWindow.KeyDown = OnKeyDown
  126.   GraphicsWindow.KeyUp = OnKeyDown
  127. EndSub
  128.  
  129. Sub PrintScore
  130.   GraphicsWindow.BrushColor = "Black"
  131.   GraphicsWindow.FillRectangle(10, 5, 100, 20)
  132.   GraphicsWindow.BrushColor = "White"
  133.   GraphicsWindow.DrawText(5, 5, "Score: " + score)
  134. EndSub
  135.  
  136. Sub OnMouseMove
  137.   If GraphicsWindow.MouseX >= 50 And GraphicsWindow.Mousex <= 515 Then
  138.     Shapes.Move(mainBlock, GraphicsWindow.MouseX - 50, 480)
  139.   EndIf
  140.   Program.Delay(5)
  141. EndSub
  142.  
  143. Sub OnKeyDown
  144.   keyPress = GraphicsWindow.LastKey
  145.   shapeXCoord = Shapes.GetLeft(mainBlock)
  146.  
  147.   If keyPress = "Left" Then
  148.     shapeXCoord = shapeXCoord - 10
  149.   ElseIf keyPress = "Right" Then
  150.     shapeXCoord = shapeXCoord + 10
  151.   EndIf  
  152.  
  153.   Shapes.Move(mainBlock, shapeXCoord, 480)
  154.   Program.Delay(2)
  155.  
  156.   If keyPress = "Escape" Then
  157.     Program.End()
  158.   EndIf
  159. EndSub
  160.  
  161. Sub EndGame
  162.   Sound.PlayBellRing()
  163.   GraphicsWindow.Clear()
  164.   GraphicsWindow.FontSize = 30
  165.   GraphicsWindow.BrushColor = "Red"
  166.   GraphicsWindow.DrawText(200, 185, "Game Over!")
  167.   GraphicsWindow.FontSize = 25
  168.   GraphicsWindow.DrawText(195, 235, "Your score is: " +  score)
  169.   Program.Delay(3000)
  170.   Program.End()
  171. EndSub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement