<pre style="color:rgb(0,0,0);font-family:'굴림체';font-size:9pt;background-color:rgb(255,255,255);"><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(0,0,128);font-weight:bold;">public class </span>GameView <span style="color:rgb(0,0,128);font-weight:bold;">extends </span>View <span style="color:rgb(0,0,128);font-weight:bold;">implements </span>View.OnTouchListener {<br><br><span style="color:rgb(0,0,128);font-weight:bold;">int </span><span style="color:rgb(102,14,122);font-weight:bold;">width</span>, <span style="color:rgb(102,14,122);font-weight:bold;">height</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 화면의 폭과 높이<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">static int </span><span style="color:rgb(102,14,122);font-style:italic;">x</span>, <span style="color:rgb(102,14,122);font-style:italic;">y</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터의 현재 좌표<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">int </span><span style="color:rgb(102,14,122);font-weight:bold;">dx</span>, <span style="color:rgb(102,14,122);font-weight:bold;">dy</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터가 이동할 방향과 거리<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">int </span><span style="color:rgb(102,14,122);font-weight:bold;">charaterWidth</span>, <span style="color:rgb(102,14,122);font-weight:bold;">charaterHeight</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터의 폭과 높이<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">int </span><span style="color:rgb(102,14,122);font-weight:bold;">counter</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 루프 카운터<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span>Bitmap <span style="color:rgb(102,14,122);font-weight:bold;">character</span>[] = <span style="color:rgb(0,0,128);font-weight:bold;">new </span>Bitmap[<span style="color:rgb(0,0,255);">2</span>]; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터의 비트맵 이미지<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"> //-----------------------------------<br></span><span style="color:rgb(128,128,128);font-style:italic;"> // Constructor - 게임 초기화<br></span><span style="color:rgb(128,128,128);font-style:italic;"> //-----------------------------------<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">public </span>GameView(Context context) {<br><span style="color:rgb(0,0,128);font-weight:bold;">super</span>(context);<br><br> Display display = ((WindowManager) context.getSystemService(Context.<span style="color:rgb(102,14,122);font-style:italic;font-weight:bold;">WINDOW_SERVICE</span>))<br> .getDefaultDisplay();<br><span style="color:rgb(102,14,122);font-weight:bold;">width </span>= display.getWidth(); <span style="color:rgb(128,128,128);font-style:italic;">// 화면의 가로폭<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">height </span>= display.getHeight(); <span style="color:rgb(128,128,128);font-style:italic;">// 화면의 세로폭<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-style:italic;">x </span>= <span style="color:rgb(0,0,255);">100</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터의 현재 x위치<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-style:italic;">y </span>= <span style="color:rgb(0,0,255);">100</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터의 현재 y위치<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">dx </span>= <span style="color:rgb(0,0,255);">4</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터가 x축으로 이동할 거리<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">dy </span>= <span style="color:rgb(0,0,255);">6</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터가 y축으로 이동할 거리<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"> // 캐릭터의 비트맵 읽기<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">character</span>[<span style="color:rgb(0,0,255);">0</span>] = BitmapFactory.<span style="font-style:italic;">decodeResource</span>(getResources(), R.mipmap.<span style="color:rgb(102,14,122);font-style:italic;font-weight:bold;">ic_launcher</span>);<br><span style="color:rgb(102,14,122);font-weight:bold;">character</span>[<span style="color:rgb(0,0,255);">1</span>] = BitmapFactory.<span style="font-style:italic;">decodeResource</span>(getResources(), R.mipmap.<span style="color:rgb(102,14,122);font-style:italic;font-weight:bold;">ic_launcher</span>);<br><br><span style="color:rgb(102,14,122);font-weight:bold;">charaterWidth </span>= <span style="color:rgb(102,14,122);font-weight:bold;">character</span>[<span style="color:rgb(0,0,255);">0</span>].getWidth() / <span style="color:rgb(0,0,255);">2</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터의 폭/2<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">charaterHeight </span>= <span style="color:rgb(102,14,122);font-weight:bold;">character</span>[<span style="color:rgb(0,0,255);">0</span>].getHeight() / <span style="color:rgb(0,0,255);">2</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 캐릭터의 높이/2<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">mHandler</span>.sendEmptyMessageDelayed(<span style="color:rgb(0,0,255);">0</span>, <span style="color:rgb(0,0,255);">10</span>);<br> }<br><br><span style="color:rgb(128,128,128);font-style:italic;">//-----------------------------------<br></span><span style="color:rgb(128,128,128);font-style:italic;"> // 실제 그림을 그리는 부분<br></span><span style="color:rgb(128,128,128);font-style:italic;"> //-----------------------------------<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">public void </span>onDraw(Canvas canvas) {<br><span style="color:rgb(102,14,122);font-style:italic;">x </span>+= <span style="color:rgb(102,14,122);font-weight:bold;">dx</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 가로 방향으로 이동<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-style:italic;">y </span>= <span style="color:rgb(102,14,122);font-weight:bold;">height </span>- <span style="color:rgb(102,14,122);font-weight:bold;">charaterHeight</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 세로 위치 세팅<br></span><span style="color:rgb(128,128,128);font-style:italic;"> // y += dy; // 세로 방향으로 이동<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">if </span>(<span style="color:rgb(102,14,122);font-style:italic;">x </span>< <span style="color:rgb(102,14,122);font-weight:bold;">charaterWidth </span>|| <span style="color:rgb(102,14,122);font-style:italic;">x </span>> <span style="color:rgb(102,14,122);font-weight:bold;">width </span>- <span style="color:rgb(102,14,122);font-weight:bold;">charaterWidth</span>) <span style="color:rgb(102,14,122);font-weight:bold;">dx </span>= <span style="color:rgb(0,0,255);">0</span>; <span style="color:rgb(128,128,128);font-style:italic;">// 좌우의 벽이면 방향을 바꿈<br></span><span style="color:rgb(128,128,128);font-style:italic;"> //if (y < charaterHeight || y > height - charaterHeight) dy = -dy; // 천정이거나 바닥이면 방향을 바꿈<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">counter</span>++;<br><span style="color:rgb(0,0,128);font-weight:bold;">int </span>n = <span style="color:rgb(102,14,122);font-weight:bold;">counter </span>% <span style="color:rgb(0,0,255);">20 </span>/ <span style="color:rgb(0,0,255);">10</span>;<br><br><br><br> canvas.drawBitmap(<span style="color:rgb(102,14,122);font-weight:bold;">character</span>[n], <span style="color:rgb(102,14,122);font-style:italic;">x </span>- <span style="color:rgb(102,14,122);font-weight:bold;">charaterWidth</span>, <span style="color:rgb(102,14,122);font-style:italic;">y </span>- <span style="color:rgb(102,14,122);font-weight:bold;">charaterHeight</span>, <span style="color:rgb(0,0,128);font-weight:bold;">null</span>);<br> } <span style="color:rgb(128,128,128);font-style:italic;">// onDraw 끝<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"> //------------------------------------<br></span><span style="color:rgb(128,128,128);font-style:italic;"> // Timer Handler<br></span><span style="color:rgb(128,128,128);font-style:italic;"> //------------------------------------<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span>Handler <span style="color:rgb(102,14,122);font-weight:bold;">mHandler </span>= <span style="color:rgb(0,0,128);font-weight:bold;">new </span>Handler() { <span style="color:rgb(128,128,128);font-style:italic;">// 타이머로 사용할 Handler<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(0,0,128);font-weight:bold;">public void </span>handleMessage(Message msg) {<br> invalidate(); <span style="color:rgb(128,128,128);font-style:italic;">// onDraw() 다시 실행<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(102,14,122);font-weight:bold;">mHandler</span>.sendEmptyMessageDelayed(<span style="color:rgb(0,0,255);">0</span>, <span style="color:rgb(0,0,255);">10</span>); <span style="color:rgb(128,128,128);font-style:italic;">// 10/1000초마다 실행<br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span>}<br> }; <span style="color:rgb(128,128,128);font-style:italic;">// Handler<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"> </span><span style="color:rgb(128,128,0);">@Override<br></span><span style="color:rgb(128,128,0);"> </span><span style="color:rgb(0,0,128);font-weight:bold;">public boolean </span>onTouch(View v, MotionEvent event) {<br><span style="color:rgb(0,0,128);font-weight:bold;">if</span>(event.getAction()==MotionEvent.<span style="color:rgb(102,14,122);font-style:italic;font-weight:bold;">ACTION_DOWN</span>) {<br><br><span style="color:rgb(102,14,122);font-style:italic;">x </span>= <span style="color:rgb(102,14,122);font-style:italic;">x </span>- <span style="color:rgb(0,0,255);">1000 </span>;<br> invalidate();<br> }<br><span style="color:rgb(0,0,128);font-weight:bold;">return true</span>;<br> }<br>} <span style="color:rgb(128,128,128);font-style:italic;">// GameView 끝<br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;">// 프로그램 끝</span></pre> <div> </div> <div><em><font color="#808080" face="굴림체"></font></em> </div> <div> X 의 위치가 변화 되어야 할 거 같은데 인식이 안되네요..</div><pre style="color:rgb(0,0,0);font-family:'굴림체';font-size:9pt;background-color:rgb(255,255,255);"><span style="color:rgb(128,128,128);font-style:italic;"><br></span><span style="color:rgb(128,128,128);font-style:italic;"><br></span></pre>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.