개발

베네치아 타자게임 예제 본문

Frontend/JavaScript

베네치아 타자게임 예제

Study 2021. 3. 21. 09:22

 

체력, 어레이를 랜덤으로 불러와 내용 랜덤, 높이 랜덤, 점수, 재도전여부

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #wrapper{
            width: 1200px;
            height: 600px;
            background-color: yellowgreen;
            margin: auto;
            overflow: hidden;
        }
        #dashboard{
            width: 200px;
            height: 100%;
            background-color: aqua;
            float: left;
        }
        #content{
            width: 1000px;
            height: 100%;
            background: url(../images/bg.jpg);
            float: left;
            position: relative;
        }
        #dashboard input{
            width: 95%;
            height: 20px;
            background-color: dodgerblue;
            color: white;
            font-weight: bold;
        }
        #hpBox{
            width: 100%;
            height: 190px;
            background: gray;
            margin-top: 50px;
            box-sizing: border-box;
            padding: 4px 2px 2px 2px;
        }
        #scoreArea{
            width: 100%;
            height: 50px;
            background: cornsilk;
            font-size: 40px;
            text-align: center;
        }
        #content span{
            border-radius: 10%;
        }
    </style>
    <script src="../js/lib.js">function getRandom(n){var r =parseInt(Math.random()*n);return r;}</script>
    <script>
        var hpBox;
        var content;
        var scoreArea;
        var spanArray=[];
        var keyWord;
        var velY=10;
        var score=0;
        var wordArray=[
        ["마우스","광화문","핸드폰","지구","개구리","태풍","콘센트","자바","강아지","실로폰"],
        ["지하철","사과","지갑","건널목","블랙홀","아비규환","전기","도서관","아스팔트","나무"],
        ["방송국","구두","식량","온난화","버섯","늦잠","노트북","염치","학교","주사"],
        ["재벌","바다","돼지","습기","구리","한강","벽돌","게임","김치","이순신"]
        ];
 
        
        var hpArray=[];
        var dropFlag=true;
 
        function init(){
            hpBox=document.getElementById("hpBox");
            content=document.getElementById("content");
            scoreArea=document.getElementById("scoreArea");
            keyWord=document.getElementById("keyWord");
            keyWord.addEventListener("keyup"function(){
                if(event.keyCode==13){
                    for(var i=0;i<spanArray.length;i++){
                        if(spanArray[i].innerText==keyWord.value){
                            removeObject(content,spanArray[i],spanArray,i);
                            score+=10;
                            printScore();
                        }
                    }
                    keyWord.value="";
                    checkSuccesed()
                }
            });
        }
        
        function createHp(){
            for(var i=0;i<9;i++){
                var rect = document.createElement("div")
                rect.style.width=60+"px";
                rect.style.height=50+"px";
                rect.style.backgroundColor="red";
                rect.style.border="1px solid yellow";
                rect.style.borderRadius=20+"%";
                rect.style.float="left";
                hpBox.appendChild(rect);
                hpArray.push(rect);
                
            }
        }
        function createWord(){
            var renNo=getRandom(4);
            for(var i=0;i<wordArray[renNo].length;i++){
                var span =document.createElement("span");
 
                span.innerText=wordArray[renNo][i];
                span.style.backgroundColor="white";
                span.style.position="absolute";
                span.style.left=50+(85*i)+"px";
                span.style.top=getRandom(-300)+"px";
                content.appendChild(span);
                spanArray.push(span)
            }
        }
        function checkSuccesed(){
            //남아있는 단어가 있는지 체크, 즉 spanArray가 0인지
            if(spanArray.length<=0&&hpArray.length>0){
                dropFlag=false;
                alert("1단계 통과!");
            }
        }   
 
        function printScore(){
            scoreArea.innerText=score+"점";
        }
        function dropDown(){
            if(dropFlag){
                for(var i=0;i<spanArray.length;i++){
                    spanArray[i].style.top=parseInt(spanArray[i].style.top)+velY+"px";
                    if(parseInt(spanArray[i].style.top)>=600){
                        removeObject(content,spanArray[i],spanArray,i);
                        score-=10;
                        if(hpArray[hpArray.length-1!= undefined){
                            removeObject(hpBox,hpArray[hpArray.length-1],hpArray,hpArray.length-1);
                            checkSuccesed()
                        }
                    }
                }
                if(hpArray.length<=0){
                    dropFlag=false;
                    if(confirm("미션 실패 \n 재도전?")){
                        location.reload();
                    }
                }
            }
        }
        function removeObject(container,child,arr,index){
            //부모 div로 부터 제거
            container.removeChild(child);
            //배열에서 삭제
            arr.splice(index,1);
        }
        window.addEventListener("load"function(){
            init()
            createHp();
            printScore();
            createWord();
            setInterval("dropDown()"170);
        });
    </script>
</head>
<body>
    <div id="wrapper">
        <div id="dashboard">
            <div id="hpBox"></div>
            <input type="text" placeholder="단어 입력" id="keyWord">
            <div id="scoreArea"></div>
        </div>
        <div id="content"></div>
    </div>
</body>
</html>
cs

'Frontend > JavaScript' 카테고리의 다른 글

회원관리 페이지 예제  (0) 2021.03.21
팝업 이미지카드 예제  (0) 2021.03.21
그림판 만들기 예제  (0) 2021.03.20
좌표를 이용한 배열, 맵 연동  (0) 2021.03.20
배열을 이용한 좌표녹화, 이동  (0) 2021.03.20