android游戏开发自学笔记4 Activity相关

第一个游戏Mr.Nom 的最终实现:

Assets.java
package com.gamestudy.mrnom;
import com.gamestudy.mrnom.framework.Pixmap;
import com.gamestudy.mrnom.framework.Sound;
public class Assets {
    public static Pixmap background;
    public static Pixmap logo;
    public static Pixmap mainMenu;
    public static Pixmap buttons;
    public static Pixmap help1;
    public static Pixmap help2;
    public static Pixmap help3;
    public static Pixmap numbers;
    public static Pixmap ready;
    public static Pixmap pause;
    public static Pixmap gameOver;
    public static Pixmap headUp;
    public static Pixmap headLeft;
    public static Pixmap headDown;
    public static Pixmap headRight;
    public static Pixmap tail;
    public static Pixmap stain1;
    public static Pixmap stain2;
    public static Pixmap stain3;
    public static Sound click;
    public static Sound eat;
    public static Sound bitten;
}

Settings.java
package com.gamestudy.mrnom;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.gamestudy.mrnom.framework.FileIO;

public class Settings {
	public static boolean soundEnabled = true;
	public static int[] highscores = new int[] { 100, 80, 50, 30, 10 };
	public static void load(FileIO files) {
		BufferedReader in = null;
		try {
		in = new BufferedReader(new InputStreamReader(
		files.readFile(".mrnom")));
		soundEnabled = Boolean.parseBoolean(in.readLine());
		for (int i = 0; i < 5; i++) {
		highscores[i] = Integer.parseInt(in.readLine());
		}
		} catch (IOException e) {
		// :( It's ok we have defaults
		} catch (NumberFormatException e) {
		// :/ It's ok, defaults save our day
		} finally {
		try {
		if (in != null)
		in.close();
		} catch (IOException e) {
		}
		}
	}

	public static void save(FileIO files) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(files.writeFile(".mrnom")));
			out.write(Boolean.toString(soundEnabled));
			for (int i = 0; i < 5; i++) {
				out.write(Integer.toString(highscores[i]));
			}
		} catch (IOException e) {
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
			}
		}// end of finally
	}
	
	public static void addScore(int score) {
		for (int i = 0; i < 5; i++) {
			if (highscores[i] < score) {
				for (int j = 4; j > i; j--)
				highscores[j] = highscores[j - 1];
				highscores[i] = score;
				break;
			}
		}
	}

}


LoadingScreen.java
package com.gamestudy.mrnom;

import com.gamestudy.mrnom.framework.Game;
import com.gamestudy.mrnom.framework.Graphics;
import com.gamestudy.mrnom.framework.Graphics.PixmapFormat;
import com.gamestudy.mrnom.framework.Screen;

public class LoadingScreen extends Screen {
	public LoadingScreen(Game game) {
		super(game);
	}

	@Override
	public void update(float deltaTime) {
	Graphics g = game.getGraphics();
	Assets.background = g.newPixmap("background.png", PixmapFormat.RGB565);
	Assets.logo = g.newPixmap("logo.png", PixmapFormat.ARGB4444);
	Assets.mainMenu = g.newPixmap("mainmenu.png", PixmapFormat.ARGB4444);
	Assets.buttons = g.newPixmap("buttons.png", PixmapFormat.ARGB4444);
	Assets.help1 = g.newPixmap("help1.png", PixmapFormat.ARGB4444);
	Assets.help2 = g.newPixmap("help2.png", PixmapFormat.ARGB4444);
	Assets.help3 = g.newPixmap("help3.png", PixmapFormat.ARGB4444);
	Assets.numbers = g.newPixmap("numbers.png", PixmapFormat.ARGB4444);
	Assets.ready = g.newPixmap("ready.png", PixmapFormat.ARGB4444);
	Assets.pause = g.newPixmap("pausemenu.png", PixmapFormat.ARGB4444);
	Assets.gameOver = g.newPixmap("gameover.png", PixmapFormat.ARGB4444);
	Assets.headUp = g.newPixmap("headup.png", PixmapFormat.ARGB4444);
	Assets.headLeft = g.newPixmap("headleft.png", PixmapFormat.ARGB4444);
	Assets.headDown = g.newPixmap("headdown.png", PixmapFormat.ARGB4444);
	Assets.headRight = g.newPixmap("headright.png", PixmapFormat.ARGB4444);
	Assets.tail = g.newPixmap("tail.png", PixmapFormat.ARGB4444);
	Assets.stain1 = g.newPixmap("stain1.png", PixmapFormat.ARGB4444);
	Assets.stain2 = g.newPixmap("stain2.png", PixmapFormat.ARGB4444);
	Assets.stain3 = g.newPixmap("stain3.png", PixmapFormat.ARGB4444);
	Assets.click = game.getAudio().newSound("click.ogg");
	Assets.eat = game.getAudio().newSound("eat.ogg");
	Assets.bitten = game.getAudio().newSound("bitten.ogg");
	Settings.load(game.getFileIO());
	game.setScreen(new MainMenuScreen(game));
	}

	@Override
	public void present(float deltaTime) {
	}
	@Override
	public void pause() {
	}
	@Override
	public void resume() {
	}
	@Override
	public void dispose() {
	}

}

package com.gamestudy.mrnom;

import java.util.List;

import com.gamestudy.mrnom.framework.Game;
import com.gamestudy.mrnom.framework.Graphics;
import com.gamestudy.mrnom.framework.Input.TouchEvent;
import com.gamestudy.mrnom.framework.Screen;

public class MainMenuScreen  extends Screen {
	public MainMenuScreen(Game game) {
		super(game);
	}

	@Override
	public void update(float deltaTime) {
	Graphics g = game.getGraphics();
	List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
	game.getInput().getKeyEvents();
	int len = touchEvents.size();
	for(int i = 0; i < len; i++) {
		TouchEvent event = touchEvents.get(i);
		if(event.type == TouchEvent.TOUCH_UP) {
		if(inBounds(event, 0, g.getHeight() - 64, 64, 64)) {
		Settings.soundEnabled = !Settings.soundEnabled;
		if(Settings.soundEnabled)
		Assets.click.play(1);
		}
		if(inBounds(event, 64, 220, 192, 42) ) {
		game.setScreen(new GameScreen(game));
		if(Settings.soundEnabled)
		Assets.click.play(1);
		return;
		}
		if(inBounds(event, 64, 220 + 42, 192, 42) ) {
		game.setScreen(new HighscoreScreen(game));
		if(Settings.soundEnabled)
		Assets.click.play(1);
		return;
		}
		if(inBounds(event, 64, 220 + 84, 192, 42) ) {
		game.setScreen(new HelpScreen(game));
		if(Settings.soundEnabled)
		Assets.click.play(1);
		return;
		}
		}
		}
		}

	private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
		if(event.x > x && event.x < x + width - 1 &&
		event.y > y && event.y < y + height - 1)
		return true;
		else
		return false;
		}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.background, 0, 0);
		g.drawPixmap(Assets.logo, 32, 20);
		g.drawPixmap(Assets.mainMenu, 64, 220);
		if(Settings.soundEnabled)
			g.drawPixmap(Assets.buttons, 0, 416, 0, 0, 64, 64);
		else
			g.drawPixmap(Assets.buttons, 0, 416, 64, 0, 64, 64);
	}

	@Override
	public void pause() {
		Settings.save(game.getFileIO());
	}

	@Override
	public void resume() {
	}
	@Override
	public void dispose() {
	}

}


package com.gamestudy.mrnom;

import java.util.List;

import com.gamestudy.mrnom.framework.Game;
import com.gamestudy.mrnom.framework.Graphics;
import com.gamestudy.mrnom.framework.Input.TouchEvent;
import com.gamestudy.mrnom.framework.Screen;

public class HelpScreen extends Screen {
	public HelpScreen(Game game) {
		super(game);
		}
		@Override
		public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		int len = touchEvents.size();
		for(int i = 0; i < len; i++) {
		TouchEvent event = touchEvents.get(i);
		if(event.type == TouchEvent.TOUCH_UP) {
		if(event.x > 256 && event.y > 416 ) {
		game.setScreen(new HelpScreen2(game));
		if(Settings.soundEnabled)
		Assets.click.play(1);
		return;
		}
		}
		}
		}
		@Override
		public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.background, 0, 0);
		g.drawPixmap(Assets.help1, 64, 100);
		g.drawPixmap(Assets.buttons, 256, 416, 0, 64, 64, 64);
		}
		@Override
		public void pause() {
		}
		@Override
		public void resume() {
		}
		@Override
		public void dispose() {
		}

}

HelpScreen2.java

package com.gamestudy.mrnom;

import java.util.List;
import com.gamestudy.mrnom.framework.Game;
import com.gamestudy.mrnom.framework.Graphics;
import com.gamestudy.mrnom.framework.Input.TouchEvent;
import com.gamestudy.mrnom.framework.Screen;

public class HelpScreen2 extends Screen {
	 public HelpScreen2(Game game) {
	        super(game);
	        // TODO Auto-generated constructor stub
	    }

	    @Override
	    public void update(float deltaTime) {
	        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
	        game.getInput().getKeyEvents();
	        
	        Graphics g = game.getGraphics();
	        int len = touchEvents.size();
	        for(int i = 0; i < len; i++) {
	            TouchEvent event = touchEvents.get(i);
	            if(event.type == TouchEvent.TOUCH_UP) {
	                if(event.x > g.getWidth() - 64 && event.y > g.getHeight() - 64 ) {
	                    game.setScreen(new HelpScreen3(game));
	                    if(Settings.soundEnabled)
	                        Assets.click.play(1);
	                    return;
	                }
	            }
	        }
	    }

	    @Override
	    public void present(float deltaTime) {
	        Graphics g = game.getGraphics();      
	        g.drawPixmap(Assets.background, 0, 0);
	        g.drawPixmap(Assets.help2, 64, 100);
	        g.drawPixmap(Assets.buttons, 256, 416, 0, 64, 64, 64);
	    }

	    @Override
	    public void pause() {
	        // TODO Auto-generated method stub

	    }

	    @Override
	    public void resume() {
	        // TODO Auto-generated method stub

	    }

	    @Override
	    public void dispose() {
	        // TODO Auto-generated method stub

	    }
}

HelpScreen3.java
package com.gamestudy.mrnom;

import java.util.List;

import com.gamestudy.mrnom.framework.Game;
import com.gamestudy.mrnom.framework.Graphics;
import com.gamestudy.mrnom.framework.Input.TouchEvent;
import com.gamestudy.mrnom.framework.Screen;

public class HelpScreen3 extends Screen {
    
    public HelpScreen3(Game game) {
        super(game);
    }
    
    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x > 256 && event.y > 416 ) {
                    game.setScreen(new MainMenuScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();      
        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.help3, 64, 100);
        g.drawPixmap(Assets.buttons, 256, 416, 0, 128, 64, 64);
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }
}
一帖代码,文章铁定长,再起一篇继续吧。无奈。

你可能感兴趣的:(android游戏开发自学笔记4 Activity相关)