complete exercise 1.5
This commit is contained in:
parent
fd2df012af
commit
d6f4d5e863
15
1. Vectors/5. Car Simulation/index.html
Normal file
15
1. Vectors/5. Car Simulation/index.html
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/p5@1.9.0/lib/p5.js"></script>
|
||||||
|
<script src="sketch.js"></script>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background: #212121;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<main>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
42
1. Vectors/5. Car Simulation/sketch.js
Normal file
42
1. Vectors/5. Car Simulation/sketch.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
let position
|
||||||
|
let velocity
|
||||||
|
let accelaration
|
||||||
|
|
||||||
|
function setup() {
|
||||||
|
createCanvas(600, 300)
|
||||||
|
|
||||||
|
position = createVector(width/2, height/2)
|
||||||
|
velocity = createVector(0, 0)
|
||||||
|
accelaration = createVector(0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw() {
|
||||||
|
let keyCodeW = 87
|
||||||
|
let keyCodeS = 83
|
||||||
|
|
||||||
|
accelaration.x = 0
|
||||||
|
if (keyIsDown(keyCodeW)) {
|
||||||
|
accelaration.x += 0.001
|
||||||
|
}
|
||||||
|
if (keyIsDown(keyCodeS)) {
|
||||||
|
accelaration.x -= 0.001
|
||||||
|
}
|
||||||
|
|
||||||
|
velocity.add(p5.Vector.mult(accelaration, deltaTime))
|
||||||
|
velocity.limit(2)
|
||||||
|
|
||||||
|
if (position.x < 0) {
|
||||||
|
velocity.x = 0
|
||||||
|
position.x = 0
|
||||||
|
}
|
||||||
|
if (position.x > width) {
|
||||||
|
velocity.x = 0
|
||||||
|
position.x = width
|
||||||
|
}
|
||||||
|
|
||||||
|
position.add(p5.Vector.mult(velocity, deltaTime))
|
||||||
|
|
||||||
|
background(255)
|
||||||
|
|
||||||
|
circle(position.x, position.y, 10)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user