-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
147 lines (100 loc) · 2.62 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
var PLAY = 1;
var END = 0;
var gameState = PLAY;
var score = 0;
var monster, monsterImage, enemyGroup,ds;
var fruitsGroup, fr1, fr1img;
var fr2, fr2img;
var fr3, fr3img;
var fr4, fr4img;
var sword, swordImage, gmoi,gos;
function preload() {
swordImage = loadImage("sword.png");
gmoi = loadImage("gameover.png");
fr1img = loadImage("fruit1.png");
fr2img = loadImage("fruit2.png");
fr3img = loadImage("fruit3.png");
fr4img = loadImage("fruit4.png");
monsterImage = loadAnimation("alien1.png", "alien2.png");
ds=loadSound("knifeSwooshSound.mp3");
gos=loadSound("gameover.mp3");
}
function setup() {
createCanvas(400, 400);
sword = createSprite(200, 200, 10, 10);
sword.addImage("sword", swordImage);
sword.addImage("game",gmoi);
sword.scale = 0.5;
fruitsGroup = createGroup();
enemyGroup = createGroup();
}
function draw() {
background("lightblue");
text("Score: " + score, 300, 300);
if (gameState === PLAY) {
sFruit();
Enemy();
sword.y = World.mouseY;
sword.x = World.mouseX;
if (sword.isTouching(fruitsGroup)) {
fruitsGroup.destroyEach();
score = score + 2;
ds.play();
}
if (sword.isTouching(enemyGroup)) {
gameState = END;
gos.play();
}
}
if (gameState === END) {
sword.changeImage("game",gmoi);
sword.scale=1.5;
fruitsGroup.destroyEach();
enemyGroup.destroyEach();
fruitsGroup.setVelocityEach(0);
enemyGroup.setVelocityEach(0);
sword.x=200;
sword.y=200;
}
drawSprites();
}
function sFruit() {
if (World.frameCount % 80 === 0) {
var fruits = createSprite(400, 200, 20, 20);
fruits.scale = 0.2;
r = Math.round(random(1, 4));
if (r === 1) {
fruits.addImage(fr1img);
} else if (r === 2) {
fruits.addImage(fr2img);
} else if (r == 3) {
fruits.addImage(fr3img);
} else {
fruits.addImage(fr4img);
}
fruits.y = Math.round(random(50, 340));
position=Math.round(random(1,2))
console.log(position);
if(position===1){
fruits.x=400;
fruits.velocityX = -(7+3*score/4);
}
else {
if(position===2){
fruits.x=0;
fruits.velocityX = (7+3*score/4);}
}
fruits.setLifetime = 100;
fruitsGroup.add(fruits);
}
}
function Enemy() {
if (World.frameCount % 200 === 0) {
monster = createSprite(400, 250, 20, 20);
monster.addAnimation("moving", monsterImage);
monster.y = Math.round(random(100, 300));
monster.velocityX = -(8+2*score/10);
monster.setLifetime = 50;
enemyGroup.add(monster);
}
}