Code Monkey home page Code Monkey logo

android_simple_game_engine's Introduction

android_simple_game_engine

Simple 2D Android Game Framework.

Warning

This repository is not currently maintained and is a simple proof of concept. Feel free to improve this code but keep in mind that this is likely not to work for anything beyond the most of basic use cases.

Usage

Change the "main" game activity to extend from GameActivity:

public class MyGameActivity extends GameActivity {
  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyGamePanel panel = new MyGamePanel(this);
	 	setContentView(panel);
	}

}

GamePanel

Create the game panel which will manage the various aspects of your game (input => update state => render):

public class MyGamePanel extends AbstractGamePanel {
	public MyGamePanel(Context context) {
		super(context);
	}

	// This method is called when the game first launches. Use this to
	// initialize variables and set starting values.
	public void onStart() {
		// Initialize your game
	}

	// This method is called once a "tick" to redraw the canvas,
	// so you can do things like draw the game actors.
	public void redrawCanvas(Canvas canvas) {
		// Draw things here using the canvas
	}

	// This method is called once a "tick", and it is a good place to
	// implement the game logic.
	public void onTimer() {
		// Game Logic Here
	}

	// This method is called whenever a keyboard button is pressed
	// within your game. The keyCode represents the key the actual key pushed.
	// You can check which keyCode using 'KeyEvent' constants.
	// i.e keyCode == KeyEvent.KEYCODE_N
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d("GameActivity", keyCode);
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log.d("GameActivity", event.getX() + ", " + event.getY());
    return true;
	}
}

Creating your Actors

Anything object nside a game world that behaviors or logic is called an Actor. A player or an enemy or a ship, et al. To create an actor, simply create a class extending from one of the actor base types. There are a few base types available for an Actor:

  • Actor - Simplest type which provides "enabled" and "visible" properties as well as a few key methods.
  • SimpleMovingActor - Basic actor with x+y coordinates, width and height and velocity supported.
  • SpriteMovingActor - Extends from SimpleMovingActor, bitmap based (rather than painted) actors.
  • AnimatedMovingActor- Extends from SpriteMovingActor, animated (multiple frame) sprite based actors.

Creating the actor just looks like:

public class HeroActor extends SimpleMovingActor {
	public HeroActor(int x, int y) {
    // x, y and then width,height of actor
		super(x, y, 25, 25);
	}
  
  // Setup the color and style of the "paint" for the actor.
	@Override
	public void stylePaint(Paint paint) {
		paint.setColor(Color.GREEN);
		paint.setStyle(Paint.Style.FILL);
		// p.setStyle(Paint.Style.STROKE);
		// p.setStrokeWidth(5);
	}
  
  // Specify how to draw the actor on the panel
  // getRect() returns the rectangle containing the actor
  // getPaint() returns the primary paint to use when drawing the actor
	@Override
	public void draw(Canvas canvas) {
	  canvas.drawRect(getRect(), getPaint());
	}
}

You can then add an actor to the panel:

public class MyGamePanel extends AbstractGamePanel {

  HeroActor player;

	public MyGamePanel(Context context) {
		super(context);
	}

	// This method is called when the game first launches. Use this to
	// initialize variables and set starting values.
	public void onStart() {
    // sets initial position of actor
		player = new HeroActor(50, 50);
	}

	// This method is called once a "tick" to redraw the canvas,
	// so you can do things like draw the game actors.
	public void redrawCanvas(Canvas canvas) {
		player.draw(canvas);
	}

	// This method is called once a "tick", and it is a good place to
	// implement the game logic.
	public void onTimer() {
		if (player.something()) {
      // do something
    } else {
      // something else
    }
	}

}

Now the actor will be painted on the board.

android_simple_game_engine's People

Contributors

nesquena avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.