-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
147 lines (125 loc) · 3.81 KB
/
Copy pathgame.js
File metadata and controls
147 lines (125 loc) · 3.81 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
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 path,boy,cash,diamonds,jwellery,sword;
var pathImg,boyImg,cashImg,diamondsImg,jwelleryImg,swordImg;
var cashGroup,diamondsGroup,jwelleryGroup,swordGroup;
var treasureCollection = 0;
var speed = 5;
var PLAY=1;
var END=0;
var gameState=1;
function preload(){
pathImg = loadImage("./Assets/Road.png");
boyImg = loadAnimation("./Assets/Runner-1.png","./Assets/Runner-2.png");
cashImg = loadImage("./Assets/cash.png");
diamondsImg = loadImage("./Assets/diamonds.png");
jwelleryImg = loadImage("./Assets/jwell.png");
swordImg = loadImage("./Assets/sword.png");
endImg =loadAnimation("./Assets/gameOver.png");
}
function setup(){
//Create the canvas
canvasW = windowWidth/1.5;
canvasH = windowHeight/1.5;
var canvas = createCanvas(canvasW, canvasH);
canvas.parent('Game');
rectMode(CENTER);
//create the path for the player to run on
path=createSprite(width/2,200);
path.addImage(pathImg);
//Create the player
boy = createSprite(width/2,height-20,20,20);
boy.addAnimation("SahilRunning",boyImg);
boy.scale=0.08;
//Create the grou
cashGroup=new Group();
diamondsGroup=new Group();
jwelleryGroup=new Group();
swordGroup=new Group();
}
function draw() {
if(gameState === PLAY) {
background(0);
path.velocityY = speed
boy.x = World.mouseX;
edges= createEdgeSprites();
boy.collide(edges);
//code to reset the background
if(path.y > height ){
path.y = height/2;
}
createCash();
createDiamonds();
createJwellery();
createSword();
if (cashGroup.isTouching(boy)) {
cashGroup.destroyEach();
treasureCollection=treasureCollection + 50;
}
else if (diamondsGroup.isTouching(boy)) {
diamondsGroup.destroyEach();
treasureCollection=treasureCollection + 100;
} else if (jwelleryGroup.isTouching(boy)) {
jwelleryGroup.destroyEach();
treasureCollection= treasureCollection + 150;
} else {
if (swordGroup.isTouching(boy)) {
gameState=END;
boy.addAnimation("SahilRunning",endImg);
boy.x=width/2;
boy.y=height/2;
boy.scale=0.6;
cashGroup.destroyEach();
diamondsGroup.destroyEach();
jwelleryGroup.destroyEach();
swordGroup.destroyEach();
cashGroup.setVelocityYEach(0);
diamondsGroup.setVelocityYEach(0);
jwelleryGroup.setVelocityYEach(0);
swordGroup.setVelocityYEach(0);
}
}
drawSprites();
textSize(20);
fill(255);
text("Treasure: "+ treasureCollection,width-150,30);
}
}
function createCash() {
if (World.frameCount % 200 == 0) {
var cash = createSprite(Math.round(random(50, width-50),40, 10, 10));
cash.addImage(cashImg);
cash.scale=0.12;
cash.velocityY = speed;
cash.lifetime = 200;
cashGroup.add(cash);
}
}
function createDiamonds() {
if (World.frameCount % 320 == 0) {
var diamonds = createSprite(Math.round(random(50, width-50),40, 10, 10));
diamonds.addImage(diamondsImg);
diamonds.scale=0.03;
diamonds.velocityY = speed;
diamonds.lifetime = 200;
diamondsGroup.add(diamonds);
}
}
function createJwellery() {
if (World.frameCount % 410 == 0) {
var jwellery = createSprite(Math.round(random(50, width-50),40, 10, 10));
jwellery.addImage(jwelleryImg);
jwellery.scale=0.13;
jwellery.velocityY = speed;
jwellery.lifetime = 200;
jwelleryGroup.add(jwellery);
}
}
function createSword(){
if (World.frameCount % 530 == 0) {
var sword = createSprite(Math.round(random(50, width-50),40, 10, 10));
sword.addImage(swordImg);
sword.scale=0.1;
sword.velocityY = speed;
sword.lifetime = 200;
swordGroup.add(sword);
}
}