![COM 이미지](images/dice01.png)
![ME 이미지](images/dice01.png)
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
2023.06.26.월
지금까지 익힌 자바스크립트로 컴퓨터와 주사위/가위바위보 게임을 하는 예제를 만들어 보자
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS 실습 예제</title>
<style type="text/css">
#title {
width: 450px;
height: 30px;
margin: 5px;
background-color: lightgreen;
cursor: pointer;
line-height: 30px;
text-align: center;
}
#content {
width: 450px;
margin: 5px;
background-color: lightblue;
}
span, img {
margin: 5px;
}
</style>
</head>
<body>
<div id="title">게 임 시 작</div>
<div id="content">
<span>COM</span> <img id="com" src="images/dice01.png" alt="COM 이미지">
<span>ME</span> <img id="me" src="images/dice01.png" alt="ME 이미지">
</div>
<script type="text/javascript">
document.querySelector('#title').onclick = function() {
if (this.firstChild.nodeValue == '게 임 시 작') {
this.firstChild.nodeValue = '결 과 확 인';
this.style.backgroundColor = 'green';
this.style.color = 'white';
play();
} else {
this.firstChild.nodeValue = '게 임 시 작';
this.style.backgroundColor = 'lightgreen';
this.style.color = 'black';
stop();
}
};
function play() {
document.querySelector('#com').src = 'images/dice.gif';
document.querySelector('#me').src = 'images/dice.gif';
}
function stop() {
var comNum = Math.floor(Math.random() * 6) + 1;
var meNum = Math.floor(Math.random() * 6) + 1;
document.querySelector('#com').src = 'images/dice0' + comNum
+ '.png';
document.querySelector('#me').src = 'images/dice0' + meNum + '.png';
setTimeout(function() {
if (comNum < meNum) {
alert('승리!');
} else if (comNum == meNum) {
alert('무승부');
} else {
alert('패배...');
}
}, 1000);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>실습 가위바위보</title>
<style type="text/css">
#title {
width: 450px;
height: 30px;
margin: 5px;
background-color: lightgreen;
cursor: pointer;
line-height: 30px;
text-align: center;
}
#content {
width: 450px;
margin: 5px;
background-color: lightblue;
}
span, img {
margin: 5px;
}
</style>
</head>
<body>
<div id="title">게 임 시 작</div>
<div id="content">
<span>COM</span> <img id="com" src="images/game1.png" alt="COM 이미지">
<span>ME</span> <img id="me" src="images/game2.png" alt="ME 이미지">
</div>
<script type="text/javascript">
document.querySelector('#title').onclick = function() {
if (this.firstChild.nodeValue == '게 임 시 작') {
this.firstChild.nodeValue = '결 과 확 인';
this.style.backgroundColor = 'green';
this.style.color = 'white';
play();
} else {
this.firstChild.nodeValue = '게 임 시 작';
this.style.backgroundColor = 'lightgreen';
this.style.color = 'black';
stop();
}
};
function play(){
document.querySelector('#com').src = 'images/game.gif';
document.querySelector('#me').src = 'images/game.gif';
}
function stop(){
var comNum = Math.floor(Math.random() * 3) + 1;
var meNum = Math.floor(Math.random() * 3) + 1;
document.querySelector('#com').src = 'images/game'+comNum+'.png';
document.querySelector('#me').src = 'images/game'+meNum+'.png';
setTimeout(function(){
if(comNum == meNum) {
alert('무승부');
}
else if((comNum - meNum == -1) || (comNum - meNum == -2)) {
alert('승리!');
}
else {
alert('패배...');
}
},1000);
}
</script>
</body>
</html>
[Web] day37 : ajax() 메서드와 JSON 데이터 (0) | 2023.07.05 |
---|---|
[Web] day36 : jQuery 자바스크립트 라이브러리 (1) | 2023.07.05 |
[Web] day35 : BOM (Browser Object Model) (0) | 2023.07.04 |
[Web] day35 : 자바스크립트 이벤트 활용 예제 (0) | 2023.07.04 |
[Web] day35 : 자바스크립트 이벤트 연결02 (0) | 2023.07.04 |