Falls irgendjemand lust hat das durchzuschauen oder wenn jemand das Tutorial selber gemacht hat und mir die Dateien geben kann gerne melden
GameActivity Quellcode:1package com.panjutorials.lazypudding;
2
3import android.app.Activity;
4import android.os.Bundle;
5
6public class GameActivity extends Activity {
7 /** Called when the activity is first created. */
8 @Override
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(new GameView(this));
12 }
13}
GameView Quellcode:1package com.panjutorials.lazypudding;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import android.annotation.SuppressLint;
8import android.content.Context;
9import android.graphics.Bitmap;
10import android.graphics.BitmapFactory;
11import android.graphics.Canvas;
12import android.graphics.Color;
13import android.view.SurfaceHolder;
14import android.view.SurfaceView;
15
16public class GameView extends SurfaceView {
17 private List<Sprite> spriteList = new ArrayList<Sprite>();
18 private List<Integer> spriteListNum = new ArrayList<Integer>();
19 private SurfaceHolder surfaceHolder;
20 private Bitmap bmp;
21 private GameLoopThread theGameLoopThread;
22 private boolean createSprites=true;
23
24 public GameView(Context context) {
25 super(context);
26 theGameLoopThread = new GameLoopThread(this);
27 surfaceHolder = getHolder();
28 surfaceHolder.addCallback(new SurfaceHolder.Callback() {
29
30 public void surfaceDestroyed(SurfaceHolder holder) {
31 boolean retry = true;
32 theGameLoopThread.setRunning(false);
33 while (retry) {
34 try {
35 theGameLoopThread.join();
36 retry = false;
37 } catch (InterruptedException e) {
38
39 }
40 }
41
42 }
43
44 public void surfaceCreated(SurfaceHolder holder) {
45 theGameLoopThread.setRunning(true);
46 theGameLoopThread.start();
47 }
48
49 public void surfaceChanged(SurfaceHolder holder, int format,
50 int width, int height) {
51 // TODO Auto-generated method stub
52
53 }
54 });
55 }
56
57 @SuppressLint("WrongCall") @Override
58 protected void onDraw(Canvas canvas) {
59 canvas.drawColor(Color.DKGRAY);
60 if(createSprites==true){
61 initialSprites();
62 }
63 for (Sprite sprite : spriteList) {
64 sprite.onDraw(canvas);
65 }
66 }
67
68 private void createSprite(int index) {
69 Bitmap bmp = null;
70 switch (index) {
71 case 0:
72 bmp = BitmapFactory.decodeResource(getResources(),
73 R.drawable.alienspriteblue);
74 break;
75 case 1:
76 bmp = BitmapFactory.decodeResource(getResources(),
77 R.drawable.alienspritered);
78 break;
79 case 2:
80 bmp = BitmapFactory.decodeResource(getResources(),
81 R.drawable.alienspritegreen);
82 break;
83 case 3:
84 bmp = BitmapFactory.decodeResource(getResources(),
85 R.drawable.alienspriteyellow);
86 break;
87 }
88 Sprite sprite = new Sprite(this, bmp);
89 spriteList.add(sprite);
90 spriteListNum.add(index);
91 }
92 private void initialSprites() {
93 for (int i = 0; i < 4; i++) {
94 for (int j = 0; j < 3; j++)
95 createSprite(i);
96 }
97 createSprites=false;
98 }
99 private void rndCreateSprite() {
100 Random rnd = new Random(System.currentTimeMillis());
101 int i = rnd.nextInt(4);
102 createSprite(i);
103 }
104}
GameLoopThread Quellcode:1package com.panjutorials.lazypudding;
2
3import com.panjutorials.lazypudding.GameView;
4
5import android.annotation.SuppressLint;
6import android.graphics.Canvas;
7
8public class GameLoopThread extends Thread {
9 static final long FPS = 20;
10 private GameView theView;
11 private boolean isRunning = false;
12
13 public GameLoopThread(GameView theView) {
14 this.theView = theView;
15 }
16
17 public void setRunning(boolean run) {
18 isRunning = run;
19 }
20// @SuppressLint("WrongCall") hinzugefügt am 20.02.14
21 @SuppressLint("WrongCall") @Override
22
23 public void run() {
24 long TPS = 1000 / FPS;
25 long startTime, sleepTime;
26 while (isRunning) {
27 Canvas theCanvas = null;
28 startTime = System.currentTimeMillis();
29 try {
30 theCanvas = theView.getHolder().lockCanvas();
31 synchronized (theView.getHolder()) {
32 theView.draw(theCanvas);
33 }
34 } finally {
35 if (theCanvas != null) {
36 theView.getHolder().unlockCanvasAndPost(theCanvas);
37 }
38 }
39 sleepTime = TPS - (System.currentTimeMillis() - startTime);
40 try {
41 if (sleepTime > 0)
42 sleep(sleepTime);
43 else
44 sleep(10); }
45 catch (Exception e) { }
46 }
47 }
48
49}
Sprite Quellcode:1package com.panjutorials.lazypudding;
2
3import java.util.Random;
4
5import android.annotation.SuppressLint;
6import android.graphics.Bitmap;
7import android.graphics.Canvas;
8import android.graphics.Rect;
9
10@SuppressLint("DrawAllocation")
11 public class Sprite {
12 static final private int BMP_ROWS = 4;
13 static final private int BMP_COLUMNS = 4;
14 private int[] DIRECTION_TO_SPRITE_SHEET = { 2, 0, 3, 1 };
15 private int x;
16 private int y;
17 private int xSpeed;
18 private int ySpeed;
19 private int width;
20 private int height;
21 private Bitmap bmp;
22 private GameView theGameView;
23 private int currentFrame=0;
24
25
26 public Sprite(GameView theGameView, Bitmap bmp){
27 this.theGameView = theGameView;
28 this.bmp = bmp;
29 this.width = bmp.getWidth() / BMP_COLUMNS;
30 this.height = bmp.getHeight() / BMP_ROWS;
31 Random rnd = new Random();
32 x=rnd.nextInt(theGameView.getWidth()- width);
33 y=rnd.nextInt(theGameView.getHeight() - height);
34 ySpeed = rnd.nextInt(10) - 4;
35 xSpeed = rnd.nextInt(10) - 4;
36 }
37
38 private void bounceOff() {
39 if (x > theGameView.getWidth() - width - xSpeed || x + xSpeed < 0) {
40 xSpeed = -xSpeed;
41 }
42 x = x + xSpeed;
43 if (y > theGameView.getHeight() - height - ySpeed || y + ySpeed < 0) {
44 ySpeed = -ySpeed;
45 }
46 y = y + ySpeed;
47 currentFrame = ++currentFrame % BMP_COLUMNS;
48 }
49 @SuppressLint("DrawAllocation")
50 public void onDraw(Canvas canvas){
51 bounceOff();
52 int sourceX = currentFrame * width;
53 int sourceY = getAnimationRow() * height;
54 Rect source = new Rect(sourceX, sourceY, sourceX + width, sourceY + height);
55 Rect destine = new Rect(x, y, x + width, y + height);
56 canvas.drawBitmap(bmp, source, destine, null);
57
58 }
59
60 private int getAnimationRow() {
61 double directionDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2)+2);
62 int spriteDir = (int) Math.round(directionDouble) % BMP_ROWS;
63 return DIRECTION_TO_SPRITE_SHEET[spriteDir];
64 }
65 }
— geändert am 28.02.2014, 01:44:21