프로그래밍

[Web] day36 : 자바스크립트 주사위/가위바위보 예제 본문

Web/Web

[Web] day36 : 자바스크립트 주사위/가위바위보 예제

시케 2023. 7. 4. 22:03
728x90
반응형

2023.06.26.월

자바스크립트 주사위/가위바위보 예제

지금까지 익힌 자바스크립트로 컴퓨터와 주사위/가위바위보 게임을 하는 예제를 만들어 보자

 

주사위

JS 실습 예제
게 임 시 작
COM COM 이미지 ME ME 이미지
<!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>

가위바위보

실습 가위바위보
게 임 시 작
COM COM 이미지 ME ME 이미지
<!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>

 

728x90
반응형
Comments