Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[===================
- Made by: Nemo Eriksson
- Pastebin: BTfHqkdW
- Description: A simple auto played pong game
- ====================]]--
- -- Recommended monitor size: 4x3
- -- All of the pong game
- function main()
- -- Gets the monitor
- local monitor = peripheral.find('monitor')
- monitor.setTextScale(4)
- -- Gets the speaker
- local speaker = peripheral.find('speaker')
- -- Returns error if no monitor was found
- if not monitor then
- print('ERROR: No monitor found')
- return false
- end
- -- Returns error if no speaker was found
- if not speaker then
- print('ERROR: No speaker found')
- return false
- end
- -- Game variables
- local boardSymbol = '|'
- local p1x, p1y = 1, 3
- local p2x, p2y = 10, 3
- local screenWidth, screenHeight = monitor.getSize()
- local moveRight = true
- local collisionSound = 'minecraft:entity.experience_orb.pickup'
- local playSound = false
- -- Ball variables
- local defaultBallIcon = 'o'
- local ballIcon = defaultBallIcon
- local xVel, yVel = 1, 1
- local ballX, ballY = 5, 3
- -- Print scores to the computer terminal
- print('===============')
- print('SCORES: ')
- print('Player 1: 0')
- print('Player 2: 0')
- print('===============')
- -- Main loop
- while true do
- monitor.clear()
- -- Draw player 1 board on screen
- monitor.setCursorPos(p1x, p1y)
- monitor.write(boardSymbol)
- -- Draws player 2 board on screen
- monitor.setCursorPos(p2x, p2y)
- monitor.write(boardSymbol)
- -- Draws the ball on screen
- monitor.setCursorPos(ballX, ballY)
- monitor.write(ballIcon)
- -- Plays sound when
- if playSound then
- speaker.playSound(collisionSound)
- playSound = false
- end
- -- Gets next position of the ball
- ballX, ballY = ballX + xVel, ballY + yVel
- -- Inverts ball X velocity if it has collided
- if ballX >= screenWidth-1 or ballX <= 2 then
- xVel = xVel * -1
- playSound = true
- end
- -- Inverts ball Y velocity if it has collided
- if ballY >= screenHeight or ballY <= 1 then
- yVel = yVel * -1
- end
- -- Updates player 1 positions
- if not moveRight then
- p1y = ballY
- end
- -- Updates player 2 positions
- if moveRight then
- p2y = ballY
- end
- moveRight = xVel > 0
- os.sleep(1/3)
- end
- end
- -- Start screen | loading screen
- function startScreen()
- -- Gets monitor
- local monitor = peripheral.find('monitor')
- monitor.setTextScale(2)
- -- Returns error if no monitor is connected to computer
- if not monitor then
- print('ERROR: No monitor found')
- return false
- end
- -- Loading screen variables
- local loadingIterations = 1
- local dotAmount = 5
- -- Loading screen that loades nothing
- for i = 1, loadingIterations, 1 do
- monitor.clear()
- -- Write Pong on monitor so user knows what is loading
- monitor.setCursorPos(8,2)
- monitor.write('Pong!')
- -- Write Loading in middle of monitor
- monitor.setCursorPos(7,6)
- monitor.write('Loading')
- -- Adds the dots
- for i = 1, dotAmount, 1 do
- os.sleep(1/3)
- monitor.write('.')
- end
- os.sleep(0.25)
- end
- end
- -- Main
- shell.run('clear')
- print('Made by Nemo Eriksson - u/_n3m0__')
- startScreen()
- main()
Add Comment
Please, Sign In to add comment