-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
128 lines (85 loc) · 2.48 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
var monkey , monkey_running
var banana ,bananaImage, obstacle, obstacleImage
var FoodGroup, obstacleGroup
var score
function preload(){
monkey_running = loadAnimation("sprite_0.png","sprite_1.png","sprite_2.png","sprite_3.png","sprite_4.png","sprite_5.png","sprite_6.png","sprite_7.png","sprite_8.png")
bananaImage = loadImage("banana.png");
obstaceImage = loadImage("obstacle.png");
}
function setup() {
// createCanvas(600, 600);
var survivalTime=0;
//creating monkey
monkey=createSprite(80,315,20,20);
monkey.addAnimation("moving", monkey_running);
// monkey.addImage(bananaImage)
monkey.scale=0.1
ground = createSprite(400,350,900,10);
ground.velocityX=-4;
ground.x=ground.width/2;
console.log(ground.x)
FoodGroup = new Group();
obstaclesGroup = new Group();
score = 0;
}
function draw() {
background(255);
if(ground.x<0) {
ground.x=ground.width/2;
}
if(keyDown("space") ) {
monkey.velocityY = -12;
}
monkey.velocityY = monkey.velocityY + 0.8;
monkey.collide(ground);
spawnFood();
spawnObstacles();
drawSprites();
stroke("white");
textSize(20);
fill("white");
text("Score: "+ score, 500,50);
if(obstaclesGroup.isTouching(monkey)){
ground.velocityX = 0;
monkey.velocityY = 0;
obstaclesGroup.setVelocityXEach(0);
FoodGroup.setVelocityXEach(0);
obstaclesGroup.setLifetimeEach(-1);
FoodGroup.setLifetimeEach(-1);
}
stroke("black");
textSize(20);
fill("black");
survivalTime=Math.ceil(frameCount/frameRate())
text("Survival Time: "+ survivalTime, 100,50);
}
function spawnFood() {
//write code here to spawn the Food
if (frameCount % 80 === 0) {
banana = createSprite(600,250,40,10);
banana.y = random(120,200);
banana.velocityX = -5;
//assign lifetime to the variable
banana.lifetime = 300;
monkey.depth = banana.depth + 1;
//add image of banana
banana.addImage(bananaImage);
banana.scale=0.05;
//add each banana to the group
FoodGroup.add(banana);
}
}
function spawnObstacles() {
if(frameCount % 300 === 0) {
obstacle = createSprite(800,320,10,40);
obstacle.velocityX = -6;
//add image to the obstacle
obstacle.addImage(obstaceImage);
obstacle.scale=0.15;
//lifetime to the obstacle
obstacle.lifetime = 300;
//add each obstacle to the group
obstaclesGroup.add(obstacle);
}
}