Updated webdev

This commit is contained in:
sam
2023-11-28 13:18:36 +01:00
parent e47ec307b7
commit 8a8353b645
3 changed files with 61 additions and 1 deletions

View File

@@ -70,7 +70,9 @@ async function disconnect() {
let Playerposx = 500;
let Playerposy = 300;
let bullets = [];
let myBullet = new bullet();
let playerSpeed = 5;
async function Movementloop() {
@@ -83,6 +85,20 @@ if (booleanArray[2]) {
if (booleanArray[0]) {
Playerposy -= 2;}
}
function keyPressed() {
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
playerPosX -= playerSpeed;
}
if (keyIsDown(RIGHT_ARROW) && playerPosX < width - radius) {
playerPosX += playerSpeed;
}
if (keyIsDown(UP_ARROW) && playerPosY > 0 + radius) {
playerPosY -= playerSpeed;
}
if (keyIsDown(DOWN_ARROW) && playerPosY < height - radius) {
playerPosY += playerSpeed;
}
}
// the function setup() is called once when the page is loaded

View File

@@ -8,6 +8,8 @@
<!-- support p5-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js"></script>
<script src="Serial.js"></script>
<script src="js/basicbullet.js"></script>
<script src="game.js"></script>
</head>
<body>

42
webdev/js/basicbullet.js Normal file
View File

@@ -0,0 +1,42 @@
class bullet {
//Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd.
constructor(targetx, targety, radius, speed) {
//"This" moet gebruikt worden om de variabelen aan te maken in de class en het zorgt er voor dat de variabelen niet alleen in de constructor gebruikt kunnen worden,
//maar ook in de rest van de class
this.x = targetx;
this.y = targety;
this.radius = radius;
this.speed = speed;
// Set the initial position of the projectile to the player's position
let projectile = createVector(shotPosX, shotPosY);
x = directionX;
y = directionY;
// Calculate the initial direction
direction = createVector(targetX - projectile.x, targetY - projectile.y);
direction.normalize();
}
draw() {
//push en pop zorgen er voor dat de code tussen push en pop een eigen canvas heeft. Dus dat er meerdere projectielen tegelijk kunnen zijn.
fill(255, 0, 0);
circle(500, 500, 5);
projectile.add(p5.Vector.mult(direction, shotSpeed));
if (dist(projectile.x, projectile.y, directionX, directionY) <= radius) {
hit = true;
}
else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) {
bounceX = projectile.x;
bounceY = projectile.y;
shot = false;
}
}
}