-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
148 lines (119 loc) · 3.21 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
var path,boy,cash,diamonds,jwellery,sword;
var pathImg,boyImg,cashImg,diamondsImg,jwelleryImg,swordImg;
var treasureCollection = 0;
var cashG,diamondsG,jwelleryG,swordGroup;
var gameOver,gameOverImage;
var PLAY = 1;
var END = 0;
var gameState = PLAY;
function preload(){
pathImg = loadImage("Road.png");
boyImg = loadAnimation("runner1.png","runner2.png");
cashImg = loadImage("cash.png");
diamondsImg = loadImage("diamonds.png");
jwelleryImg = loadImage("jwell.png");
swordImg = loadImage("sword.png");
endImg =loadAnimation("gameOver.png");
}
function setup(){
createCanvas(400,400);
// Moving background
path=createSprite(200,200);
path.addImage(pathImg);
path.velocityY = 4;
//creating boy running
boy = createSprite(70,380,20,20);
boy.addAnimation("SahilRunning",boyImg);
boy.scale=0.08;
cashG=new Group();
diamondsG=new Group();
jwelleryG=new Group();
swordGroup=new Group();
}
function draw() {
if(gameState===PLAY){
background(0);
boy.x = World.mouseX;
edges= createEdgeSprites();
boy.collide(edges);
//code to reset the background
if(path.y > 400 ){
path.y = height/2;
}
createCash();
createDiamonds();
createJwellery();
createSword();
if (cashG.isTouching(boy)) {
cashG.destroyEach();
treasureCollection=treasureCollection+80;
}
else if (diamondsG.isTouching(boy)) {
diamondsG.destroyEach();
treasureCollection=treasureCollection+100;
}else if(jwelleryG.isTouching(boy)) {
jwelleryG.destroyEach();
treasureCollection= treasureCollection + 5;
}else{
if(swordGroup.isTouching(boy)) {
gameState=END;
boy.addAnimation("SahilRunning",endImg);
boy.x=200;
boy.y=200;
boy.scale=0.6;
cashG.destroyEach();
diamondsG.destroyEach();
jwelleryG.destroyEach();
swordGroup.destroyEach();
cashG.setVelocityYEach(0);
diamondsG.setVelocityYEach(0);
jwelleryG.setVelocityYEach(0);
swordGroup.setVelocityYEach(0);
}
}
drawSprites();
textSize(20);
fill(255);
text("Treasure: "+ treasureCollection,150,30);
}
}
function createCash() {
if (World.frameCount % 200 == 0) {
var cash = createSprite(Math.round(random(50, 350),40, 10, 10));
cash.addImage(cashImg);
cash.scale=0.12;
cash.velocityY = 3;
cash.lifetime = 150;
cashG.add(cash);
}
}
function createDiamonds() {
if (World.frameCount % 320 == 0) {
var diamonds = createSprite(Math.round(random(50, 350),40, 10, 10));
diamonds.addImage(diamondsImg);
diamonds.scale=0.03;
diamonds.velocityY = 3;
diamonds.lifetime = 150;
diamondsG.add(diamonds);
}
}
function createJwellery() {
if (World.frameCount % 410 == 0) {
var jwellery = createSprite(Math.round(random(50, 350),40, 10, 10));
jwellery.addImage(jwelleryImg);
jwellery.scale=0.13;
jwellery.velocityY = 3;
jwellery.lifetime = 150;
jwelleryG.add(jwellery);
}
}
function createSword(){
if (World.frameCount % 530 == 0) {
var sword = createSprite(Math.round(random(50, 350),40, 10, 10));
sword.addImage(swordImg);
sword.scale=0.1;
sword.velocityY = 3;
sword.lifetime = 150;
swordGroup.add(sword);
}
}