double g_accelerate = 1.0125; // get dimensions vector g_full = System.GetScreenMode(); vector g_m.Set(640, 480, 0); //= ScreenMode(); g_m = g_full; // do networking //return a.ReadString(); vector g_size.Set(g_m.x * 0.03, g_m.y * 0.25,0); point a_wpos = (g_full - g_m) * 0.5; // centre window // make a window to paint screen to with window win { Create(640,480); SetText("Pong!"); FullScreen(true); ShowCursor(false); Show(true); } var& scr = win.BackBuffer();; // make a screen to draw to and resize it with scr { //Resize(g_m.x, g_m.y); SetMonochrome(true); } double g_startTime = System.Timer(); int g_frames = 0; //scr.SetColour(false); scr.SetTint(color(0,0.25,0), color(0.4,0.4,0.5), color(1,1,1));//vecto0, 0.5, 1); int a_score[] = [0,0]; int a_serve = 1; double doBeep = 0; function DoBeep() { doBeep += 1; } while (a_score[0] < 5 && a_score[1] < 5 && win.IsValid()) { g_full = g_m = win.GetDims(); if (a_serve == 0) a_serve = 1; scr.Cls(); win.Paint(); point bpos = g_m * 0.5;//.{x = 0; y = g_m.y * 0.5}; // centre of the screen point bvel = point(0,0); time g_lastTime.GetLocal(); //double g_lastTime = System.Timer(); bool a_hit = 0; double a_pos[2]; // set up the 2 paddle a_pos[0] = a_pos[1] = 0.5; rect a_lastBall; while (win.IsValid()) { scr.bg = Math.sin(doBeep*2) * 0.25;// ? 1 : 0; doBeep -= 0.05; if (doBeep < 0) doBeep = 0; scr.Cls(); System.Refresh(); int a_me = 0;// == 2 ? 1 : 0; int keypress = win.GetKey(); if (keypress == 27) // ESC return; if (a_serve != 0 && keypress > 0) { bvel.(x = g_m.x * 0.5, y = (Math.random()-0.5) * g_m.x * 0.1); a_hit = 1; if (a_serve == 2) bvel.(x = -x); a_serve = 0; } a_pos[a_me] = System.GetCursorPos().x / g_full.x; a_pos[1] = 1 - a_pos[0]; var pos = a_pos[0] * (g_m.y - g_size.y); var pos2 = a_pos[1] * (g_m.y - g_size.y); time a_thisTime.GetLocal(); double a_t = a_thisTime - g_lastTime; // get elapsed time g_lastTime = a_thisTime; //double a_t = System.Timer() - g_lastTime; // get elapsed time //a_t = 0.05; //g_lastTime += a_t; // bvel += g_grav * a_t; double a_speed = bvel.Magnitude(); vector a_step = bvel * a_t; if (bvel.x > 0) // hit right side? { if (bpos.x > g_m.x - g_size.x) // edge of screen { a_score[0]++; a_serve = 2; break; } else if (bpos.x + a_step.x > g_m.x - g_size.x * 3) // will a bat hit it? { if (bpos.y + g_size.x > pos2 && bpos.y < pos2 + g_size.y) { DoBeep(); a_score[0]++; a_speed *= g_accelerate; bvel.x = -bvel.x; bvel.y = (bvel.y + (Math.random() - 0.5) * bvel.x * 4) * 0.5; } } } else if (bvel.x < 0) { if (bpos.x < 0) // edge of screen { a_score[1]++; a_serve = 1; break; } else if (bpos.x + a_step.x < g_size.x * 2) // will a bat hit it? { if (bpos.y + g_size.x > pos && bpos.y < pos + g_size.y) { DoBeep(); a_score[1]++; a_speed *= g_accelerate; bvel.x = -bvel.x; bvel.y = (bvel.y + (Math.random() - 0.5) * bvel.x * 4) * 0.5; } } } if ((bvel.y > 0 && bpos.y > g_m.y - g_size.x) || (bvel.y < 0 && bpos.y < 0)) { a_speed *= g_accelerate; bvel.y = -bvel.y; } bvel = !bvel * a_speed; bpos += a_step; if (a_serve == 1) bpos.( x = g_size.x * 2, y = pos + g_size.y * 0.5); if (a_serve == 2) bpos.( x = g_m.x - g_size.x * 3, y = pos2 + g_size.y * 0.5); // now render the bits rect a_rcBall = rect(bpos.x, bpos.y, bpos.x + g_size.x , bpos.y + g_size.x); rect a_rcBat1 = rect(g_size.x, pos, g_size.x * 2, pos + g_size.y); rect a_rcBat2 = rect(g_m.x - g_size.x * 2 , pos2, g_m.x - g_size.x, pos2 + g_size.y); with scr { rgb.Set(1,1,1); Oblong(a_rcBall); Oblong(a_rcBat1); Oblong(a_rcBat2); //rgb.Set(0.5,0.5,0.5); if (true) { rect a_rcScore1 = rect(0,0,g_m.x, g_m.y) * rect(0.35, 0.025, 0.45, 0.125); rect a_rcScore2 = rect(0,0,g_m.x, g_m.y) * rect(0.55, 0.025, 0.65, 0.125); DrawDigit(a_score[0], a_rcScore1); DrawDigit(a_score[1], a_rcScore2); } else { rect a_rcScore = rect(0,0,g_m.x, g_m.y) * rect(0.45, 0.025, 0.55, 0.125); DrawDigit(a_score[0] + a_score[1], a_rcScore); } } win.Paint(); // erase the bits in the back buffer (immediately after we have updated the screen with them) a_lastBall = a_rcBall; g_frames++; System.Wait(0.01); // allow some system time } } if (win.IsValid()) // dont wait if window is closed System.Wait(2); return g_frames / (System.Timer() - g_startTime); /* // the pattern for drawing the numbers (see below) 1 2 4 8 16 32 64 */ // add a digit drawing thing function screen::DrawDigit(int p_n, rect p_r) { var& a = static [ 127 - 8, // 0 4 + 32, // 1 127 - 2 - 32, //2 127 - 2 - 16, // 3 2 + 4 + 8 + 32, //4 127 - 4 - 16, // 5 127 - 4, // 6 1 + 4 + 32, // 7 127, // 8 127 - 16 // 9 ]; int a_n = a[p_n % 10]; if (a_n & 1) Block(p_r * rect(0,0,1, 0.2)); if (a_n & 2) Block(p_r * rect(0,0,0.3, 0.5)); if (a_n & 4) Block(p_r * rect(0.7,0,1, 0.5)); if (a_n & 8) Block(p_r * rect(0,0.4,1, 0.6)); if (a_n & 16) Block(p_r * rect(0,0.5,0.3, 1)); if (a_n & 32) Block(p_r * rect(0.7,0.5,1, 1)); if (a_n & 64) Block(p_r * rect(0,0.8,1, 1)); }