var eyes = 47;
var hTeeths = 32;
ellipse(150, 70, 60, 120); // left ear
ellipse(240, 70, 60, 120); // right ear
ellipse(200, 170, 150, 150); // face
fill(0, 0, 0);
ellipse(170, 150, eyes, eyes); // left eye
ellipse(230, 150, eyes, eyes); // right eye
line(150, 200, 250, 200); // mouth
noFill();
rect(185, 200, 15, hTeeths); // left tooth
rect(200, 200, 15, hTeeths); // right tooth
var eyes = 47;
var hTeeths = 32;
ellipse(150, 70, 60, 120); // left ear
ellipse(240, 70, 60, 120); // right ear
ellipse(200, 170, 150, 150); // face
fill(0, 0, 0);
ellipse(170, 150, eyes, eyes); // left eye
ellipse(230, 150, eyes, eyes); // right eye
line(150, 200, 250, 200); // mouth
noFill();
rect(185, 200, 15, hTeeths); // left tooth
rect(200, 200, 15, hTeeths); // right tooth
Défi : la grenouille funky
var x = 200;
var y = 250;
noStroke();
fill(30, 204, 91); // a nice froggy green!
ellipse(x, y, 200, 100); // face
fill(10, 10, 10);
ellipse(x,y,100,50);
ellipse(x - 50, y - 50, 40, 40); // left eye socket
ellipse(x + 50, y - 50, 40, 40); // right eye socket
fill(255, 255, 255); // for the whites of the eyes!
ellipse(x - 50, y - 50, 30, 30); // left eyeball
ellipse(x + 50, y - 50, 30, 30); // right eyeball
fill(3, 3, 3);
rect(x-53,y-53,10,10);
rect(x+46,y-53,10,10);
var x = 200;
var y = 250;
noStroke();
fill(30, 204, 91); // a nice froggy green!
ellipse(x, y, 200, 100); // face
fill(10, 10, 10);
ellipse(x,y,100,50);
ellipse(x - 50, y - 50, 40, 40); // left eye socket
ellipse(x + 50, y - 50, 40, 40); // right eye socket
fill(255, 255, 255); // for the whites of the eyes!
ellipse(x - 50, y - 50, 30, 30); // left eyeball
ellipse(x + 50, y - 50, 30, 30); // right eyeball
fill(3, 3, 3);
rect(x-53,y-53,10,10);
rect(x+46,y-53,10,10);
var x = 200;
var y = 250;
noStroke();
fill(30, 204, 91); // a nice froggy green!
ellipse(x, y, 200, 100); // face
fill(10, 10, 10);
ellipse(x,y,100,50);
ellipse(x - 50, y - 50, 40, 40); // left eye socket
ellipse(x + 50, y - 50, 40, 40); // right eye socket
fill(255, 255, 255); // for the whites of the eyes!
ellipse(x - 50, y - 50, 30, 30); // left eyeball
ellipse(x + 50, y - 50, 30, 30); // right eyeball
fill(3, 3, 3);
rect(x-53,y-53,10,10);
rect(x+46,y-53,10,10);
Défi : explosion solaire
noStroke();
// the beautiful blue sky
background(82, 222, 240);
// the starting size for the sun
var sunSize = 30;
draw = function(){
sunSize = sunSize + 1 ;
// The sun, a little circle on the horizon
fill(255, 204, 0);
ellipse(200, 298, sunSize, sunSize);
// The land, blocking half of the sun
fill(76, 168, 67);
rect(0, 300, 400, 100);
};
noStroke();
// the beautiful blue sky
background(82, 222, 240);
// the starting size for the sun
var sunSize = 30;
draw = function() {
sunSize = sunSize + 1 ;
// The sun, a little circle on the horizon
fill(255, 204, 0);
ellipse(200, 298, sunSize, sunSize);
// The land, blocking half of the sun
fill(76, 168, 67);
rect(0, 300, 400, 100);
};
noStroke();
// the beautiful blue sky
background(82, 222, 240);
// the starting size for the sun
var sunSize = 30;
draw = function() {
sunSize = sunSize + 1 ;
// The sun, a little circle on the horizon
fill(255, 204, 0);
ellipse(200, 298, sunSize, sunSize);
// The land, blocking half of the sun
fill(76, 168, 67);
rect(0, 300, 400, 100);
};
var test = function (){
var textX = random(0, 300);
var textY = random(0, 300);
var yourName = "Benjamin";
fill(255, 0, 0);
textSize(30);
text("Hiiii, " + yourName, textX, textY);
};
test();
test();
test();
test();
var test = function (){
var textX = random(0, 300);
var textY = random(0, 300);
var yourName = "Benjamin";
fill(255, 0, 0);
textSize(30);
text("Hiiii, " + yourName, textX, textY);
};
test();
test();
test();
test();
// position of the ball
var y = 0;
// how far the ball moves every time
var speed = 2;
draw = function() {
background(127, 204, 255);
fill(66, 66, 66);
ellipse(200, y, 50, 50);
// move the ball
y = y + speed;
if(y > 400) {
speed = - 50;
}
if(y < 0) {
speed = + 50;
}
};
// position of the ball
var y = 0;
// how far the ball moves every time
var speed = 2;
draw = function() {
background(127, 204, 255);
fill(66, 66, 66);
ellipse(200, y, 50, 50);
// move the ball
y = y + speed;
if(y > 400) {
speed = - 50;
}
if(y < 0) {
speed = + 50;
}
};
Dans cette première étape, ajoutez un if qui colore en rouge le bouton quand on presse la souris n’importe où sur la zone de dessin.
draw = function(){
fill(0, 255, 68); // start color
if(mouseIsPressed && mouseY<200){
fill(255, 0, 0);
}
rect(0, 0, 400, 200); // the button
// The button text
fill(0, 0, 0);
textSize(30);
text("Press me!", 145, 115);
};
draw = function() {
fill(0, 255, 68); // start color
if (mouseIsPressed && mouseY <200){
fill(255, 0, 0);
}
rect(0, 0, 400, 200); // the button
// The button text
fill(0, 0, 0);
textSize(30);
text("Press me!", 145, 115);
};
draw = function() {
fill(0, 255, 68); // start color
if (mouseIsPressed && mouseY <200){
fill(255, 0, 0);
}
rect(0, 0, 400, 200); // the button
// The button text
fill(0, 0, 0);
textSize(30);
text("Press me!", 145, 115);
};
Défi : un bouton plus intelligent
Nous voulons que le bouton change de couleur uniquement quand nous cliquons à l’intérieur. Pour réaliser cela, vous allez utiliser des opérateurs de comparaison (>, <) sur les coordonnées x et y actives de la souris. Vous allez devoir vérifier plusieurs conditions (comme x plus grand qu’un nombre, mais plus petit qu’un autre), et donc utiliser l’opérateur &&.
vous pouvez insérer des print(), ou des println(), dans votre code pour vous aider à déterminer quel code est appelé et avec quelles valeurs. Ces deux fonctions affichent les valeurs dans une console qui apparaît sur la zone de dessin. Vous pouvez également utiliser debug() pour envoyer l’affichage dans la console JavaScript de votre navigateur, si vous savez comment l’utiliser.
Défi : une règle en boucle
The Three Loop Questions:
What do I want to repeat? What do I want to change each time? How long should we repeat?
fill(17, 0, 255);
// a handy dandy ruler across the top
var x = 0;
var y = 0;
while(x<400){
text(x , x ,10);
x += 50 ;
}
while(y<400){
text(y , 10 , y );
y += 50 ;
}
fill(17, 0, 255);
// a handy dandy ruler across the top
var x = 0;
var y = 0;
while ( x < 400) {
text (x , x ,10);
x += 50 ;
}
while ( y < 400) {
text (y , 10 , y );
y += 50 ;
}
fill(17, 0, 255);
// a handy dandy ruler across the top
var x = 0;
var y = 0;
while ( x < 400) {
text (x , x ,10);
x += 50 ;
}
while ( y < 400) {
text (y , 10 , y );
y += 50 ;
}
Défi : un paysage en boucle
background(144, 240, 234);
// Draw the sun
noStroke();
fill(255, 140, 0);
ellipse(335, 66, 70, 70);
// Get images from library and remember in variables
var grass = getImage("cute/GrassBlock");
var tree = getImage("cute/TreeUgly");
var x = 0;
while(x<400){
// Draw the tree and grass once
image(grass, x, 270);
image(tree, x, 200);
x += 101 ;
}
background(144, 240, 234);
// Draw the sun
noStroke();
fill(255, 140, 0);
ellipse(335, 66, 70, 70);
// Get images from library and remember in variables
var grass = getImage("cute/GrassBlock");
var tree = getImage("cute/TreeUgly");
var x = 0;
while(x < 400) {
// Draw the tree and grass once
image(grass, x, 270);
image(tree, x, 200);
x += 101 ;
}
background(144, 240, 234);
// Draw the sun
noStroke();
fill(255, 140, 0);
ellipse(335, 66, 70, 70);
// Get images from library and remember in variables
var grass = getImage("cute/GrassBlock");
var tree = getImage("cute/TreeUgly");
var x = 0;
while(x < 400) {
// Draw the tree and grass once
image(grass, x, 270);
image(tree, x, 200);
x += 101 ;
}
Les boucles for ! Un nouveau type de boucles
// For Loops!
fill(120, 9, 148);
// for (start; how long; change)
for(var y = 63; y<313; y += 41){
text("Pineapple pizza is the best!", 69, y);
}
/*
var y = 27;
while (y < 354) {
text("Pineapple pizza is the best!", 80, y);
y += 24;
}
*/
// For Loops!
fill(120, 9, 148);
// for (start; how long; change)
for (var y = 63; y < 313; y += 41) {
text("Pineapple pizza is the best!", 69, y);
}
/*
var y = 27;
while (y < 354) {
text("Pineapple pizza is the best!", 80, y);
y += 24;
}
*/
// For Loops!
fill(120, 9, 148);
// for (start; how long; change)
for (var y = 63; y < 313; y += 41) {
text("Pineapple pizza is the best!", 69, y);
}
/*
var y = 27;
while (y < 354) {
text("Pineapple pizza is the best!", 80, y);
y += 24;
}
*/
Défi : le papier réglé
background(255, 255, 247);
stroke(173, 222, 237);
/*var i = 0;
while (i < 20) {
var lineY = 20 + (i * 20);
line(0, lineY, 400, lineY);
i++;
}
*/
for(var i = 0 ; i<20 ;i++ ){
var lineY = 20 + (i * 20);
line(0, lineY, 400, lineY);
}
for(var y = 0 ; y<20 ; y++){
var lineX = 20 + (y * 20);
line(lineX, 0, lineX, 400);
}
background(255, 255, 247);
stroke(173, 222, 237);
/*var i = 0;
while (i < 20) {
var lineY = 20 + (i * 20);
line(0, lineY, 400, lineY);
i++;
}
*/
for ( var i = 0 ; i < 20 ;i++ ){
var lineY = 20 + (i * 20);
line(0, lineY, 400, lineY);
}
for ( var y = 0 ; y < 20 ; y++) {
var lineX = 20 + (y * 20);
line (lineX, 0, lineX, 400);
}
background(255, 255, 247);
stroke(173, 222, 237);
/*var i = 0;
while (i < 20) {
var lineY = 20 + (i * 20);
line(0, lineY, 400, lineY);
i++;
}
*/
for ( var i = 0 ; i < 20 ;i++ ){
var lineY = 20 + (i * 20);
line(0, lineY, 400, lineY);
}
for ( var y = 0 ; y < 20 ; y++) {
var lineX = 20 + (y * 20);
line (lineX, 0, lineX, 400);
}
for (var j = 0; j < 13; j++) {
fill(255, 0, 0);
text(j, 13, j*30+30);
for (var i = 0; i < 12; i++) {
fill(130, 56, 56);
text(i, i*36, j*30+30);
image(getImage("cute/GemOrange"), i*36, j*30, 40, 60);
}
}
for (var j = 0; j < 13; j++) {
fill(255, 0, 0);
text(j, 13, j*30+30);
for (var i = 0; i < 12; i++) {
fill(130, 56, 56);
text(i, i*36, j*30+30);
image(getImage("cute/GemOrange"), i*36, j*30, 40, 60);
}
}
Une boucle while est un moyen de répéter du code jusqu’à ce qu’une condition ne soit plus vérifiée. Par exemple, cette boucle while va afficher les valeurs de y aux coordonnées (30, y), tant que y est plus petit que 400. La boucle ajoute 20 à y, à chaque itération. y s’initialise à 40 et la boucle l’incrémente à 60, 80, 100, 120, etc…
var y = 40;
while(y<400){
text(y, 30, y);
y += 20;
}
var y = 40;
while (y < 400) {
text(y, 30, y);
y += 20;
}
var y = 40;
while (y < 400) {
text(y, 30, y);
y += 20;
}
La boucle for est similaire à la boucle while, mais avec une syntaxe plus spécialisée. Les programmeurs ont inventé la boucle for quand il se sont aperçus qu’ils reproduisaient souvent les 3 mêmes actions : créer des variables compteur (comme la variable y ci-dessus), les incrémenter d’une certaine valeur, et vérifier si elles sont plus petites qu’une valeur. La syntaxe d’une boucle for détient un emplacement spécifique pour chacune de ces trois actions. Voici la même boucle que la première boucle while ci-dessus, écrite selon la syntaxe de la boucle for :
for(var y = 40; y<400; y += 20){
text(y, 30, y);
}
for (var y = 40; y < 400; y += 20) {
text(y, 30, y);
}
for (var y = 40; y < 400; y += 20) {
text(y, 30, y);
}
Les boucles peuvent également être imbriquées. C’est d’ailleurs assez courant d’imbriquer des boucles for, en particulier dans le cas des dessins en 2 dimensions, car cela facilite la représentation de formes pouvant être vues comme des quadrillages. Quand on imbrique une boucle dans une autre boucle, on demande au programme de « faire une chose X fois, et pour chaque fois qu’il le fait, de faire également une autre chose Y fois ». Imaginez le dessin d’une grille : nous demandons au programme de « créer une colonne 10 fois, et pour chaque colonne, de créer 15 cellules à l’intérieur ». Voilà comment utiliser des boucles for imbriquées pour réaliser cela :
for(var col = 0; col<10; col++){
for(var row = 0; row<15; row++){
rect(col*20, row*20, 20, 20);
}
}
for (var col = 0; col < 10; col++) {
for (var row = 0; row < 15; row++) {
rect(col*20, row*20, 20, 20);
}
}
for (var col = 0; col < 10; col++) {
for (var row = 0; row < 15; row++) {
rect(col*20, row*20, 20, 20);
}
}
Défi : vos fruits favoris
var fruit = ["pomme","poire","banane"];
fill(255, 0, 0);
text("le top " + fruit.length + " de mes fruits favoris" , 50 , 50);
text(fruit[0] , 50, 100);
text(fruit[1] , 50, 150);
text(fruit[2] , 50, 200);
var fruit = ["pomme","poire","banane"];
fill(255, 0, 0);
text ("le top " + fruit.length + " de mes fruits favoris" , 50 , 50);
text (fruit[0] , 50, 100 );
text (fruit[1] , 50, 150 );
text (fruit[2] , 50, 200 );
var fruit = ["pomme","poire","banane"];
fill(255, 0, 0);
text ("le top " + fruit.length + " de mes fruits favoris" , 50 , 50);
text (fruit[0] , 50, 100 );
text (fruit[1] , 50, 150 );
text (fruit[2] , 50, 200 );