require("oct_utils") require("termbox_defs") math.randomseed(os.time()) OCT_LOG_INFO("Hello, world! From lua"); width = 74; height = 25; paddle_left = oct_tb_sprite_new(); paddle_right = oct_tb_sprite_new(); ball = oct_tb_sprite_new(); top_border = oct_tb_sprite_new(); bottom_border = oct_tb_sprite_new(); left_border = oct_tb_sprite_new(); right_border = oct_tb_sprite_new(); score_p1_sprite = oct_tb_sprite_new(); score_p2_sprite = oct_tb_sprite_new(); score_p1 = 0; score_p2 = 0; init_text = oct_tb_sprite_new(); border_topbottom = ""; dx = 0; dy = 0; delay = 10; counter = 0; for i=1,width,1 do border_topbottom = border_topbottom .. "-" end border_leftright = ""; for i=1,height-1,1 do border_leftright = border_leftright .. "|\n"; end -- Get rid of the last \n border_leftright = border_leftright:sub(1,-2); pause_game = 1; function pause() pause_game = 1; init_text["x"] = math.floor(width/2) - math.floor(string.len(init_text["shape"])/2); init_text["y"] = math.floor(height/2)+2; ball["x"] = math.floor(width/2); ball["y"] = math.floor(height/2); end function unpause() pause_game = 0; -- unpause game init_text["x"] = 1000; -- draw offscreen init_text["y"] = 1000; -- draw offscreen end function oct_init() paddle_left["shape"] = load_termbox_sprite("paddle.txt"); paddle_right["shape"] = load_termbox_sprite("paddle.txt"); ball["shape"] = load_termbox_sprite("ball.txt"); paddle_left["x"] = 1; paddle_left["y"] = math.floor(height/2)-2; paddle_left["fg"] = TB_RED; paddle_right["x"] = width-3; paddle_right["y"] = math.floor(height/2)-2; paddle_right["fg"] = TB_GREEN; top_border["shape"] = border_topbottom; bottom_border["shape"] = border_topbottom; top_border["x"] = 0; bottom_border["x"] = 0; top_border["y"] = 0; bottom_border["y"] = height; left_border["shape"] = border_leftright; left_border["x"] = 0; left_border["y"] = 1; right_border["shape"] = border_leftright; right_border["x"] = width-1; right_border["y"] = 1; score_p1_sprite["x"] = 0; score_p2_sprite["x"] = 0; score_p1_sprite["y"] = height+1; score_p2_sprite["y"] = height+2; score_p1_sprite["shape"] = "Player 1: " .. tostring(score_p1); score_p2_sprite["shape"] = "Player 2: " .. tostring(score_p2); init_text["shape"] = "Press SPACE to begin"; pause(); return OCT_NOT_NEEDS_NETWORKING, OCT_NEEDS_TERMBOX; end function oct_loop(key, ch) if (pause_game == 0) then -- handle keys if (key == TB_KEY_ARROW_UP and paddle_right["y"] >= 2) then paddle_right["y"] = paddle_right["y"] - 1; end if (key == TB_KEY_ARROW_DOWN and paddle_right["y"] <= height-5-1) then paddle_right["y"] = paddle_right["y"] + 1; end if (ch == string.byte("w") and paddle_left["y"] >= 2) then paddle_left["y"] = paddle_left["y"] - 1; end if (ch == string.byte("s") and paddle_left["y"] <= height-5-1) then paddle_left["y"] = paddle_left["y"] + 1; end counter = counter + 1; if (counter == delay) then counter = 0; -- update ball ball["x"] = ball["x"] + dx; ball["y"] = ball["y"] + dy; if (ball["y"] <= 1 or ball["y"] >= height-1) then dy = -dy; end if (ball["x"] <= 3) then if (ball["y"] >= paddle_left["y"] and ball["y"] <= paddle_left["y"] + 5) then dx = -dx; else score_p2 = score_p2 + 1; score_p2_sprite["shape"] = "Player 2: " .. score_p2; pause(); end end if (ball["x"] >= width-4) then if (ball["y"] >= paddle_right["y"] and ball["y"] <= paddle_right["y"] + 5) then dx = -dx; else score_p1 = score_p1 + 1; score_p1_sprite["shape"] = "Player 1: " .. score_p1; pause(); end end end else if (ch == string.byte(" ")) then unpause(); -- ensure the ball is always started moving in the x direction or game will stall dx = math.random(0, 1); if (dx == 0) then dx = -1 end dy = math.random(-1, 1); end end end