Klar, kannst dir gerne mal anschauen.
Im Buch wird auf vieles kurz eingegangen, aber nicht alles ausführlich erklärt.
Es handelt sich um das Buch "Spieleprogrammierung mit Android Studio".
Hier ist so dass was ich bisher so abgetippt und teilweise einwenig zum testen umgeändert habe.
Ich habe jetzt nicht alles durchgeschaut, es kann auch sein, dass noch was "ausgeklammertes" dabei ist.
Denke aber nicht.
MainActivity.java
Java
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends BaseGameActivity implements View.OnClickListener, Bubble.OnBurstListener {
private int points,countdown,rounds;
private ViewGroup container;
private TextView btnStart, btnHighscore,btnQuit, btnPlayAgain;
private int BUBBLE_ID = 666666;
private ImageView bubbleView;
private Random rnd = new Random();
private Handler handler = new Handler();
private int scoreHighscore;
private static final int BUBBLE_MAX = 0;
private static float V_MAX = 0.5f;
private static final float SIZE_MAX = 128f;
private Drawable bubbleDrawable ;
private Set <Bubble> bubbles = new HashSet<Bubble>();
private ScheduledExecutorService executor;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = findViewById(R.id.container);
bubbleDrawable = getResources().getDrawable(R.drawable.bubbledraw);
}
private void showMenuFragment(){
container = findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.menu_layout, null));
btnStart = findViewById(R.id.btnStart);
btnStart.setOnClickListener(this);
btnHighscore = findViewById(R.id.btnHighscore);
btnHighscore.setOnClickListener(this);
btnQuit = findViewById(R.id.btnQuit);
btnQuit.setOnClickListener(this);
}
private void newGame(){
container = findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.game_layout,null));
bubbles.add(new Bubble((FrameLayout)container, scale(V_MAX), scale(SIZE_MAX), rnd, bubbleDrawable, this));
executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(moveRunnable, 0 , 50, TimeUnit.MILLISECONDS);
points = 0;
rounds = 1;
initRound();
}
private Runnable moveRunnable = new Runnable() {
@Override
public void run() {
for (final Bubble b : bubbles){
runOnUiThread(new Runnable() {
@Override
public void run() {
b.move();
}
});
}
}
};
private void showHighscoreFragment(){
container = findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.highscore_layout, null));
}
private void showGameOverFragment(){
container = findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.gameover_layout, null));
btnPlayAgain = findViewById(R.id.btnPlayAgain);
btnPlayAgain.setOnClickListener(this);
btnQuit = findViewById(R.id.btnQuit);
btnQuit.setOnClickListener(this);
}
private void initRound() {
countdown = 10;
ViewGroup container = findViewById(R.id.playField);
container.removeAllViews();
update();
handler.postDelayed(runnable, 1000-rounds*50);
}
private void fillTextView(int id, String text){
TextView tv = findViewById(id);
tv.setText(text);
}
private void update() {
loadHighscore();
fillTextView(R.id.tvHighscore, Integer.toString(scoreHighscore));
fillTextView(R.id.tvRounds, Integer.toString(rounds));
fillTextView(R.id.tvPoints, Integer.toString(points));
fillTextView(R.id.tvCountdown, Integer.toString(countdown * 1000));
}
private void loadHighscore() {
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
scoreHighscore = sharedPreferences.getInt("highscore", 0);
}
private void saveScoreHighscore(int points) {
scoreHighscore = points;
SharedPreferences sp = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor e = sp.edit();
e.putInt("highscore", scoreHighscore);
e.commit();
}
private void countdown(){
countdown--;
update();
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
countdown();
}
};
@Override
protected void onPause(){
super.onPause();
executor.shutdown();
bubbles.clear();
handler.removeCallbacks(runnable);
}
@Override
protected void onResume(){
super.onResume();
showMenuFragment();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnStart) {
newGame();
} else if (v.getId() == R.id.btnHighscore) {
showHighscoreFragment();
} else if (v.getId() == R.id.btnPlayAgain) {
newGame();
} else if (v.getId() == BUBBLE_ID){
handler.removeCallbacks(runnable);
Toast.makeText(this, R.string.toastText, Toast.LENGTH_SHORT).show();
points += countdown * 1000;
rounds++;
initRound();
} else if (v.getId() == R.id.btnQuit) {
System.exit(0);
}
}
@Override
public void onBurst(Bubble b) {
bubbles.remove(b);
handler.removeCallbacks(runnable);
Toast.makeText(this, R.string.toastText, Toast.LENGTH_SHORT).show();
points += countdown * 1000;
rounds++;
V_MAX++;
initRound();
for (int i = 0; i <= BUBBLE_MAX; i++){
bubbles.remove(b);
bubbles.add(new Bubble((FrameLayout)container, scale(V_MAX), scale(SIZE_MAX), rnd, bubbleDrawable, this));
}
}
}
Alles anzeigen
BaseGameActivity.java
Code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class BaseGameActivity extends Activity {
private float density;
protected float scale(float v){
return density * v;
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
density = getResources().getDisplayMetrics().density;
}
}
Alles anzeigen
Bubble.java
Java
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import java.util.Random;
public class Bubble implements View.OnClickListener {
private OnBurstListener burstListener;
interface OnBurstListener {
void onBurst(Bubble b);
}
private float x,y,vx,vy,size;
private int lifetime;
private ImageView view;
private int LIFETIME = 1000;
public Bubble (FrameLayout container, float vMax, float sizeMax, Random rnd, Drawable drawable, OnBurstListener listener){
burstListener = listener;
lifetime = LIFETIME;
size = (0.5f + rnd.nextFloat() / 2) * sizeMax;
x = rnd.nextFloat() * (container.getWidth()-size);
y = rnd.nextFloat() * (container.getHeight()-size);
vx = rnd.nextFloat() * vMax * (rnd.nextBoolean()?1:-1);
vy = rnd.nextFloat() * vMax * (rnd.nextBoolean()?1:-1);
view = new ImageView(container.getContext());
view.setImageDrawable(drawable);
view.setOnClickListener(this);
container.addView(view);
move();
}
public void move() {
x += vx;
y += vy;
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
params.width = Math.round(size);
params.height = Math.round(size);
params.leftMargin = Math.round(x);
params.topMargin = Math.round(y);
params.gravity = Gravity.LEFT + Gravity.TOP;
view.setLayoutParams(params);
lifetime--;
if(lifetime <= 0) burst();
}
private void burst(){
ViewGroup parent = (ViewGroup) view.getParent();
burstListener.onBurst(this);
}
@Override
public void onClick(View v) {
burst();
}
}
Alles anzeigen