Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("right")
- modem.open(6660)
- -- Initialize game variables
- local BallX, BallY = 50, 20
- local BallVelX, BallVelY = 1, 1
- local P1Y, P2Y = 20, 20
- local P1Score, P2Score = 0, 0
- local P1Present, P2Present = false, false
- while true do
- -- Handle incoming messages
- local event, side, senderId, message, distance = os.pullEvent("modem_message")
- if senderId == 1 then
- local data = textutils.unserialize(message)
- if data then
- if data.action == "move" then
- if data.player == 1 then
- P1Y = data.y
- P1Present = true
- elseif data.player == 2 then
- P2Y = data.y
- P2Present = true
- end
- end
- end
- end
- -- Update ball position
- BallX = BallX + BallVelX
- BallY = BallY + BallVelY
- -- Ball collision with paddles
- if BallX == 1 and BallY >= P1Y and BallY <= P1Y + 5 then
- BallVelX = -BallVelX
- elseif BallX == 98 and BallY >= P2Y and BallY <= P2Y + 5 then
- BallVelX = -BallVelX
- end
- -- Ball collision with top/bottom
- if BallY <= 1 or BallY >= 38 then
- BallVelY = -BallVelY
- end
- -- Ball out of bounds
- if BallX <= 0 then
- P2Score = P2Score + 1
- BallX, BallY = 50, 20
- BallVelX, BallVelY = 1, 1
- elseif BallX >= 99 then
- P1Score = P1Score + 1
- BallX, BallY = 50, 20
- BallVelX, BallVelY = -1, -1
- end
- -- Broadcast game state
- modem.transmit(1, "all", textutils.serialize({
- ball = {x = BallX, y = BallY},
- paddle1 = P1Y,
- paddle2 = P2Y,
- score1 = P1Score,
- score2 = P2Score,
- present1 = P1Present,
- present2 = P2Present
- }))
- -- Reset presence flags
- P1Present, P2Present = false, false
- -- Sleep to control game speed
- sleep(0.05)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement