Created
July 4, 2014 19:26
-
-
Save darkwave/51b2d761f39aae5120b9 to your computer and use it in GitHub Desktop.
WIP Simple Space Shooter made with Processing
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
Game game; | |
Player player; | |
String[] imagesFilenames = new String[] { | |
"ufoRed.png", "ufoGreen.png", "ufoBlue.png", | |
"meteorGrey_tiny2.png", "meteorGrey_tiny1.png", "meteorGrey_small2.png", "meteorGrey_small1.png", "meteorGrey_med2.png", "meteorGrey_med1.png", "meteorGrey_big4.png", "meteorGrey_big3.png", "meteorGrey_big2.png", "meteorGrey_big1.png", "meteorBrown_tiny2.png", "meteorBrown_tiny1.png", "meteorBrown_small2.png", "meteorBrown_small1.png", "meteorBrown_med3.png", "meteorBrown_med1.png", "meteorBrown_big4.png", "meteorBrown_big3.png", "meteorBrown_big2.png", "meteorBrown_big1.png", | |
}; | |
String playerSkin = "playerShip1_blue.png"; | |
void setup() { | |
size(800, 600, P2D); | |
imageMode(CENTER); | |
game = new Game(); | |
for (int i = 0; i < 1000; i++) { | |
String skin = imagesFilenames[int(random(imagesFilenames.length))]; | |
Sprite s; | |
if (skin.indexOf("ufo") >= 0) | |
s = new Ufo(); | |
else | |
s = new Sprite(random(-2000, 2000), random(-2000, 2000)); | |
s.frame = game.getImage(skin); | |
game.addSprite(s); | |
} | |
player = new Player(); | |
player.frame = game.getImage(playerSkin); | |
player.direction = 90; | |
game.addSprite(player); | |
PFont ubuntuFont = createFont("Ubuntu Bold", 30); | |
textFont(ubuntuFont, 30); | |
} | |
void draw() { | |
background(0); | |
pushMatrix(); | |
translate(width / 2 - player.position.x, height / 2 - player.position.y); | |
game.run(); | |
popMatrix(); | |
fill(255, 128, 0); | |
text(frameRate, 30, 30); | |
} | |
void keyPressed() { | |
switch(keyCode) { | |
case LEFT: | |
player.buttons[player.LEFT_BUTTON] = true; | |
break; | |
case RIGHT: | |
player.buttons[player.RIGHT_BUTTON] = true; | |
break; | |
case UP: | |
player.buttons[player.UP_BUTTON] = true; | |
break; | |
} | |
} | |
void keyReleased() { | |
switch(keyCode) { | |
case LEFT: | |
player.buttons[player.LEFT_BUTTON] = false; | |
break; | |
case RIGHT: | |
player.buttons[player.RIGHT_BUTTON] = false; | |
break; | |
case UP: | |
player.buttons[player.UP_BUTTON] = false; | |
break; | |
} | |
} | |
class Game { | |
ArrayList<Sprite> sprites = new ArrayList<Sprite>(); | |
ArrayList<Sprite> newSprites = new ArrayList<Sprite>(); | |
HashMap<String, PImage> cachedImages = new HashMap<String, PImage>(); | |
boolean paused = false; | |
PImage getImage(String uri) { | |
if (cachedImages.containsKey(uri)) { | |
return cachedImages.get(uri); | |
} else { | |
PImage newImage = loadImage(uri); | |
cachedImages.put(uri, newImage); | |
return newImage; | |
} | |
} | |
void addSprite(Sprite s) { | |
newSprites.add(s); | |
} | |
void run() { | |
if (newSprites.size() > 0) { | |
for (Sprite s : newSprites) { | |
sprites.add(s); | |
} | |
newSprites.clear(); | |
} | |
if (!paused) { | |
for (Sprite s : sprites) { | |
s.update(); | |
} | |
} | |
for (Sprite s : sprites) { | |
s.display(); | |
} | |
//pruning | |
for (int index = sprites.size () - 1; index >= 0; index--) { | |
Sprite s = sprites.get(index); | |
if (s.dead) | |
sprites.remove(index); | |
} | |
} | |
} | |
class Player extends Sprite { | |
boolean[] buttons = new boolean[6]; | |
int LEFT_BUTTON = 0; | |
int RIGHT_BUTTON = 1; | |
int UP_BUTTON = 2; | |
int DOWN_BUTTON = 3; | |
int FIRE_BUTTON = 4; | |
int SHIELD_BUTTON = 5; | |
float MAX_SPEED = 10; | |
Player() { | |
super(0, 0); | |
} | |
@Override | |
void update() { | |
if (buttons[LEFT_BUTTON]) { | |
direction += 2; | |
} | |
if (buttons[RIGHT_BUTTON]) { | |
direction -= 2; | |
} | |
if (buttons[UP_BUTTON]) { | |
if (speed <= MAX_SPEED) | |
speed += 0.1; | |
} else if (speed > 0) { | |
speed -= 0.1; | |
} else if (speed < 0) { | |
speed = 0; | |
} | |
super.update(); | |
} | |
} | |
class Sprite { | |
PVector position; | |
float direction = 0; | |
float speed = 0; | |
PImage frame; | |
boolean dead = false; | |
Sprite(float x, float y) { | |
position = new PVector(x, y); | |
} | |
void update() { | |
position.x += sin(radians(direction)) * speed; | |
position.y -= cos(radians(direction)) * speed; | |
} | |
void display() { | |
pushMatrix(); | |
translate(position.x, position.y); | |
rotate(radians(direction)); | |
image(frame, 0, 0); | |
popMatrix(); | |
} | |
} | |
class Ufo extends Sprite { | |
Ufo() { | |
super(random(-2000, 2000), random(-2000, 2000)); | |
direction = random(0, 360); | |
speed = random(1, 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment