-
Notifications
You must be signed in to change notification settings - Fork 0
/
C91_Student_Template.js
197 lines (177 loc) · 4.6 KB
/
C91_Student_Template.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
canvas = document.getElementById('myCanvas');
ctx = canvas.getContext("2d");
car1_width = 120;
car1_height = 70;
car1_image = "car1.png";
car1_x = 10;
car1_y = 10;
car2_width = 120;
car2_height = 70;
car2_image = "car2.png";
car2_x = 10;
car2_y = 100;
background_image = "racing.jpg";
function add() {
background_imgTag = new Image(); //defining a variable with a new image
background_imgTag.onload = uploadBackground; // setting a function, onloading this variable
background_imgTag.src = background_image; // load image
car1_imgTag = new Image(); //defining a variable with a new image
car1_imgTag.onload = uploadcar1; // setting a function, onloading this variable
car1_imgTag.src = car1_image; // load image
car2_imgTag = new Image(); //defining a variable with a new image
car2_imgTag.onload = uploadcar2; // setting a function, onloading this variable
car2_imgTag.src = car2_image; // load image
}
function uploadBackground() {
ctx.drawImage(background_imgTag, 0, 0, canvas.width, canvas.height);
}
function uploadcar1() {
ctx.drawImage(car1_imgTag, car1_x, car1_y, car1_width, car1_height);
}
function uploadcar2() {
ctx.drawImage(car2_imgTag, car2_x, car2_y, car2_width, car2_height);
}
window.addEventListener("keydown", my_keydown);
function my_keydown(e)
{
keyPressed = e.keyCode;
console.log(keyPressed);
if(keyPressed == '38')
{
car1_up();
console.log("up arrow key");
}
if(keyPressed == '40')
{
car1_down();
console.log("down arrow key");
}
if(keyPressed == '37')
{
car1_left();
console.log("left arrow key");
}
if(keyPressed == '39')
{
car1_right();
console.log("right arrow key");
}
if(keyPressed == '87')
{
car2_up();
console.log("key w");
}
if(keyPressed == '83')
{
car2_down();
console.log("key s");
}
if(keyPressed == '65')
{
car2_left();
console.log("key a");
}
if(keyPressed == '68')
{
car2_right();
console.log("key d");
}
}
function car1_up()
{
if(car1_y >=0)
{
car1_y = car1_y - 10;
console.log("When up arrow is pressed, x = " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
function car1_down()
{
if(car1_y <=500)
{
car1_y = car1_y + 10;
console.log("When down arrow is pressed, x = " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
function car1_left()
{
if(car1_x >=0)
{
car1_x = car1_x - 10;
console.log("When left arrow is pressed, x =1 " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
function car1_right()
{
if(car1_y <=700)
{
car1_x = car1_x + 10;
console.log("When right arrow is pressed, x = " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
function car2_up()
{
if(car1_y >=0)
{
car1_y = car1_y - 10;
console.log("When up arrow is pressed, x = " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
function car2_down()
{
if(car1_y <=500)
{
car1_y = car1_y + 10;
console.log("When down arrow is pressed, x = " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
function car2_left()
{
if(car1_x >=0)
{
car1_x = car1_x - 10;
console.log("When left arrow is pressed, x =1 " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
function car2_right()
{
if(car1_y <=700)
{
car1_x = car1_x + 10;
console.log("When right arrow is pressed, x = " + car1_x + " | y = " +car1_y)
uploadBackground();
uploadcar1();
uploadcar2();
}
}
if(car1_x > 700)
{
console.log("car1 Won")
document.getElementById('game_status').innerHTML = "Car 1 Won!!"
}
if(car2_x > 700)
{
console.log("car2 Won")
document.getElementById('game_status').innerHTML = "Car 2 Won!!"
}