Demo
구성
- activity
- view
- gif resource
- [MainActivity.java] 시작화면
package com.example.recyclerview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startGame(View view) {
Intent intent = new Intent(this, StartGame.class);
startActivity(intent);
finish();
}
}
- [GameView.java]
package com.example.recyclerview;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import java.util.Random;
public class GameView extends View {
//this is our custom view class
private Handler handler;//handler is required to schedule a runnable after some delay
private Runnable runnable;
private final int UPDATE_MILLIS = 30;
private Bitmap background, toptube, bottomtube;
private Display display;
private Point point;
private int dWidth, dHeight;
private Rect rect;
//let's create a bitmap array for the bug
private Bitmap[] bugs;
//we need an integer variable to keep track of bug image/frame
private int bugFrame = 0;
private int velocity = 0, gravity = 3;//lets play around with these values
//we need to keep track of bug position
private int bugX, bugY;
private boolean gameState = false;
private int gap = 400; // gap between top tube and bottom tube
private int minTubeOffset, maxTubeOffset;
private int numberOfTubes = 4;
private int distanceBetweenTubes;
private int[] tubeX = new int[numberOfTubes];
private int[] topTubeY = new int[numberOfTubes];
private Random random;
private int tubeVelocity = 8;
public GameView(Context context) {
super(context);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
invalidate();// this will call onDraw()
}
};
background = BitmapFactory.decodeResource(getResources(),R.drawable.guide_jungle_vines);
toptube = BitmapFactory.decodeResource(getResources(),R.drawable.tobtube);
bottomtube = BitmapFactory.decodeResource(getResources(),R.drawable.tobtube);
display = ((Activity)getContext()).getWindowManager().getDefaultDisplay();
point = new Point();
display.getSize(point);
dWidth = point.x;
dHeight = point.y;
rect = new Rect(0,0,dWidth,dHeight);
bugs = new Bitmap[11];
bugs[0] = BitmapFactory.decodeResource(getResources(),R.drawable.bug1);
bugs[1] = BitmapFactory.decodeResource(getResources(),R.drawable.bug2);
bugs[2] = BitmapFactory.decodeResource(getResources(),R.drawable.bug3);
bugs[3] = BitmapFactory.decodeResource(getResources(),R.drawable.bug4);
bugs[4] = BitmapFactory.decodeResource(getResources(),R.drawable.bug5);
bugs[5] = BitmapFactory.decodeResource(getResources(),R.drawable.bug6);
bugs[6] = BitmapFactory.decodeResource(getResources(),R.drawable.bug7);
bugs[7] = BitmapFactory.decodeResource(getResources(),R.drawable.bug8);
bugs[8] = BitmapFactory.decodeResource(getResources(),R.drawable.bug9);
bugs[9] = BitmapFactory.decodeResource(getResources(),R.drawable.bug10);
bugs[10] = BitmapFactory.decodeResource(getResources(),R.drawable.bug11);
bugX = dWidth / 2 - bugs[0].getWidth() / 2;//Initially bug will be on center
bugY = dHeight/2 - bugs[0].getHeight() / 2;//Initially bug will be on center
distanceBetweenTubes = dWidth * 3 / 4;
minTubeOffset = gap / 2;
maxTubeOffset = dHeight - minTubeOffset - gap;
random = new Random();
for(int i=0;i<numberOfTubes;i++) {
tubeX[i] = dWidth + i*distanceBetweenTubes;
topTubeY[i] = minTubeOffset + random.nextInt(maxTubeOffset - minTubeOffset + 1);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//we'll draw our view inside onDraw()
//draw the background on canvas
canvas.drawBitmap(background,null,rect,null);//fixed
if(bugFrame == 10) {
bugFrame = 0;
} else {
bugFrame = bugFrame + 1;
}
if(gameState) {
//the bug should be on the screen
if(bugY < dHeight - bugs[0].getHeight() || velocity < 0){ // this way bug does not go beyond the bottom edge of the screen
velocity += gravity;// as the bug falls, it gets faster and faster as the velocity value increments by gravity each time
bugY += velocity;
}
for(int i=0;i<numberOfTubes;i++){
tubeX[i] -= tubeVelocity;
//부딪혓을대
if(tubeX[i] < -toptube.getWidth()) {
tubeX[i] += numberOfTubes * distanceBetweenTubes;
topTubeY[i] = minTubeOffset + random.nextInt(maxTubeOffset - minTubeOffset + 1);
}
canvas.drawBitmap(toptube, tubeX[i], topTubeY[i] - toptube.getHeight(), null);
canvas.drawBitmap(bottomtube, tubeX[i], topTubeY[i] + gap, null);
}
}
//we want the bug to be displayed at the centre of the screen
//all bugs have same dimension
canvas.drawBitmap(bugs[bugFrame], bugX, bugY, null);
handler.postDelayed(runnable,UPDATE_MILLIS);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN) { //that is tap is detected on screen
//here we want the bug to move upwards bu some unit
velocity = -30;
gameState = true;
}
return true;// by returning true indicates that we've done with touch event and no further action is required by android
}
}
- [StartGame.java]
package com.example.recyclerview;
import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
public class StartGame extends Activity {
private GameView gameView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
setContentView(gameView);
}
}
- [activity_main.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/guide_jungle_vines">
<ImageButton
android:layout_centerInParent="true"
android:id="@+id/ib_start"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/bug1"
android:onClick="startGame"/>
</RelativeLayout>
'System Programming > Android' 카테고리의 다른 글
[Android/Firebase] Firebase 연동 (0) | 2021.06.16 |
---|---|
[Android/Java] 지나가는 비행기 (0) | 2021.06.14 |
[Android/Java] video view 가로 모드 (0) | 2021.06.14 |
[Android/Java] 날씨 api (0) | 2021.06.14 |
[Android/Java] 타이머 (0) | 2021.06.14 |