`

《Beginning Android Games》Chapter3给出的基本框架

阅读更多

一个游戏的基础框架大致分为这几个部分:

1 Window management

2 Input

3 File I/O

4 Graphics

5 Audio

6 Game framework

 

之后书中给出了具体每一部分接口的定义,如下:

Input接口

 

public interface Input {
	public static class KeyEvent{
		public static final int KEY_DOWN=0;
		public static final int KEY_UP=1;
	
		public int type;
		public int keyCode;
		public char keyChar;
	}
	
	public static class TouchEvent{
		public static final int TOUCH_DOWN=0;
		public static final int TOUCH_UP=1;
		public static final int TOUCH_DRAGGED=2;
		
		public int type;
		public int x,y;
		public int pointer;
	}
	
	public boolean isKeyPressed(int keyCode);
	
	public boolean isTouchDown(int pointer);
	
	public int getTouchX(int pointer);
	
	public int getTouchY(int pointer);
	
	public float getAccelX();
	
	public float getAccelY();
	
	public float getAccelZ();
	
	public List<KeyEvent> getKeyEvents();
	
	public List<TouchEvent> getTouchEvents();
}

 

 

FileIO接口

 

public interface FileIO {
	public InputStream readAsset(String fileName) throws IOException;
	
	public InputStream readFile(String fileName) throws IOException;
	
	public OutputStream writeFile(String fileName) throws IOException;
}

 

 

Audio接口 其中还包含了Music和Sound的接口定义

 

public interface Audio {
	public Music newMusic(String filename);
	
	public Sound newSound(String filename);
}
 

 

public interface Music {
	public void play();
	
	public void stop();
	
	public void pause();
	
	public void setLooping(boolean looping);
	
	public void setVolume(float volume);
	
	public boolean isPlaying();
	
	public boolean isStopped();
	
	public boolean isLooping();
	
	public void dispose();
}

 

 

 

public interface Sound {
	public void play(float volume);
	
	public void dispose();
}

 

 

Graphics接口的定义 包括Pixmap

 

public interface Graphics {
	public static enum PixmapFormat {
		ARGB8888, ARGB84444, RGB565
	}

	public Pixmap newPixmap(String fileName, PixmapFormat format);

	public void clear(int color);

	public void drawPixel(int x, int y, int color);

	public void drawLine(int x, int y, int x2, int y2, int color);

	public void drawRect(int x, int y, int width, int height, int color);

	public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY,
			int srcWidth, int srcHeight);
	
	public void drawPixmap(Pixmap pixmap,int x,int y);
	
	public int getWidth();
	
	public int getHeight();
}

 

public interface Pixmap {
	public int getWidth();
	
	public PixmapFormat getFormat();
	
	public void dispose();
}

 

最后是Game接口的定义 以及抽象类Screen

public interface Game {
	public Input getInput();
	
	public FileIO getFileIO();
	
	public Graphics getGraphics();
	
	public Audio getAudio();
	
	public void setScreen(Screen screen);
	
	public Screen getCurrentScreen();
	
	public Screen getStartScreen();
}

 

public abstract class Screen {
	protected final Game game;
	
	public Screen(Game game){
		this.game=game;
	}
	//更新状态
	public abstract void update(float deltaTime);
	//刷新画面
	public abstract void present(float deltaTime);
	
	public abstract void pause();
	
	public abstract void resume();
	
	public abstract void dispose();
}

 

以上就基本实现了最简单的框架 觉得还是比较清晰的 所以照搬记录一下

 

最后Chapter3还提到了Frame Rate–Independent Movement 也就是固定帧数的问题 

例如若每次循环移动1个像素点 在60FPS的情况下 1秒就移动了60个像素 而30FPS则移动了30个像素 这样移动的速度相当于提升了2倍 作者给出的解决方案是根据deltaTime 计算应该移动的实际距离 也就是从x+=1 改成x+=30*deltaTime 30是希望的每秒移动像素值

 ————————————题外话——————————

再想到之前看到的一个介绍 游戏基本是一个循环 :  更新状态——重新绘制——休眠——更新状态

而其中的休眠时间并不是一个固定值 而是动态计算的 休眠长度=理想值-处理用去的时间 其实也跟上面的思路类似

 

0
0
分享到:
评论

相关推荐

    Beginning Android Games.pdf

    Beginning Android Games.pdf

    Beginning Android Games 3rd Edition.pdf

    Beginning Android Games, Third Edition gives you everything you need to branch out and write your own Android games for a variety of hardware. Do you have an awesome idea for the next break-through ...

    Beginning Android Games

    "Beginning Android Games, Second Edition offers everything you need to join the ranks of successful Android game developers, including Android tablet game app development considerations. You'll start...

    Beginning Android Games 3rd Edition

    Android. While some of the material may be old news for you, there are still a lot of tips and hints contained here that should make reading this book worthwhile. Android is a strange beast at times, ...

    Beginning Android Games - Mario Zechner

    开始Android的游戏提供一切您需要加入Android的游戏开发成功的行列。你会开始与游戏设计基础和编程的基础,然后逐步实现建立自己的基本的游戏引擎和游戏可玩。这会给你一切你需要另辟蹊径,写自己的Andr​​oid游戏...

    Beginning Android Games(Apress,3ed,2015)

    Beginning Android Games, Third Edition gives you everything you need to branch out and write your own Android games for a variety of hardware. Do you have an awesome idea for the next break-through ...

    Beginning Android Games [3rd Edition,2016]

    Beginning Android Games English | 6 Jan. 2017 | ISBN: 1484204735 | 636 Pages | PDF | 11.87 MB Learn all of the basics needed to join the ranks of successful Android game developers. You'll start with...

    Beginning Android Games 2012

    Beginning Android Games 2012 (新版)

    Beginning Android Games, 2nd Edition

    Beginning Android Games, 2nd Edition

    Beginning Android 3D Game Development

    Beginning Android 3D Game Development is a unique book for today’s Android and game app developers who want to learn how to build 3D game apps that run on the latest Android KitKat platform using ...

    beginning android games

    beginning android games

    android.开发书籍 高清PDF Beginning Android Games

    android.开发书籍 高清PDF Beginning Android Games

    Beginning Android Games(3rd) 无水印pdf 0分

    Beginning Android Games(3rd) 英文无水印pdf 第3版 pdf使用FoxitReader和PDF-XChangeViewer测试可以打开

    Beginning Android 4 Games Development

    Beginning Android 4 Games Development offers everything you need to join the ranks of successful Android game developers. You'll start with game design fundamentals and programming basics, and then ...

    Beginning Android Games( Android 游戏开发入门)

    这会给你一切你需要另辟蹊径,写自己的Android游戏。 这本书将指导制作为Android平台的几个示例游戏的过程,并涉及广泛的议题: &gt;游戏开发的基础 &gt;Android平台的基础到应用的背景下做一个游戏的基础 的2D和3D...

    Beginning Android Games 1

    * Android平台的基础,适用于那些在游戏中基本面 * 2D和3D游戏的设计和他们在Android平台上的成功实施 你将学到什么 *如何开发你的第一个Android应用程序的设置和使用的开发工具 *在Android平台中的游戏编程的基础...

    Beginning Android

    Beginning Android Beginning Android Beginning Android

    Beginning Android Games 2

    * Android平台的基础,适用于那些在游戏中基本面 * 2D和3D游戏的设计和他们在Android平台上的成功实施 你将学到什么 *如何开发你的第一个Android应用程序的设置和使用的开发工具 *在Android平台中的游戏编程的基础...

    Android 4 游戏开发入门(Beginning Android 4 Games Development)

    Beginning Android 4 Games Development offers everything you need to join the ranks of successful Android game developers. You'll start with game design fundamentals and programming basics, and then ...

Global site tag (gtag.js) - Google Analytics