-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
142 lines (112 loc) · 3.18 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
//gamestates
var PLAY = 1;
var END = 0;
var gameState = 1;
var sword, swordImage, swordSound;
var fruit, fruitGroup, fruit1, fruit2, fruit3, fruit4;
var monster, monsterImage, monsterGroup;
var gameoverImage, gameoverSound;
var score;
var position, side;
function preload(){
swordImage = loadImage("sword.png");
swordSound = loadSound("knifeSwooshSound.mp3");
fruit1 = loadImage("fruit1.png");
fruit2 = loadImage("fruit2.png");
fruit3 = loadImage("fruit3.png");
fruit4 = loadImage("fruit4.png");
gameoverImage = loadImage("gameover.png");
gameoverSound = loadSound("gameover.mp3");
monsterImage = loadAnimation("alien1.png", "alien2.png");
}
function setup(){
createCanvas(400,400);
sword = createSprite(40,200,20,20);
sword.addImage(swordImage);
sword.scale = 0.7;
//sword.debug = true;
fruitGroup = createGroup();
monsterGroup = createGroup();
score = 0;
}
function draw(){
background(200);
text("SCORE: "+score,300,50);
if(gameState === 1) {
sword.x = World.mouseX;
sword.y = World.mouseY;
if(fruitGroup.isTouching(sword)) {
fruitGroup.destroyEach();
swordSound.play();
score = score+1;
}
if(monsterGroup.isTouching(sword)) {
gameState = END;
gameoverSound.play();
}
}
else if(gameState === 0) {
sword.addImage(gameoverImage);
sword.x = 200;
sword.y = 200;
fruitGroup.destroyEach();
monsterGroup.destroyEach();
fruitGroup.setVelocityXEach(0);
monsterGroup.setVelocityXEach(0);
}
fruits();
enemy();
drawSprites();
}
function fruits() {
if(frameCount % 80 === 0) {
fruit = createSprite(400,Math.round(random(50,340)),20,20);
fruit.scale = 0.2;
fruit.setCollider("circle",0,0,70);
//fruit.debug = true;
position = Math.round(random(1,2));
if(position === 1) {
fruit.x = 400;
fruit.velocityX = -(7 + score/4);
}
else if(position === 2) {
fruit.x = 0;
fruit.velocityX = (7 + score/4);
}
r = Math.round(random(1,4));
if(r === 1) {
fruit.addImage(fruit1);
}
else if(r === 2) {
fruit.addImage(fruit2);
}
else if(r === 3) {
fruit.addImage(fruit3);
}
else if(r === 4) {
fruit.addImage(fruit4);
}
fruit.setLifetime = 100;
fruitGroup.add(fruit);
}
}
function enemy() {
if(frameCount % 200 === 0) {
monster = createSprite(400,Math.round(random(100,300)),20,20);
monster.addAnimation("moving", monsterImage);
monster.velocityX = -(8 + score/10);
monster.lifetime = 50;
monster.setCollider("circle",0,0,20);
side = Math.round(random(1,2));
if(side === 1) {
monster.x = 400;
monster.velocityX = -(7 + score/4);
}
else if(side === 2) {
monster.x = 0;
monster.velocityX = (7 + score/4);
}
//monster.debug = true;
monsterGroup.add(monster);
}
}