본문 바로가기
학교 강의/모겜프

모겜프 10주차

by hoshi03 2023. 11. 9.

비눗방울

 

Bubble.java

package com.example.mgp_lecture;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.Random;

public class Bubble {
    public int w, h;
    public int sx, sy;

    public Bitmap bubble;
    public int x, y, bw;

    public Bubble(Context context, int sw, int sh, int px, int py) {
        w = sw;
        h = sh;
        x = px;
        y = py;

        Random rnd = new Random();
        bw = rnd.nextInt(101) + 50;

        bubble = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble);
        bubble = Bitmap.createScaledBitmap(bubble, bw * 2, bw * 2, true);

        sx = rnd.nextInt(5) + 1;
        sy= rnd.nextInt(5) + 1;

        sx= rnd.nextInt(2) == 0 ? sx: -sx;
        sy= rnd.nextInt(2) == 0 ? sy: -sy;
    }

    public void update() {
        x += sx;
        y += sy;

        if (x < bw || x > w -bw)
        {
            sx = -sx;
            x += sx;
        }

        if (y < bw || y > h -bw) {
            sy = -sy;
            y += sy;
        }
    }
}

 

 

GameView.java

 

package com.example.mgp_lecture;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.jar.Attributes;

public class GameView extends View {

//    Handler mHandler;
    private  Context context;

    private Bitmap imgBack;
    private int w, h;

    private ArrayList<Bubble> mBubble = new ArrayList<Bubble>();

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        this.w = w;
        this.h = h;

        imgBack = BitmapFactory.decodeResource(getResources(), R.drawable.sky);
        imgBack = Bitmap.createScaledBitmap(imgBack,w,h,true);

        mHandler.sendEmptyMessageDelayed(0, 10);
    }

    private void moveBubble(){
        for (Bubble tmp : mBubble){
            tmp.update();
        }
    }

    @Override
    protected void onDraw( Canvas canvas) {
        canvas.drawBitmap(imgBack, 0, 0, null);

        for(Bubble tmp : mBubble){
            canvas.drawBitmap(tmp.bubble, tmp.x - tmp.bw, tmp.y - tmp.bw, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            int x = (int) event.getX();
            int y = (int) event.getY();

            mBubble.add(new Bubble(context, w, h , x ,y));
        }
        return true;
    }

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage( Message msg) {
            moveBubble();
            invalidate();
            mHandler.sendEmptyMessageDelayed(0,10);
        }
    };


}

 

MainACtivity

 

package com.example.mgp_lecture;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setNavigationBarColor(Color.BLACK);
        setTitle("2019026380 이찬호");

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}

'학교 강의 > 모겜프' 카테고리의 다른 글

모겜프 12주차  (0) 2023.11.23
11주차  (1) 2023.11.16
모겜프 시험대비  (1) 2023.10.23
모겜프 7주차  (1) 2023.10.19
시험대비  (0) 2023.10.12