Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- c1 = " Easy "
- c2 = "Medium"
- c3 = " Hard "
- c4 = " Comp "
- c5 = " Quit "
- local w,h = term.getSize()
- ms = 1
- m = true
- loc = 23
- trim = 21
- ti = 0.12
- local tiID = os.startTimer(ti)
- titleball = {
- xpos = math.floor(w/2);
- ypos = math.floor(h/2);
- xvel = -1;
- yvel = -1;
- update = function(self)
- self.xpos = self.xpos + self.xvel
- self.ypos = self.ypos + self.yvel
- if self.xpos <=1 and self.xvel == -1 then
- self.xvel = self.xvel * -1
- elseif self.xpos >=w and self.xvel == 1 then
- self.xvel = self.xvel * -1
- elseif self.ypos <=3 and self.yvel == -1 then
- self.yvel = self.yvel * -1
- elseif self.ypos >=h-2 and self.yvel == 1 then
- self.yvel = self.yvel * -1
- end
- end;
- draw = function(self)
- term.current().setVisible(false)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(self.xpos,self.ypos)
- term.write(" ")
- term.current().setVisible(true)
- end
- }
- function draw()
- pm()
- titleball:draw()
- end
- function update()
- local event, button = os.pullEvent()
- if event == "key" then
- mys(button)
- end
- if event == "timer" and button == tiID then
- titleball:update()
- tiID = os.startTimer(ti)
- end
- end
- function pm()
- term.current().setVisible(false)
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.green)
- term.clear()
- bg = paintutils.loadImage("pongworld")
- paintutils.drawImage(bg,1,1)
- term.setBackgroundColor(colors.lime)
- term.setCursorPos(loc,5)
- print(c1)
- term.setCursorPos(loc,6)
- print(c2)
- term.setCursorPos(loc,7)
- print(c3)
- term.setCursorPos(loc,8)
- print(c4)
- term.setCursorPos(loc,9)
- print(c5)
- if ms == 1 then
- term.setCursorPos(trim,5)
- print("[ "..c1.." ]")
- elseif ms == 2 then
- term.setCursorPos(trim,6)
- print("[ "..c2.." ]")
- elseif ms == 3 then
- term.setCursorPos(trim,7)
- print("[ "..c3.." ]")
- elseif ms == 4 then
- term.setCursorPos(trim,8)
- print("[ "..c4.." ]")
- elseif ms == 5 then
- term.setCursorPos(trim,9)
- print("[ "..c5.." ]")
- end
- term.current().setVisible(true)
- mys()
- end
- term.setTextColor(colors.white)
- function mys(button)
- if button == keys.up then
- if ms ~= 1 then
- ms = ms - 1
- pm()
- end
- end
- if button == keys.down then
- if ms ~= 5 then
- ms = ms + 1
- pm()
- end
- end
- if button == keys.enter then
- if ms == 1 then
- gameRate = 0.16
- run = true
- ball:update()
- gtID = os.startTimer(gameRate)
- return gameLoop()
- elseif ms == 2 then
- gameRate = 0.12
- run = true
- ball:update()
- gtID = os.startTimer(gameRate)
- return gameLoop()
- elseif ms == 3 then
- gameRate = 0.06
- run = true
- ball:update()
- gtID = os.startTimer(gameRate)
- return gameLoop()
- elseif ms == 4 then
- gameRate = 0.12
- cRate = 0.16
- run = true
- ball:update()
- gtID = os.startTimer(gameRate)
- comp = true
- coID = os.startTimer(cRate)
- return compLoop()
- elseif ms == 5 then
- m = false
- return
- end
- end
- end
- function loop()
- while m do
- draw()
- update()
- end
- end
- -------------------------------------------------------------------
- run = true
- local score = 0
- p1p = 0
- p2p = 0
- wp = 10
- ball = {
- xpos = math.max(math.random(3,40),0);
- ypos = math.max(math.random(3,15),0);
- xvel = -1;
- yvel = -1;
- update = function(self)
- self.xpos = self.xpos + self.xvel
- self.ypos = self.ypos + self.yvel
- if self.ypos >= bat.ypos-1 and self.ypos <= bat.ypos + bat.height and self.xpos == bat.xpos+1 then
- self.xvel = self.xvel * -1
- end
- if self.ypos >= bat2.ypos-1 and self.ypos <= bat2.ypos + bat2.height and self.xpos == bat2.xpos-1 then
- self.xvel = self.xvel * -1
- end
- if self.xpos <=1 and self.xvel == -1 then
- if p2p == wp-1 then
- textx = math.floor(w/2-8);
- term.setCursorPos(textx,5)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- print "Player 2 Wins!"
- sleep(1)
- run = false
- m = false
- else
- p2p = p2p + 1
- term.setBackgroundColor(colors.red)
- term.clear()
- sleep(1)
- self.xpos = math.max(math.random(3,40),0);
- self.ypos = math.max(math.random(3,15),0);
- term.setBackgroundColor(colors.green)
- end
- elseif self.xpos >=w and self.xvel == 1 then
- if p1p == wp-1 then
- p1p = p1p + 1
- drawGame()
- textx = math.floor(w/2-8);
- term.setCursorPos(textx,5)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- print "Player 1 Wins!"
- sleep(1)
- run = false
- m = false
- else
- p1p = p1p + 1
- term.setBackgroundColor(colors.red)
- term.clear()
- sleep(1)
- self.xpos = math.max(math.random(3,40),0);
- self.ypos = math.max(math.random(3,15),0);
- term.setBackgroundColor(colors.green)
- end
- elseif self.ypos <=3 and self.yvel == -1 then
- self.yvel = self.yvel * -1
- elseif self.ypos >=h-2 and self.yvel == 1 then
- self.yvel = self.yvel * -1
- end
- end;
- draw = function(self)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(self.xpos,self.ypos)
- term.write(" ")
- end
- }
- bat = {
- xpos = 1;
- ypos = 1;
- height = 4;
- update = function(self,key)
- if key == keys.w and self.ypos ~=3 then
- self.ypos = self.ypos - 1
- elseif key == keys.s and self.ypos ~= 18-self.height then
- self.ypos = self.ypos + 1
- end
- end;
- updatedrag = function(self,_y)
- if _y ~=3 and _y ~= 18-self.height then
- self.ypos = _y
- end
- end;
- draw = function (self)
- term.setBackgroundColor(colors.blue)
- term.setCursorPos(self.xpos,self.ypos)
- term.setTextColor(colors.black)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+1)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+2)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+3)
- term.write(" ")
- end
- }
- bat2 = {
- xpos = w;
- ypos = 1;
- height = 4;
- update = function(self,key)
- if key == keys.up and self.ypos ~=3 then
- self.ypos = self.ypos - 1
- elseif key == keys.down and self.ypos ~= 18-self.height then
- self.ypos = self.ypos + 1
- end
- end;
- updatedrag = function(self,_y)
- if _y ~=3 and _y ~= 18-self.height then
- self.ypos = _y
- end
- end;
- draw = function (self)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.black)
- term.setCursorPos(self.xpos,self.ypos)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+1)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+2)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+3)
- term.write(" ")
- end
- }
- -- if self.ypos == 3 then
- --self.ypos = self.ypos - 1
- --elseif self.ypos == 18-self.height then
- --self.ypos = self.ypos + 1
- --else
- cball = {
- xpos = math.max(math.random(3,40),0);
- ypos = math.max(math.random(3,15),0);
- xvel = -1;
- yvel = -1;
- update = function(self)
- self.xpos = self.xpos + self.xvel
- self.ypos = self.ypos + self.yvel
- if self.ypos >= bat.ypos-1 and self.ypos <= bat.ypos + bat.height and self.xpos == bat.xpos+1 then
- self.xvel = self.xvel * -1
- end
- if self.ypos >= comP.ypos-1 and self.ypos <= comP.ypos + comP.height and self.xpos == comP.xpos-1 then
- self.xvel = self.xvel * -1
- end
- if self.xpos <=1 and self.xvel == -1 then
- if p2p == wp-1 then
- textx = math.floor(w/2-8);
- term.setCursorPos(textx,5)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- print "Computer Wins!"
- sleep(1)
- run = false
- m = false
- else
- p2p = p2p + 1
- term.setBackgroundColor(colors.red)
- term.clear()
- sleep(1)
- self.xpos = math.max(math.random(3,40),0);
- self.ypos = math.max(math.random(3,15),0);
- term.setBackgroundColor(colors.green)
- gtID = os.startTimer(gameRate)
- coID = os.startTimer(cRate)
- return compLoop()
- end
- elseif self.xpos >=w and self.xvel == 1 then
- if p1p == wp-1 then
- p1p = p1p + 1
- drawGame()
- textx = math.floor(w/2-8);
- term.setCursorPos(textx,5)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- print "Player 1 Wins!"
- sleep(1)
- run = false
- m = false
- else
- p1p = p1p + 1
- term.setBackgroundColor(colors.red)
- term.clear()
- sleep(1)
- self.xpos = math.max(math.random(3,40),0);
- self.ypos = math.max(math.random(3,15),0);
- term.setBackgroundColor(colors.green)
- gtID = os.startTimer(gameRate)
- coID = os.startTimer(cRate)
- return compLoop()
- end
- elseif self.ypos <=3 and self.yvel == -1 then
- self.yvel = self.yvel * -1
- elseif self.ypos >=h-2 and self.yvel == 1 then
- self.yvel = self.yvel * -1
- end
- end;
- draw = function(self)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(self.xpos,self.ypos)
- term.write(" ")
- end
- }
- comP = {
- xpos = w;
- ypos = 1;
- height = 4;
- update = function(self,key)
- lose = math.max(math.random(1,5),0)
- if cball.yvel == 1 then
- if lose == 4 or lose == 5 then
- if self.ypos <= 3 then
- self.ypos = 4
- end
- else
- if self.ypos <= 3 then
- self.ypos = 4
- else
- self.ypos = self.ypos + 1
- end
- end
- elseif cball.yvel == -1 then
- if lose == 4 or lose == 5 then
- if comP.ypos >= 14 then
- comP.ypos = 13
- end
- else
- if self.ypos >= 14 then
- self.ypos = 13
- else
- self.ypos = self.ypos - 1
- end
- end
- end
- end;
- draw = function (self)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.black)
- term.setCursorPos(self.xpos,self.ypos)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+1)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+2)
- term.write(" ")
- term.setCursorPos(self.xpos,self.ypos+3)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- end
- }
- function drawComp()
- term.current().setVisible(false)
- term.setBackgroundColor(colors.green)
- term.clear()
- bg = paintutils.loadImage("pongworld")
- paintutils.drawImage(bg,1,1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(22,4)
- print(""..p1p)
- term.setCursorPos(25,4)
- print(""..p2p)
- cball:draw()
- bat:draw()
- comP:draw()
- term.current().setVisible(true)
- end
- function drawGame()
- term.current().setVisible(false)
- term.setBackgroundColor(colors.green)
- term.clear()
- bg = paintutils.loadImage("pongworld")
- paintutils.drawImage(bg,1,1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(22,4)
- print(""..p1p)
- term.setCursorPos(25,4)
- print(""..p2p)
- ball:draw()
- bat:draw()
- bat2:draw()
- term.current().setVisible(true)
- end
- function gameUpdate()
- local event, button, _,my = os.pullEvent()
- if event == "timer" and button == gtID then
- ball:update()
- gtID = os.startTimer(gameRate)
- end
- if event == "key" then
- bat:update(button)
- bat2:update(button)
- if button == keys.backspace then run = false m = false end
- end
- end
- function compUpdate()
- local event, button, _,_y = os.pullEvent()
- if event == "timer" and button == gtID then
- cball:update()
- gtID = os.startTimer(gameRate)
- end
- if event == "mouse_drag" then
- bat:updatedrag(_y)
- end
- if event == "timer" and button == coID then
- if comP.ypos >= 14 then
- comP.ypos = 13
- end
- comP:update()
- coID = os.startTimer(cRate)
- end
- if event == "key" then
- bat:update(button)
- if button == keys.backspace then run = false m = false end
- end
- end
- function compLoop()
- while run == true do
- m = false
- drawComp()
- compUpdate()
- end
- while not run do
- break
- end
- end
- function gameLoop()
- while run == true do
- m = false
- drawGame()
- gameUpdate()
- end
- while not run do
- break
- end
- end
- loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement