Advertisement
gapples2

math game code

Oct 6th, 2022
663
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mathify(){
  2.     document.body.innerHTML="<h1>Random Math Thing™</h1><h2><span id='pn'>0</span> points [Best: <span id='bp'>0</span>]</h2><div id='mn'><span id='n1'></span> <span id='op'></span> <span id='n2'></span> = <input id='in'> <button id='sm' style='border:2px solid white;font-family:inherit;color:white;background-color:black'>Submit</button>  <span id='rw'></span><br><span id='tm'></span></div><div id='ps' style='display:none'><h3>Paused</h3></div><br><button id='pb'>▶️</button>"
  3.     document.body.style.color = "white"
  4.     document.body.style.backgroundColor = "black"
  5.     document.body.style.textAlign = "center"
  6.     document.body.style.fontFamily = "Verdana"
  7.     const $ = x => document.getElementById(x)
  8.     const n1 = $("n1")
  9.     const n2 = $("n2")
  10.     const op = $("op")
  11.     const inp = $("in")
  12.     const sm = $("sm")
  13.     const rw = $("rw")
  14.     const tm = $("tm")
  15.     const pn = $("pn")
  16.     const main = $("mn")
  17.     const ps = $("ps")
  18.     const pb = $("pb")
  19.     const bp = $("bp")
  20.     let nm1 = 1
  21.     let nm2 = 2
  22.     let opr = 0
  23.     let ansr = 3
  24.     let maxtime = 3
  25.     let time = 3
  26.     let int = 0
  27.     let d = Date.now()
  28.     let canSubmit = true
  29.     let points = 0
  30.     let paused = false
  31.     let bestpoints = 0
  32.     function run(){
  33.         inp.selected = true
  34.         canSubmit = true
  35.         time = maxtime
  36.         n1.textContent = nm1
  37.         n2.textContent = nm2
  38.         op.textContent = (["+","-","*"])[opr]
  39.         rw.textContent = "➖"
  40.         inp.value = ""
  41.         int = setInterval(function(){
  42.             if(paused)return;
  43.             let diff = (Date.now()-d)/1000
  44.             d = Date.now()
  45.             time-=diff
  46.             if(time<=0){
  47.                 tm.textContent = "0s"
  48.                 generate(false)
  49.             }else tm.textContent = time.toFixed(2)+"s"
  50.         },50)
  51.     }
  52.     function rand(x){
  53.         return Math.floor(Math.random()*x)
  54.     }
  55.     function cop(a,b,c){
  56.         return opr==0?a:(opr==1?b:c)
  57.     }
  58.     function pav(){
  59.         return (points+bestpoints)/2
  60.     }
  61.     function generate(overload){
  62.         if(!canSubmit||(overload&&!inp.value))return;
  63.         canSubmit = false
  64.         clearInterval(int)
  65.         let input = inp.value
  66.         if(overload&&Number(input)==ansr){
  67.             let base = (nm1*nm2)**0.5*(0.2+opr/50)
  68.             let diff = (maxtime-time)/maxtime
  69.             points+=base/2+base*diff
  70.         }else points=points-Math.min(points-points*0.75,1)
  71.         points = Math.max(points,0)
  72.         bestpoints = Math.max(points,bestpoints)
  73.         pn.textContent = points.toFixed(2)
  74.         bp.textContent = bestpoints.toFixed(2)
  75.         rw.textContent = (!overload)?"⏲️":Number(input)==ansr?"✔️":"❌"
  76.         nm1 = cop(rand(pav()**1.2+5),rand(pav()**0.8+7),rand(pav()**0.4+6))+1
  77.         nm2 = cop(rand(pav()**1.2+5),rand(pav()**0.8+7),rand(pav()**0.4+6))+1
  78.         opr = rand((bestpoints>=5?3:(bestpoints>=2?2:1)))
  79.         ansr = cop(nm1+nm2,nm1-nm2,nm1*nm2)
  80.         maxtime = 3+opr*0.5+Math.sqrt(pav())/2
  81.         if(nm1==1||nm2==1)maxtime-=0.25
  82.         setTimeout(run,1000)
  83.     }
  84.     function pause(){
  85.         paused = !paused
  86.         main.style.display = paused?"none":""
  87.         ps.style.display = paused?"":"none"
  88.         pb.textContent = paused?"▶️":"⏸️"
  89.         d = Date.now()
  90.     }
  91.     sm.addEventListener("click",generate)
  92.     pb.addEventListener("click",pause)
  93.     inp.addEventListener("keydown",function(key){
  94.         if(key.key=="Enter"){
  95.             generate(true)
  96.         }
  97.     })
  98.     run()
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement