-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
166 lines (136 loc) · 4.57 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
162
163
164
165
var PLAY = 1;
var END = 0;
var gameState = PLAY;
var ground,ground_image,invisible_ground;
var girl,girl_running,girl_collided,girlImage,zombie,zombie_running,zombie_attack;
var obstaclesGroup,obstacle1,obstacle2,obstacle3,obstacle4;
var jumpSound,dieSound,checkpointSound;
var score;
var gameOver,restart,gameOverImage,restartImage;
function preload(){
ground_image=loadImage("Background.png");
girl_running=loadAnimation("Run (1).png","Run (2).png","Run (3).png","Run (4).png","Run (5).png","Run (6).png","Run (7).png","Run (8).png","Run (9).png","Run (10).png","Run (11).png","Run (12).png","Run (14).png","Run (15).png","Run (16).png","Run (17).png","Run (18).png","Run (19).png","Run (20).png");
zombie_running=loadAnimation("Walk (1).png","Walk (2).png","Walk (3).png","Walk (4).png","Walk (5).png","Walk (6).png","Walk (7).png","Walk (8).png","Walk (9).png","Walk (10).png");
zombie_attack=loadAnimation("Attack (2).png","Attack (3).png","Attack (4).png","Attack (5).png","Attack (6).png","Attack (7).png","Attack (8).png");
obstacle1=loadImage("obstacle1.png");
zombie_idle=loadImage("Stand.png");
jumpSound = loadSound("jump.mp3")
dieSound = loadSound("die.mp3")
checkPointSound = loadSound("checkPoint.mp3")
gameOverImage=loadImage("gameOver1.png");
restartImage=loadImage("restart1.png");
girl_collided=loadImage("Dead (30).png");
girlImage=loadImage("Idle (1).png");
}
function setup() {
createCanvas(600,500);
ground=createSprite(0,0,0,0);
ground.shapeColor="white";
ground.addImage("ground_image",ground_image);
ground.scale=1.4;
ground.velocityX=-1
girl=createSprite(300,420,600,10);
girl.addAnimation("girl_running",girl_running);
girl.addImage("girl_collided",girl_collided);
girl.addImage("girlImage",girlImage);
girl.scale=0.2;
// girl.velocityX=2;
girl.debug=false;
girl.setCollider("rectangle",0,0,girl.width,girl.height)
zombie=createSprite(50,410,600,10);
zombie.addAnimation("zombie_running",zombie_running);
zombie.addAnimation("zombie_attack",zombie_attack);
zombie.addImage("zombie_idle",zombie_idle);
zombie.scale=0.2;
zombie.debug=false;
// zombie.velocityY=-13;
// zombie.velocityX=Math.round(random(1,2));
invisible_ground=createSprite(300,470,600,10);
invisible_ground.visible=false;
gameOver = createSprite(300,100);
gameOver.addImage(gameOverImage);
restart = createSprite(300,180);
restart.addImage(restartImage);
obstaclesGroup=new Group();
score=0;
}
function draw() {
background("black");
// console.log(girl.y);
//Gravity
girl.velocityY = girl.velocityY + 0.8;
girl.collide(invisible_ground);
//Gravity
zombie.velocityY = zombie.velocityY + 0.8;
zombie.collide(invisible_ground);
if (gameState===PLAY){
gameOver.visible=false;
restart.visible=false;
// zombie.y=girl.y;
score = score + Math.round(getFrameRate()/60);
spawnObstacles();
if (obstaclesGroup.isTouching(zombie)){
zombie.velocityY=-12;
}
ground.velocityX = -(4 + 3* score/100);
if (ground.x < 0){
ground.x = ground.width/2;
}
if(score>0 && score%100 === 0){
checkPointSound.play()
}
if((keyDown("space")&& girl.y >= 220)) {
girl.velocityY = -12;
jumpSound.play();
}
if (girl.isTouching(obstaclesGroup)){
gameState=END;
dieSound.play();
}
}
else if ( gameState===END) {
gameOver.visible=true;
restart.visible=true;
ground.velocityX = 0;
girl.velocityY = 0
girl.changeImage("girlImage",girlImage);
zombie.changeAnimation("zombie_attack",zombie_attack);
zombie.x=girl.x;
if (zombie.isTouching(girl)) {
girl.changeImage("girl_collided",girl_collided);
zombie.changeImage("zombie_idle",zombie_idle);
}
//set lifetime of the game objects so that they are never destroyed
obstaclesGroup.setLifetimeEach(-1);
obstaclesGroup.setVelocityXEach(0);
if(mousePressedOver(restart)) {
reset();
}
}
drawSprites();
fill("lightpink");
textSize(20);
text("Score: "+ score, 500,50);
}
function reset(){
gameState=PLAY;
gameOver.visible=false;
restart.visible=false;
girl.changeAnimation("girl_running",girl_running);
obstaclesGroup.destroyEach();
score=0;
zombie.x=50;
}
function spawnObstacles() {
if (frameCount % 60 === 0){
var obstacle = createSprite(600,450,10,40);
obstacle.velocityX = -6 ;//+ score/100);
//generate random obstacles
var rand = Math.round(random(1,6));
obstacle.addImage(obstacle1);
obstacle.scale=0.1;
obstaclesGroup.add(obstacle);
obstacle.debug=false;
obstacle.setCollider("circle",0,0,1);
}
}