Last active
August 29, 2015 13:58
-
-
Save keshavsaharia/9987807 to your computer and use it in GitHub Desktop.
Java Game Development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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