Accueil > > > PONG PUDDING
PONG PUDDING
Information sur la source
Description
Jeu de pong avec des raquettes et une balle en pudding, c'est-à-dire que les éléments se déforment. Le rendu graphique est simple mais assez joli. Il est possible de jouer seul contre l'ordinateur ou bien à deux. La version avec menu utilise le Canvas (ou bien la bibliothèque Javascript pour IE). Une version complètement en CSS est également fonctionnelle. Une version on-line est disponible ici : http://webjavascript.fr/pudding
Source
- var pong_timegrain = 15;
- var pong_timeunit = pong_timegrain/100;
- var pong_point_size= 15, pong_sqr_point_size = pong_point_size*pong_point_size;
- var pong_a_joueur=0.1;
-
- function AddEvent( obj, event, func, mode){
- if( obj.addEventListener)
- obj.addEventListener( event, func, mode? mode:false);
- else
- obj.attachEvent( 'on'+event, func);
- }
-
- function Pong(type_jeu) // objet
- {
- this.type_jeu = type_jeu;
- this.name = "Pong";
- var me = this;
- var freed = false;
- var canvas = document.getElementById("c");
- canvas.style.visibility = "visible";
-
- if (type_jeu == "menu")
- {
- canvas.style.backgroundColor = "transparent";
- canvas.style.border = "none";
- } else
- {
- canvas.style.backgroundColor = "#80A0C0";
- canvas.style.border = "1px solid black";
- }
-
- this.tx = parseInt(canvas.getAttribute("width"));
- this.ty = parseInt(canvas.getAttribute("height"));
-
- var balle;
- var computer1,computer2;
-
- this.fillpoly = function(c, len, color) // note : c = (x1,y1,x2,y2,x3,y3...)
- {
- if (len < 2) return;
-
- var ctx = canvas.getContext("2d");
- ctx.fillStyle = color;
- ctx.beginPath();
- ctx.moveTo(c[0],c[1]);
- for (var n=2; n<len; n+=2)
- {
- ctx.lineTo(c[n],c[n+1]);
- }
- ctx.closePath();
- ctx.fill();
- }
-
- var joueur1, joueur2;
-
- if (type_jeu=="menu") balle = new PetiteBalle(this,30,[this.tx/2,this.ty/2],[10*pong_timeunit,-5*pong_timeunit]); else
- if (type_jeu=="jeu_seul")
- {
- joueur1 = new Joueur(this,40,100, [50,Math.random()*(this.ty-100)+50], [0,0], "blue", [0,0,this.tx*2/3,this.ty]);
- joueur1.name += "1";
- joueur2 = new Joueur(this,40,100, [this.tx-50,Math.random()*(this.ty-100)+50], [0,0], "orange", [this.tx*1/3,0,this.tx,this.ty]);
- joueur2.name += "2";
- balle = new PetiteBalle(this,30,[120,150],[0,0]);
- //computer1 = true;
- computer2 = true;
- } else
- if (type_jeu=="jeu_deux")
- {
- joueur1 = new Joueur(this,40,100, [this.tx-50,Math.random()*(this.ty-100)+50], [0,0], "green", [this.tx*1/3,0,this.tx,this.ty]);
- joueur1.name += "1";
- joueur2 = new Joueur(this,40,100, [50,Math.random()*(this.ty-100)+50], [0,0], "blue", [0,0,this.tx*2/3,this.ty]);
- joueur2.name += "2";
- balle = new PetiteBalle(this,30,[this.tx/2,this.ty/2],[0,0]);
- }
-
- this.free = function()
- {
- if (balle) { balle.free(); balle = null; }
- if (joueur1) { joueur1.free(); joueur1 = null; }
- if (joueur2) { joueur2.free(); joueur2 = null; }
- if (canvas) canvas.style.visibility = "hidden";
- freed = true;
- }
-
- var prevtime = -1;
- var LeftKey1, RightKey1, UpKey1, DownKey1, RotateLeft1, RotateRight1;
- var LeftKey2, RightKey2, UpKey2, DownKey2, RotateLeft2, RotateRight2;
-
- var wait = function()
- {
- setTimeout( loop, 15);
- }
-
- var loop = function()
- {
- if (freed) return;
- var newtime = (new Date()).getTime();
- if (prevtime==-1) prevtime = newtime;
-
- var stepcount = Math.floor((newtime-prevtime)/pong_timegrain);
- if (stepcount>50)
- prevtime = newtime; else
- {
- prevtime += stepcount*pong_timegrain;
- if (joueur1)
- {
- if (computer1)
- joueur1.ia(-1, joueur2, balle); else
- joueur1.acceleration(LeftKey1?-pong_a_joueur:(RightKey1?pong_a_joueur:0),UpKey1?-pong_a_joueur:(DownKey1?pong_a_joueur:0));
- }
- if (joueur2)
- {
- if (computer2)
- joueur2.ia(+1, joueur1, balle); else
- joueur2.acceleration(LeftKey2?-pong_a_joueur:(RightKey2?pong_a_joueur:0),UpKey2?-pong_a_joueur:(DownKey2?pong_a_joueur:0));
- }
- for (var k=0; k<stepcount; k++)
- {
- if (balle) balle.step();
- if (joueur1) joueur1.step();
- if (joueur2) joueur2.step();
- }
-
- var ctx = canvas.getContext("2d");
- ctx.clearRect(0,0,me.tx,me.ty);
-
- if (balle) balle.dessine();
- if (joueur1) joueur1.dessine();
- if (joueur2) joueur2.dessine();
- }
-
- wait();
- }
-
- this.canMove = function(obj,nx,ny,vx,vy)
- {
- var acc_factor = 1;
- if (obj!=joueur1&&joueur1) if (joueur1.ptIn(nx,ny,vx/acc_factor,vy/acc_factor)) return false;
- if (obj!=joueur2&&joueur2) if (joueur2.ptIn(nx,ny,vx/acc_factor,vy/acc_factor)) return false;
- if (obj!=balle&&balle) if (balle.ptIn(nx,ny,vx/acc_factor,vy/acc_factor)) return false;
- return true;
- }
-
- this.go = function()
- {
- wait();
- }
-
- this.keyDown = function(e)
- {
- var keyCode = window.event ? e.keyCode : e.which;
- if (keyCode==37||keyCode==52||keyCode==100)
- {
- LeftKey1 = true;
- RightKey1 = false;
- } else
- if (keyCode==38||keyCode==56||keyCode==104)
- {
- UpKey1 = true;
- DownKey1 = false;
- } else
- if (keyCode==39||keyCode==54||keyCode==102)
- {
- RightKey1 = true;
- LeftKey1 = false;
- } else
- if (keyCode==40||keyCode==50||keyCode==98)
- {
- DownKey1 = true;
- UpKey1 = false;
- } else
- if (keyCode==81)
- {
- LeftKey2 = true;
- RightKey2 = false;
- } else
- if (keyCode==90)
- {
- UpKey2 = true;
- DownKey2 = false;
- } else
- if (keyCode==68)
- {
- RightKey2 = true;
- LeftKey2 = false;
- } else
- if (keyCode==83)
- {
- DownKey2 = true;
- UpKey2 = false;
- }
- // else alert(keyCode);
- return false;
- }
-
- this.keyUp = function(e)
- {
- var keyCode = window.event ? e.keyCode : e.which;
- if (keyCode==37||keyCode==52||keyCode==100)
- {
- LeftKey1 = false;
- } else
- if (keyCode==38||keyCode==56||keyCode==104)
- {
- UpKey1 = false;
- } else
- if (keyCode==39||keyCode==54||keyCode==102)
- {
- RightKey1 = false;
- } else
- if (keyCode==40||keyCode==50||keyCode==98)
- {
- DownKey1 = false;
- } else
- if (keyCode==81)
- {
- LeftKey2 = false;
- } else
- if (keyCode==90)
- {
- UpKey2 = false;
- } else
- if (keyCode==68)
- {
- RightKey2 = false;
- } else
- if (keyCode==83)
- {
- DownKey2 = false;
- }
- return false;
- }
-
- if (navigator.appName == 'Netscape')
- {
- document.onkeydown = this.keyDown;
- document.onkeyup = this.keyUp;
- } else
- {
- AddEvent(document.body, "keydown", this.keyDown);
- AddEvent(document.body, "keyup", this.keyUp);
- }
- }
-
- function Joueur(jeu, tx, ty, p, v, color, rect) // objet
- {
- this.name = "Joueur";
- var nbp = Math.round(ty/pong_point_size);
- var pts = new Array();
- var dir = new Array();
- var dist_pts = ty/(nbp-1);
- var elasticite = 10;
- var a= [0,0];
- var temp_a = [0,0];
-
- for (var i=0; i<nbp; i++)
- {
- pts.push(p[0]+tx/2+(Math.random()-0.5)*pong_point_size/4);
- pts.push(p[1]+(i-(nbp-1)/2)*dist_pts+(Math.random()-0.5)*pong_point_size/4);
- dir.push(v[0]);
- dir.push(v[1]);
- }
-
- for (var i=0; i<nbp; i++)
- {
- pts.push(p[0]-tx/2+(Math.random()-0.5)*pong_point_size/4);
- pts.push(p[1]-(i-(nbp-1)/2)*dist_pts+(Math.random()-0.5)*pong_point_size/4);
- dir.push(v[0]);
- dir.push(v[1]);
- }
-
- var len_visible = pts.length;
-
- var lon_diag0 = Math.sqrt(Math.pow(pts[(nbp*2-2)*2]-pts[0],2)+Math.pow(pts[(nbp*2-2)*2+1]-pts[1],2));
- var minmax = calcMinMax(pts,pts.length);
-
- this.dessine = function()
- {
- jeu.fillpoly(pts,len_visible, color);
- }
-
- this.free = function()
- {
- }
-
- this.ptIn = function (x,y,vx,vy)
- {
- if (x<minmax[0]-pong_point_size||x>minmax[1]+pong_point_size||y<minmax[2]-pong_point_size||y>minmax[3]+pong_point_size) return false;
- for (i=0; i<pts.length; i+=2)
- {
- if (Math.pow(pts[i]-x,2)+Math.pow(pts[i+1]-y,2) < pong_sqr_point_size)
- {
- dir[i] += vx;
- dir[i+1] += vy;
- return true;
- }
- }
- return false;
- }
-
- this.canMove = function(nx,ny,vx,vy)
- {
- if (nx < rect[0]||ny<rect[1]||nx>rect[2]||ny>rect[3]) return false; else
- return jeu.canMove(this,nx,ny,vx,vy);
- }
-
- this.acceleration = function(dx,dy)
- {
- a = [dx,dy];
- }
-
- this.step = function(stepcount)
- {
- if (!stepcount) stepcount = 1;
- var lon_ref,ndx,ndy;
- for (var k=0; k<stepcount; k++) {
- ia_elapse --;
- for (var i=0; i<2*nbp; i++)
- {
- var i_cur = i*2;
- var i_suiv = ((i+1)%(2*nbp))*2;
- var i_suiv2 = ((i+1)%(2*nbp))*2;
- var i_opp = (nbp*2-1-i)*2;
-
- var i_diag = ((nbp*2-1-i+2*nbp-1)%(2*nbp))*2, diag_bi = false;
- if (i_diag==i_cur) { i_diag = ((nbp*2-1-i+1)%(2*nbp))*2; diag_bi = true; }
-
- var x = pts[i_cur], y = pts[i_cur+1];
- var dx = dir[i_cur], dy = dir[i_cur+1];
-
- if (i_suiv2-(i_suiv2%(nbp*2))==i_cur-(i_cur%(nbp*2)))
- {
- var x_suiv2 = pts[i_suiv2], y_suiv2 = pts[i_suiv2+1];
- var lon_suiv2 = Math.sqrt(Math.pow(x_suiv2-x,2)+Math.pow(y_suiv2-y,2));
- if (lon_suiv2<1) lon_suiv2 = 1;
- lon_ref = dist_pts;
-
- ndx = (lon_suiv2-lon_ref)*(x_suiv2-x)/lon_suiv2/elasticite*2;
- ndy = (lon_suiv2-lon_ref)*(y_suiv2-y)/lon_suiv2/elasticite*2;
- dx += ndx; dy += ndy;
- dir[i_suiv2] -= ndx; dir[i_suiv2+1] -= ndy;
- }
-
- if (i_suiv!=i_opp)
- {
- var x_suiv = pts[i_suiv], y_suiv = pts[i_suiv+1];
- var lon_suiv = Math.sqrt(Math.pow(x_suiv-x,2)+Math.pow(y_suiv-y,2));
- if (lon_suiv<1) lon_suiv = 1;
- lon_ref = dist_pts;
-
- ndx = (lon_suiv-lon_ref)*(x_suiv-x)/lon_suiv/elasticite/2;
- ndy = (lon_suiv-lon_ref)*(y_suiv-y)/lon_suiv/elasticite/2;
- dx += ndx; dy += ndy;
- dir[i_suiv] -= ndx; dir[i_suiv+1] -= ndy;
- }
-
- var x_opp = pts[i_opp], y_opp = pts[i_opp+1];
- var lon_opp = Math.sqrt(Math.pow(x_opp-x,2)+Math.pow(y_opp-y,2));
- if (lon_opp<1) lon_opp = 1;
- lon_ref = tx;
- ndx = (lon_opp-lon_ref)*(x_opp-x)/lon_opp/elasticite;
- ndy = (lon_opp-lon_ref)*(y_opp-y)/lon_opp/elasticite;
- dx += ndx; dy += ndy;
-
- var x_diag = pts[i_diag], y_diag = pts[i_diag+1];
- var lon_diag = Math.sqrt(Math.pow(x_diag-x,2)+Math.pow(y_diag-y,2));
- if (lon_diag<1) lon_diag = 1;
- lon_ref = lon_diag0;
- ndx = (lon_diag-lon_ref)*(x_diag-x)/lon_diag/elasticite;
- ndy = (lon_diag-lon_ref)*(y_diag-y)/lon_diag/elasticite;
- dx += ndx; dy += ndy;
- if (diag_bi) { dir[i_diag] -= ndx; dir[i_diag+1] -= ndy; }
-
- dir[i_cur] = dx; dir[i_cur+1] = dy;
- }
-
- var i_top = 0;
- var i_bottom = nbp*2;
- var x_top = pts[i_top], y_top = pts[i_top+1];
- var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
- var lon = Math.sqrt(Math.pow(x_bottom-x_top,2)+Math.pow(y_bottom-y_top,2));
- if (lon<1) lon = 1;
- lon_ref = Math.sqrt(tx*tx+ty*ty);
-
- dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
- dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
- dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
- dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
-
- var i_top = (nbp*2-1)*2;
- var i_bottom = (nbp-1)*2;
- var x_top = pts[i_top], y_top = pts[i_top+1];
- var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
- var lon = Math.sqrt(Math.pow(x_bottom-x_top,2)+Math.pow(y_bottom-y_top,2));
- if (lon<1) lon = 1;
- lon_ref = Math.sqrt(tx*tx+ty*ty);
-
- dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
- dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
- dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
- dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
-
- var i_top = 0;
- var i_bottom = (nbp-1)*2;
- var x_top = pts[i_top], y_top = pts[i_top+1];
- var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
-
- var lon = Math.abs(x_bottom-x_top);
- if (lon<1) lon = 1;
- lon_ref = 0;
- dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
- dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
-
- var lon = Math.abs(y_bottom-y_top);
- if (lon<1) lon = 1;
- lon_ref = ty;
- dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
- dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
-
- var i_top = (nbp*2-1)*2;
- var i_bottom = nbp*2;
- var x_top = pts[i_top], y_top = pts[i_top+1];
- var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
-
- var lon = Math.abs(x_bottom-x_top);
- if (lon<1) lon = 1;
- lon_ref = 0;
- dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
- dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
-
- var lon = Math.abs(y_bottom-y_top);
- if (lon<1) lon = 1;
- lon_ref = ty;
- dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
- dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
-
- var moydx=0, moydy=0, moyx=0, moyy=0;
- for (var i=0; i<pts.length; i+=2)
- {
- var x = pts[i], y = pts[i+1];
- var dx = dir[i]+temp_a[0], dy = dir[i+1]+temp_a[0];
- if (dx<-pong_point_size/2) dx = -pong_point_size/2;
- if (dy<-pong_point_size/2) dy = -pong_point_size/2;
- if (dx>pong_point_size/2) dx = pong_point_size/2;
- if (dy>pong_point_size/2) dy = pong_point_size/2;
- if (this.canMove(x+dx,y+dy,dx,dy))
- {
- x += dx;
- y += dy;
- } else
- {
- var len;
- if (Math.abs(dx)>Math.abs(dy))
- len = Math.abs(dx); else
- len = Math.abs(dy);
- for (var n=0; n<len; n++)
- {
- if (this.canMove(x+dx/len,y+dy/len,0,0))
- {
- x += dx/len;
- y += dy/len;
- } else break;
- }
-
- var bloquex=(dx>0 && !this.canMove(x+1,y,0,0))||(dx<0 && !this.canMove(x-1,y,0,0));
- var bloquey=(dy>0 && !this.canMove(x,y+1,0,0))||(dy<0 && !this.canMove(x,y-1,0,0));
- if (bloquex&&bloquey) { dx = 0; dy = 0; }
- else if (bloquex) { dx = 0; dy = dy*0.9; }
- else if (bloquey) { dy = 0; dx = dx*0.9; }
-
- }
-
- pts[i] = x;
- pts[i+1] = y;
- moyx += x;
- moyy += y;
- dir[i] = dx;
- dir[i+1] = dy;
- moydx += dx;
- moydy += dy;
- }
- moydx /= (pts.length)/2;
- moydy /= (pts.length)/2;
- this.v = [moydx,moydy];
- moyx /= (pts.length)/2;
- moyy /= (pts.length)/2;
- this.p = [moyx,moyy];
- for (var i=0; i<pts.length; i+=2)
- {
- dir[i] = dir[i]*0.87 + moydx*0.1 + a[0];
- dir[i+1] = dir[i+1]*0.87 + moydy*0.1 + a[1];
- }
- temp_a = [0,0];
- }
- minmax = calcMinMax(pts,pts.length);
- }
-
- var ia_elapse = 0;
- var mode_ia = 0;
- var ia_effect;
- this.ia = function(dir, autre, balle)
- {
- if (this.p&&this.v&&balle.p&&balle.v)
- {
- var murx,dirx=0,diry=0, marge=tx/2+balle.taille+pong_point_size;
- if (dir > 0) murx = jeu.tx- marge; else murx = marge;
-
- var behind = (this.p[0] > balle.p[0] + balle.taille + tx/2 && dir > 0) || (this.p[0] < balle.p[0] - balle.taille - tx/2 && dir < 0);
- if (!behind&&mode_ia==-1)
- {
- if ((this.p[0] > balle.p[0] && dir > 0) || (this.p[0] < balle.p[0] && dir < 0)) behind = true;
- }
- var ahead = !behind;
- var incoming = (balle.v[0] > 0 && dir > 0) || (balle.v[0] < 0 && dir < 0);
- var inhome = (balle.p[0] > jeu.tx/2 && dir > 0) || (balle.p[0] < jeu.tx/2 && dir < 0);
- var reachable = (balle.p[0] > rect[0] && balle.p[0] < rect[2]);
- var slow = Math.abs(balle.v[0]) < 10* pong_timeunit;
- var veryslow = Math.abs(balle.v[0]) < 3* pong_timeunit;
- if (!reachable&&slow) incoming = false;
-
- if (incoming || (slow&&inhome))
- {
- if (mode_ia>0) mode_ia = 0;
-
- var mury;
- if (incoming&&!veryslow)
- mury = balle.p[1]+(this.p[0]-dir*marge-balle.p[0])/balle.v[0]*balle.v[1]; else
- mury = balle.p[1];
- var nbbounce=0;
- while (mury<0||mury>jeu.ty)
- {
- if (mury<0) mury = -mury; else
- if (mury>jeu.ty) mury = 2*jeu.ty-mury;
- nbbounce++;
- }
- if (nbbounce>=3) mury = balle.p[1];
-
- if (ahead)
- {
- if (this.p[1]+ty/2 > balle.p[1]-balle.taille && this.p[1]-ty/2 < balle.p[1]+balle.taille)
- {
- if (this.p[1] >= mury) { if (mury > jeu.ty-ty) diry = -1; else diry = 1; } else
- if (this.p[1] < mury) { if (mury < ty) diry = +1; else diry = -1; }
- } else
- {
- dirx = dir;
- }
- }
- else
- {
- if (this.p[1] < mury-ty/4 && this.p[1] > ty*0.6) diry = 1; else
- if (this.p[1] > mury+ty/4 && this.p[1] < jeu.ty-ty*0.6) diry = -1; else
- {
- if (this.v[1] < pong_a_joueur) diry = 1; else
- if (this.v[1] > pong_a_joueur) diry = -1;
- if (behind) {
- dirx = -dir;
- if (mode_ia==0)
- {
- mode_ia = -1;
- ia_effect = Math.floor(Math.random()*2.9)-1;
- }
- if (this.p[1] < mury-ty/5) diry = 1; else
- if (this.p[1] > mury+ty/5) diry = -1; else
- diry = ia_effect;
- }
- }
- }
- } else
- {
- var safedist = autre.p[0]+dir*jeu.tx/2;
- if (safedist < tx/2) safedist = safedist < tx/2;
- if (safedist > jeu.tx-tx/2) safedist = jeu.tx-tx/2;
- if ((this.p[0]-safedist)*dir < 0) dirx = dir;
- if (ia_elapse<0) mode_ia = 0;
- if (mode_ia==0)
- {
- mode_ia = Math.floor(Math.random()*2.9)+1;
- ia_elapse = (Math.random()*5000/pong_timegrain)+(500/pong_timegrain);
- }
- var ywant;
- if (mode_ia==1) ywant = autre.p[1]; else
- if (mode_ia==2) ywant = jeu.ty/2; else
- if (mode_ia==3) ywant = balle.p[1];
- if (this.p[1] < ywant-ty/4) diry = 1; else
- if (this.p[1] > ywant+ty/4) diry = -1;
- }
- if (this.p[1] < ty*0.6&&diry==-1) diry= 0;
- if (this.p[1] > jeu.ty-ty*0.6&&diry==1) diry= 0;
- this.acceleration(dirx*pong_a_joueur,diry*pong_a_joueur);
- }
- }
-
- this.dessine();
- }
-
- function PetiteBalle(jeu, taille, p, v) // objet
- {
- this.name = "Balle";
- var pts = new Array();
- var dir = new Array();
- var elasticite = 5;
- var temp_a = [0,0];
-
- var nbp = 6;
- for (var i=0; i<nbp; i++)
- {
- var angle = i*2*Math.PI/nbp;
- pts.push(p[0]+Math.cos(angle)*taille/2);
- pts.push(p[1]+Math.sin(angle)*taille/2);
- dir.push(v[0]);
- dir.push(v[1]);
- }
-
- var pos_int = pts.length;
- pts.push(p[0]);
- pts.push(p[1]);
- dir.push(v[0]);
- dir.push(v[1]);
-
- var lon_cote = Math.sqrt(Math.pow(pts[0]-pts[2],2)+Math.pow(pts[1]-pts[3],2));
- this.taille = taille;
-
- var len_visible = nbp*2;
- var minmax = calcMinMax(pts,pts.length);
-
- this.dessine = function()
- {
- var pts2 = new Array();
- var cx = (minmax[0]+minmax[1])/2;
- var cy = (minmax[2]+minmax[3])/2;
- var fact = (taille+2*pong_point_size)/taille;
- for (var i=0; i<len_visible; i+=2)
- {
- var x1=(pts[i]-cx)*fact, y1 = (pts[i+1]-cy)*fact;
- var x2=(pts[(i+2)%len_visible]-cx)*fact;
- var y2=(pts[(i+3)%len_visible]-cy)*fact;
- pts2.push(x1+cx);
- pts2.push(y1+cy);
- pts2.push((x1+x2)*0.55+cx);
- pts2.push((y1+y2)*0.55+cy);
- }
- jeu.fillpoly(pts2,len_visible*2, "white");
- }
-
- this.free = function()
- {
- }
-
- this.ptIn = function (x,y,vx,vy)
- {
- if (x<minmax[0]-pong_point_size||x>minmax[1]+pong_point_size||y<minmax[2]-pong_point_size||y>minmax[3]+pong_point_size) return false;
- for (i=0; i<pts.length; i+=2)
- {
- if (Math.pow(pts[i]-x,2)+Math.pow(pts[i+1]-y,2) < pong_sqr_point_size)
- {
- dir[i] += vx;
- dir[i+1] += vy;
- return true;
- }
- }
- return false;
- }
-
- this.canMove = function(nx,ny,vx,vy)
- {
- if (nx < pong_point_size||nx > jeu.tx-1-pong_point_size||
- ny < pong_point_size||ny > jeu.ty-1-pong_point_size) return false;
- return jeu.canMove(this,nx,ny,vx,vy);
- }
-
- this.step = function(stepcount)
- {
- if (!stepcount) stepcount = 1;
- var lon_ref;
- for (var k=0; k<stepcount; k++) {
- for (var i=0; i<nbp; i++)
- {
- var i_cur = i*2;
- var i_suiv = ((i+1)%nbp)*2;
- var i_opp = ((i+nbp/2)%nbp)*2;
- var i_int = pos_int;
-
- var x = pts[i_cur], y = pts[i_cur+1];
- var dx = dir[i_cur], dy = dir[i_cur+1];
-
- var x_suiv = pts[i_suiv], y_suiv = pts[i_suiv+1];
- var lon_suiv = Math.sqrt(Math.pow(x_suiv-x,2)+Math.pow(y_suiv-y,2));
- if (lon_suiv<1) lon_suiv = 1;
- lon_ref = lon_cote;
-
- dx += (lon_suiv-lon_ref)*(x_suiv-x)/lon_suiv/elasticite;
- dy += (lon_suiv-lon_ref)*(y_suiv-y)/lon_suiv/elasticite;
- dir[i_suiv] -= (lon_suiv-lon_ref)*(x_suiv-x)/lon_suiv/elasticite;
- dir[i_suiv+1] -= (lon_suiv-lon_ref)*(y_suiv-y)/lon_suiv/elasticite;
-
- var x_opp = pts[i_opp], y_opp = pts[i_opp+1];
- var lon_opp = Math.sqrt(Math.pow(x_opp-x,2)+Math.pow(y_opp-y,2));
- if (lon_opp<1) lon_opp = 1;
- lon_ref = taille;
-
- dx += (lon_opp-lon_ref)*(x_opp-x)/lon_opp/elasticite;
- dy += (lon_opp-lon_ref)*(y_opp-y)/lon_opp/elasticite;
-
- var x_int = pts[i_int], y_int = pts[i_int+1];
- var lon_int = Math.sqrt(Math.pow(x_int-x,2)+Math.pow(y_int-y,2));
- if (lon_int<1) lon_int = 1;
- lon_ref = taille/2;
-
- dx += (lon_int-lon_ref)*(x_int-x)/lon_int/elasticite/2;
- dy += (lon_int-lon_ref)*(y_int-y)/lon_int/elasticite/2;
-
- dir[i_int] -= (lon_int-lon_ref)*(x_int-x)/lon_int/elasticite;
- dir[i_int+1] -= (lon_int-lon_ref)*(y_int-y)/lon_int/elasticite;
-
- dir[i_cur] = dx;
- dir[i_cur+1] = dy;
- }
-
- var moydx=0, moydy=0, moyx=0, moyy = 0;
- for (var i=0; i<pts.length; i+=2)
- {
- var x = pts[i], y = pts[i+1];
- var dx = dir[i]+temp_a[0], dy = dir[i+1]+temp_a[1];
- if (dx<-pong_point_size/2) dx = -pong_point_size/2;
- if (dy<-pong_point_size/2) dy = -pong_point_size/2;
- if (dx>pong_point_size/2) dx = pong_point_size/2;
- if (dy>pong_point_size/2) dy = pong_point_size/2;
- if (this.canMove(x+dx,y+dy,dx,dy))
- {
- x += dx;
- y += dy;
- } else
- {
- var len;
- if (Math.abs(dx)>Math.abs(dy))
- len = Math.abs(dx); else
- len = Math.abs(dy);
- for (var n=0; n<len; n++)
- {
- if (this.canMove(x+dx/len,y+dy/len,0,0))
- {
- x += dx/len;
- y += dy/len;
- } else break;
- }
-
- var bloquex=(dx>0 && !this.canMove(x+1,y,0,0))||(dx<0 && !this.canMove(x-1,y,0,0));
- var bloquey=(dy>0 && !this.canMove(x,y+1,0,0))||(dy<0 && !this.canMove(x,y-1,0,0));
- if (bloquex&&bloquey) { dx = 0; dy = 0; }
- else if (bloquex) { dx = 0; dy = dy*0.9; }
- else if (bloquey) { dy = 0; dx = dx*0.9; }
-
- }
-
- pts[i] = x;
- pts[i+1] = y;
- moyx += x;
- moyy += y;
- dir[i] = dx;
- dir[i+1] = dy;
- moydx += dx;
- moydy += dy;
- }
- temp_a = [0,0];
- moydx /= (pts.length)/2;
- moydy /= (pts.length)/2;
- this.v = [moydx, moydy];
- moyx /= (pts.length)/2;
- moyy /= (pts.length)/2;
- this.p = [moyx,moyy];
- for (var i=0; i<pts.length; i+=2)
- {
- dir[i] = dir[i]*0.97 + moydx*0.03;
- dir[i+1] = dir[i+1]*0.97 + moydy*0.03;
- }
- }
-
- minmax = calcMinMax(pts,pts.length);
- }
-
- this.dessine();
- }
-
- function calcMinMax(c, len)
- {
- //liste des positions verticales à parcourir
- var minx, maxx,x;
- var miny, maxy,y;
- minx = c[0]; maxx = c[0];
- miny = c[1]; maxy = c[1];
- for (var i=0; i<len; i+=2)
- {
- x = c[i];
- if (x < minx) minx = x; else
- if (x > maxx) maxx = x;
- y = c[i+1];
- if (y < miny) miny = y; else
- if (y > maxy) maxy = y;
- }
- return [minx,maxx,miny,maxy];
- }
var pong_timegrain = 15;
var pong_timeunit = pong_timegrain/100;
var pong_point_size= 15, pong_sqr_point_size = pong_point_size*pong_point_size;
var pong_a_joueur=0.1;
function AddEvent( obj, event, func, mode){
if( obj.addEventListener)
obj.addEventListener( event, func, mode? mode:false);
else
obj.attachEvent( 'on'+event, func);
}
function Pong(type_jeu) // objet
{
this.type_jeu = type_jeu;
this.name = "Pong";
var me = this;
var freed = false;
var canvas = document.getElementById("c");
canvas.style.visibility = "visible";
if (type_jeu == "menu")
{
canvas.style.backgroundColor = "transparent";
canvas.style.border = "none";
} else
{
canvas.style.backgroundColor = "#80A0C0";
canvas.style.border = "1px solid black";
}
this.tx = parseInt(canvas.getAttribute("width"));
this.ty = parseInt(canvas.getAttribute("height"));
var balle;
var computer1,computer2;
this.fillpoly = function(c, len, color) // note : c = (x1,y1,x2,y2,x3,y3...)
{
if (len < 2) return;
var ctx = canvas.getContext("2d");
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(c[0],c[1]);
for (var n=2; n<len; n+=2)
{
ctx.lineTo(c[n],c[n+1]);
}
ctx.closePath();
ctx.fill();
}
var joueur1, joueur2;
if (type_jeu=="menu") balle = new PetiteBalle(this,30,[this.tx/2,this.ty/2],[10*pong_timeunit,-5*pong_timeunit]); else
if (type_jeu=="jeu_seul")
{
joueur1 = new Joueur(this,40,100, [50,Math.random()*(this.ty-100)+50], [0,0], "blue", [0,0,this.tx*2/3,this.ty]);
joueur1.name += "1";
joueur2 = new Joueur(this,40,100, [this.tx-50,Math.random()*(this.ty-100)+50], [0,0], "orange", [this.tx*1/3,0,this.tx,this.ty]);
joueur2.name += "2";
balle = new PetiteBalle(this,30,[120,150],[0,0]);
//computer1 = true;
computer2 = true;
} else
if (type_jeu=="jeu_deux")
{
joueur1 = new Joueur(this,40,100, [this.tx-50,Math.random()*(this.ty-100)+50], [0,0], "green", [this.tx*1/3,0,this.tx,this.ty]);
joueur1.name += "1";
joueur2 = new Joueur(this,40,100, [50,Math.random()*(this.ty-100)+50], [0,0], "blue", [0,0,this.tx*2/3,this.ty]);
joueur2.name += "2";
balle = new PetiteBalle(this,30,[this.tx/2,this.ty/2],[0,0]);
}
this.free = function()
{
if (balle) { balle.free(); balle = null; }
if (joueur1) { joueur1.free(); joueur1 = null; }
if (joueur2) { joueur2.free(); joueur2 = null; }
if (canvas) canvas.style.visibility = "hidden";
freed = true;
}
var prevtime = -1;
var LeftKey1, RightKey1, UpKey1, DownKey1, RotateLeft1, RotateRight1;
var LeftKey2, RightKey2, UpKey2, DownKey2, RotateLeft2, RotateRight2;
var wait = function()
{
setTimeout( loop, 15);
}
var loop = function()
{
if (freed) return;
var newtime = (new Date()).getTime();
if (prevtime==-1) prevtime = newtime;
var stepcount = Math.floor((newtime-prevtime)/pong_timegrain);
if (stepcount>50)
prevtime = newtime; else
{
prevtime += stepcount*pong_timegrain;
if (joueur1)
{
if (computer1)
joueur1.ia(-1, joueur2, balle); else
joueur1.acceleration(LeftKey1?-pong_a_joueur:(RightKey1?pong_a_joueur:0),UpKey1?-pong_a_joueur:(DownKey1?pong_a_joueur:0));
}
if (joueur2)
{
if (computer2)
joueur2.ia(+1, joueur1, balle); else
joueur2.acceleration(LeftKey2?-pong_a_joueur:(RightKey2?pong_a_joueur:0),UpKey2?-pong_a_joueur:(DownKey2?pong_a_joueur:0));
}
for (var k=0; k<stepcount; k++)
{
if (balle) balle.step();
if (joueur1) joueur1.step();
if (joueur2) joueur2.step();
}
var ctx = canvas.getContext("2d");
ctx.clearRect(0,0,me.tx,me.ty);
if (balle) balle.dessine();
if (joueur1) joueur1.dessine();
if (joueur2) joueur2.dessine();
}
wait();
}
this.canMove = function(obj,nx,ny,vx,vy)
{
var acc_factor = 1;
if (obj!=joueur1&&joueur1) if (joueur1.ptIn(nx,ny,vx/acc_factor,vy/acc_factor)) return false;
if (obj!=joueur2&&joueur2) if (joueur2.ptIn(nx,ny,vx/acc_factor,vy/acc_factor)) return false;
if (obj!=balle&&balle) if (balle.ptIn(nx,ny,vx/acc_factor,vy/acc_factor)) return false;
return true;
}
this.go = function()
{
wait();
}
this.keyDown = function(e)
{
var keyCode = window.event ? e.keyCode : e.which;
if (keyCode==37||keyCode==52||keyCode==100)
{
LeftKey1 = true;
RightKey1 = false;
} else
if (keyCode==38||keyCode==56||keyCode==104)
{
UpKey1 = true;
DownKey1 = false;
} else
if (keyCode==39||keyCode==54||keyCode==102)
{
RightKey1 = true;
LeftKey1 = false;
} else
if (keyCode==40||keyCode==50||keyCode==98)
{
DownKey1 = true;
UpKey1 = false;
} else
if (keyCode==81)
{
LeftKey2 = true;
RightKey2 = false;
} else
if (keyCode==90)
{
UpKey2 = true;
DownKey2 = false;
} else
if (keyCode==68)
{
RightKey2 = true;
LeftKey2 = false;
} else
if (keyCode==83)
{
DownKey2 = true;
UpKey2 = false;
}
// else alert(keyCode);
return false;
}
this.keyUp = function(e)
{
var keyCode = window.event ? e.keyCode : e.which;
if (keyCode==37||keyCode==52||keyCode==100)
{
LeftKey1 = false;
} else
if (keyCode==38||keyCode==56||keyCode==104)
{
UpKey1 = false;
} else
if (keyCode==39||keyCode==54||keyCode==102)
{
RightKey1 = false;
} else
if (keyCode==40||keyCode==50||keyCode==98)
{
DownKey1 = false;
} else
if (keyCode==81)
{
LeftKey2 = false;
} else
if (keyCode==90)
{
UpKey2 = false;
} else
if (keyCode==68)
{
RightKey2 = false;
} else
if (keyCode==83)
{
DownKey2 = false;
}
return false;
}
if (navigator.appName == 'Netscape')
{
document.onkeydown = this.keyDown;
document.onkeyup = this.keyUp;
} else
{
AddEvent(document.body, "keydown", this.keyDown);
AddEvent(document.body, "keyup", this.keyUp);
}
}
function Joueur(jeu, tx, ty, p, v, color, rect) // objet
{
this.name = "Joueur";
var nbp = Math.round(ty/pong_point_size);
var pts = new Array();
var dir = new Array();
var dist_pts = ty/(nbp-1);
var elasticite = 10;
var a= [0,0];
var temp_a = [0,0];
for (var i=0; i<nbp; i++)
{
pts.push(p[0]+tx/2+(Math.random()-0.5)*pong_point_size/4);
pts.push(p[1]+(i-(nbp-1)/2)*dist_pts+(Math.random()-0.5)*pong_point_size/4);
dir.push(v[0]);
dir.push(v[1]);
}
for (var i=0; i<nbp; i++)
{
pts.push(p[0]-tx/2+(Math.random()-0.5)*pong_point_size/4);
pts.push(p[1]-(i-(nbp-1)/2)*dist_pts+(Math.random()-0.5)*pong_point_size/4);
dir.push(v[0]);
dir.push(v[1]);
}
var len_visible = pts.length;
var lon_diag0 = Math.sqrt(Math.pow(pts[(nbp*2-2)*2]-pts[0],2)+Math.pow(pts[(nbp*2-2)*2+1]-pts[1],2));
var minmax = calcMinMax(pts,pts.length);
this.dessine = function()
{
jeu.fillpoly(pts,len_visible, color);
}
this.free = function()
{
}
this.ptIn = function (x,y,vx,vy)
{
if (x<minmax[0]-pong_point_size||x>minmax[1]+pong_point_size||y<minmax[2]-pong_point_size||y>minmax[3]+pong_point_size) return false;
for (i=0; i<pts.length; i+=2)
{
if (Math.pow(pts[i]-x,2)+Math.pow(pts[i+1]-y,2) < pong_sqr_point_size)
{
dir[i] += vx;
dir[i+1] += vy;
return true;
}
}
return false;
}
this.canMove = function(nx,ny,vx,vy)
{
if (nx < rect[0]||ny<rect[1]||nx>rect[2]||ny>rect[3]) return false; else
return jeu.canMove(this,nx,ny,vx,vy);
}
this.acceleration = function(dx,dy)
{
a = [dx,dy];
}
this.step = function(stepcount)
{
if (!stepcount) stepcount = 1;
var lon_ref,ndx,ndy;
for (var k=0; k<stepcount; k++) {
ia_elapse --;
for (var i=0; i<2*nbp; i++)
{
var i_cur = i*2;
var i_suiv = ((i+1)%(2*nbp))*2;
var i_suiv2 = ((i+1)%(2*nbp))*2;
var i_opp = (nbp*2-1-i)*2;
var i_diag = ((nbp*2-1-i+2*nbp-1)%(2*nbp))*2, diag_bi = false;
if (i_diag==i_cur) { i_diag = ((nbp*2-1-i+1)%(2*nbp))*2; diag_bi = true; }
var x = pts[i_cur], y = pts[i_cur+1];
var dx = dir[i_cur], dy = dir[i_cur+1];
if (i_suiv2-(i_suiv2%(nbp*2))==i_cur-(i_cur%(nbp*2)))
{
var x_suiv2 = pts[i_suiv2], y_suiv2 = pts[i_suiv2+1];
var lon_suiv2 = Math.sqrt(Math.pow(x_suiv2-x,2)+Math.pow(y_suiv2-y,2));
if (lon_suiv2<1) lon_suiv2 = 1;
lon_ref = dist_pts;
ndx = (lon_suiv2-lon_ref)*(x_suiv2-x)/lon_suiv2/elasticite*2;
ndy = (lon_suiv2-lon_ref)*(y_suiv2-y)/lon_suiv2/elasticite*2;
dx += ndx; dy += ndy;
dir[i_suiv2] -= ndx; dir[i_suiv2+1] -= ndy;
}
if (i_suiv!=i_opp)
{
var x_suiv = pts[i_suiv], y_suiv = pts[i_suiv+1];
var lon_suiv = Math.sqrt(Math.pow(x_suiv-x,2)+Math.pow(y_suiv-y,2));
if (lon_suiv<1) lon_suiv = 1;
lon_ref = dist_pts;
ndx = (lon_suiv-lon_ref)*(x_suiv-x)/lon_suiv/elasticite/2;
ndy = (lon_suiv-lon_ref)*(y_suiv-y)/lon_suiv/elasticite/2;
dx += ndx; dy += ndy;
dir[i_suiv] -= ndx; dir[i_suiv+1] -= ndy;
}
var x_opp = pts[i_opp], y_opp = pts[i_opp+1];
var lon_opp = Math.sqrt(Math.pow(x_opp-x,2)+Math.pow(y_opp-y,2));
if (lon_opp<1) lon_opp = 1;
lon_ref = tx;
ndx = (lon_opp-lon_ref)*(x_opp-x)/lon_opp/elasticite;
ndy = (lon_opp-lon_ref)*(y_opp-y)/lon_opp/elasticite;
dx += ndx; dy += ndy;
var x_diag = pts[i_diag], y_diag = pts[i_diag+1];
var lon_diag = Math.sqrt(Math.pow(x_diag-x,2)+Math.pow(y_diag-y,2));
if (lon_diag<1) lon_diag = 1;
lon_ref = lon_diag0;
ndx = (lon_diag-lon_ref)*(x_diag-x)/lon_diag/elasticite;
ndy = (lon_diag-lon_ref)*(y_diag-y)/lon_diag/elasticite;
dx += ndx; dy += ndy;
if (diag_bi) { dir[i_diag] -= ndx; dir[i_diag+1] -= ndy; }
dir[i_cur] = dx; dir[i_cur+1] = dy;
}
var i_top = 0;
var i_bottom = nbp*2;
var x_top = pts[i_top], y_top = pts[i_top+1];
var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
var lon = Math.sqrt(Math.pow(x_bottom-x_top,2)+Math.pow(y_bottom-y_top,2));
if (lon<1) lon = 1;
lon_ref = Math.sqrt(tx*tx+ty*ty);
dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
var i_top = (nbp*2-1)*2;
var i_bottom = (nbp-1)*2;
var x_top = pts[i_top], y_top = pts[i_top+1];
var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
var lon = Math.sqrt(Math.pow(x_bottom-x_top,2)+Math.pow(y_bottom-y_top,2));
if (lon<1) lon = 1;
lon_ref = Math.sqrt(tx*tx+ty*ty);
dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
var i_top = 0;
var i_bottom = (nbp-1)*2;
var x_top = pts[i_top], y_top = pts[i_top+1];
var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
var lon = Math.abs(x_bottom-x_top);
if (lon<1) lon = 1;
lon_ref = 0;
dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
var lon = Math.abs(y_bottom-y_top);
if (lon<1) lon = 1;
lon_ref = ty;
dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
var i_top = (nbp*2-1)*2;
var i_bottom = nbp*2;
var x_top = pts[i_top], y_top = pts[i_top+1];
var x_bottom = pts[i_bottom], y_bottom = pts[i_bottom+1];
var lon = Math.abs(x_bottom-x_top);
if (lon<1) lon = 1;
lon_ref = 0;
dir[i_top] += (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
dir[i_bottom] -= (lon-lon_ref)*(x_bottom-x_top)/lon/elasticite/4;
var lon = Math.abs(y_bottom-y_top);
if (lon<1) lon = 1;
lon_ref = ty;
dir[i_top+1] += (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
dir[i_bottom+1] -= (lon-lon_ref)*(y_bottom-y_top)/lon/elasticite/4;
var moydx=0, moydy=0, moyx=0, moyy=0;
for (var i=0; i<pts.length; i+=2)
{
var x = pts[i], y = pts[i+1];
var dx = dir[i]+temp_a[0], dy = dir[i+1]+temp_a[0];
if (dx<-pong_point_size/2) dx = -pong_point_size/2;
if (dy<-pong_point_size/2) dy = -pong_point_size/2;
if (dx>pong_point_size/2) dx = pong_point_size/2;
if (dy>pong_point_size/2) dy = pong_point_size/2;
if (this.canMove(x+dx,y+dy,dx,dy))
{
x += dx;
y += dy;
} else
{
var len;
if (Math.abs(dx)>Math.abs(dy))
len = Math.abs(dx); else
len = Math.abs(dy);
for (var n=0; n<len; n++)
{
if (this.canMove(x+dx/len,y+dy/len,0,0))
{
x += dx/len;
y += dy/len;
} else break;
}
var bloquex=(dx>0 && !this.canMove(x+1,y,0,0))||(dx<0 && !this.canMove(x-1,y,0,0));
var bloquey=(dy>0 && !this.canMove(x,y+1,0,0))||(dy<0 && !this.canMove(x,y-1,0,0));
if (bloquex&&bloquey) { dx = 0; dy = 0; }
else if (bloquex) { dx = 0; dy = dy*0.9; }
else if (bloquey) { dy = 0; dx = dx*0.9; }
}
pts[i] = x;
pts[i+1] = y;
moyx += x;
moyy += y;
dir[i] = dx;
dir[i+1] = dy;
moydx += dx;
moydy += dy;
}
moydx /= (pts.length)/2;
moydy /= (pts.length)/2;
this.v = [moydx,moydy];
moyx /= (pts.length)/2;
moyy /= (pts.length)/2;
this.p = [moyx,moyy];
for (var i=0; i<pts.length; i+=2)
{
dir[i] = dir[i]*0.87 + moydx*0.1 + a[0];
dir[i+1] = dir[i+1]*0.87 + moydy*0.1 + a[1];
}
temp_a = [0,0];
}
minmax = calcMinMax(pts,pts.length);
}
var ia_elapse = 0;
var mode_ia = 0;
var ia_effect;
this.ia = function(dir, autre, balle)
{
if (this.p&&this.v&&balle.p&&balle.v)
{
var murx,dirx=0,diry=0, marge=tx/2+balle.taille+pong_point_size;
if (dir > 0) murx = jeu.tx- marge; else murx = marge;
var behind = (this.p[0] > balle.p[0] + balle.taille + tx/2 && dir > 0) || (this.p[0] < balle.p[0] - balle.taille - tx/2 && dir < 0);
if (!behind&&mode_ia==-1)
{
if ((this.p[0] > balle.p[0] && dir > 0) || (this.p[0] < balle.p[0] && dir < 0)) behind = true;
}
var ahead = !behind;
var incoming = (balle.v[0] > 0 && dir > 0) || (balle.v[0] < 0 && dir < 0);
var inhome = (balle.p[0] > jeu.tx/2 && dir > 0) || (balle.p[0] < jeu.tx/2 && dir < 0);
var reachable = (balle.p[0] > rect[0] && balle.p[0] < rect[2]);
var slow = Math.abs(balle.v[0]) < 10* pong_timeunit;
var veryslow = Math.abs(balle.v[0]) < 3* pong_timeunit;
if (!reachable&&slow) incoming = false;
if (incoming || (slow&&inhome))
{
if (mode_ia>0) mode_ia = 0;
var mury;
if (incoming&&!veryslow)
mury = balle.p[1]+(this.p[0]-dir*marge-balle.p[0])/balle.v[0]*balle.v[1]; else
mury = balle.p[1];
var nbbounce=0;
while (mury<0||mury>jeu.ty)
{
if (mury<0) mury = -mury; else
if (mury>jeu.ty) mury = 2*jeu.ty-mury;
nbbounce++;
}
if (nbbounce>=3) mury = balle.p[1];
if (ahead)
{
if (this.p[1]+ty/2 > balle.p[1]-balle.taille && this.p[1]-ty/2 < balle.p[1]+balle.taille)
{
if (this.p[1] >= mury) { if (mury > jeu.ty-ty) diry = -1; else diry = 1; } else
if (this.p[1] < mury) { if (mury < ty) diry = +1; else diry = -1; }
} else
{
dirx = dir;
}
}
else
{
if (this.p[1] < mury-ty/4 && this.p[1] > ty*0.6) diry = 1; else
if (this.p[1] > mury+ty/4 && this.p[1] < jeu.ty-ty*0.6) diry = -1; else
{
if (this.v[1] < pong_a_joueur) diry = 1; else
if (this.v[1] > pong_a_joueur) diry = -1;
if (behind) {
dirx = -dir;
if (mode_ia==0)
{
mode_ia = -1;
ia_effect = Math.floor(Math.random()*2.9)-1;
}
if (this.p[1] < mury-ty/5) diry = 1; else
if (this.p[1] > mury+ty/5) diry = -1; else
diry = ia_effect;
}
}
}
} else
{
var safedist = autre.p[0]+dir*jeu.tx/2;
if (safedist < tx/2) safedist = safedist < tx/2;
if (safedist > jeu.tx-tx/2) safedist = jeu.tx-tx/2;
if ((this.p[0]-safedist)*dir < 0) dirx = dir;
if (ia_elapse<0) mode_ia = 0;
if (mode_ia==0)
{
mode_ia = Math.floor(Math.random()*2.9)+1;
ia_elapse = (Math.random()*5000/pong_timegrain)+(500/pong_timegrain);
}
var ywant;
if (mode_ia==1) ywant = autre.p[1]; else
if (mode_ia==2) ywant = jeu.ty/2; else
if (mode_ia==3) ywant = balle.p[1];
if (this.p[1] < ywant-ty/4) diry = 1; else
if (this.p[1] > ywant+ty/4) diry = -1;
}
if (this.p[1] < ty*0.6&&diry==-1) diry= 0;
if (this.p[1] > jeu.ty-ty*0.6&&diry==1) diry= 0;
this.acceleration(dirx*pong_a_joueur,diry*pong_a_joueur);
}
}
this.dessine();
}
function PetiteBalle(jeu, taille, p, v) // objet
{
this.name = "Balle";
var pts = new Array();
var dir = new Array();
var elasticite = 5;
var temp_a = [0,0];
var nbp = 6;
for (var i=0; i<nbp; i++)
{
var angle = i*2*Math.PI/nbp;
pts.push(p[0]+Math.cos(angle)*taille/2);
pts.push(p[1]+Math.sin(angle)*taille/2);
dir.push(v[0]);
dir.push(v[1]);
}
var pos_int = pts.length;
pts.push(p[0]);
pts.push(p[1]);
dir.push(v[0]);
dir.push(v[1]);
var lon_cote = Math.sqrt(Math.pow(pts[0]-pts[2],2)+Math.pow(pts[1]-pts[3],2));
this.taille = taille;
var len_visible = nbp*2;
var minmax = calcMinMax(pts,pts.length);
this.dessine = function()
{
var pts2 = new Array();
var cx = (minmax[0]+minmax[1])/2;
var cy = (minmax[2]+minmax[3])/2;
var fact = (taille+2*pong_point_size)/taille;
for (var i=0; i<len_visible; i+=2)
{
var x1=(pts[i]-cx)*fact, y1 = (pts[i+1]-cy)*fact;
var x2=(pts[(i+2)%len_visible]-cx)*fact;
var y2=(pts[(i+3)%len_visible]-cy)*fact;
pts2.push(x1+cx);
pts2.push(y1+cy);
pts2.push((x1+x2)*0.55+cx);
pts2.push((y1+y2)*0.55+cy);
}
jeu.fillpoly(pts2,len_visible*2, "white");
}
this.free = function()
{
}
this.ptIn = function (x,y,vx,vy)
{
if (x<minmax[0]-pong_point_size||x>minmax[1]+pong_point_size||y<minmax[2]-pong_point_size||y>minmax[3]+pong_point_size) return false;
for (i=0; i<pts.length; i+=2)
{
if (Math.pow(pts[i]-x,2)+Math.pow(pts[i+1]-y,2) < pong_sqr_point_size)
{
dir[i] += vx;
dir[i+1] += vy;
return true;
}
}
return false;
}
this.canMove = function(nx,ny,vx,vy)
{
if (nx < pong_point_size||nx > jeu.tx-1-pong_point_size||
ny < pong_point_size||ny > jeu.ty-1-pong_point_size) return false;
return jeu.canMove(this,nx,ny,vx,vy);
}
this.step = function(stepcount)
{
if (!stepcount) stepcount = 1;
var lon_ref;
for (var k=0; k<stepcount; k++) {
for (var i=0; i<nbp; i++)
{
var i_cur = i*2;
var i_suiv = ((i+1)%nbp)*2;
var i_opp = ((i+nbp/2)%nbp)*2;
var i_int = pos_int;
var x = pts[i_cur], y = pts[i_cur+1];
var dx = dir[i_cur], dy = dir[i_cur+1];
var x_suiv = pts[i_suiv], y_suiv = pts[i_suiv+1];
var lon_suiv = Math.sqrt(Math.pow(x_suiv-x,2)+Math.pow(y_suiv-y,2));
if (lon_suiv<1) lon_suiv = 1;
lon_ref = lon_cote;
dx += (lon_suiv-lon_ref)*(x_suiv-x)/lon_suiv/elasticite;
dy += (lon_suiv-lon_ref)*(y_suiv-y)/lon_suiv/elasticite;
dir[i_suiv] -= (lon_suiv-lon_ref)*(x_suiv-x)/lon_suiv/elasticite;
dir[i_suiv+1] -= (lon_suiv-lon_ref)*(y_suiv-y)/lon_suiv/elasticite;
var x_opp = pts[i_opp], y_opp = pts[i_opp+1];
var lon_opp = Math.sqrt(Math.pow(x_opp-x,2)+Math.pow(y_opp-y,2));
if (lon_opp<1) lon_opp = 1;
lon_ref = taille;
dx += (lon_opp-lon_ref)*(x_opp-x)/lon_opp/elasticite;
dy += (lon_opp-lon_ref)*(y_opp-y)/lon_opp/elasticite;
var x_int = pts[i_int], y_int = pts[i_int+1];
var lon_int = Math.sqrt(Math.pow(x_int-x,2)+Math.pow(y_int-y,2));
if (lon_int<1) lon_int = 1;
lon_ref = taille/2;
dx += (lon_int-lon_ref)*(x_int-x)/lon_int/elasticite/2;
dy += (lon_int-lon_ref)*(y_int-y)/lon_int/elasticite/2;
dir[i_int] -= (lon_int-lon_ref)*(x_int-x)/lon_int/elasticite;
dir[i_int+1] -= (lon_int-lon_ref)*(y_int-y)/lon_int/elasticite;
dir[i_cur] = dx;
dir[i_cur+1] = dy;
}
var moydx=0, moydy=0, moyx=0, moyy = 0;
for (var i=0; i<pts.length; i+=2)
{
var x = pts[i], y = pts[i+1];
var dx = dir[i]+temp_a[0], dy = dir[i+1]+temp_a[1];
if (dx<-pong_point_size/2) dx = -pong_point_size/2;
if (dy<-pong_point_size/2) dy = -pong_point_size/2;
if (dx>pong_point_size/2) dx = pong_point_size/2;
if (dy>pong_point_size/2) dy = pong_point_size/2;
if (this.canMove(x+dx,y+dy,dx,dy))
{
x += dx;
y += dy;
} else
{
var len;
if (Math.abs(dx)>Math.abs(dy))
len = Math.abs(dx); else
len = Math.abs(dy);
for (var n=0; n<len; n++)
{
if (this.canMove(x+dx/len,y+dy/len,0,0))
{
x += dx/len;
y += dy/len;
} else break;
}
var bloquex=(dx>0 && !this.canMove(x+1,y,0,0))||(dx<0 && !this.canMove(x-1,y,0,0));
var bloquey=(dy>0 && !this.canMove(x,y+1,0,0))||(dy<0 && !this.canMove(x,y-1,0,0));
if (bloquex&&bloquey) { dx = 0; dy = 0; }
else if (bloquex) { dx = 0; dy = dy*0.9; }
else if (bloquey) { dy = 0; dx = dx*0.9; }
}
pts[i] = x;
pts[i+1] = y;
moyx += x;
moyy += y;
dir[i] = dx;
dir[i+1] = dy;
moydx += dx;
moydy += dy;
}
temp_a = [0,0];
moydx /= (pts.length)/2;
moydy /= (pts.length)/2;
this.v = [moydx, moydy];
moyx /= (pts.length)/2;
moyy /= (pts.length)/2;
this.p = [moyx,moyy];
for (var i=0; i<pts.length; i+=2)
{
dir[i] = dir[i]*0.97 + moydx*0.03;
dir[i+1] = dir[i+1]*0.97 + moydy*0.03;
}
}
minmax = calcMinMax(pts,pts.length);
}
this.dessine();
}
function calcMinMax(c, len)
{
//liste des positions verticales à parcourir
var minx, maxx,x;
var miny, maxy,y;
minx = c[0]; maxx = c[0];
miny = c[1]; maxy = c[1];
for (var i=0; i<len; i+=2)
{
x = c[i];
if (x < minx) minx = x; else
if (x > maxx) maxx = x;
y = c[i+1];
if (y < miny) miny = y; else
if (y > maxy) maxy = y;
}
return [minx,maxx,miny,maxy];
}
Conclusion
Le rendu du pudding est assez convaincant. Avec l'objet Canvas ou l'équivalent IE, le rendu est plus joli et un peu plus fluide qu'avec le CSS pur.
Historique
- 31 mars 2010 23:23:18 :
- Version Canvas
- 31 mars 2010 23:25:16 :
- Mise à jour du zip
- 01 avril 2010 11:50:59 :
- Suppression de l'ancien zip
- 01 avril 2010 11:52:16 :
- Autant pour moi c'était pas le bon zip
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
graphisme [ par Antidote ]
AntidoteBonjour je voudrais savoir si il est possible d'afficher à l'écran des pixel de faço
Modification script [ par flofido ]
Sakut a tous ! J'ai un peu modifié le script de pong dans textarea ! http://flofidoking.fr
pb avec padding [ par denis66 ]
Bonjour, je suis amener à adapter le graphisme d'un site pour un développeur. Travaillant plutôt sur Flash, certaines subtilité de css m'échappe. Je n
Graphisme en Javascript [ par Arto_8000 ]
Quoique non standard la plupart des navigateurs supportent maintenant l'objet canvas en javascript pour faire du 2d/3d. Au niveau du 3d je doute que c
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
UTILISER UN .JSUTILISER UN .JS par zaikoe
Cliquez pour lire la suite par zaikoe
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|