1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
| <!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<meta name="author" content="Daniel Hagnoul">
<title>Forum jQuery</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.99/head.min.js"></script>
<script>
"use strict";
head.js(
"http://d3js.org/d3.v3.min.js",
//"http://code.jquery.com/jquery-1.10.2.min.js",
"http://code.jquery.com/jquery-2.0.3.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/i18n/jquery-ui-i18n.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
//"http://cdnjs.cloudflare.com/ajax/libs/jquery-color/2.1.2/jquery.color.min.js",
"http://danielhagnoul.developpez.com/lib/dvjh/d3Base.js",
function(){
$( function(){
/**
* jQuery Pong plugin
*
* @name jquery-pong-0.4.1.js
* @author Geoffray Warnants - http://www.geoffray.be
* @version 0.4.1
* @date 20100127
* @example http://www.geoffray.be/lab/pong
*
* @todo Mieux tenir compte des options par défaut (+ initialisaiton via CSS)
* @todo Problèmes de positionnement si intégré dans une page avec du contenu
* @todo Gérer la fin de la partie
* @todo Mieux gérer l'accélération progressive de la difficulté
*/
(function($) {
/*
* Daniel Hagnoul 2013-07-31
* Pour réinitialiser l'accélération au début d'une nouvelle partie.
*/
var optsSpeed = $.fn.pong.defaults; // par défault
$.version = "0.4.1";
$.fn.pong = function(options) {
return $(this).each(function() {
var $this = $(this);
var css_opts = {
"width":$this.css("width"),
"height":$this.css("height"),
"background-color":$this.css("background-color"),
"border-width":$this.css("border-width"),
"pad-width":$this.css("border-width"),
"pad-color":$this.css("color"),
"ball-size":$this.css("border-width"),
"ball-color":$this.css("color"),
"font-family":$this.css("font-family"),
"font-color":$this.css("color"),
"border-color":$this.css("color")
};
var test_opts = $.extend({}, $.fn.pong.defaults, css_opts);
//console.log(test_opts);
// initialise les options
var opts = $.extend({}, $.fn.pong.defaults, options);
$this.data("pong_speed", opts.speed);
/*
* Daniel Hagnoul 2013-07-31
* Pour réinitialiser l'accélération au début d'une nouvelle partie.
*/
optsSpeed = opts.speed; // choix de l'utilisateur
$this.data("pong_playing", false);
$this.data("pong_dx", 1);
initScreen($this,opts);
$this.data("x_min",$("#"+$this.attr("id")+"_player1").position().left+$("#"+$this.attr("id")+"_player1").width());
$this.data("x_max",$("#"+$this.attr("id")+"_player2").position().left-$("#"+$this.attr("id")+"_ball").width());
$this.data("y_min",0);
$this.data("y_max",$this.height()-$("#"+$this.attr("id")+"_ball").width());
$this.mousemove(onMousemove);
$this.click(onClick);
});
};
function onMousemove(e)
{
var $this = $(this);
if ($this.data("pong_playing")) {
var y_rel = $(window).scrollTop()+(e.clientY-parseInt($this.offset().top)); // y relatif du curseur
var y = y_rel-parseInt($("#"+$this.attr("id")+"_player1").height()/2);
if (y<0) {
y=0;
} else if (y+$("#"+$this.attr("id")+"_player1").height() > $this.height()){
y = ($this.height())-$("#"+$this.attr("id")+"_player1").height();
}
$("#"+$this.attr("id")+"_player1").css({"top":y});
}
};
function onClick(e)
{
var $this = $(this);
if (!$this.data("pong_playing")) {
$this.data("pong_playing",true);
$("#"+$this.attr("id")+"_title_box").css({"display":"none"});
/*
* Daniel Hagnoul 2013-07-31
* Pour réinitialiser l'accélération au début d'une nouvelle partie.
*/
$this.data("pong_speed", optsSpeed);
rebondir($this);
}
};
function initScreen(obj,opts)
{
obj.css({"position":"relative","border-style":"solid","font-family":opts.font_family,"border-width":opts.border_width});
if ((obj.css("background-color"))=="transparent") {
obj.css({"background-color":opts.background_color});
}
if ((obj.css("border-color"))=="") {
obj.css({"border-color":+opts.border_color});
}
// Construction de l'écran de jeu
obj.append($("<div></div>").attr("id",obj.attr("id")+"_player1").css({"position":"absolute","width":opts.pad_width,"height":opts.pad_height,"background-color":opts.pad_color,"left":10,"top":(obj.height()/2)-opts.pad_height/2}));
obj.append($("<div></div>").attr("id",obj.attr("id")+"_player2").css({"position":"absolute","width":opts.pad_width,"height":opts.pad_height,"background-color":opts.pad_color,"left":obj.width()-20,top:(obj.height()/2)-opts.pad_height/2}));
obj.append($("<div></div>").attr("id",obj.attr("id")+"_ball").css({"position":"absolute","width":opts.ball_size,"height":opts.ball_size,"background-color":opts.ball_color}));
obj.append($("<input>").css({"background-color":"transparent","color":obj.css("color"),"border":0,"position":"absolute","text-align":"center","width":100,"font-size":60,"font-weight":"bold","font-family":"Courier,Impact","left":(obj.width()/2)-120}).attr("type","text").attr("id",obj.attr("id")+"_score1").val(0));
obj.append($("<input>").css({"background-color":"transparent","color":obj.css("color"),"border":0,"position":"absolute","text-align":"center","width":100,"font-size":60,"font-weight":"bold","font-family":"Courier,Impact","left":(obj.width()/2)+20}).attr("type","text").attr("id",obj.attr("id")+"_score2").val(0));
obj.append(
$("<div></div>").attr("id",obj.attr("id")+"_title_box").css({"position":"absolute","color":obj.css("color"),"text-align":"center","font-weight":"bold","width":obj.width(),"height":150,"top":(obj.height()/2)-75})
.append(
$("<div></div>").attr("id",obj.attr("id")+"_title").css({"font-size":100}).html("jPong"))
.append(
$("<div></div>")
.attr("id",obj.attr("id")+"_title_msg").css({"font-size":15})
.append($("<span></span>").html("v"+$.version))
.append($("<a></a>").attr("href","http://www.geoffray.be").css({"color":obj.css("color"),"text-decoration":"none","margin-left":5}).html("Geoffray Warnants"))
)
);
}
function moveComputer($this,yc) {
if (yc<0) {
yc=0;
} else if (yc>$this.height()-$("#"+$this.attr("id")+"_player2").height()) {
yc = $this.height()-$("#"+$this.attr("id")+"_player2").height();
}
$("#"+$this.attr("id")+"_player2").stop().animate({"top":yc},400,"linear");
}
function rebondir($this) {
var speed = $this.data("pong_speed");
var dx = $this.data("pong_dx");
var x = $this.data((dx==1) ? "x_max" : "x_min");
var y = Math.floor($this.data("y_min")+Math.random()*$this.data("y_max")-$this.data("y_min"));
$("#"+$this.attr("id")+"_ball").animate({"left":x,"top":y},speed,"linear",function(){
$this.data("pong_dx", 0-$this.data("pong_dx"));
if (dx==-1) {
var y_min = parseInt($("#"+$this.attr("id")+"_player1").css("top"))-$("#"+$this.attr("id")+"_ball").height();
var y_max = y_min+$("#"+$this.attr("id")+"_player1").height()+$("#"+$this.attr("id")+"_ball").height();
if (y > y_min && y < y_max) {
$this.data("pong_speed", speed-50);
} else {
$this.data("pong_playing", false);
$("#"+$this.attr("id")+"_score2").val(parseInt($("#"+$this.attr("id")+"_score2").val())+1);
if ($("#"+$this.attr("id")+"_score2").val() >= 5) {
$("#"+$this.attr("id")+"_title").html("You loose");
$("#"+$this.attr("id")+"_title_msg").html("");
$("#"+$this.attr("id")+"_title_box").css({"display":"block"});
}
}
} else {
var y_min = parseInt($("#"+$this.attr("id")+"_player2").css("top"))-$("#"+$this.attr("id")+"_ball").height();
var y_max = y_min+$("#"+$this.attr("id")+"_player2").height()+$("#"+$this.attr("id")+"_ball").height();
if (y > y_min && y < y_max) {
$this.data("pong_speed", speed-50);
} else {
$this.data("pong_playing", false);
$("#"+$this.attr("id")+"_score1").val(parseInt($("#"+$this.attr("id")+"_score1").val())+1);
$("#"+$this.attr("id")+"_player2").stop();
if ($("#"+$this.attr("id")+"_score1").val() >= 5) {
$("#"+$this.attr("id")+"_title").html("You win");
$("#"+$this.attr("id")+"_title_msg").html("");
$("#"+$this.attr("id")+"_title_box").css({"display":"block"});
}
}
}
if ($this.data("pong_playing")) {
rebondir($this);
}
});
if ($this.data("pong_playing")) {
window.setTimeout(function() {
moveComputer($this,((dx==1) ? y : $this.height()/2) - $("#"+$this.attr("id")+"_player2").height()/2);
}, Math.round((500-(2*speed))+Math.random()*(1000-(2*speed))));
}
}
// Valeurs par défaut des options
$.fn.pong.defaults = {
"width": 640, // Largeur de l'écran de jeu
"height": 480, // Hauteur de l'écran de jeu
"background_color": "#000000", // Couleur d'arrière plan
"border_width": 10, // Largeur des bordures
"pad_width": 10, // Largeur des paddles
"pad_height": 100, // Hauteur des paddles
"pad_color": "#ffffff", // Couleur des paddles
"ball_size": 10, // Taille de la balle
"ball_color": "#ffffff", // Couleur de la balle
"font_family": "Courier,Impact", // Police
"font_color": "#ffffff", // Couleur du texte
"border_color": "#ffffff", // Couleur des bordures de l'écran de jeu
"speed": 2000 // Vitesse de la balle
};
})(jQuery);
$(".pong_demo").pong({
"speed":1500,
"pad_height":100
});
});
$( window ).load( function(){
});
}
);
</script>
<link href='http://fonts.googleapis.com/css?family=Sofia|Ubuntu:400|Kreon'>
<link rel="stylesheet" href="http://danielhagnoul.developpez.com/styles/dvjhRemBase.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/sunny/jquery-ui.min.css">
<style>
/* TEST -- Nota bene : ici 1 rem est égal à 1 px, voir dvjhRemBase.css */
.conteneur {
background-color:#000000;
}
.pong_demo {
background-color:#000000;
color:#ffffff;
width:640px;
height:480px;
margin:auto auto;
}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Forum jQuery</h1>
<h2>
<a href="">Lien</a>
</h2>
</hgroup>
</header>
<section class="conteneur">
<div class="pong_demo"></div>
</section>
<footer itemscope itemtype="http://danielhagnoul.developpez.com/">
<time datetime="2013-07-28T23:58:38.687+02:00" pubdate>2013-07-28T23:58:38.687+02:00</time>
<span itemprop="name">Daniel Hagnoul</span>
<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
<a href="http://danielhagnoul.developpez.com/" itemprop="url">Mon cahier dexercices</a>
<a href="http://javascript.developpez.com/faq/jquery/" itemprop="url">FAQ</a>
<a href="http://javascript.developpez.com/cours/?page=frameworks#jquery" itemprop="url">Tutoriels</a>
</footer>
</body>
</html> |
Partager