coverted database interactions and menu to classes

This commit is contained in:
Sam
2024-01-23 19:45:03 +01:00
parent cf843b80c8
commit 68005ea72b
6 changed files with 212 additions and 198 deletions

View File

@@ -1,7 +1,11 @@
function preload() {
controlsImage = loadImage('./contols.png');
}
// the function setup() is called once when the page is loaded // the function setup() is called once when the page is loaded
function setup() { function setup() {
// create a canvas element and append it to the body // create a canvas element and append it to the body
getData() DB.getData()
createCanvas(width, height); createCanvas(width, height);
frameRate(framerate); frameRate(framerate);
angleMode(DEGREES); angleMode(DEGREES);
@@ -25,13 +29,16 @@ function reset() {
isDead = false; isDead = false;
entered = false; entered = false;
buttonSelect = 0; buttonSelect = 0;
nameHS = '';
submitted = false; submitted = false;
escaped = false; escaped = false;
bossPosX = width / 2; bossPosX = width / 2;
bossPosY = height / 6; bossPosY = height / 6;
bossVelX = 5; bossVelX = 5;
bossVelY = 5; bossVelY = 5;
dataIsCalled = false;
DB.getData();
} }
function movementCheck() { function movementCheck() {
@@ -79,7 +86,7 @@ async function keyPressed() {
homescreenOn = true; homescreenOn = true;
reset(); reset();
keyReleasedFlag = false; keyReleasedFlag = false;
getData() DB.getData()
return; return;
} }
} }
@@ -169,10 +176,11 @@ async function keyPressed() {
} }
} }
} }
} }
function submit() { function submit() {
if (entered == true) { if (entered == true) {
sendData(nameHS, int(time)); DB.sendData(nameHS, int(time));
console.log(nameHS + ": " + int(time)); console.log(nameHS + ": " + int(time));
nameHS = ''; nameHS = '';
submitted = true; submitted = true;
@@ -255,7 +263,7 @@ function nameDraw(positiony) {
// the function draw() is called every frame // the function draw() is called every frame
function draw() { function draw() {
keyPressed(); keyPressed();
gameOver(); Menus.gameOver();
// draw background // draw background
background(0, 0, 0, 100); background(0, 0, 0, 100);
ffps = frameRate(); ffps = frameRate();
@@ -263,7 +271,7 @@ function draw() {
playerSpeed = 300 / ffps; playerSpeed = 300 / ffps;
if (homescreenOn) { if (homescreenOn) {
homescreen(); Menus.homescreen();
buttonSelect = constrain(buttonSelect, 0, 1); buttonSelect = constrain(buttonSelect, 0, 1);
if (buttonSelect == 0) { if (buttonSelect == 0) {
push() push()
@@ -286,7 +294,7 @@ function draw() {
game(); game();
} }
if (scorescreenOn) { if (scorescreenOn) {
scoremenu(); Menus.scoreMenu();
buttonSelect = constrain(buttonSelect, 0, 1); buttonSelect = constrain(buttonSelect, 0, 1);
if (buttonSelect == 1 && entered) { if (buttonSelect == 1 && entered) {
push() push()
@@ -298,7 +306,7 @@ function draw() {
} }
} }
if (pausescreenOn) { if (pausescreenOn) {
pauseMenu(); Menus.pauseMenu();
buttonSelect = constrain(buttonSelect, 0, 1); buttonSelect = constrain(buttonSelect, 0, 1);
if (buttonSelect == 0) { if (buttonSelect == 0) {
push() push()
@@ -353,7 +361,7 @@ function draw() {
} }
if (!hasMoved && !isDead && !homescreenOn && !pausescreenOn && !scorescreenOn) { if (!hasMoved && !isDead && !homescreenOn && !pausescreenOn && !scorescreenOn) {
dodgeBanner(); Menus.dodgeBanner();
} }
} }
async function randPatern() { async function randPatern() {

View File

@@ -15,10 +15,11 @@
<!-- support p5--> <!-- 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/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js"></script>
<script src="js/variables.js"></script>
<script src="js/basicbullet.js"></script> <script src="js/basicbullet.js"></script>
<script src="js/Database.js"></script> <script src="js/Button.js"></script>
<script src="js/Menu.js"></script> <script src="js/Menu.js"></script>
<script src="js/Database.js"></script>
<script src="js/variables.js"></script>
<script src="game.js"></script> <script src="game.js"></script>
</head> </head>

14
web/js/Button.js Normal file
View File

@@ -0,0 +1,14 @@
class Button{
button(r, g, b, buttonX, buttonY, buttonText) {
push()
noFill()
rectMode(CENTER);
rect(buttonX, buttonY, buttonWidth, buttonHeight)
textSize(25)
fill(r, g, b)
textAlign(CENTER);
text(buttonText, buttonX, buttonY)
pop()
}
}

View File

@@ -1,9 +1,9 @@
class database{
function sendData(naam, score) { sendData(naam, score) {
fetch(`https://oege.ie.hva.nl/~hossan/postData.php?name=${naam}&score=${score}`) fetch(`https://oege.ie.hva.nl/~hossan/postData.php?name=${naam}&score=${score}`)
} }
function getData(name){ getData(name) {
// Fetch data from the database, put it in an array, and log it to the console // Fetch data from the database, put it in an array, and log it to the console
//met backticks als " " kan je variabelen in een string zetten //met backticks als " " kan je variabelen in een string zetten
return fetch(`https://oege.ie.hva.nl/~hossan/getData.php?name=${name}`) // Add return here return fetch(`https://oege.ie.hva.nl/~hossan/getData.php?name=${name}`) // Add return here
@@ -13,3 +13,7 @@ function getData(name){
console.log(data); console.log(data);
}); });
} }
}

View File

@@ -1,23 +1,15 @@
let searchName = ''; class Menu {
let dataIsCalled = false; constructor() {
let clearDataOnce = false;
function keyPressed() {
// Check if the back button was selected and the Enter key was pressed
} }
function preload() { homescreen() {
controlsImage = loadImage('./contols.png');
}
function homescreen() {
textAlign(CENTER, CENTER); textAlign(CENTER, CENTER);
textSize(70); textSize(70);
fill(100, 255, 100); fill(100, 255, 100);
text("Slime\nSymphony", width / 2, height / 4); text("Slime\nSymphony", width / 2, height / 4);
button(255, 255, 255, width / 2, height / 2, "Start Game") button.button(255, 255, 255, width / 2, height / 2, "Start Game")
button(255, 255, 255, width / 2, height / 2 + height / 12, "Scores") button.button(255, 255, 255, width / 2, height / 2 + height / 12, "Scores")
push() push()
fill(255, 255, 255) fill(255, 255, 255)
@@ -50,19 +42,7 @@ function homescreen() {
pop() pop()
} }
function button(r, g, b, buttonX, buttonY, buttonText) { pauseMenu() {
push()
noFill()
rectMode(CENTER);
rect(buttonX, buttonY, buttonWidth, buttonHeight)
textSize(25)
fill(r, g, b)
textAlign(CENTER);
text(buttonText, buttonX, buttonY)
pop()
}
function pauseMenu() {
textAlign(CENTER); textAlign(CENTER);
textSize(50); textSize(50);
fill(255, 255, 255); fill(255, 255, 255);
@@ -71,7 +51,7 @@ function pauseMenu() {
button(255, 255, 255, width / 2, height / 2 + height / 12, "Main Menu"); button(255, 255, 255, width / 2, height / 2 + height / 12, "Main Menu");
} }
function scoremenu() { scoreMenu() {
textAlign(CENTER, CENTER); textAlign(CENTER, CENTER);
textSize(50); textSize(50);
fill(255, 255, 255); fill(255, 255, 255);
@@ -97,7 +77,7 @@ function scoremenu() {
rect(width / 2, height / 3, 90, 40) rect(width / 2, height / 3, 90, 40)
pop() pop()
if (!dataIsCalled) { if (!dataIsCalled) {
getData(nameHS); DB.getData(nameHS);
dataIsCalled = true; dataIsCalled = true;
} }
} }
@@ -139,11 +119,11 @@ function scoremenu() {
pop() pop()
push() push()
button(255, 255, 255, width / 2, height / 2, "Main Menu") button.button(255, 255, 255, width / 2, height / 2, "Main Menu")
pop() pop()
} }
function gameOver() { gameOver() {
if (lives <= 0) { if (lives <= 0) {
//game over screen //game over screen
iIndex = constrain(iIndex, 0, 2); iIndex = constrain(iIndex, 0, 2);
@@ -173,7 +153,7 @@ function gameOver() {
} }
function dodgeBanner(){ dodgeBanner() {
push() push()
fill(255, 0, 0) fill(255, 0, 0)
textSize(100) textSize(100)
@@ -181,3 +161,4 @@ function dodgeBanner(){
text("Dodge!", width / 2, height / 2) text("Dodge!", width / 2, height / 2)
pop() pop()
} }
}

View File

@@ -1,4 +1,8 @@
//--------------------Game-------------------- //--------------------Game--------------------
// Classes
const Menus = new Menu();
const button = new Button();
const DB = new database();
// Game variables // Game variables
const width = window.innerWidth; const width = window.innerWidth;
const height = window.innerHeight; const height = window.innerHeight;
@@ -10,7 +14,9 @@ let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
let currentIndex = [0, 0, 0]; let currentIndex = [0, 0, 0];
let nameHS = ''; let nameHS = '';
let pressed = false; let pressed = false;
let searchName = '';
let dataIsCalled = false;
let clearDataOnce = false;
// Player variables // Player variables
const playerSize = 10; const playerSize = 10;
let radius = playerSize / 2; let radius = playerSize / 2;