Skip to content

Instantly share code, notes, and snippets.

@keshavsaharia
Last active August 29, 2015 13:58
Show Gist options
  • Save keshavsaharia/9987807 to your computer and use it in GitHub Desktop.
Save keshavsaharia/9987807 to your computer and use it in GitHub Desktop.
Java Game Development
import Zen.*;
public class MyGame extends ZenGame {
// What code should happen when you click run?
public static void main(String[] args) {
MyGame game = new MyGame();
game.setName("My first game!");
game.setSize(800, 600);
game.run();
}
public void setup() {
// Make a sprite for your game.
MySprite self;
self = new MySprite();
// Add yourself to the game.
addSprite(self);
// How do you set up your game?
}
public void loop() {
Zen.setBackground("blue");
// What happens during each frame of your game?
}
}
public class MySprite extends ZenSprite {
// How should this sprite move at each step?
public void move() {
// Check if a key is pressed
if (Zen.isKeyPressed("left")) {
changeX(-10);
}
// Check if the mouse is clicked
if (Zen.isMouseClicked()) {
specialAttack();
}
}
// How should this sprite draw itself?
public void draw() {
Rectangle example;
example = new Rectangle(50, 40);
example.setColor("green");
example.set(getX(), getY());
Zen.draw(example);
// Your code here
}
// An example of an additional function added to MySprite
public void specialAttack() {
Circle explosion = new Circle(50);
explosion.setColor("yellow");
explosion.goto(this);
Zen.draw(explosion);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment