-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
104 lines (84 loc) · 2.02 KB
/
main.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
canvas = document.getElementById('myCanvas');
ctx = canvas.getContext("2d");
greencar_width = 75;
greencar_height = 100;
background_image = "parkingLot.jpg";
greencar_image = "car2.png";
greencar_x = 25;
greencar_y = 325;
function add(){
background_imgTag = new Image;
background_imgTag.onload = uploadBackground;
background_imgTag.src = background_image;
greencar_imgTag = new Image;
greencar_imgTag.onload = uploadgreencar;
greencar_imgTag.src = greencar_image;
}
function uploadBackground() {
ctx.drawImage(background_imgTag, 0, 0, canvas.width, canvas.height);
}
function uploadgreencar() {
ctx.drawImage(greencar_imgTag, greencar_x, greencar_y, greencar_width, greencar_height);
}
window.addEventListener("keydown", my_keydown);
function my_keydown(e)
{
keyPressed = e.keyCode;
console.log(keyPressed);
if(keyPressed == '38')
{
up();
console.log("up");
}
if(keyPressed == '40')
{
down();
console.log("down");
}
if(keyPressed == '37')
{
left();
console.log("left");
}
if(keyPressed == '39')
{
right();
console.log("right");
}
}
function up()
{
if (greencar_y >= 0){
greencar_y = greencar_y - 10;
console.log("When up arrow is pressed, x = " + greencar_x + " and y = " + greencar_y);
uploadBackground();
uploadgreencar();
}
}
function down()
{
if (greencar_y <= 700){
greencar_y = greencar_y + 10;
console.log("When down arrow is pressed, x = " + greencar_x + " and y = " + greencar_y);
uploadBackground();
uploadgreencar();
}
}
function left()
{
if (greencar_x >= 0){
greencar_x = greencar_x - 10;
console.log("When left arrow is pressed, x = " + greencar_x + " and y = " + greencar_y);
uploadBackground();
uploadgreencar();
}
}
function right()
{
if (greencar_x >= 0){
greencar_x = greencar_x + 10;
console.log("When right arrow is pressed, x = " + greencar_x + " and y = " + greencar_y);
uploadBackground();
uploadgreencar();
}
}