forked from theotheperson10/Snake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
94 lines (94 loc) · 2.75 KB
/
sketch.js
File metadata and controls
94 lines (94 loc) · 2.75 KB
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
var x=200;
var os=0
var loops=5;
var snakelinenumber=0;
var score=1;
var play=false;
function setup(){
snakeline=createGroup();
snakehead=createSprite(200,200,10,10)
screen=createSprite(200,200,400,400)
screen.visible=false;
apple=createSprite(random(25,375),random(25,375),20,20)
apple.shapeColor="red"
snakehead.shapeColor="yellow"
}
function draw(){
background("blue");
if (play==false){
fill("black")
textAlign(CENTER)
textSize(30)
text("Snake",200,100)
textSize(20)
text("Press p to play",200,200)
if (keyDown("p")){snakehead.velocityX=2;play=true;}
}else{
fill("black")
textAlign(CENTER)
textSize(30)
if (score>1){
text("Score:"+(score-1)/2,65,30)}else{text("Score:"+0,65,30)}
drawSprites();
snakelinecrt();
controls();
if(snakehead.isTouching(snakeline)){window.alert("Your final score is:"+(score-1)/2);if(eee==eee){}}
if(snakehead.y>400){snakehead.y=0}
if(snakehead.y<0){snakehead.y=400}
if(snakehead.x>400){snakehead.x=0}
if(snakehead.x<0){snakehead.x=400}
if (snakeline.isTouching(apple)){
apple.destroy();
apple=createSprite(random(25,375),random(25,375),20,20)
apple.shapeColor="red";}
if (snakehead.isTouching(apple)){
apple.destroy();
apple=createSprite(random(25,375),random(25,375),20,20)
apple.shapeColor="red"
score+=2;
}
if (snakeline.isTouching(apple)){
apple.destroy();
apple=createSprite(random(25,375),random(25,375),20,20)
apple.shapeColor="red"
}
}
}
function screentest(screen,snakeline){
if (Math.abs(past.x-snakeline.x)<15){
if (Math.abs(past.y-snakeline.y)<15){
stroke(68,255,0)
strokeWeight(3)
line(snakeline.x,snakeline.y,past.x,past.y)
past.destroy();}}
past=createSprite(snakeline.x,snakeline.y,10,10);
past.visible=false;
}
function snakelinecrt(){
swap=createSprite(snakehead.x,snakehead.y,10,10);
if (os%2==0){swap.shapeColor="green"}else{swap.shapeColor="purple"}
if (snakehead.velocityX==2){swap.x=swap.x-2}
if (snakehead.velocityX==-2){swap.x=swap.x+2}
if (snakehead.velocityY==2){swap.y=swap.y-2}
if (snakehead.velocityY==-2){swap.y=swap.y+2}
if (snakehead.isTouching(swap)){
if(snakehead.x>swap.x){snakehead.x=snakehead.x+10}
if(snakehead.x<swap.x){snakehead.x=snakehead.x-10}
if(snakehead.y>swap.y){snakehead.y=snakehead.y+10}
if(snakehead.y<swap.y){snakehead.y=snakehead.y-10}
}
swap.life=score-snakelinenumber
snakeline.add(swap)
if(snakelinenumber=>score){snakelinenumber=0;}
if(os>score){os=0;}
os++;
snakelinenumber++;}
function controls(){
if (snakehead.velocityX==2){}else{
if(keyDown("LEFT_ARROW")){snakehead.velocityX=-2;snakehead.velocityY=0}}
if (snakehead.velocityX==-2){}else{
if(keyDown("RIGHT_ARROW")){snakehead.velocityX=2;snakehead.velocityY=0}}
if (snakehead.velocityY==2){}else{
if(keyDown("UP_ARROW")){snakehead.velocityY=-2;snakehead.velocityX=0}}
if (snakehead.velocityY==-2){}else{
if(keyDown("DOWN_ARROW")){snakehead.velocityY=2;snakehead.velocityX=0}}}