-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
191 lines (185 loc) · 4.37 KB
/
sketch.js
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
var Knife;
var PLAY=1;
var END=0;
var start;
var gameState =start;
var score=0;
var count=0;
var Life;
var Life1;
var Life2;
//var bg;
function preload(){
knifeImg=loadImage("sword.png")
fruit1=loadImage("fruit1.png")
fruit2=loadImage("fruit2.png")
fruit3=loadImage("fruit3.png")
fruit4=loadImage("fruit4.png")
monsterImg=loadAnimation("alien1.png","alien2.png")
Over=loadImage("game over.PNG")
lifeImg=loadImage("life .PNG")
life1Img=loadImage("life -1.PNG")
life2Img=loadImage("life -2.PNG")
bgImg=loadImage("ninja background.png")
knifeSound=loadSound("knifeSwooshSound.mp3")
gameSound=loadSound("Game Over.mp3")
youwin=loadImage("you-win-1.jpg")
}
function setup(){
createCanvas(500, 500);
life=createSprite(20,45,20,20);
life1=createSprite(45,45,20,20);
life2=createSprite(70,45,20,20);
Knife=createSprite(200,200,20,20);
Knife.addImage(knifeImg);
Knife.scale=0.7;
fruitGroup=new Group();
enemyGroup=new Group();
life.addImage(lifeImg)
life1.addImage(life1Img)
life2.addImage(life2Img)
//bg=createSprite(250,250);
// bg.addImage(bgImg)
}
function draw(){
background(bgImg)
if(gameState===start){
fill("yellow");
textSize(16);
textStyle(BOLDITALIC);
text("Press space to start the Game", 135, 230);
text("Hit 30 points to win",180,250);
Knife.y=300;
Knife.x=250
}
if(keyDown("space")&&gameState===start){
gameState=PLAY;
}
//text(mouseX+","+mouseY,mouseX,mouseY);
if(gameState===PLAY){
Knife.y=mouseY;
Knife.x=mouseX;
textSize(16);
textStyle(BOLDITALIC);
fill("yellow")
text("score:"+score,400,40);
random(fruitGroup,enemyGroup) ;
if(fruitGroup.isTouching(Knife)){
fruitGroup.destroyEach();
score=score+1
knifeSound.play();
}
fruit();
if(enemyGroup.isTouching(Knife)){
enemyGroup.destroyEach();
score=score-2;
count=count+1
}
enemy();
if(count===1){
life2.remove();
}
if(count===2){
life1.remove();
}
if(count===3){
life.remove();
}
if(count===4){
gameState=END;
}
if(score===30){
Knife.addImage(youwin);
Knife.x=250;
Knife.y=250;
fruitGroup.destroyEach();
enemyGroup.destroyEach();
fruitGroup.setVelocityXEach(0);
enemyGroup.setVelocityXEach(0);
fruitGroup.setVelocityYEach(0);
enemyGroup.setVelocityYEach(0);
}
}
else if(gameState===END){
//gameOver=createSprite(200,200);
fruitGroup.destroyEach();
enemyGroup.destroyEach();
fruitGroup.setVelocityXEach(0);
enemyGroup.setVelocityXEach(0);
fruitGroup.setVelocityYEach(0);
enemyGroup.setVelocityYEach(0);
Knife.addImage(Over);
Knife.x=250;
Knife.y=250;
gameSound.play()
}
drawSprites();
}
function fruit(){
if(frameCount % 80===0){
var fruit=createSprite(400,200,20,20);
fruit.scale=0.2;
var rand=Math.round(random(1,4))
switch(rand){
case 1:fruit.addImage(fruit1);
break;
case 2:fruit.addImage(fruit2);
break;
case 3:fruit.addImage(fruit3);
break;
case 4:fruit.addImage(fruit4);
break;
default:break;
}
position = Math.round(random(1,2));
if(position==1){
fruit.x =600;
if(score % 4===0){
fruit.velocityX=Math.round(random(-7,-20))+score;
}
else{
fruit.velocityX=Math.round(random(-7,-20));
}
}
if(position==2){
fruit.x =-10;
if(score % 4===0){
fruit.velocityX=Math.round(random(20,7))+score;
}
else{
fruit.velocityX=Math.round(random(20,7))
}
}
fruit.y=Math.round(random(50,350));
//fruit.velocityX=Math.round(random(20,-20));
fruit.setLifetime=100;
fruitGroup.add(fruit);
}
}
function enemy(){
if(frameCount % 200===0){
var monster=createSprite(400,200,20,20);
monster.addAnimation("moving",monsterImg);
monster.y=Math.round(random(100,300));
// monster.velocityX=Math.round(random(-7,-20));
monster.setLifetime=50;
enemyGroup.add(monster);
position = Math.round(random(1,2));
if(position==1){
monster.x =600;
if(score % 10===0){ monster.velocityX=Math.round(random(-7,-20))+score;
}
else{
monster.velocityX=Math.round(random(-7,-20))
}
}
if(position==2){
monster.x =-10;
if(score % 10===0){ monster.velocityX=Math.round(random(20,7))+score;
}
else{
monster.velocityX=Math.round(random(20,7))
}
}
}
}