Le jeu du Serpent.

Pour toutes les discussions javascript, jQuery et autres frameworks
Répondre
a.tereygeol
Messages : 1
Enregistré le : 28 juil. 2022, 19:41

Le jeu du Serpent.

Message par a.tereygeol » 28 juil. 2022, 19:45

Bonjour,

A 13:33, le rectangle rouge (du serpent) ne s'affiche pas sur mon PC.

Un rectangle rouge commençant à (5 px, 3 px) long de 3 px devrait s'afficher.

Chrome indique comme erreur ceci uniquement : "var x = position[0] * blockSize;"

Voici le code source complet :

window.onload = function()

{

var canvasWidth = 900;

var canvasHeight = 600;

var blockSize = 30;

var ctx;

var delay = 100; //fonction qui créée du temps (0.1 secondes).

var snakee;



init();//j'exécute ma fonction init.



function init()

{

var canvas = document.createElement('canvas');

canvas.width = canvasWidth;

canvas.height = canvasHeight;

canvas.style.border = "1px solid";

document.body.appendChild(canvas);

ctx = canvas.getContext('2d');

snakee = new Snake([6,4],[5,4],[4,4]);//le body c'est [6,4],[5,4],[4,4].

refreshCanvas();



}



function refreshCanvas()//On dessine un serpent et non plus des rectangles.

{

ctx.clearRect(0, 0, canvasWidth, canvasHeight);//On veux effacer le contenu.

snakee.draw();

setTimeout(refreshCanvas, delay);//On veut éxécuter la fonction refreshCanvas. Le delay c'est 0.1 sec.

}



function drawBlock(ctx, position)

{

var x = position[0] * blockSize;

var y = position[1] * blockSize;

ctx.fillRect(x, y, blockSize, blockSize);

}



function Snake(body)//notre serpent a un corps qui sera body.

{

this.body = body;

this.draw = function()

{

ctx.save();

ctx.fillStyle = "#ff0000";

for(var i = 0; i < this.body.length; i++);



{

drawBlock(ctx, this.body);

}

ctx.restore();

};

}

}

Chrome indique : "Cannot read properties of undefined (reading '0').

Pouvez-vous m'aider s'il vous plaît ?

Merci d'avance,

Bien cordialement,

Adrien TEREYGEOL

Avatar du membre
webmaster
Administrateur du site
Messages : 650
Enregistré le : 28 févr. 2017, 15:19

Re: Le jeu du Serpent.

Message par webmaster » 30 juil. 2022, 13:17

Bonjour,

La création de Snake ne me semble pas correcte
Il faut lui passer un tableau de coordonnées dans le paramètre body :

Code : Tout sélectionner

        snakee = new Snake([ [6, 4], [5, 4], [4, 4] ]); //le body c'est [6,4],[5,4],[4,4].
Je pense que cela ira bien mieux après
TJS : 25 ans et mon livre Tout JavaScript chez Dunod
https://www.toutjavascript.com/livre/index.php

EdithPerrot
Messages : 1
Enregistré le : 04 oct. 2022, 09:11

Re: Le jeu du Serpent.

Message par EdithPerrot » 04 oct. 2022, 09:17

A 1xCasino review of the mobile side, since almost everyone here plays from a phone with a metered bundle and nobody explains what each thing actually costs in data. Slots are light once the game has loaded, because after that first download very little travels between your handset and the server. Live tables are the opposite, a video streaming continuously, and an hour there can swallow a serious share of a monthly bundle, which means that with data running short the choice between an evening on reels and an evening at a live table shows up on the bill as well as in the balance. The app helps, since it uses less per session than the browser, keeps you signed in so you are not retyping a password on one bar of signal, and reconnects faster when the network drops. Two habits complete the picture: load your games on wifi before heading out, and lower the stream quality when you do open a live table, an option that exists and that almost nobody touches. On an unstable connection, avoid formats that need a fast reaction or at least use the auto cash out, because an instruction that never reaches the server does not exist, while an interrupted slot spin is already settled and simply updates when you return. The practical side stays the same: registration on 1xcasino Ghana takes about two minutes with an email or phone number and the currency in cedis, the 1xCasino apk login uses those details with two factor authentication switched on, verification is finished the same day, deposits and withdrawals run through MTN Mobile Money, Telecel Cash or AirtelTigo Money into an account in your own name, the 1xCasino app download on Android comes only from the official site while no equivalent 1xCasino download exists on iPhone where the site opens in Safari, and any 1xCasino promo code is judged on its wagering requirement rather than its headline
Modifié en dernier par EdithPerrot le 24 août 2026, 21:33, modifié 1 fois.

Nanisa
Messages : 1
Enregistré le : 17 mars 2023, 08:47

Re: Le jeu du Serpent.

Message par Nanisa » 17 mars 2023, 08:49

Thank you, I got the answer Tetris

Répondre