#!/usr/bin/ruby#init cursesrequire "ncurses"Ncurses::initscr#CONFIGNcurses::nlNcurses::noechoNcurses::curs_set(0)Ncurses::nodelay(Ncurses::stdscr, true)Ncurses::rawNcurses::keypad(Ncurses::stdscr,1)Ncurses::start_colorNcurses::init_pair(1,Ncurses::COLOR_GREEN, Ncurses::COLOR_RED)Ncurses::init_pair(2,Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK)my_window = Ncurses::WINDOW.new(24,75,0,0)#the pong classclass Pong attr_accessor :x, :y, :keyUP, :keyDOWN, :length, :score def move(direction) if (direction == 1) @y = @y-1 elsif (direction == 2) @y = @y+1 end endendclass Ball attr_accessor :x, :y, :xspeed, :yspeed, :oldy, :oldx def move @oldx = @x @oldy = @y @x = @x + @xspeed @y = @y + @yspeed endend#create a pongfirstpong = Pong.newfirstpong.x = 4firstpong.y = 4firstpong.keyUP = "KEY_UP"firstpong.keyDOWN = "KEY_DOWN"firstpong.length = 6firstpong.score = 0#and another onesecondpong = Pong.newsecondpong.x = 70secondpong.y = 15secondpong.keyUP = "KEY_LEFT"secondpong.keyDOWN= "KEY_RIGHT"secondpong.length = 6secondpong.score = 0#okay now we have the pongs. #lets put a ball on the field!ball = Ball.newball.x = 15ball.y = 10ball.xspeed=1ball.yspeed=1#main looppause = falsebegin case(Ncurses.getch) when "q"[0] break when "p"[0] if pause pause = false else pause = true end when Ncurses::KEY_UP secondpong.move(1) when Ncurses::KEY_DOWN secondpong.move(2) when "a"[0] firstpong.move(1) when "z"[0] firstpong.move(2) end #do some magic ball things, like move it and why do we do that? because WE LIKE TO MOVE IT MOVE IT, WE LIKE TO MOVE IT MOVE IT if(!pause) ball.move #check if ball hits wall if(ball.x >= 72) ball.xspeed = -1 elsif(ball.x <=1) ball.xspeed=1 elsif(ball.y <= 2) ball.yspeed = 1 elsif(ball.y >= 22) ball.yspeed = -1 end #check if ball hits a pong if(ball.x == firstpong.x) if(ball.y >= firstpong.y && ball.y <= firstpong.y + firstpong.length) ball.xspeed *= -1; end elsif(ball.x == secondpong.x) if(ball.y >=secondpong.y && ball.y <= secondpong.y + secondpong.length) ball.xspeed *= -1; end #check if ball hits a goal #uglyhack - goal starts at x 0 and x74, and its 15 long, both start at y5 later do this dynamical elsif(ball.x == 1 && ball.y >= 5 && ball.y<= 5 + 15 ) secondpong.score += 1 ball.x = 25 elsif(ball.x == 72 && ball.y >=5 && ball.y<= 5 + 15 ) firstpong.score += 1 ball.x = 25 end #draw things my_window.border(*([0]*8)) my_window.color_set(2,nil) #draw a black square on the old ball location my_window.mvaddstr(ball.oldy , ball.oldx , " ") my_window.mvaddstr(ball.y,ball.x, "*") #do the PONGS! c=0 my_window.mvaddstr(firstpong.y - 1, firstpong.x, " "); my_window.color_set(1,nil) begin my_window.mvaddstr(firstpong.y + c,firstpong.x, " ") c = c+1 end while c<firstpong.length my_window.color_set(2,nil) my_window.mvaddstr(firstpong.y + c , firstpong.x, " ") #second pong my_window.color_set(2,nil) my_window.mvaddstr(secondpong.y - 1, secondpong.x , " ") c=0 my_window.color_set(1,nil) begin my_window.mvaddstr(secondpong.y + c,secondpong.x, " ") c=c+1 end while c<secondpong.length my_window.color_set(2,nil) my_window.mvaddstr(secondpong.y + c, secondpong.x, " ") #and we need some goals as well. otherwise it is impossible to wine the game! i = 0 begin my_window.mvaddstr(5 + i, 0, " ") my_window.mvaddstr(5 + i, 74, " ") i+=1 end while i < 15 #write out what we have here now my_window.mvaddstr(23, 4, "| Ping: " + firstpong.score.to_s + " | Pong: " + secondpong.score.to_s + " | q to quit, p to pause") #update screen my_window.noutrefresh() Ncurses::doupdate sleep(0.05) endend while trueNcurses::echoNcurses::nocbreakNcurses::curs_set(1)Ncurses::endwin