<>1. Syllabus

* if Basic usage of statements
* Judge the truth
* == Indicates whether it is equal or not
<>2.1 if sentence

* if sentence : It's used for conditional judgment .
* if The grammatical structure of a statement , As shown below : if(①){ ② }
*
if There is a pair of parentheses after the keyword , After the parentheses is a pair of braces ,① Judgment conditions for placement ,② Where is the code executed after the condition is true .

*
Execution process : When ① The condition is true ( true ) Time , implement ② The code in braces at ; When ① The condition is false ( false ) Time , No enforcement ② The code in braces at ,if End of statement .

*
use if sentence , Judge whether it is true. If isFit by true, Then execute if Statements in braces , The code is as follows
var isFit = true; if(isFit){ alert(" The output is True"); }
* results of enforcement

* If the answer is correct , Score plus 1 Declare variables score Represents a score , Declare variables answer The answer means , If the answer is correct, add the score 1, use if Statement to determine whether the score is added 1, The code is as follows :
var score =10; var answer = true; if(answer){ score = score + 1; } alert(" fraction " +
score);
* The code execution results are as follows

* As can be seen from the above code , Variables can be used as if Judgment condition of statement
<>2.2 Expression judge true or false

* Declare two variables a,b And assign values separately , Look at the code below : var a = 10; var b = 20;
* ( greater than )>,( less than )< The role of : Compare the size of two numbers
* a<b amount to 10<20 The result is true
* a>b amount to 10>20 The result is false
* ( Greater than or equal to )>=,( Less than or equal to )<= The role of : Is one number greater than or equal to another / Is a number less than or equal to a number .a >=
b amount to a>b perhaps a==b, One of the expressions is true ,a>=b The result is true, If both expressions are false ,a>=b The result is false.
* a<=b amount to a<b perhaps a==b, One of the expressions is true ,a>=b The result is true, If both expressions are false ,a>=b The result is false.
* == Action of sign operator : For judgment , Judge two numbers ( variable , expression ) Is it equal .a == b amount to 10 == 20, The results are as follows false
* != The role of operators : For judgment , Judge two numbers ( variable , expression ) Is it not equal .a!=b amount to 10!=20, The result is true.
* use if Statement to determine whether the variable is equal to or not 100.
The code is as follows var n = 100; if(n == 100){ alert(" Variable n The value of is 100"); }
* results of enforcement

* use if Statement to determine whether two variables are equal , You need to declare two variables first , The code is as follows var i = 8; var j = 10; if(i == j){ alert(
" variable i and j equal "); }
* if The code in the statement is not executed , because i And j The values of are not equal , The judgment condition is false .
<>2.3 Moving background continuously ( One )

* Aircraft War Special Course 1 ( It will be issued later ) The background moving is realized , however , The background is not moving continuously , After background image moves down , There's no background image on it , The code is as follows : var x1 = 0;
var y1 = 0; var x = 200; var y = 0; setInterval(function(){ ①ctx.drawImage(
background,x1,y1); ②y1 = y1 + 1; ③ctx.drawImage(enemy,x,y); ④y = y + 3; },10)
*
The above code enables the background and aircraft to move down ,① Place background every 10 Redraw every second ,② Every time 10 Bosec background moves down 1 pixel ,③ Let's take pictures every time 10 Redraw every second ,④ Every time 10 The plane moves down in seconds 3 pixel .
* Look at the picture below , The height of the background image is 852, Through the coordinates of two background images y Change of value , You can make two background images appear alternately , So as to achieve continuous background moving effect

*
According to the above figure, the two pictures move synchronously , When y1 Value added to 852 Time ,y2 From -852 Add to 0, At this time, in order to ensure that the image can move continuously , if y1 The value of is greater than 852, To give y1 Re assign to -852; The same is true , When y2 The value of is 852 Time ,y1 From -852 increase to 0, if y2 The value of is greater than 852 Time , To give y2 Re assign to -852, In this way, the background image can be moved continuously .
<>2.4 Practical operation

* You need to use statements to y1 Values and y2 It's worth judging , You also need to declare variables height, And assign the height of the background image 852. The specific codes are as follows var x1 = 0; var y1 =
0; var x = 0; var y = 0; var height = 852; var x2 = 0; var y2 = -height;
setInterval(function(){ ctx.drawImage(background,x1,y1); y1 = y1 + 1; ctx.
drawImage(background,x2,y2); y2 = y2 + 1; ①if(y2>height){ y2 = -height; } ②if(y1
>height){ y1 = -height; } ctx.drawImage(enemy,x,y); y = y + 3; },10)
* Draw two consecutive backgrounds separately and let the two backgrounds cycle alternately ,①,② When the image coordinates exceed 852 Time , Let the coordinates go back -852 position , move in circles , The background has been moving .

Technology
Daily Recommendation