android游戏开发自学笔记1 接口设计

既然有了android的工作经验,没有游戏的编程经验,就从Beginning Android Games一书开始学习吧。

前两章比较没什么知识,从第三章开始吧。慢慢写,一天一点,估计一个月以后,就会有点基础的。

作者对手机游戏分类如下:

Causal Games, Puzzle Games,Action and Arcade Games,Tower-Defense Games以及一些不能简单对其分类的游戏。

游戏设计中整体构架相关的几大组成部分:input,File I/O,Audio,Graphics.现在接触这些,感觉有点东西繁多。慢慢啃。

1.对于Graphics必须要了解的知识点

a.Of Rasters, Pixels, and Framebuffers

b.Vsync and Double-Buffering

c.Color Models

d.encoding Colors Digitally

f.Image Formats and Compression

g.Alpha Compositing and Blending


与普通android应用一样,游戏设计也是从接口设计开始。以Beginning Android Games介绍的Mr Nom为例,做下接口设计。

Input的接口如下:

package com.gamestudy.mrnom;
 
import java.util.List;
 
public interface Input {
    publicstaticclassKeyEvent {
        publicstaticfinalintKEY_DOWN = 0;
        publicstaticfinalintKEY_UP = 1;
        publicinttype;
        publicintkeyCode;
        publiccharkeyChar;
    }
     
    publicstaticclassTouchEvent {
        publicstaticfinalintTOUCH_DOWN = 0;
        publicstaticfinalintTOUCH_UP = 1;
        publicstaticfinalintTOUCH_DRAGGED = 2;
        publicinttype;
        publicintx, y;
        publicintpointer;
     }
     
    publicbooleanisKeyPressed(intkeyCode);
    publicbooleanisTouchDown(intpointer);
    publicintgetTouchX(intpointer);
    publicintgetTouchY(intpointer);
    publicfloatgetAccelX();
    publicfloatgetAccelY();
    publicfloatgetAccelZ();
    publicList<KeyEvent> getKeyEvents();
    publicList<TouchEvent> getTouchEvents();
}


FileIO接口:

package com.gamestudy.mrnom;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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的三个接口Sound,Music,Audio:

Audio.java
package com.gamestudy.mrnom;
public interface Audio {
    public Music newMusic(String filename);
    public Sound newSound(String filename);
}

Music.java
package com.gamestudy.mrnom;
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();
}


Sound.java

package com.gamestudy.mrnom;
public interface Sound {
    public void play(float volume);
    public void dispose();
}


Graphics相关的两个接口:

Graphics.java

package com.gamestudy.mrnom;
public interface Graphics {
    public static enum PixmapFormat {
        ARGB8888, ARGB4444, 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();
}
Pixmap.java
package com.gamestudy.mrnom;
import com.gamestudy.mrnom.Graphics.PixmapFormat;
public interface Pixmap {
    public int getWidth();
    public int getHeight();
    public PixmapFormat getFormat();
    public void dispose();
}


Game和Screen接口

Game.java

package com.gamestudy.mrnom;
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();
}


Screen.java
package com.gamestudy.mrnom;
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();
}


对接口利用的示例类:

package com.gamestudy.mrnom;

import com.gamestudy.mrnom.Graphics.PixmapFormat;

public class MySuperAwesomeStartScreen extends Screen {
    Pixmap awesomePic;
    int x;
    public MySuperAwesomeStartScreen(Game game) {
    super(game);
    awesomePic = game.getGraphics().newPixmap("data/pic.png",
    PixmapFormat.RGB565);
    }
    @Override
    public void update(float deltaTime) {
    x += 1;
    if (x > 100)
    x = 0;
    }
    @Override
    public void present(float deltaTime) {
    game.getGraphics().clear(0);
    game.getGraphics().drawPixmap(awesomePic, x, 0, 0, 0,
    awesomePic.getWidth(), awesomePic.getHeight());
    }
    @Override
    public void pause() {
    // nothing to do here
    }
    @Override
    public void resume() {
    // nothing to do here
    }
    @Override
    public void dispose() {
    }
}

学习到这里有些停滞了。因为程序最重要的是运行。目前的接口似乎都已经全了,就连最具有启动特性的MySuperAwesomeStartScreen类都有了。但是,如何启动它书里没有详细说明。这个需要摸索。文章写道这里,如果运行成功了,再把怎样运行的写上来。

这算是学习游戏遇到的第一个难题,如何运行已经写好的接口和类。

接上所述,翻看了这本书的后面章节,原来这些类仅仅是游戏的framework部分。还不足以使程序运行。所以,不可能通过简单地在activity实现几个接口,然后调用那个类就可以运行起来的。看来,要等到第6章左右才能运行。与我而言,那就最迟在自学笔记3里实现运行吧。这篇笔记就到此为止吧。

你可能感兴趣的:(android游戏开发自学笔记1 接口设计)