-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[카드 뒤집기 미션 - 2단계] 곽가영 미션 제출합니다. #10
base: gykwak03
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,106 @@ | ||
.card { | ||
width: 80px; | ||
height: 100px; | ||
|
||
background: #F9A825; | ||
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25); | ||
border-radius: 3px; | ||
|
||
font-family: 'Inter'; | ||
font-style: normal; | ||
font-weight: 600; | ||
font-size: 20px; | ||
line-height: 24px; | ||
|
||
color: #FFFFFF; | ||
|
||
|
||
} | ||
|
||
|
||
#cards { | ||
display: flex; | ||
gap: 10px; | ||
|
||
display: flex; | ||
gap: 10px; | ||
|
||
position: absolute; | ||
width: 365px; | ||
height: 100px; | ||
left: 458px; | ||
top: 165px; | ||
align-items: center; | ||
justify-content: center; | ||
|
||
} | ||
|
||
|
||
#title1{ | ||
white-space: nowrap; | ||
position: absolute; | ||
width: 152px; | ||
height: 24px; | ||
left: 561px; | ||
top: 97px; | ||
|
||
font-family: 'Inter'; | ||
font-style: normal; | ||
font-weight: 600; | ||
font-size: 20px; | ||
line-height: 24px; | ||
|
||
color: #000000; | ||
|
||
} | ||
|
||
#rect{ | ||
position: absolute; | ||
width: 410px; | ||
height: 160px; | ||
left: 435px; | ||
top: 138px; | ||
|
||
|
||
background: #FDD853; | ||
border-radius: 10px; | ||
|
||
} | ||
|
||
.restart{ | ||
|
||
position: absolute; | ||
width: 140px; | ||
height: 48px; | ||
left: 560px; | ||
top: 200px; | ||
|
||
background: rgba(255, 255, 255, 0.86); | ||
border-radius: 10px; | ||
|
||
|
||
|
||
} | ||
|
||
.information{ | ||
|
||
white-space: nowrap; | ||
position: absolute; | ||
width: 82px; | ||
height: 19px; | ||
|
||
top: 150px; | ||
justify-content: center; | ||
|
||
|
||
font-family: 'Inter'; | ||
font-style: normal; | ||
font-weight: 400; | ||
font-size: 16px; | ||
line-height: 19px; | ||
|
||
color: #000000; | ||
|
||
|
||
|
||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,70 @@ | ||
class Card { | ||
constructor(cardsContainer) { | ||
let card = document.createElement("button"); // | ||
card.classList.add("card"); // | ||
card.textContent = "카드입니다"; | ||
let card = document.createElement("button"); | ||
card.classList.add("card"); | ||
|
||
this.card = card; | ||
this.cardsContainer = cardsContainer; | ||
this.render(); | ||
} | ||
|
||
render() { | ||
this.cardsContainer.appendChild(this.card); //부모 노드와 자식 노드 연결 | ||
this.cardsContainer.appendChild(this.card); | ||
} | ||
} | ||
|
||
export default Card; | ||
class GameManager { | ||
constructor() { | ||
this.chance = 2; | ||
this.numbers = [0, 1, 2, 3]; | ||
this.gameOver = false; | ||
this.information = document.createElement("p"); | ||
this.information.classList.add("information"); | ||
this.cardsContainer = document.getElementById("cards"); | ||
this.cardsContainer.appendChild(this.information); | ||
} | ||
|
||
initButtons(randomNum) { | ||
const buttons = document.querySelectorAll("button"); | ||
buttons.forEach((button, index) => { | ||
this.startGame(button, randomNum, index); | ||
}); | ||
} | ||
|
||
startGame(button, randomNum, index) { | ||
this.information.textContent = `남은 횟수 : ${this.chance} 회`; | ||
|
||
button.addEventListener("click", () => { | ||
if (this.gameOver) return; | ||
|
||
if (randomNum === index) { | ||
button.textContent = "당첨"; | ||
this.endGame("(성공!) 당첨되었습니다.", randomNum); | ||
} else { | ||
button.textContent = "꽝"; | ||
this.chance = this.chance - 1; | ||
|
||
if (this.chance === 0) { | ||
this.endGame("(실패!) 게임이 종료되었습니다.", randomNum); | ||
} else { | ||
this.startGame(button, this.cardsContainer, randomNum, index); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
endGame(message, randomNum) { | ||
this.gameOver = true; | ||
this.information.textContent = message; | ||
|
||
let restart = document.createElement("button"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. button과 div element의 차이점은 무엇일까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. button은 사용자가 클릭하면 동작하는 상호작용이 가능한 엘리먼트이고 |
||
restart.textContent = "재시작"; | ||
restart.classList.add("restart"); | ||
document.body.appendChild(restart); | ||
restart.addEventListener("click", () => { | ||
location.reload(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 버튼을 클릭했을 때 새로고침을 함으로써 재시작이 되도록 기능을 작성해주셨네요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. start 메소드를 end에서 재활용을 해보려 했는데 재시작과 변수 변경 등등이 오류가 나서.. 좀만 시간을 두고 다시 해보겠습니다 ㅠㅠ |
||
}); | ||
} | ||
} | ||
|
||
export { Card, GameManager }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,13 @@ | ||
import "../index.css"; | ||
import Card from "./Card.js"; | ||
import { Card, GameManager } from "./Card.js"; | ||
|
||
let cardsContainer = document.querySelector("#cards"); | ||
|
||
const card1 = new Card(cardsContainer); // cardsContainer를 생성자에 전달 | ||
const card2 = new Card(cardsContainer); // cardsContainer를 생성자에 전달 | ||
const card3 = new Card(cardsContainer); // cardsContainer를 생성자에 전달 | ||
var cards = {}; | ||
for (var i = 1; i < 5; i++) { | ||
cards[`card${i}`] = new Card(cardsContainer); | ||
} | ||
var randomNum = Math.floor(Math.random() * 4); | ||
const gameManager = new GameManager(); | ||
|
||
const buttons = document.querySelectorAll("button"); | ||
|
||
const randomNum = Math.floor(Math.random() * 3); | ||
|
||
const numbers = [0, 1, 2]; | ||
numbers.forEach(function (number) { | ||
buttons[number].addEventListener("click", () => { | ||
cardsContainer.innerHTML = ""; | ||
const result = document.createElement("p"); // 새로운 <p> 엘리먼트 생성 | ||
if (randomNum == number) { | ||
result.textContent = "정답입니다"; | ||
} else { | ||
result.textContent = "꽝입니다"; | ||
} | ||
cardsContainer.appendChild(result); | ||
}); | ||
}); | ||
gameManager.initButtons(randomNum); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
중앙 배치가 되어있지 않은데 align-items과 justify 속성에 대해서 알아볼까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align-items: center; 를 추가하면 가로축을 기준으로 카드들이 정렬하게 되고,
justify-content: center; 를 추가하면 세로축을 기준으로 가운데 정렬하게 됩니다!
코드에 삽입하겠습니다.