Troche poprawek

This commit is contained in:
Mariusz Pacek committed 2012-12-05 18:32:05 +01:00
1 parent 266b04bb45
commit 8ffa981d90
18 files changed
+123 -92

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
+58 -48
View File
@@ -16,6 +16,7 @@ import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
@@ -31,9 +32,8 @@ import com.android.game.pacman.model.SoundStuff;
import com.android.game.pacman.model.Vect;
import com.android.game.pacman.utils.GameEnum;
public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,SensorEventListener {
public class GameLogic extends SurfaceView implements SurfaceHolder.Callback,
SensorEventListener {
private PacMan pacman;
@@ -45,7 +45,6 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
private GameLoop loop;
private Board boardGame;
private SoundStuff ss;
private Bitmap background;
private CountDownTimer task;
private SensorManager sensorManager;
private Vibrator v;
@@ -53,38 +52,47 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
private int points = 0;
private int eatInRow = 0; // mnoznik do zjedzonych duszkow
private boolean accOn;
private final static Paint paint=new Paint();
public final static int BOARD_TILE_SIZE=17;
private boolean start = false;
private final static Paint paint = new Paint();
public static int BOARD_TILE_SIZE = 17;
public final static int BOARD_HEIGHT = 31;
public final static int BOARD_WIDTH = 28;
public final static int GHOST_EAT_POINT = 200;
public final static int FOODUP_POINT = 150;
public final static int FOOD_POINT = 50;
public static int WIDTH;
public static int HEIGHT;
public GameLogic(Context context) {
public GameLogic(Context context, int w, int h) {
super(context);
setFocusable(true);
getHolder().addCallback(this);
WIDTH = w;
HEIGHT = h;
BOARD_TILE_SIZE = (int) (WIDTH / BOARD_WIDTH);
loop = new GameLoop(getHolder(), this);
ss = new SoundStuff(context);
paint.setStyle(Paint.Style.FILL);
v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
//przez 10 sek po 5 zaczna mrugac duchy
// przez 10 sek po 5 zaczna mrugac duchy
task = new CountDownTimer(10000, 1000) {
private boolean first = false;
private int ID;
int times=0;
int times = 0;
@Override
public void onTick(long millisUntilFinished) {
if(!first){
ID=SoundStuff.sp.play(SoundStuff.ghostSiren, 1, 1, 1, -1, 1);
first=true;
if (!first) {
ID = SoundStuff.sp.play(SoundStuff.ghostSiren, 1, 1, 1, -1,
1);
first = true;
}
if(times==5)
if (times == 5)
for (Ghost ghost : ghosts) {
ghost.setBlinking(true);
}
@@ -98,28 +106,28 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
ghost.setEatable(false);
ghost.setBlinking(false);
}
SoundStuff.sp.stop(ID);
eatInRow = 0;
first = false;
times=0;
SoundStuff.sp.stop(ID);
eatInRow = 0;
first = false;
times = 0;
}
};
sensorManager=(SensorManager)context.getSystemService(context.SENSOR_SERVICE);
sensorManager = (SensorManager) context
.getSystemService(context.SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
}
public void render(Canvas canvas) {
// tlo
paint.setColor(Color.BLUE);
canvas.drawRect(0, 0, 480, 900, paint);
canvas.drawRect(0, 0, WIDTH, HEIGHT, paint);
// napis z punktami
paint.setColor(Color.WHITE);
paint.setTextSize(30);
@@ -135,7 +143,7 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
for (Ghost ghost : ghosts) {
ghost.draw(canvas);
}
}
public void prepareLevel() {
@@ -186,7 +194,8 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
ghost.setIsTouched(true);
eatInRow++;
points += GHOST_EAT_POINT * eatInRow;
SoundStuff.sp.play(SoundStuff.pacmanEatGhost, 1, 1,1, 0, 1f);
SoundStuff.sp.play(SoundStuff.pacmanEatGhost, 1, 1, 1, 0,
1f);
v.vibrate(100);
} else
@@ -202,7 +211,7 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
if (food.get(i).kind == GameEnum.FOODUP) {
points += FOODUP_POINT;
v.vibrate(100);
task.start();
for (Ghost ghost : ghosts) {
ghost.setEatable(true);
@@ -222,7 +231,7 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
public boolean onTouchEvent(android.view.MotionEvent event) {
pacman.dir(event);
return true;
}
@@ -235,7 +244,7 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
@Override
public void surfaceCreated(SurfaceHolder holder) {
//BOARD_TILE_SIZE=getWidth()/28;
// BOARD_TILE_SIZE=getWidth()/28;
boardGame = new Board(BOARD_WIDTH, BOARD_HEIGHT, BOARD_TILE_SIZE,
getResources());
wall = boardGame.getWall();
@@ -243,23 +252,24 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
food = boardGame.getbGame();
pacman = new PacMan(getResources(), new Vect((BOARD_TILE_SIZE
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
boardGame.getBlock(), BOARD_TILE_SIZE, 100f);
boardGame.getBlock(), 100f);
ghosts = new LinkedList<Ghost>();
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
new Vect(11 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
new Vect(13 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
new Vect(15 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
new Vect(17 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
11 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
13 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
15 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
17 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
loop.setRunning(true);
loop.start();
int id=-1;
start = true;
int id = -1;
do {
id=SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
} while(id==0 );
id = SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
} while (id == 0);
}
@Override
@@ -278,15 +288,15 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback ,Se
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER && start) {
pacman.dirAcc(event);
}
}
public boolean isAccOn() {
+13 -5
View File
@@ -27,15 +27,23 @@ public class GameLoop extends Thread {
double delay;
while (running) {
canvas = null;
last = System.currentTimeMillis();
delay = (last - lastFpsTime);
lastFpsTime = last;
canvas = this.surfaceHolder.lockCanvas();
gamePanel.render(canvas);
surfaceHolder.unlockCanvasAndPost(canvas);
gamePanel.update(delay / 1000);
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
gamePanel.render(canvas);
gamePanel.update(delay / 1000);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
@@ -4,6 +4,7 @@ import com.adroid.game.pacman.R;
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
@@ -16,7 +17,11 @@ public class MainActivity extends Activity {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameLogic(this));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
setContentView(new GameLogic(this,screenWidth,screenHeight));
}
+7 -2
View File
@@ -23,8 +23,13 @@ public class Block extends SolidObject {
public void draw(Canvas canvas) {
if (kind == GameEnum.PATH) {
boundingRect.set((float) x-size/2, (float) y-size/2, (float) x + size/2+size, (float) y
+ size/2+size);
if (selected) {
paint.setColor(Color.RED);
}else
paint.setColor(Color.BLACK);
boundingRect.set((float) x - size / 2, (float) y - size / 2,
(float) x + size / 2 + size, (float) y + size / 2 + size);
canvas.drawRect(boundingRect, paint);
}
@@ -5,13 +5,14 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class FoodUp extends SolidObject {
private static Bitmap bitmap;
public FoodUp(int x, int y, Resources res) {
super(x * 17, y * 17);
super(x * GameLogic.BOARD_TILE_SIZE, y * GameLogic.BOARD_TILE_SIZE);
kind = GameEnum.FOODUP;
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(res,
@@ -10,35 +10,35 @@ import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class GameObject {
protected Vect position;
protected Bitmap bitmap;
public RectF boundingRect = new RectF();
public RectF destRect = new RectF();
protected Vect position;
protected Bitmap bitmap;
protected boolean isTouched = false;
protected int dirToChange;
protected double speed;
protected boolean rotate = false;
protected double angle = 0;
protected Vect direction;
protected Vect target = new Vect(0, 0);
protected boolean stop;
protected int size;
private Block[][] board;
protected int nextDir;
protected Rect sourceRect; // the rectangle to be drawn from the animation
protected int spriteWidth; // the width of the sprite to calculate the cut
private boolean targetReached = true;
protected int spriteHeight;
protected Resources res;
private Block[][] board;
private boolean targetReached = true;
private Vect newdir = new Vect(0, 0);
public void update() {
}
public GameObject(int size, double speed, Block[][] board) {
public GameObject(double speed, Block[][] board) {
this.speed = speed;
this.size = size;
this.board = board;
}
+23 -22
View File
@@ -9,6 +9,7 @@ import android.graphics.Canvas;
import android.graphics.Rect;
import com.adroid.game.pacman.R;
import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class Ghost extends GameObject {
@@ -26,7 +27,6 @@ public class Ghost extends GameObject {
private boolean onlyEyes = false;
private boolean blinking = false;
private double frameTicker;
private double framePeriod = 0.5f;
private boolean swap;
@@ -34,10 +34,8 @@ public class Ghost extends GameObject {
private double time; // do mrugania;
private int currentFrame;
public Ghost(int size, double speed, Block[][] board, Resources res,
Vect initPosition) {
super(size, speed, board);
public Ghost(double speed, Block[][] board, Resources res, Vect initPosition) {
super(speed, board);
bitmap = BitmapFactory.decodeResource(res,
com.adroid.game.pacman.R.drawable.ghost);
@@ -60,7 +58,6 @@ public class Ghost extends GameObject {
position = initPosition;
nextDir = GameEnum.STOP;
direction = new Vect(0, 0);
this.size = size;
stop = false;
}
@@ -84,21 +81,25 @@ public class Ghost extends GameObject {
public void draw(Canvas canvas) {
destRect.set((float) position.x-size/2, (float) position.y-size/2,
(float) position.x + spriteWidth/2+spriteWidth, (float) position.y
+ spriteHeight/2+spriteHeight);
destRect.set((float) position.x - GameLogic.BOARD_TILE_SIZE / 2,
(float) position.y - GameLogic.BOARD_TILE_SIZE / 2,
(float) position.x + spriteWidth / 2 + spriteWidth,
(float) position.y + spriteHeight / 2 + spriteHeight);
boundingRect.set((float) position.x, (float) position.y,
(float) position.x + spriteWidth, (float) position.y
+ spriteHeight);
if (eatable) {
boundingRect.set((float)position.x, (float)position.y,(float) position.x
+ spriteEatWidth, (float)position.y + spriteEatHeight);
destRect.set((float) position.x-size/2, (float) position.y-size/2,
(float) position.x + spriteWidth/2+spriteWidth, (float) position.y
+ spriteHeight/2+spriteHeight);
boundingRect.set((float) position.x, (float) position.y,
(float) position.x + spriteEatWidth, (float) position.y
+ spriteEatHeight);
destRect.set((float) position.x - GameLogic.BOARD_TILE_SIZE / 2,
(float) position.y - GameLogic.BOARD_TILE_SIZE / 2,
(float) position.x + spriteWidth / 2 + spriteWidth,
(float) position.y + spriteHeight / 2 + spriteHeight);
canvas.drawBitmap(eatableBitmap, sourceEatRect, destRect, null);
} else if (onlyEyes)
canvas.drawBitmap(eyes,(float) position.x, (float)position.y, null);
canvas.drawBitmap(eyes, (float) position.x, (float) position.y,
null);
else
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
}
@@ -136,9 +137,9 @@ public class Ghost extends GameObject {
}
} else if (eatable && blinking) {
frameTicker+= gameTime;
time+=gameTime;
if (frameTicker > framePeriod) {
frameTicker += gameTime;
time += gameTime;
if (frameTicker > framePeriod) {
frameTicker = 0;
// increment the frame
if (!swap)
@@ -164,10 +165,10 @@ public class Ghost extends GameObject {
public void setBlinking(boolean blinking) {
this.blinking = blinking;
if (!blinking) {
time=0;
currentFrame=0;
framePeriod=500;
eatable=false;
time = 0;
currentFrame = 0;
framePeriod = 500;
eatable = false;
}
}
}
@@ -8,6 +8,7 @@ import android.graphics.Rect;
import android.hardware.SensorEvent;
import android.view.MotionEvent;
import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class PacMan extends GameObject {
@@ -27,8 +28,8 @@ public class PacMan extends GameObject {
private float yAcc;
public PacMan(Resources res, Vect initPosition, Context context,
Block[][] board, int size, double speed) {
super(size, speed, board);
Block[][] board, double speed) {
super(speed, board);
dirToChange = GameEnum.STOP;
this.res = res;
position = initPosition;
@@ -47,14 +48,14 @@ public class PacMan extends GameObject {
direction = new Vect(0, 1);
this.size = size;
stop = true;
}
@Override
public void draw(Canvas canvas) {
destRect.set((float) position.x-size/2, (float) position.y-size/2,
destRect.set((float) position.x - GameLogic.BOARD_TILE_SIZE / 2,
(float) position.y - GameLogic.BOARD_TILE_SIZE/2,
(float) position.x + spriteWidth/2+spriteWidth, (float) position.y
+ spriteHeight/2+spriteHeight);
boundingRect.set((float) position.x, (float) position.y,
@@ -165,7 +166,7 @@ public class PacMan extends GameObject {
xAcc = event.values[0];
yAcc = event.values[1];
// jezeli wartosc akcelometra jest wieksza od 1 lub -1
if (xAcc > 1) {
setDirection(GameEnum.LEFT);
} else if (xAcc < -1) {