시작프로그램에 넣으면 됨
코드 짠거 올리기
직접 만든 코드들 올리는 블로그 c언어, python, opencv, java, android, 네트워크프로그래밍
6/09/2022
12/22/2019
안드로이드 스튜디오, JAVA로만든 벽돌깨기게임
3학년 기말고사
간단한 벽돌깨기게임을 만들어봄
문제점이 많이있음
벽돌깨기게임 import할 파일
압축풀어서 임포트하면됨
문제점과 코드설명?
주요코드
MyGameView.java
다펴지는데 로딩이걸리는듯
package com.example.벽돌깨기게임; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Paint; import android.media.AudioManager; import android.media.SoundPool; import android.os.Handler; import android.os.Message; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import java.util.ArrayList; import java.util.Random; public class MyGameView extends View { static int blockwidth; static int blockheight; static int marginleft; static int margintop; int width, height; int cx, cy; int bw, bh; int pw, ph; int sx, sy; int num = 0; int input; int item1 = 0; int item2 = 0; int item3 = 0; int item4 = 0; int ix, iy, i2x, i2y, i3x, i3y, i4x, i4y, i5x, i5y; int iw; int paddlewidth; int itemcreatex, itemcreatey; int itemtake = 0; int item2take = 0; int item3take = 0; int item4take = 0; int thru = 0; int score = 0; int max = 0; SoundPool sound = new SoundPool(1, AudioManager.STREAM_ALARM, 0); int soundId = sound.load(getContext(), R.raw.bang11, 1); int soundId2 = sound.load(getContext(), R.raw.boing, 1); int soundId3 = sound.load(getContext(), R.raw.base, 1); int soundId4 = sound.load(getContext(), R.raw.explode, 1); int soundId5 = sound.load(getContext(), R.raw.shrink, 1); int soundId6 = sound.load(getContext(), R.raw.expand, 1); int soundId7 = sound.load(getContext(), R.raw.speed, 1); int soundId8 = sound.load(getContext(), R.raw.thru, 1); String scorea = "파괴한 벽돌 수 : "; float Brickpos[][] = { {0, 0}, {1, 0}, {2, 0}, {3, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {0, 2}, {1, 2}, {2, 2}, {3, 2}, {0, 3}, {1, 3}, {2, 3}, {3, 3}, {0, 4}, {1, 4}, {2, 4}, {3, 4}, {0, 5}, {1, 5}, {2, 5}, {3, 5} }; Context mContext; Paint paint; Paddle mPaddle; Ball mBall; Item mitem; Item2 mitem2; Item3 mitem3; Item4 mitem4; ArrayList<Brick> mBrick = new ArrayList<Brick>(); Random random = new Random(); public MyGameView(Context context) { super(context); paint = new Paint(); mContext = context; Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); width = display.getWidth(); height = display.getHeight(); blockwidth = width / 5; blockheight = width / 9; marginleft = (width - blockwidth * 4) / 2; margintop = marginleft * 2; paddlewidth = width / 5; for(int i = 0; i < Brickpos.length; i++) mBrick.add(new Brick(mContext, Brickpos[i][0], Brickpos[i][1])); mPaddle = new Paddle(mContext, width/2, height-margintop * 2, paddlewidth, 60); mBall = new Ball(mContext, width/2, height/2 + 200, marginleft/2, marginleft/2); mitem = new Item(mContext, itemcreatex, itemcreatey, blockheight, blockheight); mitem2 = new Item2(mContext, itemcreatex, itemcreatey, blockheight, blockheight); mitem3 = new Item3(mContext, itemcreatex, itemcreatey, blockheight, blockheight); mitem4 = new Item4(mContext, itemcreatex, itemcreatey, blockheight, blockheight); bw = mBall.imgBall.getWidth()/2; bh = mBall.imgBall.getHeight()/2; pw = mPaddle.imgPaddle.getWidth()/2; ph = mPaddle.imgPaddle.getHeight(); iw = mitem.imgexpand.getWidth()/2; cx = width / 2; cy = height / 2; sx = 5; sy = 10; ix = 7; iy = 7; i2x = 7; i2y = 7; i3x = 7; i3y = 7; i4x = 7; i4y = 7; i5x = 7; i5y = 7; mHandler.sendEmptyMessageDelayed(0, 1); //핸들러 호출 } public void onDraw(Canvas canvas) { canvas.drawText(scorea, 10, blockheight/2, paint); canvas.drawText(String.valueOf(score), 430, blockheight/2, paint); paint.setTextSize(60); if(item1 == 1) { mitem.x = mitem.x + ix; mitem.y = mitem.y + iy; } if(item2 == 1) { mitem2.x = mitem2.x + i2x; mitem2.y = mitem2.y + i2y; } if(item3 == 1) { mitem3.x = mitem3.x + i3x; mitem3.y = mitem3.y + i3y; } if(item4 == 1) { mitem4.x = mitem4.x + i4x; mitem4.y = mitem4.y + i4y; } //-----------------------------------------아이템시작 //아이템1 화면에 충돌 if (mitem.x < iw) { //왼쪽벽에 충돌 mitem.x = iw; ix = -ix; } if (mitem.x + iw > width) { //오른쪽에 충돌 mitem.x = width - iw; ix = -ix; } if (mitem.y > height - iw) { //바닥에 충돌 mitem.x = width + 1000; //먹었으면 이미지를 구석으로 보내서 화면에 안보이게 mitem.y = height + 1000; ix = 0; iy = 0; if(itemtake == 1) //아이템 다시 나오게 itemtake = 0; } //아이템2 화면에 충돌 if (itemcreatex < iw) { //왼쪽벽에 충돌 i2x = -i2x; } if (mitem2.x + iw > width) { //오른쪽에 충돌 mitem2.x = width - iw; i2x = -i2x; } if (mitem2.y > height - iw) { //바닥에 충돌 mitem2.x = width + 1000; mitem2.y = height + 1000; i2x = 0; i2y = 0; if(item2take == 1) item2take = 0; } //아이템3 화면에 충돌 if (itemcreatex < iw) { //왼쪽벽에 충돌 i3x = -i3x; } if (mitem3.x + iw > width) { //오른쪽에 충돌 mitem3.x = width - iw; i3x = -i3x; } if (mitem3.y > height - iw) { //바닥에 충돌 mitem3.x = width + 1000; mitem3.y = height + 1000; i3x = 0; i3y = 0; } //아이템4 화면에 충돌 if (itemcreatex < iw) { //왼쪽벽에 충돌 i4x = -i4x; } if (mitem4.x + iw > width) { //오른쪽에 충돌 mitem4.x = width - iw; i4x = -i4x; } if (mitem4.y > height - iw) { //바닥에 충돌 mitem4.x = width + 1000; mitem4.y = height + 1000; i4x = 0; i4y = 0; if(item4take == 1) item4take = 0; } //아이템1 페달에 충돌 if(mitem.y <= mPaddle.y + 30 && mitem.y + blockheight >= mPaddle.y - 30) { if(mitem.x >= mPaddle.x - pw - blockheight && mitem.x <= mPaddle.x + pw){ int streamId2 = sound.play(soundId6, 0.5F, 0.5F, 1, 0, 1.0F); paddlewidth = paddlewidth + 100; //패달 늘리기 max--; mPaddle = new Paddle(mContext, mPaddle.x, height-margintop * 2, paddlewidth, 60); pw = mPaddle.imgPaddle.getWidth()/2; //패달늘어난 너비 적용 mitem.x = width + 1000; mitem.y = height + 1000; ix = 0; iy = 0; if(itemtake == 1) itemtake = 0; } } //아이템2 페달에 충돌 if(mitem2.y <= mPaddle.y + 30 && mitem2.y + blockheight >= mPaddle.y - 30) { if(mitem2.x >= mPaddle.x - pw - blockheight && mitem2.x <= mPaddle.x + pw){ int streamId2 = sound.play(soundId5, 0.5F, 0.5F, 1, 0, 1.0F); if(max < 1) { //패달이 너무작아지지않게하기 paddlewidth = paddlewidth - 100; //패달 max++; } mPaddle = new Paddle(mContext, mPaddle.x, height-margintop * 2, paddlewidth, 60); pw = mPaddle.imgPaddle.getWidth()/2; //패달늘어난 너비 적용 mitem2.x = width + 1000; mitem2.y = height + 1000; i2x = 0; i2y = 0; if(item2take == 1) item2take = 0; } } //아이템3 페달에 충돌 if(mitem3.y <= mPaddle.y + 30 && mitem3.y + blockheight >= mPaddle.y - 30) { if(mitem3.x >= mPaddle.x - pw - blockheight && mitem3.x <= mPaddle.x + pw){ int streamId2 = sound.play(soundId8, 0.5F, 0.5F, 1, 0, 1.0F); mitem3.x = width + 1000; mitem3.y = height + 1000; i3x = 0; i3y = 0; thru = 1; //벽돌뚫기 } } //아이템4 페달에 충돌 if(mitem4.y <= mPaddle.y + 30 && mitem4.y + blockheight >= mPaddle.y - 30) { if(mitem4.x >= mPaddle.x - pw - blockheight && mitem4.x <= mPaddle.x + pw){ int streamId2 = sound.play(soundId7, 0.5F, 0.5F, 1, 0, 1.0F); mitem4.x = width + 1000; mitem4.y = height + 1000; i4x = 0; i4y = 0; if(sy < 0) { sy = sy - 5; //속도빨라지게 } else { sy = sy + 5; } if(item4take == 1) item4take = 0; } } //----------------------------------------------------------------------------아이템끝 //공이 화면에 충돌 if(num == 1) //시작할때 공이 아래로만 떨어지게 mBall.x = mBall.x + sx; //수평으로이동 mBall.y = mBall.y + sy; //수직으로이동 if (mBall.x < bw) { //왼쪽벽에 충돌 int streamId2 = sound.play(soundId3, 0.5F, 0.5F, 1, 0, 1.0F); mBall.x = bw; sx = -sx; } if (mBall.x > width - bw) { //오른쪽에 충돌 int streamId2 = sound.play(soundId3, 0.5F, 0.5F, 1, 0, 1.0F); mBall.x = width - bw; sx = -sx; } if (mBall.y < bh) { //천장에충돌 int streamId2 = sound.play(soundId3, 0.5F, 0.5F, 1, 0, 1.0F); mBall.y = bh; sy = -sy; } if (mBall.y > height - bh) { //바닥임시 int streamId2 = sound.play(soundId4, 0.5F, 0.5F, 1, 0, 1.0F); if(mContext instanceof Activity){ Intent intent = new Intent(mContext, StageActivity.class); Activity activity = (Activity)mContext; activity.startActivity(intent); } super.setVisibility(View.INVISIBLE); //바닥에 공이닿았을때 종료 } //공이 페달에 충돌 if(mBall.y > mPaddle.y - 30 && mBall.y < mPaddle.y + 30) { if(mBall.x >= mPaddle.x - pw && mBall.x <= mPaddle.x - (pw * 2 / 5 * 1.5)){ int streamId2 = sound.play(soundId2, 0.5F, 0.5F, 1, 0, 1.0F); sx = -13; sy = -sy; num = 1;//공이 페들에 닿았을때 공 좌우로도 움직이기시작 } else if(mBall.x > mPaddle.x - (pw * 2 / 5 * 1.5) && mBall.x <= mPaddle.x - (pw * 2 / 5 * 0.5)){ int streamId2 = sound.play(soundId2, 0.5F, 0.5F, 1, 0, 1.0F); sx = -10; sy = -sy; num = 1; } else if(mBall.x > mPaddle.x - (pw * 2 / 5 * 0.5) && mBall.x <= mPaddle.x){ int streamId2 = sound.play(soundId2, 0.5F, 0.5F, 1, 0, 1.0F); sx = -3; sy = -sy; num = 1; } else if(mBall.x > mPaddle.x && mBall.x <= mPaddle.x + (pw * 2 / 5 * 0.5)){ int streamId2 = sound.play(soundId2, 0.5F, 0.5F, 1, 0, 1.0F); sx = 3; sy = -sy; num = 1; } else if(mBall.x > mPaddle.x + (pw * 2 / 5 * 0.5) && mBall.x <= mPaddle.x + (pw * 2 / 5 * 1.5)){ int streamId2 = sound.play(soundId2, 0.5F, 0.5F, 1, 0, 1.0F); sx = 10; sy = -sy; num = 1; } else if(mBall.x > mPaddle.x + (pw * 2 / 5 * 1.5) && mBall.x <= mPaddle.x + pw){ int streamId2 = sound.play(soundId2, 0.5F, 0.5F, 1, 0, 1.0F); sx = 13; sy = -sy; num = 1; } } //공이 벽돌에충돌 for (Brick tmp : mBrick) { if(thru == 0){ //벽돌을 뚫는아이템 item3를 먹지 않았을때 if(mBall.x + bw < tmp.x1 /*- 10*/ || mBall.x - bw > tmp.x2 || mBall.y + bw < tmp.y1 || mBall.y - bw > tmp.y2){ //충돌안할때 continue; //벽돌에 공이 안닿았으면 다음 문장들을 실행하지않음 } if((mBall.x + bw >= tmp.x1 && mBall.x + bw <= tmp.x1 + 10) || (mBall.x - bw <= tmp.x2 && mBall.x - bw >= tmp.x2 - 10)) { //공이 벽돌의 좌우에 닿았을때 int streamId = sound.play(soundId, 0.5F, 0.5F, 1, 0, 1.0F); sx = -sx; input = random.nextInt(99) + 1; //아이템이 생성되게하는 난수 생성 1~100사이 if(input >= 1 && input <= 10){ //이 사이의 난수가 발생하면 아이템 생성됨 item1 = 1; //아이템 생성되라 if(itemtake == 0) { ix = 10; //아이템의 속도값 iy = 10; itemcreatex = tmp.x1; //아이템의 생성좌표를 부순 블럭위치로 정함 itemcreatey = tmp.y1; mitem = new Item(mContext, itemcreatex, itemcreatey, blockheight, blockheight); //부순 블럭위치에 아이템 생성 itemtake = 1; //아이템이 생성됐으면 사라지기 전까지 다시 생성되지 않게함 } } else if(input >= 11 && input <= 20){ item2 = 1; if(item2take == 0) { i2x = 10; i2y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem2 = new Item2(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item2take = 1; } } else if(input >= 21 && input <= 30){ item3 = 1; if(item3take == 0) { i3x = 10; i3y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem3 = new Item3(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item3take = 1; } } else if(input >= 31 && input <= 40){ item4 = 1; if(item4take == 0) { i4x = 10; i4y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem4 = new Item4(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item4take = 1; } } } else {//공이 벽돌의 좌우에 닿지않았을때 (위아래부분에 닿을때) int streamId = sound.play(soundId, 0.5F, 0.5F, 1, 0, 1.0F); sy = -sy; input = random.nextInt(99) + 1; if(input >= 1 && input <= 10){ item1 = 1; if(itemtake == 0) { ix = 10; iy = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem = new Item(mContext, itemcreatex, itemcreatey, blockheight, blockheight); itemtake = 1; } } else if(input >= 11 && input <= 20){ item2 = 1; if(item2take == 0) { i2x = 10; i2y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem2 = new Item2(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item2take = 1; } } else if(input >= 21 && input <= 30){ item3 = 1; if(item3take == 0) { i3x = 10; i3y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem3 = new Item3(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item3take = 1; } } else if(input >= 31 && input <= 40){ item4 = 1; if(item4take == 0) { i4x = 10; i4y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem4 = new Item4(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item4take = 1; } } } mBrick.remove(tmp); //벽돌제거 score = score + 1; if(mBrick.isEmpty() == true) { //벽돌이 다 사라졌으면 int width = 1; MyGlobals.getInstance().setWidth(width); //전역변수에 숫자를 넣음 if(mContext instanceof Activity){ Intent intent = new Intent(mContext, StageActivity.class); Activity activity = (Activity)mContext; activity.startActivity(intent); } super.setVisibility(View.INVISIBLE); //바닥에 공이닿았을때 종료 } //벽돌부실때마다 점점 공의 상하속도가 빨라지게 /*if(sy < 0) sy = sy - 1; else sy = sy + 1;*/ break; } else if(thru == 1) {//벽돌을 뚫는아이템 item3를 먹었을때 if (mBall.x + bw < tmp.x1 /*- 10*/ || mBall.x - bw > tmp.x2 || mBall.y + bw < tmp.y1 || mBall.y - bw > tmp.y2) { //충돌안할때 continue; } if ((mBall.x + bw >= tmp.x1 && mBall.x + bw <= tmp.x1 + 10) || (mBall.x - bw <= tmp.x2 && mBall.x - bw >= tmp.x2 - 10)) { int streamId3 = sound.play(soundId, 0.5F, 0.5F, 1, 0, 0.1F); input = random.nextInt(99) + 1; if(input >= 1 && input <= 10){ item1 = 1; if(itemtake == 0) { ix = 10; iy = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem = new Item(mContext, itemcreatex, itemcreatey, blockheight, blockheight); itemtake = 1; } } else if(input >= 11 && input <= 20){ item2 = 1; if(item2take == 0) { i2x = 10; i2y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem2 = new Item2(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item2take = 1; } } else if(input >= 21 && input <= 30){ item3 = 1; if(item3take == 0) { i3x = 10; i3y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem3 = new Item3(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item3take = 1; } } else if(input >= 31 && input <= 50){ item4 = 1; if(item4take == 0) { i4x = 10; i4y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem4 = new Item4(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item4take = 1; } } } else { int streamId4 = sound.play(soundId, 0.5F, 0.5F, 1, 0, 0.1F); input = random.nextInt(99) + 1; if(input >= 1 && input <= 10){ item1 = 1; if(itemtake == 0) { ix = 10; iy = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem = new Item(mContext, itemcreatex, itemcreatey, blockheight, blockheight); itemtake = 1; } } else if(input >= 11 && input <= 20){ item2 = 1; if(item2take == 0) { i2x = 10; i2y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem2 = new Item2(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item2take = 1; } } else if(input >= 21 && input <= 30){ item3 = 1; if(item3take == 0) { i3x = 10; i3y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem3 = new Item3(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item3take = 1; } } else if(input >= 31 && input <= 50){ item4 = 1; if(item4take == 0) { i4x = 10; i4y = 10; itemcreatex = tmp.x1; itemcreatey = tmp.y1; mitem4 = new Item4(mContext, itemcreatex, itemcreatey, blockheight, blockheight); item4take = 1; } } } mBrick.remove(tmp); score = score + 1; if(mBrick.isEmpty() == true) { int width = 1; MyGlobals.getInstance().setWidth(width); if(mContext instanceof Activity){ Intent intent = new Intent(mContext, StageActivity.class); Activity activity = (Activity)mContext; activity.startActivity(intent); } super.setVisibility(View.INVISIBLE); //바닥에 공이닿았을때 종료 } /*if (sy < 0) sy = sy - 1; else sy = sy + 1;*/ break; } } //그림그리기 canvas.drawBitmap(mPaddle.imgPaddle, mPaddle.x - mPaddle.imgPaddle.getWidth()/2, mPaddle.y, null); //패달 canvas.drawBitmap(mBall.imgBall, mBall.x - bw, mBall.y - bh, null); //공 for (Brick tmp : mBrick) //벽돌 canvas.drawBitmap(tmp.imgBrick, tmp.x1, tmp.y1, null); if(item1 == 1) //아이템1 패달눌리기 canvas.drawBitmap(mitem.imgexpand, mitem.x - ix, mitem.y - iy, null); if(item2 == 1) //아이템2 패달줄이기 canvas.drawBitmap(mitem2.imgshrink, mitem2.x - i2x, mitem2.y - i2y, null); if(item3 == 1) //아이템3 벽돌뚫기 canvas.drawBitmap(mitem3.imgthru, mitem3.x - i3x, mitem3.y - i3y, null); if(item4 == 1) //아이템4 공속도 빨라지게하기 canvas.drawBitmap(mitem4.imgfast, mitem4.x - i4x, mitem4.y - i4y, null); } Handler mHandler = new Handler(){//일정한간격으로 반복해서 실행시키는용도 public void handleMessage(Message msg){ invalidate(); //시간이되면 View를 다시그려주는메서드 mHandler.sendEmptyMessageDelayed(0, 1); //1밀리세컨드마다 새로고침 } }; @Override public boolean onTouchEvent(MotionEvent event){ if(event.getAction() == MotionEvent.ACTION_MOVE) //터치를하고 움직였을때 mPaddle.x = (int)event.getX(); //움직인 x좌표값을 패달의 x좌표에 넣음 return true; } }
피드 구독하기:
글 (Atom)
-
3학년 과제 putty에서 만듬 스타크래프트유즈맵 경매디펜스의 경매시스템을 따라 만들어봄 처음 들어온 클라이언트에게 게임실행문구 보여줌 2명이상 안될때 실행해도 안됨 2명이상 되면 게임시작 서로 채팅가능 숫자는 서로에게 전달이안됨 소지금...
-
3학년 기말고사 영상처리 프로그래밍 python opencv 캠에서 손을 찾아서 손가락 수에 따라 작업해주기 코드설명, 문제점등 보고서 #기말 프로젝트 12월 7일 import cv2 import numpy ...
-
3학년 기말고사 간단한 벽돌깨기게임을 만들어봄 문제점이 많이있음 벽돌깨기게임 import할 파일 압축풀어서 임포트하면됨 문제점과 코드설명? 주요코드 MyGameView.java 다펴지는데 로딩이걸리는듯 package c...