Added score + some more UI changes

This commit is contained in:
Enrique Jose Delgado Garcia 2025-05-09 00:49:15 +09:00
parent 5038416cd5
commit 85c09d2843
4 changed files with 105 additions and 26 deletions

View File

@ -13,7 +13,12 @@ p5 docs
https://www.geeksforgeeks.org/how-to-shuffle-an-array-using-javascript/ https://www.geeksforgeeks.org/how-to-shuffle-an-array-using-javascript/
https://stackoverflow.com/questions/4550013/html-element-display-in-horizontal-line https://stackoverflow.com/questions/4550013/html-element-display-in-horizontal-line
figma figma
https://stackoverflow.com/questions/66886318/how-do-i-create-a-custom-event-in-svelte
TODO:
- add the powers
- add the visual flair
- add remaining text
<style> <style>
.centered { .centered {

View File

@ -3,12 +3,14 @@
import HiLo from './lib/HiLo.svelte'; import HiLo from './lib/HiLo.svelte';
const windowRatio = 0.9; const windowRatio = 0.9;
const width = window.innerWidth * windowRatio; const width = window.innerWidth * windowRatio * 0.8;
const height = window.innerHeight * windowRatio; const height = window.innerHeight * windowRatio;
const imageRatio = 3 / 2; const imageRatio = 3 / 2;
const cardImageWidth = height * 0.6; const cardImageWidth = height * 0.6;
const canvasXMargin = height * 0.03; const canvasXMargin = height * 0.03;
const canvasYMargin = height * 0.07; const canvasYMargin = height * 0.07;
const textXMargin = width * 0.01;
const textSize = width * 0.03;
let gameLogic; let gameLogic;
let currentCard; let currentCard;
@ -19,17 +21,24 @@
} }
p5.setup = function(){ p5.setup = function(){
p5.createCanvas(width, height); p5.createCanvas(width, height);
p5.background(100); p5.background(60);
currentCard = gameLogic.getCurrentCard();
}; };
p5.draw = function() { p5.draw = function() {
//console.log(currentCard); if(gameLogic.getRecentScore() > 0) {
p5.image(currentCard.getImage(), canvasXMargin, canvasYMargin, cardImageWidth, cardImageWidth*imageRatio); p5.background(10, 150, 10);
}
else if(gameLogic.getRecentScore() < 0){
p5.background(150, 10, 10);
}
p5.image(gameLogic.getCurrentCard().getImage(), canvasXMargin, canvasYMargin, cardImageWidth, cardImageWidth*imageRatio);
p5.image(gameLogic.getBackCardImage(), (canvasXMargin * 2) + cardImageWidth, canvasYMargin, cardImageWidth, cardImageWidth*imageRatio); p5.image(gameLogic.getBackCardImage(), (canvasXMargin * 2) + cardImageWidth, canvasYMargin, cardImageWidth, cardImageWidth*imageRatio);
}
p5.mouseDragged = function(){ p5.fill(255);
//console.log("mouse dragged"); p5.textSize(textSize)
p5.textAlign(p5.TOP, p5.TOP);
p5.text("Score: " + gameLogic.getScore(), (canvasXMargin * 2) + (cardImageWidth * 2) + textXMargin, canvasYMargin);
p5.fill(220);
p5.text(gameLogic.getScoreMessage(), (canvasXMargin * 8) + (cardImageWidth * 2) + textXMargin, (canvasYMargin) + textSize)
} }
} }
</script> </script>

View File

@ -4,6 +4,14 @@
let images = {}; let images = {};
export let deck = []; export let deck = [];
export const NUMBER = "Number";
export const FACE = "Face";
export const ACE = "Ace";
export const HEARTS = "Hearts";
export const DIAMONDS = "Diamonds";
export const SPADES = "Spades";
export const CLUBS = "Clubs";
class Card { class Card {
value; value;
suit; suit;
@ -55,22 +63,22 @@
export function createDeck() export function createDeck()
{ {
let indexSuit = "Hearts" let indexSuit = HEARTS
let indexVal = 1; let indexVal = 1;
function switchSuit() function switchSuit()
{ {
switch(indexSuit) { switch(indexSuit) {
case "Hearts": case HEARTS:
indexSuit = "Diamonds"; indexSuit = DIAMONDS;
break; break;
case "Diamonds": case DIAMONDS:
indexSuit = "Clubs"; indexSuit = CLUBS;
break; break;
case "Clubs": case CLUBS:
indexSuit = "Spades"; indexSuit = SPADES;
break; break;
case "Spades": case SPADES:
indexSuit = "Hearts"; indexSuit = HEARTS;
break; break;
} }
} }
@ -84,16 +92,16 @@
let type; let type;
if(indexVal == 1){ if(indexVal == 1){
type = "Ace"; type = ACE;
nameString += type; nameString += type;
} }
else if(indexVal <= 10) else if(indexVal <= 10)
{ {
type = "Number"; type = NUMBER;
nameString += indexVal nameString += indexVal
} }
else { else {
type = "Face"; type = FACE;
switch(indexVal) { switch(indexVal) {
default: default:
nameString += "Back"; nameString += "Back";

View File

@ -6,12 +6,15 @@
let currentCard; let currentCard;
let deck; let deck;
let score = 0; let score = 0;
let recentScore = 0;
let gameEnd = false;
export function initiateGame(p5) export function initiateGame(p5)
{ {
deck.loadDeckImages(p5); deck.loadDeckImages(p5);
deck.createDeck(); deck.createDeck();
deck.shuffleDeck(); deck.shuffleDeck();
gameEnd = false;
currentCard = deck.drawCard(); currentCard = deck.drawCard();
console.log(currentCard); console.log(currentCard);
score = currentCard.getValue(); score = currentCard.getValue();
@ -19,29 +22,83 @@
export function nextRound(highChosen) export function nextRound(highChosen)
{ {
const topCard = deck.drawCard(); if(gameEnd) return null;
const topHigherThanCurrent = currentCard.getValue() < topCard.getValue();
if((highChosen && topHigherThanCurrent) || (!highChosen && !topHigherThanCurrent)) const topCard = deck.drawCard();
const currentValue = currentCard.getValue();
const topValue = topCard.getValue();
const currentHigherThanTop = evaluateIfCardHigherThanCard(currentCard, topCard);
const equalToCurrent = currentValue == topValue;
if(topValue == 1)
{ {
score += topCard.getValue(); if(highChosen) recentScore = 15;
else recentScore = 1;
}
else if((highChosen && !currentHigherThanTop) || (!highChosen && currentHigherThanTop) || equalToCurrent)
{
recentScore = topValue;
} }
else else
{ {
score -= 15 - topCard.getValue(); recentScore = -(15 - topValue);
} }
score += recentScore
currentCard = topCard; currentCard = topCard;
console.log(score);
if(deck.getRemaining() == 0) {
gameEnd = true;
}
return currentCard; return currentCard;
} }
const pickHigh = () => nextRound(true); const pickHigh = () => nextRound(true);
const pickLow = () => nextRound(false); const pickLow = () => nextRound(false);
function evaluateIfCardHigherThanCard(firstCard, secondCard)
{
if(firstCard.getType() == deck.ACE)
{
const secondType = secondCard.getType();
if(secondType == deck.NUMBER) return false;
return true;
}
else
{
return firstCard.getValue() > secondCard.getValue();
}
}
export function getCurrentCard() export function getCurrentCard()
{ {
return currentCard; return currentCard;
} }
export function getScore() {
return score;
}
export function getScoreMessage() {
let message = ""
if(recentScore > 0) {
message = "+"
}
else if(recentScore == 0)
{
return "";
}
return message + recentScore;
}
export function getRecentScore() {
return recentScore;
}
export function getBackCardImage() export function getBackCardImage()
{ {
return deck.getBackCardImage(); return deck.getBackCardImage();