-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
166 lines (126 loc) · 3.39 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
166
var car
var obstacle1
//====================================================================================
//================================================================================================
function Car(){
this.x=120;
this.y=530;
this.right=function(){
this.x=260;
}
this.forward=function(){
this.y=this.y-4;
}
this.backward=function(){
this.y=this.y+4;
}
this.left=function(){
this.x=120;
}
this.show=function(){
image(img1,this.x,this.y,80,150);
// fill(255);
// ellipse(this.x, this.y, 70, 70);
}
}
//=======================================================================================================
function leftObstacles(){
this.x= 180;
this.y= 30;
speed=10;
this.hit=function(car){
if(car.y===530 && car.x===120 && this.x===180 && (this.y>510&& this.y<730)){
return true;
}
}
this.show=function(){
ellipse(this.x, this.y,60, 60);
fill(255);
}
this.update=function(){
this.y=this.y+Math.random()*3;
}
}
//=========================================================================================================
function rightObstacles(){
this.x=270;
this.y= 30;
speed=10;
this.show=function(){
ellipse(this.x, this.y,60, 60);
fill(255);
}
this.hit=function(car){
if(car.y===530 && car.x===120 && this.x===180 && (this.y>510&& this.y<730)){
return true;
}
}
this.update=function(){
this.y=this.y+2+Math.random()*3;
}
}
//=========================================================================================================
var obstacleArray= []
var obstacleArray1=[]
function setup() {
createCanvas(480,680);
imgRoad = loadImage("road.png");
car= new Car();
//obstacle1=new leftObstacles();
//obstacle2=new rightObstacles();
obstacleArray.push(new leftObstacles());
obstacleArray1.push(new rightObstacles());
}
//============================================================================================================
function draw(){
var d=(car.y%10000);
e=String(d);
// console.log(d);
s="YOU LOST THE GAME"
text(e,20,30,200,290);
background(imgRoad);
car.show();
for(var i=obstacleArray.length-1;i>=0;i--){
obstacleArray[i].show();
obstacleArray[i].update();
if(obstacleArray[i].hit(car)){
textSize(100);
fill(255, 0, 0);
text(s, 10, 10, 370, 580);
}
}
if(frameCount % (Math.floor(Math.random() * 700)+300) ==0){
obstacleArray.push(new leftObstacles());
}
for(var j=obstacleArray1.length-1;j>=0;j--){
obstacleArray1[j].show();
obstacleArray1[j].update();
if(obstacleArray1[j].hit(car)){
// textSize(100);
// fill(255, 0, 0);
// text(s, 10, 10, 370, 580);
}
}
if(frameCount % (Math.floor(Math.random() * 500)+300) ==0){
obstacleArray1.push(new rightObstacles());
}
}
function preload() {
img1 = loadImage("merc.png");
}
function keyPressed(){
if (keyCode=== RIGHT_ARROW){
car.right();
console.log("pressedRight");
}
else if (keyCode===LEFT_ARROW) {
car.left();
console.log("left");
}
else if(keyCode=== UP_ARROW){
car.forward();
}
else if(keyCode=== DOWN_ARROW){
car.backward();
}
}