-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
161 lines (118 loc) · 3.83 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
/*--------------------------------------------------------*/
var PLAY = 1;
var END = 0;
var WIN = 2;
var gameState = PLAY;
var trex, trex_running, trex_collided;
var jungle, invisiblejungle;
var obstaclesGroup, obstacle1;
var score=0;
var gameOver, restart;
function preload(){
kangaroo_running = loadAnimation("assets/kangaroo1.png","assets/kangaroo2.png","assets/kangaroo3.png");
kangaroo_collided = loadAnimation("assets/kangaroo1.png");
jungleImage = loadImage("assets/bg.png");
shrub1 = loadImage("assets/shrub1.png");
shrub2 = loadImage("assets/shrub2.png");
shrub3 = loadImage("assets/shrub3.png");
obstacle1 = loadImage("assets/stone.png");
gameOverImg = loadImage("assets/gameOver.png");
restartImg = loadImage("assets/restart.png");
jumpSound = loadSound("assets/jump.wav");
collidedSound = loadSound("assets/collided.wav");
}
function setup() {
createCanvas(800, 400);
jungle = createSprite(400,100,400,20);
jungle.addImage("jungle",jungleImage);
jungle.scale=0.3
jungle.x = width /2;
kangaroo = createSprite(50,200,20,50);
kangaroo.addAnimation("running", kangaroo_running);
kangaroo.addAnimation("collided", kangaroo_collided);
kangaroo.scale = 0.15;
kangaroo.setCollider("circle",0,0,300)
invisibleGround = createSprite(400,350,1600,10);
invisibleGround.visible = false;
shrubsGroup = new Group();
obstaclesGroup = new Group();
score = 0;
}
function draw() {
background(255);
// kangaroo.x=camera.positionX-270;
// kangaroo.x=Camera.position.x-270;
kangaroo.x=camera.position.x-270;
// kangaroo.x=Camera.Position.X-270;
if (gameState===PLAY){
jungle.velocityX=-3
if(jungle.x<100)
{
jungle.x=400
}
console.log(kangaroo.y)
if(keyDown("space")&& kangaroo.y>270) {
jumpSound.play();
kangaroo.velocityY = -16;
}
kangaroo.velocityY = kangaroo.velocityY + 0.8
spawnShrubs();
spawnObstacles();
kangaroo.collide(invisibleGround);
if(obstaclesGroup.isTouching(kangaroo)){
collidedSound.play();
gameState = END;
}
if(shrubsGroup.isTouching(kangaroo)){
shrubsGroup.destroyEach();
}
}
else if (gameState === END) {
kangaroo.velocityY = 0;
jungle.velocityX = 0;
obstaclesGroup.setVelocityXEach(0);
shrubsGroup.setVelocityXEach(0);
kangaroo.changeAnimation("collided",kangaroo_collided);
obstaclesGroup.setLifetimeEach(-1);
shrubsGroup.setLifetimeEach(-1);
}
drawSprites();
}
function spawnShrubs() {
if (frameCount % 150 === 0) {
// var shrub = createSprite(camera.position+500,330,40,10);
var shrub = createSprite(camera.position.x+500,330,40,10);
// var shrub = createSprite(camera.positionX+500,330,40,10);
// var shrub = createSprite(Camera.position.x+500,330,40,10);
shrub.velocityX = -(6 + 3*score/100)
shrub.scale = 0.6;
var rand = Math.round(random(1,3));
switch(rand) {
case 1: shrub.addImage(shrub1);
break;
case 2: shrub.addImage(shrub2);
break;
case 3: shrub.addImage(shrub3);
break;
default: break;
}
shrub.scale = 0.05;
shrub.lifetime = 400;
shrub.setCollider("rectangle",0,0,shrub.width/2,shrub.height/2)
shrubsGroup.add(shrub);
}
}
function spawnObstacles() {
if(frameCount % 120 === 0) {
// var obstacle = createSprite(camera.Position.X+400,330,40,40);
// var obstacle = createSprite(Camera.Position.x+400,330,40,40);
var obstacle = createSprite(camera.position.x+400,330,40,40);
// var obstacle = createSprite(camera.position.x.400,330,40,40);
obstacle.setCollider("rectangle",0,0,200,200)
obstacle.addImage(obstacle1);
obstacle.velocityX = -(6 + 3*score/100)
obstacle.scale = 0.15;
obstacle.lifetime = 400;
obstaclesGroup.add(obstacle);
}
}