Compare commits

...
10 Commits
Author SHA1 Message Date
Mariusz Pacek 406af7315b update 2013-01-26 10:59:21 +01:00
Mariusz Pacek 04c9b367ae brakujacy plik 2012-12-08 20:49:32 +01:00
Mariusz Pacek da8f0df869 up 2012-12-08 18:05:43 +01:00
Mariusz Pacek f09aba315e up 2012-12-05 23:28:58 +01:00
Mariusz Pacek 8ffa981d90 Troche poprawek 2012-12-05 18:32:05 +01:00
Mariusz Pacek 266b04bb45 Dodanie sterowanie akcelometrem zwiekszenie szerokosci sciezki oraz
powiekszenie rozmiarow spritow
2012-12-05 01:10:31 +01:00
Mariusz Pacek 0c24eff46b up 2012-12-02 18:04:25 +01:00
Mariusz Pacek ae660adb4a troche zmian 2012-12-02 11:32:55 +01:00
Mariusz Pacek 13c401d818 up 2012-11-29 09:50:08 +01:00
Mariusz Pacek de06fab9ba troche optymalizacji, dodanie zmiany spriata duszka,... 2012-11-28 23:46:48 +01:00
63 changed files with 967 additions and 547 deletions

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
bin/
/bin
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-3
View File
@@ -1,3 +0,0 @@
# cache for current jar dependecy. DO NOT EDIT.
# format is <lastModified> <length> <SHA-1> <path>
# Encoding is UTF-8
BIN
View File
Binary file not shown.
+2 -70
View File
@@ -1,73 +1,5 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.adroid.game.pacman;
/* This stub is for using by IDE only. It is NOT the R class actually packed into APK */
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int blue_gradien=0x7f020000;
public static final int corner=0x7f020001;
public static final int food=0x7f020002;
public static final int foodup=0x7f020003;
public static final int ghost=0x7f020004;
public static final int ic_launcher=0x7f020005;
public static final int inky=0x7f020006;
public static final int level1=0x7f020007;
public static final int pacmanta=0x7f020008;
public static final int pacmantaa=0x7f020009;
public static final int sfood=0x7f02000a;
public static final int streight=0x7f02000b;
}
public static final class id {
public static final int menu_settings=0x7f080000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f070000;
}
public static final class raw {
public static final int lvl=0x7f040000;
public static final int pacman_walk=0x7f040001;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050001;
public static final int menu_settings=0x7f050002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 1023 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

+4 -4
View File
@@ -11,9 +11,9 @@ xxxxxx.xxxxx0xx0xxxxx.xxxxxx
x.xxxxx0xx0xxxxx.x
x.xx0000000000xx.x
x.xx0xxx00xxx0xx.x
xxxxxx.xx0x000000x0xx.xxxxxx
000000.000x000000x000.000000
xxxxxx.xx0x000000x0xx.xxxxxx
x.xx0x000000x0xx.x
x.000x000000x000.x
x.xx0x000000x0xx.x
x.xx0xxxxxxxx0xx.x
x.xx0000000000xx.x
x.xx0xxxxxxxx0xx.x
@@ -28,4 +28,4 @@ x......xx....xx....xx......x
x.xxxxxxxxxx.xx.xxxxxxxxxx.x
x.xxxxxxxxxx.xx.xxxxxxxxxx.x
x..........................x
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
+13 -2
View File
@@ -7,6 +7,11 @@ x..........................x
x.xxxx.xx.xxxxxxxx.xx.xxxx.x
x.xxxx.xx.xxxxxxxx.xx.xxxx.x
x......xx....xx....xx......x
x......xx....x x..........x
x.xxxx.xxxxx.x x xxxxx.xx.x
x.x x.x x.x x.x x.xx.x
x.xxxx.xxxxx.xxxx.xxxxx.xx.x
x......xx..........xx......x
xxxxxx.xxxxx0xx0xxxxx.xxxxxx
x.xxxxx0xx0xxxxx.x
x.xx0000000000xx.x
@@ -17,7 +22,13 @@ xxxxxx.xx0x000000x0xx.xxxxxx
x.xx0xxxxxxxx0xx.x
x.xx0000000000xx.x
x.xx0xxxxxxxx0xx.x
xxxxxx.xx0xxxxxxxx0xx.xxxxxx
xxxxxx.xx0xxxx xx0xx.xxxxxx
x......xx....x x..........x
x.xxxx.xxxxx.x x xxxxx.xx.x
x.x x.x x.x x.x x.xx.x
x.xxxx.xxxxx.xxxx.xxxxx.xx.x
x......xx..........xx......x
xxxxxx.xx.xxxxxxxx.xx.xxxxxx
x............xx............x
x.xxxx.xxxxx.xx.xxxxx.xxxx.x
x.xxxx.xxxxx.xx.xxxxx.xxxx.x
@@ -28,4 +39,4 @@ x......xx....xx....xx......x
x.xxxxxxxxxx.xx.xxxxxxxxxx.x
x.xxxxxxxxxx.xx.xxxxxxxxxx.x
x..........................x
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
+43
View File
@@ -0,0 +1,43 @@
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xo...xx...................ox
x.xx.xxxxx.xx.xx.xxxxx.xxx.x
x.xx.xxxxx.xx.xx.xxxxx.xxx.x
x.xx.......xx.xx.......xxx.x
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
x.xxxxxxxxxxx0xxxxx0xxxxxx.x
x.xx..000000000000000...xx.x
x.xx.xx0xxxxx00xxxxx0xx....x
x....xx0xxxxx00xxxxx0xxxxx.x
x.xxxxx0xx00000000xx0xxxxx.x
x.xxxxx0xx00000000xx0xxxxx.x
x.xxxxx0xx00000000xx00.....x
x....xx0xxxxxxxxxxxx0xxxxxxx
x.xx.xx0xxxxxxxxxxxx0xxxxxxx
x.xx..0000000000000000.....x
x.xxxxxxx0xxxxxxxxx0xxxxxx.x
x.xxxxxxx.xxxxxxxxx.xxxxxx.x
x.xxx...............xx.....x
x.xxx.xxxxxxx.xxxxx.xx.xxxxx
x.xxx.xxxxxxx.xxxxx.xx.xxxxx
xo........................ox
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
x....xx.o..................x
x.xx.xx.xxxxxxxxxx.xxxxxxx.x
x.xx....xxxxxxxxxx.xxxxxxx.x
x.xxxxxxxxx00000xx.xxxxxxx.x
x.xxxxxxxxxxxxxxxxoxxxxxxx.x
x.xxxxxxxxxxxxxxxx.xxxxxxx.x
x..........................x
x.xxxxxx.xxxxxxxxxxxxxxxxx.x
x.xxxxxx.xxxxxxxxxxxxxxxxx.x
x.xxxxxx.xxxxxxxxxxxxxxxxx.x
x..........................x
x.xxxxxxxxxxx.xxxxxxxxxxxx.x
x.xxxxxxxxxxx.xxxxxxxxxxxx.x
xo........................ox
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+289 -148
View File
@@ -1,186 +1,327 @@
package com.android.game.pacman.game;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.util.Log;
import android.util.Pair;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.adroid.game.pacman.R;
import com.android.game.pacman.model.Block;
import com.android.game.pacman.model.Board;
import com.android.game.pacman.model.Collision;
import com.android.game.pacman.model.Ghost;
import com.android.game.pacman.model.PacMan;
import com.android.game.pacman.model.SolidObject;
import com.android.game.pacman.model.SoundStuff;
import com.android.game.pacman.model.Vect;
import com.android.game.pacman.model.*;
import com.android.game.pacman.utils.GameEnum;
public class GameLogic extends SurfaceView implements SurfaceHolder.Callback {
import java.util.LinkedList;
private PacMan pacman;
private GameLoop loop;
private ArrayList<Block> wall;
private ArrayList<Block> path;
private ArrayList<SolidObject> food;
private Board boardGame;
private SoundStuff ss;
private Bitmap background;
private Ghost ghost1;
private Ghost ghost2;
private Ghost ghost3;
private Ghost ghost4;
public class GameLogic extends SurfaceView implements SurfaceHolder.Callback,
SensorEventListener {
static int BOARD_TILE_SIZE = 17;
static int BOARD_HEIGHT = 31;
static int BOARD_WIDTH = 28;
private PacMan pacman;
public GameLogic(Context context) {
super(context);
setFocusable(true);
getHolder().addCallback(this);
Bitmap tmpbackground = BitmapFactory.decodeResource(getResources(),
R.drawable.blue_gradien);
private LinkedList<Block> wall;
private LinkedList<Block> path;
private LinkedList<SolidObject> food;
private LinkedList<Ghost> ghosts;
float scaleWidth = ((float) 480) / 256;
float scaleHeight = ((float) 800) / 256;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
background = Bitmap.createBitmap(tmpbackground, 0, 0,
256, 256, matrix,
false);
loop = new GameLoop(getHolder(), this);
ss = new SoundStuff(context);
public GameLoop loop;
private Board boardGame;
private SoundStuff ss;
private CountDownTimer task;
private SensorManager sensorManager;
private Vibrator v;
}
private int points = 0;
private int eatInRow = 0; // mnoznik do zjedzonych duszkow
private boolean accOn;
private boolean start = false;
public void render(Canvas canvas) {
canvas.drawBitmap(background, 0f, 0f, null);
private final static Paint paint = new Paint();
public static int BOARD_TILE_SIZE;
public static int BOARD_HEIGHT;
public static int BOARD_WIDTH;
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;
for (Block obj : wall) {
obj.draw(canvas);
}
for (Block obj : path) {
obj.draw(canvas);
}
for (SolidObject obj : food) {
obj.draw(canvas);
}
pacman.draw(canvas);
ghost1.draw(canvas);
ghost2.draw(canvas);
ghost3.draw(canvas);
ghost4.draw(canvas);
}
public GameLogic(Context context, int w, int h) {
super(context);
setFocusable(true);
getHolder().addCallback(this);
public void prepareLevel() {
// TODO generate level
}
WIDTH = w;
HEIGHT = h;
public void update() {
loop = new GameLoop(getHolder(), this);
ss = new SoundStuff(context);
paint.setStyle(Paint.Style.FILL);
// przez 10 sek po 5 zaczna mrugac duchy
task = new CountDownTimer(10000, 1000) {
private boolean first = false;
private int ID;
int times = 0;
otherCollision();
handleCollsion();
pacman.update(System.currentTimeMillis(), getWidth(), getHeight());
ghost1.update(System.currentTimeMillis(), getWidth(), getHeight());
ghost2.update(System.currentTimeMillis(), getWidth(), getHeight());
ghost3.update(System.currentTimeMillis(), getWidth(), getHeight());
ghost4.update(System.currentTimeMillis(), getWidth(), getHeight());
@Override
public void onTick(long millisUntilFinished) {
}
if (!first) {
ID = SoundStuff.sp.play(SoundStuff.ghostSiren, 1, 1, 1, -1,
1);
public void doAi(GameEnum deficultiy) {
switch (deficultiy) {
case EASY:
// todo
break;
// todo
case NORMAL:
break;
case HARD:
// todo
break;
}
for (Ghost ghost : ghosts) {
ghost.setEatable(true);
}
first = true;
}
}
if (times == 5)
for (Ghost ghost : ghosts) {
ghost.setBlinking(true);
}
else
times++;
}
private void otherCollision() {
for (SolidObject obj : food) {
if (Collision.detectNormalCollision(pacman.getBoundingRect(),
obj.getBoundingRect())) {
obj.setIsTouched(true);
}
}
}
@Override
public void onFinish() {
for (Ghost ghost : ghosts) {
ghost.reset();
}
SoundStuff.sp.stop(ID);
eatInRow = 0;
first = false;
times = 0;
private void handleCollsion() {
for (int i = 0; i < food.size(); i++) {
if (food.get(i).getIsTouched()) {
if (food.get(i).getKind() == GameEnum.FOODUP) {
Vibrator v = (Vibrator) getContext().getSystemService(
Context.VIBRATOR_SERVICE);
v.vibrate(100);
}
food.remove(i);
}
}
};
}
}
}
@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
public void render(Canvas canvas) {
// tlo
paint.setColor(Color.BLUE);
canvas.drawRect(0, 0, WIDTH, HEIGHT, paint);
// napis z punktami
paint.setColor(Color.WHITE);
paint.setTextSize(30);
canvas.drawText("Points: " + points, 10, getHeight() - 40, paint);
pacman.dir(event);
return true;
}
for (Block obj : path) {
obj.draw(canvas);
}
for (SolidObject obj : food) {
obj.draw(canvas);
}
pacman.draw(canvas);
for (Ghost ghost : ghosts) {
ghost.draw(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
}
public void loadNextLvl() {
task.cancel();
pacman.stop();
boardGame = new Board(getResources(), R.raw.lvl);
@Override
public void surfaceCreated(SurfaceHolder holder) {
boardGame = new Board(BOARD_WIDTH, BOARD_HEIGHT, BOARD_TILE_SIZE,
getResources());
wall = boardGame.getWall();
path = boardGame.getPath();
food = boardGame.getbGame();
pacman = new PacMan(getResources(), new Vect((int) (BOARD_TILE_SIZE
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
boardGame.getBlock(), 17, 5);
ghost1=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(12*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
ghost2=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(13*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
ghost3=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(14*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
ghost4=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(15*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
loop.setRunning(true);
loop.start();
wall = boardGame.getWall();
path = boardGame.getPath();
food = boardGame.getbGame();
pacman = new PacMan(getResources(), new Vect((BOARD_TILE_SIZE
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
boardGame.getBlock(), 100f, 3);
ghosts = new LinkedList<Ghost>();
}
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)));
int id = -1;
do {
id = SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
} while (id == 0);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
loop.join();
retry = false;
} catch (InterruptedException e) {
}
}
public void update(double time) {
}
pacman.update(time, getWidth(), getHeight());
for (Ghost ghost : ghosts) {
ghost.update(time, getWidth(), getHeight());
}
otherCollision();
handleCollsion();
if (food.isEmpty()) {
loadNextLvl();
}
}
public void doAi(int deficultiy) {
switch (deficultiy) {
case GameEnum.EASY:
// todo
break;
// todo
case GameEnum.NORMAL:
break;
case GameEnum.HARD:
// todo
break;
}
}
private void otherCollision() {
for (SolidObject obj : food) {
if (Collision.detectNormalCollision(pacman.boundingRect,
obj.boundingRect)) {
obj.setIsTouched(true);
}
}
for (Ghost ghost : ghosts) {
if (Collision.detectNormalCollision(pacman.boundingRect,
ghost.boundingRect)) {
if (ghost.isEatable() && !ghost.isOnlyEyes()) {
ghost.setIsTouched(true);
ghost.reset();
ghost.setEyes(true);
eatInRow++;
points += GHOST_EAT_POINT * eatInRow;
SoundStuff.sp.play(SoundStuff.pacmanEatGhost, 1, 1, 1, 0,
1f);
v.vibrate(100);
} else if (!ghost.isOnlyEyes())
pacman.die();
}
}
}
private void handleCollsion() {
for (int i = 0; i < food.size(); i++) {
if (food.get(i).getIsTouched()) {
if (food.get(i).kind == GameEnum.FOODUP) {
points += FOODUP_POINT;
v.vibrate(100);
task.onFinish();
task.start();
} else
points += FOOD_POINT;
food.remove(i);
}
}
}
@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
pacman.dir(event);
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
WIDTH = width;
HEIGHT = height;
BOARD_TILE_SIZE = (int) (WIDTH / BOARD_WIDTH);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
boardGame = new Board(getResources(), R.raw.lvl0);
wall = boardGame.getWall();
path = boardGame.getPath();
food = boardGame.getbGame();
pacman = new PacMan(getResources(), new Vect((BOARD_TILE_SIZE
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
boardGame.getBlock(), 100f, 3);
ghosts = new LinkedList<Ghost>();
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)));
int id = -1;
do {
id = SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
} while (id == 0);
sensorManager = (SensorManager) getContext().getSystemService(
getContext().SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
loop.setRunning(true);
loop.start();
start = true;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
loop.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
@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 && start) {
pacman.dirAcc(event);
}
}
public boolean isAccOn() {
return accOn;
}
public void setAccOn(boolean accOn) {
this.accOn = accOn;
}
public void stop() {
}
}
+13 -1
View File
@@ -1,6 +1,7 @@
package com.android.game.pacman.game;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
public class GameLoop extends Thread {
@@ -19,21 +20,32 @@ public class GameLoop extends Thread {
@Override
public void run() {
long lastFpsTime = 0;
long last = 0;
Canvas canvas;
double delay;
while (running) {
canvas = null;
last = System.currentTimeMillis();
delay = (last - lastFpsTime);
lastFpsTime = last;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
gamePanel.update();
gamePanel.render(canvas);
gamePanel.update(delay / 1000);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
public void setRunning(Boolean looping) {
@@ -4,19 +4,26 @@ 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;
public class MainActivity extends Activity {
GameLogic game;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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;
game = new GameLogic(this,screenWidth,screenHeight);
setContentView(game);
}
@@ -26,5 +33,16 @@ public class MainActivity extends Activity {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// @Override
// protected void onPause() {
// game.loop=null;
// super.onPause();
//
// }
// @Override
// protected void onResume() {
// game.loop = new GameLoop(game.getHolder(), game);
// super.onResume();
// }
}
+15 -20
View File
@@ -3,39 +3,34 @@ package com.android.game.pacman.model;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class Block extends SolidObject {
private boolean selected = false;
private int size;
private Rect sourceRect;
static Paint paint = new Paint();
public Block(int x, int y) {
super(x * GameLogic.BOARD_TILE_SIZE, y * GameLogic.BOARD_TILE_SIZE);
public Block(int x, int y, int size) {
super(x * size, y * size);
this.size = size;
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
}
public void draw(Canvas canvas) {
Paint paint = new Paint();
boundingRect.set(x, y, x + size, y + size);
if (kind == GameEnum.PATH) {
paint.setColor(Color.BLACK);
if (!isTouched)
paint.setStyle(Paint.Style.FILL);
else {
paint.setStyle(Paint.Style.STROKE);
isTouched = false;
}
if (selected) {
paint.setColor(Color.RED);
}else
paint.setColor(Color.BLACK);
boundingRect.set((float) x - GameLogic.BOARD_TILE_SIZE / 2, (float) y - GameLogic.BOARD_TILE_SIZE / 2,
(float) x + GameLogic.BOARD_TILE_SIZE / 2 + GameLogic.BOARD_TILE_SIZE, (float) y + GameLogic.BOARD_TILE_SIZE / 2 + GameLogic.BOARD_TILE_SIZE);
canvas.drawRect(boundingRect, paint);
}
}
+57 -44
View File
@@ -5,46 +5,42 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import android.R.integer;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.util.Pair;
import com.adroid.game.pacman.R;
import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class Board {
private ArrayList<SolidObject> bGame;
private int x;
private int y;
private LinkedList<SolidObject> bGame;
private int size;
private char[][] lvl;
private ArrayList<Block> path;
private ArrayList<Block> wall;
private LinkedList<Block> path;
private LinkedList<Block> wall;
private Block[][] blockTab;
Resources res;
public Board(int x, int y, int size, Resources res) {
this.x = x;
this.y = y;
this.size = size;
this.res = res;
blockTab = new Block[x][y];
bGame = new ArrayList<SolidObject>(x * y);
lvl = readLvl(x, y, res);
path = new ArrayList<Block>();
wall = new ArrayList<Block>();
// Bitmap bitmap=
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
public Board(Resources res, int sourceLvl) {
blockTab[j][i] = new Block(j, i, size);
this.size = GameLogic.BOARD_TILE_SIZE;
lvl = readLvl(res, sourceLvl);
blockTab = new Block[GameLogic.WIDTH][GameLogic.BOARD_HEIGHT];
bGame = new LinkedList<SolidObject>();
path = new LinkedList<Block>();
wall = new LinkedList<Block>();
// Bitmap bitmap=
for (int i = 0; i < GameLogic.BOARD_HEIGHT; i++) {
for (int j = 0; j < GameLogic.BOARD_WIDTH; j++) {
blockTab[j][i] = new Block(j, i);
switch (lvl[j][i]) {
case 'x':
setBlock(j, i);
@@ -59,7 +55,7 @@ public class Board {
break;
case '.':
setFood(j, i, res);
blockTab[j][i].kind = GameEnum.PATH;
break;
}
@@ -71,27 +67,27 @@ public class Board {
}
private void setBlock(int x, int y) {
Block newB = new Block(x, y, size);
Block newB = new Block(x, y);
wall.add(newB);
}
private void setPath(int x, int y) {
Block newB = new Block(x, y, size);
Block newB = new Block(x, y);
newB.kind = GameEnum.PATH;
path.add(newB);
}
private void setFood(int x, int y, Resources res) {
Food newF = new Food(x, y, res,GameEnum.FOOD);
Food newF = new Food(x, y, res);
bGame.add(newF);
setPath(x, y);
}
private void setFoodUp(int x, int y, Resources res) {
Food newF = new Food(x, y, res,GameEnum.FOODUP);
FoodUp newF = new FoodUp(x, y, res);
bGame.add(newF);
setPath(x, y);
@@ -101,45 +97,62 @@ public class Board {
return blockTab;
}
public ArrayList<SolidObject> getbGame() {
public LinkedList<SolidObject> getbGame() {
return bGame;
}
public ArrayList<Block> getPath() {
public LinkedList<Block> getPath() {
return path;
}
public ArrayList<Block> getWall() {
public LinkedList<Block> getWall() {
return wall;
}
public char[][] readLvl(int x, int y, Resources res) {
public char[][] readLvl(Resources res, int sourceLvl) {
Pair<char[][], Pair<Integer, Integer>> x_y = readl(res, sourceLvl);
GameLogic.BOARD_HEIGHT = x_y.second.second;
GameLogic.BOARD_WIDTH = x_y.second.first;
GameLogic.BOARD_TILE_SIZE = (int) ( GameLogic.WIDTH/x_y.second.first );
Log.v("dem",String.valueOf(GameLogic.BOARD_TILE_SIZE));
return x_y.first;
InputStream inputStream = res.openRawResource(R.raw.lvl);
}
public static Pair<char[][], Pair<Integer, Integer>> readl(Resources res,
int sourceLvl) {
InputStream inputStream = res.openRawResource(sourceLvl);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
ArrayList<Character> tab = new ArrayList<Character>();
int r;
char[][] buf = new char[x][y];
int _x = 0, _y = 0;
try {
while ((r = buffreader.read()) != -1) {
if (r == 10) {
_y++;
_x = 0;
} else {
buf[_x][_y] = (char) r;
_x++;
tab.add((char) r);
}
}
} catch (IOException e) {
return null;
// return null;
}
_y++;
int index = 0;
char[][] buf = new char[_x][_y];
for (int i = 0; i < _y; i++) {
for (int j = 0; j < _x; j++) {
buf[j][i] = tab.get((index));
index++;
}
return buf;
}
return new Pair<char[][], Pair<Integer, Integer>>(buf,
new Pair<Integer, Integer>(_x, _y));
}
}
@@ -1,15 +1,14 @@
package com.android.game.pacman.model;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Pair;
import com.android.game.pacman.utils.GameEnum;
import android.R.integer;
import android.graphics.Rect;
import android.util.Log;
import android.util.Pair;
public class Collision {
//nie uzywane
static public Pair<GameEnum, Integer> wallDetectCollision(Rect one, Rect two) {
static public Pair<Integer, Integer> wallDetectCollision(Rect one, Rect two) {
double w = 0.5 * (one.width()-1 + two.width());
double h = 0.5 * (one.height()-1 + two.height());
double dx = one.exactCenterX() - two.exactCenterX();
@@ -23,29 +22,29 @@ public class Collision {
if (wy > -hx) {
/* collision at the top */
return new Pair<GameEnum, Integer>(GameEnum.UP,
return new Pair<Integer, Integer>(GameEnum.UP,
Integer.valueOf((int)(one.exactCenterY() - two.exactCenterY())-17));
} else
/* on the left */
return new Pair<GameEnum, Integer>(GameEnum.RIGHT,
return new Pair<Integer, Integer>(GameEnum.RIGHT,
Integer.valueOf((int)(two.exactCenterX() - one.exactCenterX())-17));
else if (wy > -hx)
/* on the right */
return new Pair<GameEnum, Integer>(GameEnum.LEFT,
return new Pair<Integer, Integer>(GameEnum.LEFT,
Integer.valueOf((int)(two.exactCenterX() - one.exactCenterX())+17));
else {
/* at the bottom */
return new Pair<GameEnum, Integer>(GameEnum.DOWN,
return new Pair<Integer, Integer>(GameEnum.DOWN,
Integer.valueOf((int)(one.exactCenterY() - two.exactCenterY())+17));
}
}
return new Pair<GameEnum, Integer>(GameEnum.NORMAL, Integer.valueOf(0));
return new Pair<Integer, Integer>(GameEnum.NORMAL, Integer.valueOf(0));
}
//nie uzwyane
static public Pair<GameEnum, Double> pathDetectCollision(Rect one, Rect two,int size) {
static public Pair<Integer, Double> pathDetectCollision(Rect one, Rect two,int size) {
double w = 0.5 * (one.width()+size + two.width());
double h = 0.5 * (one.height()+size + two.height());
double dx = one.exactCenterX() - two.exactCenterX();
@@ -61,28 +60,28 @@ public class Collision {
if (wy > -hx) {
/* collision at the top */
return new Pair<GameEnum, Double>(GameEnum.UP,
return new Pair<Integer, Double>(GameEnum.UP,
Double.valueOf(d));
} else
/* on the left */
return new Pair<GameEnum, Double>(GameEnum.RIGHT,
return new Pair<Integer, Double>(GameEnum.RIGHT,
Double.valueOf(d));
else if (wy > -hx)
/* on the right */
return new Pair<GameEnum, Double>(GameEnum.LEFT,
return new Pair<Integer, Double>(GameEnum.LEFT,
Double.valueOf(d));
else {
/* at the bottom */
return new Pair<GameEnum, Double>(GameEnum.DOWN,
return new Pair<Integer, Double>(GameEnum.DOWN,
Double.valueOf(d));
}
}
return new Pair<GameEnum, Double>(GameEnum.NORMAL, Double.valueOf(0));
return new Pair<Integer, Double>(GameEnum.NORMAL, Double.valueOf(0));
}
public static boolean detectNormalCollision(Rect one,Rect two){
public static boolean detectNormalCollision(RectF one,RectF two){
return one.intersect(two);
}
}
+14 -10
View File
@@ -1,25 +1,29 @@
package com.android.game.pacman.model;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class Food extends SolidObject {
private static Bitmap bitmap;
public Food(int x,int y,Resources res,GameEnum kind){
super(x*17, y*17);
if(kind ==GameEnum.FOOD)
bitmap = BitmapFactory.decodeResource(res,
com.adroid.game.pacman.R.drawable.food);
else
public Food(int x, int y, Resources res) {
super(x * GameLogic.BOARD_TILE_SIZE, y * GameLogic.BOARD_TILE_SIZE);
kind = GameEnum.FOOD;
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(res,
com.adroid.game.pacman.R.drawable.foodup);
this.kind=kind;
com.adroid.game.pacman.R.drawable.food);
}
}
public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, (float)x, (float)y, null);
boundingRect.set((float)x, (float)y, (float)x + bitmap.getWidth(),(float)y + bitmap.getHeight());
}
}
@@ -0,0 +1,28 @@
package com.android.game.pacman.model;
import android.content.res.Resources;
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 * GameLogic.BOARD_TILE_SIZE, y * GameLogic.BOARD_TILE_SIZE);
kind = GameEnum.FOODUP;
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(res,
com.adroid.game.pacman.R.drawable.foodup);
}
}
public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, (float)x, (float)y, null);
boundingRect.set((float)x, (float)y, (float)x + bitmap.getWidth(),(float)y + bitmap.getHeight());
}
}
+112 -80
View File
@@ -4,136 +4,168 @@ import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.graphics.RectF;
import com.android.game.pacman.game.GameLogic;
import com.android.game.pacman.utils.GameEnum;
public class GameObject {
public RectF boundingRect = new RectF();
public RectF destRect = new RectF();
protected Vect position;
protected Bitmap bitmap;
Rect boundingRect = new Rect();
protected boolean isTouched = false;
protected GameEnum dirToChange;
protected int speed;
protected boolean rotate = false;
protected int dirToChange;
protected double speed;
protected double angle = 0;
protected Vect direction;
protected Vect target;
protected Vect target = new Vect(0, 0);
protected boolean stop;
protected int size;
private Block[][] board;
protected GameEnum nextDir;
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 out
protected int spriteHeight;
protected int spriteWidth; // the width of the sprite to calculate the cut
protected int spriteHeight;
protected Resources res;
protected boolean targetReached = true;
private Block[][] board;
private Vect newdir = new Vect(0, 0);
public void update() {
}
public GameObject(int size,int speed,Block[][] board){
this.speed=speed;
this.size=size;
this.board=board;
public GameObject(double speed, Block[][] board) {
this.speed = speed;
this.board = board;
}
public void rotate(double angle) {
rotate = true;
this.angle = angle;
}
public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, position.x, position.y, null);
boundingRect = new Rect(position.x, position.y, position.x
+ bitmap.getWidth(), position.y + bitmap.getHeight());
boundingRect.set((float) position.x, (float) position.y,
(float) position.x + spriteWidth, (float) position.y
+ spriteHeight);
canvas.drawBitmap(bitmap, sourceRect, boundingRect, null);
}
public Rect getBoundingRect() {
return boundingRect;
}
synchronized public void move() {
synchronized public void move(double dt) {
if (dirToChange == GameEnum.UP) {
direction = new Vect(0, -1);
rotate(270);
}
if (dirToChange == GameEnum.DOWN) {
direction = new Vect(0, 1);
rotate(90);
}
if (dirToChange == GameEnum.LEFT) {
direction = new Vect(-1, 0);
rotate(180);
}
if (dirToChange == GameEnum.RIGHT) {
direction = new Vect(1, 0);
rotate(0);
}
if (target == null) {
if (targetReached) {
if (dirToChange == GameEnum.UP) {
direction.x=0;
direction.y=-1;
angle=270;
}
if (dirToChange == GameEnum.DOWN) {
direction.x=0;
direction.y=1;
angle=90;
}
if (dirToChange == GameEnum.LEFT) {
direction.x=-1;
direction.y=0;
angle=180;
}
if (dirToChange == GameEnum.RIGHT) {
direction.x=1;
direction.y=0;
angle=0;
}
if (canMove(position, direction)) {
stop = false;
target = position.add(direction.mulitply(size));
targetReached = false;
target = position.add(direction
.mulitply(GameLogic.BOARD_TILE_SIZE));
} else
stop = true;
}
if (target != null) {
if (moveTowards(target))
target = null;
if (!targetReached) {
if (moveTowards(target, dt))
targetReached = true;
}
}
private boolean moveTowards(Vect goal) {
if (position.equals(goal))
return true;
Vect dir = Vect.normalize(goal.subtract(position)); //kierunek ruchu
position = position.add(dir.mulitply(speed)); //nowa pozycja
if (Math.abs(Vect.dot(dir, Vect.normalize(goal.subtract(position))) + 1) < speed) //jezeli minął cel
position = goal;
return position.equals(goal);
private boolean moveTowards(Vect goal, double dt) {
if (position.equals(goal)) //jezeli obiekt osiagna cel
return true;
Vect dir = Vect.normalize(goal.subtract(position)); // kierunek ruchu
position = position.add(dir.mulitply(speed * dt)); // nowa pozycja
if (Math.abs(Vect.dot(dir, Vect.normalize(goal.subtract(position))) + 1) < speed* dt) //czy obiekt minol cel
position=goal;
return position.equals(goal);
}
private boolean canMove(Vect position, Vect direction) {
if (direction == null)
if (direction.x == 0 && direction.y == 0)
return false;
int nx = (int) (position.x / size) + (int) direction.x;
int ny = (int) (position.y / size) + (int) direction.y;
if (nx < 0 || ny < 0 || nx >= board.length
|| ny >= board[0].length)
int nx = (int) (position.x / GameLogic.BOARD_TILE_SIZE)
+ (int) direction.x;
int ny = (int) (position.y / GameLogic.BOARD_TILE_SIZE)
+ (int) direction.y;
if (nx < 0 || ny < 0 || nx >= GameLogic.BOARD_WIDTH
|| ny >= GameLogic.BOARD_HEIGHT)
return false;
return board[nx][ny].kind == GameEnum.PATH;
}
private boolean check(GameEnum dir) {
Vect newdir = null;
if (dir == GameEnum.UP)
newdir = new Vect(0, -1);
if (dir == GameEnum.DOWN)
newdir = new Vect(0, 1);
if (dir == GameEnum.LEFT)
newdir = new Vect(-1, 0);
if (dir == GameEnum.RIGHT)
newdir = new Vect(1, 0);
//sprawdzanie czy jest mozliwosc zmiany kierunku na nowy
private boolean check(int dir) {
newdir.x = 0;
newdir.y = 0;
if (dir == GameEnum.UP) {
newdir.x = 0;
newdir.y = -1;
}
if (dir == GameEnum.DOWN) {
newdir.x = 0;
newdir.y = 1;
}
if (dir == GameEnum.LEFT) {
newdir.x = -1;
newdir.y = 0;
}
if (dir == GameEnum.RIGHT) {
newdir.x = 1;
newdir.y = 0;
}
return canMove(position, newdir);
}
public void setDirection(GameEnum dir) {
if (check(dir)) {
dirToChange=dir;
public void setDirection(int dir) {
//jezeli obiekt jest wyrownany do plytki to zmien kierunek jezeli nie to zapamietaj ten kierunke na puxniej
if (position.x % GameLogic.BOARD_TILE_SIZE == 0
&& position.y % GameLogic.BOARD_TILE_SIZE == 0) {
if (check(dir))
dirToChange = dir;
} else
nextDir = dir;
}
//sprawdza czy obiekt moze zmienic kierunke na nowy
public void canChangeDir() {
if (check(nextDir)) {
dirToChange=nextDir;
nextDir = GameEnum.STOP;
if (position.x % GameLogic.BOARD_TILE_SIZE == 0
&& position.y % GameLogic.BOARD_TILE_SIZE == 0) {
if (check(nextDir)) {
dirToChange = nextDir;
nextDir = GameEnum.STOP;
}
}
}
public void setIsTouched(boolean state) {
isTouched = state;
}
}
+165 -54
View File
@@ -1,76 +1,187 @@
package com.android.game.pacman.model;
import java.util.LinkedList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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;
import java.util.LinkedList;
public class Ghost extends GameObject {
private LinkedList<GameEnum> movement;
private int lvl;// table of actual lvl importanto to do ai
private Rect sourceRect;
private boolean eatable=false;
public Ghost(int size, int speed, Block[][] board, Resources res,Vect initPosition) {
super(size, speed, board);
bitmap = BitmapFactory.decodeResource(res,
com.adroid.game.pacman.R.drawable.ghost);
spriteHeight = bitmap.getHeight();
spriteWidth = bitmap.getWidth() / 4; // ilosc dostepnych klatek
sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
dirToChange = GameEnum.STOP;
this.res = res;
position = initPosition;
nextDir = GameEnum.STOP;
direction = new Vect(0, 0);
this.size = size;
stop=false;
}
private LinkedList<GameEnum> movement;
private Rect sourceRect;
// uzywane kied duszek jest do zjedzenia
private int spriteEatHeight;
private int spriteEatWidth;
private Rect sourceEatRect;
public void doAi() {
private Bitmap eyes;
private Bitmap eatableBitmap;
}
private boolean eatable = false;
private boolean onlyEyes = false;
private boolean blinking = false;
public void update(long gameTime, int canvasWidth, int canvasHeiht) {
private double frameTicker;
private double framePeriod = 0.5f;
private boolean swap;
move();
calculateSourceRect();
canChangeDir();
private double time; // do mrugania;
private int currentFrame;
}
public Ghost(double speed, Block[][] board, Resources res, Vect initPosition) {
super(speed, board);
@Override
public void draw(Canvas canvas) {
Rect destRect = new Rect(position.x, position.y, position.x
+ spriteWidth, position.y + spriteHeight);
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
boundingRect = destRect;
}
bitmap = BitmapFactory.decodeResource(res,
com.adroid.game.pacman.R.drawable.ghost);
private void calculateSourceRect() {
spriteHeight = bitmap.getHeight();
spriteWidth = bitmap.getWidth() / 4; // ilosc dostepnych klatek
sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
if (angle == 270) {// gora
sourceRect.left = 1 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
eatableBitmap = BitmapFactory
.decodeResource(res, R.drawable.ghost_eat2);
spriteEatHeight = eatableBitmap.getHeight();
spriteEatWidth = eatableBitmap.getWidth() / 2; // ilosc dostepnych
// klatek
sourceEatRect = new Rect(0, 0, spriteWidth, spriteHeight);
}
if (angle == 90) {// dol
sourceRect.left = 3 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
eyes = BitmapFactory.decodeResource(res, R.drawable.ghost_eye);
}
if (angle == 180) {// prawo
sourceRect.left = 0 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
dirToChange = GameEnum.STOP;
this.res = res;
position = initPosition;
nextDir = GameEnum.STOP;
direction = new Vect(0, 0);
stop = false;
}
}
if (angle == 0) {// lewo
sourceRect.left = 2 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
public void doAi() {
}
}
}
public void update(double gameTime, int canvasWidth, int canvasHeiht) {
move(gameTime);
calculateSourceRect(gameTime);
canChangeDir();
if (eatable) {
if (isTouched) {
eatable = true;
onlyEyes = true;
}
}
}
public void draw(Canvas canvas) {
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 - 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);
else
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
}
public void setEatable(boolean eat) {
eatable = eat;
}
public void reset() {
time = 0;
currentFrame = 0;
framePeriod = 0.5f;
blinking = false;
eatable = false;
frameTicker = 0;
swap = false;
}
public boolean isEatable() {
return eatable;
}
public boolean isOnlyEyes() {
return onlyEyes;
}
private void calculateSourceRect(double gameTime) {
if (!eatable) {
if (angle == 270) {// gora
sourceRect.left = 1 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
}
if (angle == 90) {// dol
sourceRect.left = 3 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
}
if (angle == 180) {// prawo
sourceRect.left = 0 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
}
if (angle == 0) {// lewo
sourceRect.left = 2 * spriteWidth;
sourceRect.right = sourceRect.left + spriteWidth;
}
} else if (eatable && blinking && !onlyEyes) {
frameTicker += gameTime;
time += gameTime;
if (frameTicker > framePeriod) {
frameTicker = 0;
// increment the frame
if (!swap)
currentFrame++;
else
currentFrame--;
if (currentFrame == 0 || currentFrame == 1) {
swap = !swap;
if (time > 1) {
time = 0;
framePeriod -= 0.08;
}
}
}
}
// define the rectangle to cut out sprite
sourceEatRect.left = currentFrame * spriteEatWidth;
sourceEatRect.right = sourceEatRect.left + spriteEatWidth;
}
public void setEyes(boolean set) {
onlyEyes = set;
}
public void setBlinking(boolean blinking) {
this.blinking = blinking;
}
}
+117 -52
View File
@@ -1,34 +1,45 @@
package com.android.game.pacman.model;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
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 {
// rectangle
private int frameNr; // number of frames in animation
private int currentFrame = 0; // the current frame
private double frameTicker; // the time of the last frame update
private int framePeriod; // milliseconds between each frame (1000/fps)
// the height of the sprite
private Resources res;
private float x1, x2, y1, y2, dx, dy;
private double framePeriod; // milliseconds between each frame (1000/fps)
// the height of the sprite
private double x1, x2, y1, y2, dx, dy;
private boolean isPlaying = false;
boolean swap = false;
private int soundID1 = -1;
private int soundID2 = -1;
private float xAcc;
private float yAcc;
private float xZero;
private float yZero;
private boolean first = true;
private boolean die = false;
private int life;
private double second = 0;
private Vect initVectl;
public PacMan(Resources res, Vect initPosition, Context context,
Block[][] board, int size, int speed) {
super(size, speed, board);
Block[][] board, double speed, int life) {
super(speed, board);
initVectl = initPosition;
this.life = life;
dirToChange = GameEnum.STOP;
this.res = res;
position = initPosition;
@@ -37,55 +48,74 @@ public class PacMan extends GameObject {
spriteHeight = bitmap.getHeight();
spriteWidth = bitmap.getWidth() / 3; // ilosc dostepnych klatek
sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
frameNr = 6;
framePeriod = 1000 / 6;
SoundStuff.sp.play(SoundStuff.pacamnWalk, 1, 1, 1, -1, 1);
framePeriod = (double) (0.25) / 2;
do {
soundID1 = SoundStuff.sp
.play(SoundStuff.pacamnWalk, 1, 1, 1, -1, 1);
} while (soundID1 == 0);
nextDir = GameEnum.STOP;
direction = new Vect(0, 1);
this.size = size;
stop=true;
stop = true;
}
@Override
public void draw(Canvas canvas) {
Rect destRect = new Rect(position.x, position.y, position.x
+ spriteWidth, position.y + spriteHeight);
if (rotate) {
canvas.save();
canvas.rotate((float) angle,
(float) position.x + (spriteWidth / 2), (float) position.y
+ (spriteHeight / 2));
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
canvas.restore();
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);
canvas.save();
canvas.rotate((float) angle, (float) position.x + (spriteWidth / 2),
(float) position.y + (spriteHeight / 2));
rotate = false;
} else {
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
canvas.restore();
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
}
boundingRect = destRect;
}
// the update method for ship
public void update(long gameTime, int canvasWidth, int canvasHeiht) {
public void update(double gameTime, int canvasWidth, int canvasHeiht) {
if (die) {
if (!stop) {
stop = true;
handleSound();
}
second += gameTime;
if (second >= 1) {
die = false;
life--;
second = 0;
position = initVectl;
targetReached = true;
}
} else {
move(gameTime);
calculateSourceRect(gameTime);
handleSound();
canChangeDir();
move();
calculateSourceRect(gameTime);
borders();
handleSound();
canChangeDir();
}
}
public void playSound(int kind) {
private void playDieSound() {
do {
soundID2 = SoundStuff.sp.play(SoundStuff.gameOver, 1, 1, 1, 0, 1);
} while (soundID2 == 0);
}
public void dir(MotionEvent event) {
switch (event.getAction()) {
@@ -123,29 +153,29 @@ public class PacMan extends GameObject {
public void handleSound() {
if (stop) {
SoundStuff.sp.pause(SoundStuff.pacamnWalk);
SoundStuff.sp.pause(soundID1);
isPlaying = false;
} else if (!isPlaying) {
SoundStuff.sp.resume(SoundStuff.pacamnWalk);
SoundStuff.sp.resume(soundID1);
isPlaying = true;
}
if(stop && die)
{
do {
soundID2 = SoundStuff.sp.play(SoundStuff.gameOver, 1, 1, 1, 0, 1);
} while (soundID2 == 0);
}
}
public void borders() {
if (position.x > 480 && position.y == 238) { // przejście przez sciane
// nie bedzie dzialac
// dla innych lvl!
position.x = -size;
}
if (position.x < -size && position.y == 238) {
position.x = 479;
}
}
private void calculateSourceRect(long gameTime) {
private void calculateSourceRect(double gameTime) {
if (!stop) {
if (gameTime > frameTicker + framePeriod) {
frameTicker = gameTime;
frameTicker += gameTime;
if (frameTicker > framePeriod) {
frameTicker = 0;
// increment the frame
if (!swap)
currentFrame++;
@@ -153,6 +183,7 @@ public class PacMan extends GameObject {
currentFrame--;
if (currentFrame == 0 || currentFrame == 2) {
swap = !swap;
}
}
@@ -163,4 +194,38 @@ public class PacMan extends GameObject {
sourceRect.right = sourceRect.left + spriteWidth;
}
public void die() {
die = true;
}
public void dirAcc(SensorEvent event) {
xAcc = event.values[0];
yAcc = event.values[1];
if (!first) {
// jezeli wartosc akcelometra jest wieksza od 1 lub -1
if (xAcc > xZero + 1) {
setDirection(GameEnum.LEFT);
} else if (xAcc < xZero - 1) {
setDirection(GameEnum.RIGHT);
} else if (yAcc > yZero + 1) {
setDirection(GameEnum.DOWN);
} else if (yAcc < yZero - 1) {
setDirection(GameEnum.UP);
}
} else {// zapamietanie pozycji poczatkowej poto aby wygodniej sie
// sterowalo
xZero = xAcc;
yZero = yAcc;
first = false;
}
}
public void stop() {
SoundStuff.sp.stop(soundID1);
}
}
@@ -2,17 +2,17 @@ package com.android.game.pacman.model;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import com.android.game.pacman.utils.GameEnum;
public class SolidObject {
protected int x;
protected int y;
protected Bitmap bitmap;
Rect boundingRect = new Rect();
protected double x;
protected double y;
static Bitmap bitmap;
public RectF boundingRect = new RectF();
protected boolean isTouched = false;
protected GameEnum kind;
public int kind;
protected boolean rotate = false;
protected double angle = 0;
public SolidObject(int x, int y){
@@ -22,12 +22,10 @@ public class SolidObject {
}
public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, x, y, null);
boundingRect = new Rect(x, y, x + bitmap.getWidth(),y + bitmap.getHeight());
}
public Rect getBoundingRect() {
return boundingRect;
canvas.drawBitmap(bitmap, (float)x, (float)y, null);
boundingRect.set((float)x, (float)y, (float)x + bitmap.getWidth(),(float)y + bitmap.getHeight());
}
public void setIsTouched(boolean b) {
isTouched = b;
@@ -37,9 +35,8 @@ public class SolidObject {
return isTouched;
}
public GameEnum getKind() {
return kind;
}
public void rotate(double angle) {
rotate = true;
this.angle = angle;
@@ -7,18 +7,24 @@ import android.media.SoundPool;
import com.adroid.game.pacman.R;
public class SoundStuff {
static SoundPool sp;
static int pacamnWalk;
public SoundStuff(Context context){
sp=new SoundPool(3,AudioManager.STREAM_MUSIC,0);
pacamnWalk = sp.load(context,R.raw.pacman_walk, 1);
}
public int getIdPacman(){
return pacamnWalk;
}
public static SoundPool sp;
public static int pacamnWalk;
public static int ghostSiren;
public static int gameStart;
public static int gameOver;
public static int pacmanCherry;
public static int pacmanEatGhost;
public SoundStuff(Context context) {
sp = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
pacamnWalk = sp.load(context, R.raw.pacman_walk, 1);
ghostSiren = sp.load(context, R.raw.pacman_siren, 1);
gameStart = sp.load(context, R.raw.pacman_start, 1);
gameOver = sp.load(context, R.raw.pacmandie, 1);
pacmanCherry = sp.load(context, R.raw.pacman_cherry, 1);
pacmanEatGhost = sp.load(context, R.raw.pacman_ghost_eating, 1);
}
}
@@ -1,5 +0,0 @@
package com.android.game.pacman.model;
public class Stage {
}
+11 -6
View File
@@ -2,18 +2,19 @@ package com.android.game.pacman.model;
public class Vect {
public int x;
public int y;
public double x;
public double y;
public Vect(int x, int y) {
public Vect(double x, double y ) {
this.x = x;
this.y = y;
}
public Vect add(Vect another) {
return new Vect(x + another.x, y + another.y);
return new Vect(x+another.x,y+another.y);
}
public Vect mulitply(int speed){
public Vect mulitply(double speed){
x=x*speed;
y=y*speed;
return this;
@@ -27,8 +28,12 @@ public class Vect {
public Vect subtract(Vect vect){
return new Vect(x-vect.x,y-vect.y);
}
public void set(double x,double y){
this.x=x;
this.y=y;
}
static public Vect normalize(Vect vect){
return new Vect((int)(vect.x/vect.getLength()), (int)(vect.y/vect.getLength()));
return new Vect((double)(vect.x/vect.getLength()), (double)(vect.y/vect.getLength()));
}
static public double dot(Vect one,Vect two){
return one.x*two.x+one.y*two.y;
@@ -1,5 +1,19 @@
package com.android.game.pacman.utils;
public enum GameEnum {
NORMAL,EASY,HARD,UP,DOWN,LEFT,RIGHT,STOP,FOOD,FOODUP,BONUS,WALL,PATH,CORNER,STRAIGHT
public class GameEnum {
public final static int NORMAL=0;
public final static int EASY=1;
public final static int HARD=2;
public final static int UP=3;
public final static int DOWN=4;
public final static int LEFT=5;
public final static int RIGHT=6;
public final static int STOP=7;
public final static int FOOD=8;
public final static int FOODUP=9;
public final static int BONUS=10;
public final static int WALL=11;
public final static int PATH=12;
public final static int CORNER=13;
public final static int STRAIGHT=14;
}