fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
Success #stdin #stdout 0.08s 54652KB
stdin
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Endless Runner — Demo</title>
<style>
  :root {
    --bg:#87CEEB;
    --ground:#4b3b2f;
    --ui:#ffffffcc;
  }
  html,body{height:100%;margin:0;font-family:system-ui,-apple-system,Segoe UI,Roboto,"Helvetica Neue",Arial;}
  #gameWrap{display:flex;align-items:center;justify-content:center;height:100%;background:linear-gradient(#7ec8ff 0%, var(--bg) 70%);overflow:hidden}
  canvas{background:transparent;display:block;border-radius:12px;box-shadow:0 8px 30px rgba(0,0,0,.25)}
  #ui {
    position: absolute; top:18px; left:18px; color:#111; font-weight:600;
    background:var(--ui); padding:.5rem .8rem; border-radius:8px;
    box-shadow:0 6px 16px rgba(0,0,0,.12);
  }
  #tip { position:absolute; bottom:18px; left:18px; color:#111; background:var(--ui); padding:.45rem .7rem;border-radius:8px;}
  #overlay {
    position:absolute; inset:0; display:flex; align-items:center; justify-content:center; pointer-events:none;
  }
  .centerBox{pointer-events:auto; background:#fff; padding:18px 22px; border-radius:12px; box-shadow:0 8px 30px rgba(0,0,0,.25); text-align:center;}
  button{padding:10px 14px;border-radius:8px;border:0;background:#0a84ff;color:#fff;font-weight:700;cursor:pointer}
  small{display:block;opacity:.8;margin-top:6px}
</style>
</head>
<body>
<div id="gameWrap">
  <canvas id="game" width="720" height="1280"></canvas>

  <div id="ui">Score: <span id="score">0</span> &nbsp; Speed: <span id="speed">1</span>x</div>
  <div id="tip">Controls: ← → to change lanes, ↑ or Space to jump. Swipe on mobile.</div>

  <div id="overlay" style="display:none;">
    <div class="centerBox">
      <h2 id="msgTitle">Game Over</h2>
      <p id="msgText">Your score: <span id="finalScore">0</span></p>
      <div style="margin-top:10px"><button id="restartBtn">Restart</button></div>
      <small>Tip: try to collect coins and dodge obstacles. Speed increases over time.</small>
    </div>
  </div>
</div>

<script>
/* --- Config --- */
const config = {
  canvasWidth: 540,             // logical canvas width (we scale to CSS size)
  canvasHeight: 960,            // logical canvas height
  lanes: 3,
  laneWidthPct: 0.25,           // visual lane width relative to canvas width
  groundHeight: 140,
  initialSpeed: 4,              // world scroll speed
  speedIncreaseEvery: 3000,     // ms
  speedIncreaseAmount: 0.3,
  jumpVelocity: -18,
  gravity: 1,
  coinSpawnInterval: 900,
  obstacleSpawnInterval: 1000,
  playerWidth: 64,
  playerHeight: 96,
};

/* --- Setup canvas & scaling for retina --- */
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const scoreEl = document.getElementById('score');
const speedEl = document.getElementById('speed');
const overlay = document.getElementById('overlay');
const finalScoreEl = document.getElementById('finalScore');
const restartBtn = document.getElementById('restartBtn');

function resizeCanvas() {
  // maintain aspect ratio using logical canvas size, fit within window
  const wrap = document.getElementById('gameWrap');
  const maxW = Math.min(window.innerWidth - 40, 420);
  const scale = maxW / config.canvasWidth;
  canvas.style.width = (config.canvasWidth * scale) + 'px';
  canvas.style.height = (config.canvasHeight * scale) + 'px';
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);

/* --- Game state --- */
let state = {
  running: false,
  score: 0,
  speed: config.initialSpeed,
  lastSpeedUp: performance.now(),
  player: null,
  obstacles: [],
  coins: [],
  lastCoinAt: 0,
  lastObstacleAt: 0,
  lastFrame: 0,
  lanesX: [],
  groundY: config.canvasHeight - config.groundHeight,
};

/* --- Utility functions --- */
function randRange(a,b){return Math.random()*(b-a)+a}
function rectsCollide(a,b){
  return !(a.x + a.w < b.x || a.x > b.x + b.w || a.y + a.h < b.y || a.y > b.y + b.h);
}

/* --- Player object --- */
function createPlayer() {
  const lanePositions = state.lanesX;
  return {
    lane: 1, // 0..lanes-1, start center lane
    x: lanePositions[1] - config.playerWidth/2,
    y: state.groundY - config.playerHeight,
    vx: 0,
    vy: 0,
    w: config.playerWidth,
    h: config.playerHeight,
    isJumping: false,
    targetX: null, // for smooth lane moves
    moveProgress: 0
  };
}

/* --- Init lanes positions --- */
function initLanes() {
  const W = config.canvasWidth;
  // use three lanes centered in middle of canvas horizontally
  const centerX = W/2;
  const spacing = Math.min(120, W * 0.22);
  const positions = [];
  const off = spacing;
  // lanes centered: left, center, right
  positions.push(centerX - off);
  positions.push(centerX);
  positions.push(centerX + off);
  state.lanesX = positions;
}

/* --- Reset / Start / Game Over --- */
function resetGame() {
  initLanes();
  state.score = 0;
  state.speed = config.initialSpeed;
  state.lastSpeedUp = performance.now();
  state.obstacles = [];
  state.coins = [];
  state.lastCoinAt = 0;
  state.lastObstacleAt = 0;
  state.player = createPlayer();
  state.running = true;
  overlay.style.display = 'none';
  scoreEl.textContent = '0';
  speedEl.textContent = state.speed.toFixed(1);
  lastFrameTime = performance.now();
}

function gameOver() {
  state.running = false;
  finalScoreEl.textContent = Math.floor(state.score);
  document.getElementById('msgTitle').textContent = 'Game Over';
  overlay.style.display = 'flex';
}

/* --- Spawning --- */
function spawnObstacle() {
  // obstacles appear in random lanes with variable size
  const lane = Math.floor(Math.random() * config.lanes);
  const x = state.lanesX[lane] - 40; // obstacle centered on lane
  const w = 56 + Math.floor(Math.random()*30);
  const h = 56 + Math.floor(Math.random()*36);
  const y = state.groundY - h;
  state.obstacles.push({x: x, y: y, w: w, h: h, lane});
}

function spawnCoin() {
  const lane = Math.floor(Math.random() * config.lanes);
  const x = state.lanesX[lane] - 20;
  const y = state.groundY - config.playerHeight - randRange(20, 120);
  state.coins.push({x: x, y: y, w: 36, h: 36, lane});
}

/* --- Input handling --- */
let touchStart = null;
function handleKey(e){
  if(!state.running) return;
  if(e.key === "ArrowLeft") changeLane(-1);
  if(e.key === "ArrowRight") changeLane(1);
  if(e.key === "ArrowUp" || e.code === "Space") jump();
}
window.addEventListener('keydown', handleKey);

function changeLane(dir){
  const p = state.player;
  const newLane = Math.max(0, Math.min(config.lanes-1, p.lane + dir));
  if(newLane === p.lane) return;
  p.lane = newLane;
  p.targetX = state.lanesX[newLane] - p.w/2;
  p.moveProgress = 0;
}

function jump(){
  const p = state.player;
  if(!p.isJumping){
    p.vy = config.jumpVelocity;
    p.isJumping = true;
  }
}

/* Touch / swipe support */
canvas.addEventListener('touchstart', (ev)=>{
  if(!state.running) return;
  touchStart = ev.touches[0];
}, {passive:true});
canvas.addEventListener('touchend', (ev)=>{
  if(!state.running) return;
  if(!touchStart) return;
  const end = ev.changedTouches[0];
  const dx = end.clientX - touchStart.clientX;
  const dy = end.clientY - touchStart.clientY;
  const adx = Math.abs(dx), ady = Math.abs(dy);
  if(adx > 40 && adx > ady) {
    if(dx > 0) changeLane(1); else changeLane(-1);
  } else if(ady > 40 && ady > adx) {
    if(dy < 0) jump();
  } else {
    // tap: left or right half
    const rect = canvas.getBoundingClientRect();
    const cx = (touchStart.clientX - rect.left) / rect.width * config.canvasWidth;
    if(cx < config.canvasWidth/2) changeLane(-1); else changeLane(1);
  }
  touchStart = null;
}, {passive:true});

/* --- Game Loop --- */
let lastFrameTime = performance.now();
function update(now) {
  if(!state.running) {
    return;
  }
  const dt = Math.min(40, now - lastFrameTime);
  lastFrameTime = now;

  // speed increase over time
  if(now - state.lastSpeedUp > config.speedIncreaseEvery){
    state.speed += config.speedIncreaseAmount;
    state.lastSpeedUp = now;
    speedEl.textContent = state.speed.toFixed(1);
  }

  // spawn coins & obstacles
  if(now - state.lastCoinAt > config.coinSpawnInterval){
    spawnCoin();
    state.lastCoinAt = now;
  }
  if(now - state.lastObstacleAt > config.obstacleSpawnInterval){
    spawnObstacle();
    state.lastObstacleAt = now;
  }

  // move obstacles & coins toward player (simulate forward movement)
  const scroll = state.speed * (dt/16);
  for(const o of state.obstacles) {
    o.y += scroll * 0.0; // obstacles fixed on ground in our view; we'll simulate forward by decreasing x
    o.x -= scroll * 6;   // move left as if player is running forward
  }
  for(const c of state.coins) {
    c.x -= scroll * 6;
  }

  // remove offscreen
  state.obstacles = state.obstacles.filter(o => o.x + o.w > -50);
  state.coins = state.coins.filter(c => c.x + c.w > -50);

  // player movement: lane smoothing
  const p = state.player;
  if(p.targetX !== null){
    p.moveProgress += 0.18 * (dt/16);
    p.x = p.x + (p.targetX - p.x) * Math.min(1, p.moveProgress);
    if(Math.abs(p.x - p.targetX) < 1) { p.x = p.targetX; p.targetX = null; p.moveProgress = 0; }
  } else {
    // ensure player x tied to lane
    p.x = state.lanesX[p.lane] - p.w/2;
  }

  // gravity & jump physics
  if(p.isJumping){
    p.vy += config.gravity * (dt/16);
    p.y += p.vy * (dt/16);
    if(p.y >= state.groundY - p.h){
      p.y = state.groundY - p.h;
      p.vy = 0;
      p.isJumping = false;
    }
  }

  // collisions: obstacles
  const playerRect = {x:p.x, y:p.y, w:p.w, h:p.h};
  // obstacles
  for(const o of state.obstacles){
    const oRect = {x:o.x, y:o.y, w:o.w, h:o.h};
    if(rectsCollide(playerRect, oRect)){
      // simple rule: collision if player low enough
      if(p.y + p.h - 10 > o.y){
        gameOver();
        return;
      }
    }
  }
  // coins
  for(let i = state.coins.length -1; i>=0; i--){
    const c = state.coins[i];
    if(rectsCollide(playerRect, {x:c.x,y:c.y,w:c.w,h:c.h})){
      state.coins.splice(i,1);
      state.score += 50;
    }
  }

  // scoring by distance
  state.score += state.speed * (dt/1000) * 10;
  scoreEl.textContent = Math.floor(state.score);

  // draw
  draw();

  requestAnimationFrame(update);
}

/* --- Rendering --- */
function drawBackground(){
  const W = config.canvasWidth, H = config.canvasHeight;
  // sky gradient already in CSS; we'll draw distant details
  // draw ground
  ctx.fillStyle = '#3f2f25';
  ctx.fillRect(0, state.groundY, W, H - state.groundY);

  // draw lanes lines
  ctx.strokeStyle = '#ffffff22';
  ctx.lineWidth = 2;
  for(let i=0;i<config.lanes;i++){
    const x = state.lanesX[i];
    // brief dashed marker ahead
    const segY = state.groundY - 20;
    ctx.beginPath();
    for(let t=0;t<10;t++){
      ctx.moveTo(x + (i-1)*6, segY - t*30);
      ctx.lineTo(x + (i-1)*6 + 8, segY - t*30 - 12);
    }
    ctx.stroke();
  }
}

function drawPlayer(){
  const p = state.player;
  // simple rounded rectangle body
  ctx.save();
  // shadow
  ctx.fillStyle = 'rgba(0,0,0,0.18)';
  ctx.beginPath();
  ctx.ellipse(p.x + p.w/2, state.groundY + 8, p.w/2, 10, 0, 0, Math.PI*2);
  ctx.fill();

  // body
  ctx.fillStyle = '#ff5a5f';
  roundRect(ctx, p.x, p.y, p.w, p.h, 10, true, false);

  // face / eyes
  ctx.fillStyle = '#fff';
  ctx.fillRect(p.x + p.w*0.2, p.y + p.h*0.25, p.w*0.22, p.h*0.18);
  ctx.fillRect(p.x + p.w*0.58, p.y + p.h*0.25, p.w*0.22, p.h*0.18);
  // small detail (hat)
  ctx.fillStyle = '#222';
  ctx.fillRect(p.x + p.w*0.08, p.y + p.h*0.02, p.w*0.84, p.h*0.14);
  ctx.restore();
}

function drawObstacles(){
  for(const o of state.obstacles){
    ctx.save();
    ctx.fillStyle = '#2d2d2d';
    roundRect(ctx, o.x, o.y, o.w, o.h, 6, true, false);
    // highlight
    ctx.fillStyle = '#444';
    ctx.fillRect(o.x + 6, o.y + 6, o.w - 12, o.h - 12);
    ctx.restore();
  }
}

function drawCoins(){
  for(const c of state.coins){
    ctx.save();
    // coin shadow
    ctx.fillStyle = 'rgba(0,0,0,0.15)';
    ctx.beginPath();
    ctx.ellipse(c.x + c.w/2, c.y + c.h + 8, c.w/2, 6, 0, 0, Math.PI*2);
    ctx.fill();

    // coin circle
    ctx.fillStyle = '#f5d042';
    ctx.beginPath();
    ctx.ellipse(c.x + c.w/2, c.y + c.h/2, c.w/2, c.h/2, 0, 0, Math.PI*2);
    ctx.fill();

    // inner shine
    ctx.fillStyle = 'rgba(255,255,255,0.6)';
    ctx.fillRect(c.x + c.w*0.55, c.y + c.h*0.22, c.w*0.12, c.h*0.12);
    ctx.restore();
  }
}

function drawHUD(){
  // nothing extra (HTML handles)
}

function draw() {
  // clear logical canvas
  ctx.clearRect(0,0,config.canvasWidth, config.canvasHeight);

  // Background sky subtle
  // ground + lanes
  drawBackground();

  // coins
  drawCoins();

  // obstacles
  drawObstacles();

  // player
  drawPlayer();
}

/* helper for rounded rect */
function roundRect(ctx, x, y, w, h, r, fill, stroke) {
  if (typeof r === 'undefined') r = 5;
  ctx.beginPath();
  ctx.moveTo(x + r, y);
  ctx.arcTo(x + w, y, x + w, y + h, r);
  ctx.arcTo(x + w, y + h, x, y + h, r);
  ctx.arcTo(x, y + h, x, y, r);
  ctx.arcTo(x, y, x + w, y, r);
  ctx.closePath();
  if (fill) ctx.fill();
  if (stroke) ctx.stroke();
}

/* --- Start / Restart UI --- */
restartBtn.addEventListener('click', ()=>{
  resetGame();
  requestAnimationFrame(update);
});

/* --- Initialize & auto-start --- */
(function init(){
  // scale drawing for crispness on high-DPI screens
  const ratio = window.devicePixelRatio || 1;
  canvas.width = config.canvasWidth * ratio;
  canvas.height = config.canvasHeight * ratio;
  canvas.style.width = canvas.style.width || (config.canvasWidth + 'px');
  canvas.style.height = canvas.style.height || (config.canvasHeight + 'px');
  ctx.setTransform(ratio,0,0,ratio,0,0); // keep logical coords

  // ground y
  state.groundY = config.canvasHeight - config.groundHeight;

  initLanes();
  state.player = createPlayer();
  state.running = true;
  lastFrameTime = performance.now();
  requestAnimationFrame(update);
})();
</script>
</body>
</html>
stdout
Standard output is empty