sWaffle7982

pong

Aug 6th, 2021 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. --[[===================
  2.  
  3. Made by: Nemo Eriksson
  4. Pastebin: BTfHqkdW
  5. Description: A simple auto played pong game
  6.  
  7. ====================]]--
  8.  
  9. -- Recommended monitor size: 4x3
  10.  
  11. -- All of the pong game
  12. function main()
  13.   -- Gets the monitor
  14.   local monitor = peripheral.find('monitor')
  15.   monitor.setTextScale(4)
  16.  
  17.   -- Gets the speaker
  18.   local speaker = peripheral.find('speaker')
  19.  
  20.   -- Returns error if no monitor was found
  21.   if not monitor then
  22.     print('ERROR: No monitor found')
  23.     return false
  24.   end
  25.  
  26.   -- Returns error if no speaker was found
  27.   if not speaker then
  28.     print('ERROR: No speaker found')
  29.     return false
  30.   end
  31.  
  32.   -- Game variables
  33.   local boardSymbol = '|'
  34.   local p1x, p1y = 1, 3
  35.   local p2x, p2y = 10, 3
  36.   local screenWidth, screenHeight = monitor.getSize()
  37.   local moveRight = true
  38.   local collisionSound = 'minecraft:entity.experience_orb.pickup'
  39.   local playSound = false
  40.  
  41.   -- Ball variables
  42.   local defaultBallIcon = 'o'
  43.   local ballIcon = defaultBallIcon
  44.   local xVel, yVel = 1, 1
  45.   local ballX, ballY = 5, 3
  46.  
  47.   -- Print scores to the computer terminal
  48.   print('===============')
  49.   print('SCORES: ')
  50.   print('Player 1: 0')
  51.   print('Player 2: 0')
  52.   print('===============')
  53.  
  54.   -- Main loop
  55.   while true do
  56.     monitor.clear()
  57.  
  58.     -- Draw player 1 board on screen
  59.     monitor.setCursorPos(p1x, p1y)
  60.     monitor.write(boardSymbol)
  61.  
  62.     -- Draws player 2 board on screen
  63.     monitor.setCursorPos(p2x, p2y)
  64.     monitor.write(boardSymbol)
  65.  
  66.     -- Draws the ball on screen
  67.     monitor.setCursorPos(ballX, ballY)
  68.     monitor.write(ballIcon)
  69.  
  70.     -- Plays sound when
  71.     if playSound then
  72.       speaker.playSound(collisionSound)
  73.       playSound = false
  74.     end
  75.  
  76.     -- Gets next position of the ball
  77.     ballX, ballY = ballX + xVel, ballY + yVel
  78.  
  79.     -- Inverts ball X velocity if it has collided
  80.     if ballX >= screenWidth-1 or ballX <= 2 then
  81.       xVel = xVel * -1
  82.       playSound = true
  83.     end
  84.  
  85.     -- Inverts ball Y velocity if it has collided
  86.     if ballY >= screenHeight or ballY <= 1 then
  87.       yVel = yVel * -1
  88.     end
  89.  
  90.     -- Updates player 1 positions
  91.     if not moveRight then
  92.       p1y = ballY
  93.     end
  94.  
  95.     -- Updates player 2 positions
  96.     if moveRight then
  97.       p2y = ballY
  98.     end
  99.  
  100.     moveRight = xVel > 0
  101.     os.sleep(1/3)
  102.   end
  103. end
  104.  
  105. -- Start screen | loading screen
  106. function startScreen()
  107.   -- Gets monitor
  108.   local monitor = peripheral.find('monitor')
  109.   monitor.setTextScale(2)
  110.  
  111.   -- Returns error if no monitor is connected to computer
  112.   if not monitor then
  113.     print('ERROR: No monitor found')
  114.     return false
  115.   end
  116.  
  117.   -- Loading screen variables
  118.   local loadingIterations = 1
  119.   local dotAmount = 5
  120.  
  121.   -- Loading screen that loades nothing
  122.   for i = 1, loadingIterations, 1 do
  123.     monitor.clear()
  124.  
  125.     -- Write Pong on monitor so user knows what is loading
  126.     monitor.setCursorPos(8,2)
  127.     monitor.write('Pong!')
  128.  
  129.     -- Write Loading in middle of monitor
  130.     monitor.setCursorPos(7,6)
  131.     monitor.write('Loading')
  132.  
  133.     -- Adds the dots
  134.     for i = 1, dotAmount, 1 do
  135.       os.sleep(1/3)
  136.       monitor.write('.')
  137.     end
  138.    
  139.     os.sleep(0.25)
  140.   end
  141. end
  142.  
  143. -- Main
  144. shell.run('clear')
  145. print('Made by Nemo Eriksson  -  u/_n3m0__')
  146. startScreen()
  147. main()
  148.  
Add Comment
Please, Sign In to add comment