Last active
August 29, 2015 14:01
-
-
Save ecounysis/71104c448ba955c2111d to your computer and use it in GitHub Desktop.
the beginnings of a weird little game in java
This file contains hidden or 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 java.awt.*; | |
import javax.swing.*; | |
import java.awt.image.*; | |
import java.awt.event.*; | |
import java.awt.geom.*; | |
import java.util.Random; | |
public class AShape | |
{ | |
public int scale; | |
int speed; | |
public int height; | |
public int width; | |
public boolean dead; | |
public Color color; | |
int[] vector; | |
int[][] deathAnimation; | |
public void setVector(int[] vector) | |
{ | |
this.vector = vector; | |
} | |
public int[] getVector() | |
{ | |
return this.vector; | |
} | |
public void setDeathAnimation(int[][] vector) | |
{ | |
this.deathAnimation = vector; | |
} | |
int[][] getDeathAnimation() | |
{ | |
return this.deathAnimation; | |
} | |
int initX, initY; | |
public Color deathColor; | |
public AShape(int x, int y, int scale, int width, int height) // only supports rectangles | |
{ | |
vector = new int[width*height]; | |
deathAnimation = new int[1][width*height]; | |
speed = scale; | |
this.width = width; | |
this.height = height; | |
this.scale = scale; | |
color = Color.BLACK; | |
deathColor = Color.RED; | |
dead = false; | |
motion = STILL; | |
this.x = x; | |
initX = x; | |
this.y = y; | |
initY = y; | |
dead_vector = -1; | |
} | |
int dead_vector; | |
int UP=0, DOWN=1, LEFT=2, RIGHT=3, STILL=-1; | |
Color TRANSPARENT = new Color(0,0,0,0); | |
int motion; | |
public int x; | |
public int y; | |
int getScale() | |
{ | |
return scale; | |
} | |
void setScale(int sz) | |
{ | |
scale = sz; | |
speed = speed*sz; | |
} | |
void moveUP() | |
{ | |
motion = UP; | |
} | |
void moveDOWN() | |
{ | |
motion = DOWN; | |
} | |
void increaseSpeed() | |
{ | |
if (speed >= scale*3) speed = scale*3; // upper limit | |
else if (speed >=scale) speed += scale; | |
else if (speed >= 0) speed +=1; | |
else if (speed < 0) speed = 0; | |
} | |
void decreaseSpeed() | |
{ | |
if (speed <= 0) speed = 0; | |
else if (speed <= scale) speed -= 1; | |
else if (speed <= scale*3) speed -= scale; | |
else if (speed > scale*3) speed = scale*3; | |
} | |
void kill() | |
{ | |
dead = true; | |
} | |
void resurrect() | |
{ | |
dead = false; | |
dead_vector = -1; | |
speed = scale; | |
x = initX; | |
y = initY; | |
} | |
void moveRIGHT() | |
{ | |
motion = RIGHT; | |
} | |
void moveLEFT() | |
{ | |
motion = LEFT; | |
} | |
void moveSTOP() | |
{ | |
motion = STILL; | |
} | |
public void move(int rows, int cols) | |
{ | |
if (motion==UP) { | |
y -= speed; | |
} | |
if (motion==DOWN) { | |
y += speed; | |
} | |
if (motion==RIGHT) { | |
x += speed; | |
} | |
if (motion==LEFT) { | |
x -= speed; | |
} | |
// let's not let the shape move outside the boudaries of the world | |
if (y < 0) y = 0; | |
if (x < 0) x = 0; | |
if (y > rows-height*scale) y = rows - height*scale; | |
if (x > cols-width*scale) x = cols - width*scale; | |
} | |
public BufferedImage draw(int rows, int cols) | |
{ | |
int[] g; | |
Color cl=color; | |
if (!dead) { | |
move(rows, cols); | |
g = vector; | |
} | |
else if (dead_vector+1 < this.deathAnimation.length) { | |
cl=deathColor; | |
dead_vector+=1; | |
g=deathAnimation[dead_vector]; | |
} | |
else { | |
g=new int[width*height]; | |
} | |
BufferedImage img = new BufferedImage(width*scale, height*scale, BufferedImage.TYPE_4BYTE_ABGR); | |
Graphics2D gr2 = (Graphics2D)img.getGraphics(); | |
for (int i = 0; i < g.length; i++) { // loop through the array | |
Color clr = TRANSPARENT; | |
if (g[i]==1) gr2.setColor(cl); | |
if (g[i]==0) gr2.setColor(TRANSPARENT); | |
int x=(i%width)*scale; | |
int y=(i/width)*scale; | |
gr2.fillRect(x,y,scale,scale); | |
} | |
return img; | |
} | |
} |
This file contains hidden or 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 java.awt.*; | |
import javax.swing.*; | |
import java.awt.image.*; | |
import java.awt.event.*; | |
import java.awt.geom.*; | |
import java.util.Random; | |
public class Creepers extends JFrame | |
{ | |
static int HEIGHT=600; | |
static int WIDTH=800; | |
private static boolean[] keys = new boolean[65536]; | |
/** | |
* This seems to be the smallest way of reading input events to the JFrame. | |
* ~notch | |
*/ | |
public void processEvent(AWTEvent e) | |
{ | |
switch (e.getID()) | |
{ | |
case WindowEvent.WINDOW_CLOSING: | |
System.exit(0); | |
case KeyEvent.KEY_PRESSED: | |
case KeyEvent.KEY_RELEASED: | |
keys[((KeyEvent)e).getKeyCode()] = e.getID() == KeyEvent.KEY_PRESSED; | |
} | |
} | |
public static void main(String[] args) throws Throwable | |
{ | |
Graphics gr; // Graphics object for Creepers (JFrame) | |
Image img2; // image of same size as gr | |
Graphics2D gr2; // Graphics2D of img2 | |
int score = 0; | |
BufferedImage background; | |
Color blue = new Color(0x00,0xcc,0xff); | |
Color gold = new Color(0xff, 0xff, 0x33); | |
Color black = new Color(0,0,0); | |
BufferedImage bg; | |
Graphics2D bgGr2; | |
Creepers a = new Creepers(); | |
a.enableEvents(KeyEvent.KEY_PRESSED); | |
a.setSize(WIDTH, HEIGHT); | |
a.setResizable(false); | |
a.setVisible(true); | |
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
gr = a.getGraphics(); // Graphics object for Creepers (JFrame) | |
//BufferedImage img = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); | |
//int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData(); | |
img2= a.createImage(a.getWidth(), a.getHeight()); // image of same size as gr | |
gr2= (Graphics2D) img2.getGraphics(); // Graphics2D of img2 | |
AShape creeper= new AShape(40,40,6,6,6); | |
AShape[] diamonds = new AShape[50]; | |
Random rand=new Random(); | |
for (int i=0; i<diamonds.length; i++) { | |
diamonds[i]=getDiamond(rand.nextInt(a.getWidth()), rand.nextInt(a.getHeight()), i<diamonds.length*2/3? gold: blue); | |
} | |
AShape[] deadDiamonds = new AShape[10]; | |
for (int i=0; i<deadDiamonds.length; i++) { | |
int yy=rand.nextInt(a.getHeight()); | |
int xx=rand.nextInt(a.getWidth()); | |
deadDiamonds[i]=getDiamond(xx,yy,black); | |
} | |
// create a single background | |
int size = 12; | |
Color[] colors= {new Color(0, 0xcc, 0x33), new Color(0x33, 0x99, 0x33), new Color(0x33, 0xcc, 0x33), new Color(0x66, 0xff, 0x66)}; | |
background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); | |
bgGr2 = (Graphics2D) background.getGraphics(); | |
for (int x=0; x<WIDTH; x+=size) | |
{ | |
for (int y=0; y<HEIGHT; y+=size) | |
{ | |
Color color = colors[0]; | |
float r=new Random().nextFloat(); | |
if (r>0.2) color=colors[1]; | |
if (r>0.6) color=colors[2]; | |
if (r>0.8) color=colors[3]; | |
bgGr2.setColor(color); | |
bgGr2.fillRect(x,y,size,size); | |
} | |
} | |
BufferedImage bgCopy = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); | |
// end background | |
// get diamonds | |
gr2=(Graphics2D)background.getGraphics(); | |
for (int i=0; i<diamonds.length; i++) { | |
AShape d = diamonds[i]; | |
} | |
// end diamonds | |
// set up creeper graphics | |
int[] v = {1,1,0,0,1,1, | |
1,1,0,0,1,1, | |
0,0,1,1,0,0, | |
0,1,1,1,1,0, | |
0,1,1,1,1,0, | |
0,1,0,0,1,0}; | |
creeper.setVector(v); | |
int[][] da = {{1,1,0,0,1,1, | |
1,1,0,0,1,1, | |
0,0,1,1,0,0, | |
0,1,0,0,1,0, | |
0,1,1,1,1,0, | |
0,1,0,0,1,0}, | |
{0,1,0,0,1,0, | |
1,1,1,1,1,1, | |
1,1,0,1,1,0, | |
0,1,1,1,1,0, | |
0,1,0,1,1,0, | |
0,1,1,0,1,0}, | |
{1,1,0,0,1,1, | |
1,1,1,0,1,1, | |
0,0,1,1,0,0, | |
0,1,0,1,1,0, | |
0,1,1,0,1,0, | |
0,1,1,1,1,0}, | |
{0,0,0,0,0,0, | |
0,0,0,1,0,0, | |
0,0,1,0,0,0, | |
0,1,0,0,1,0, | |
0,1,1,1,1,0, | |
1,1,0,0,1,1}, | |
{1,0,0,0,0,1, | |
0,0,0,0,0,0, | |
0,0,1,1,0,0, | |
0,0,0,0,0,0, | |
1,1,0,0,1,1, | |
1,1,1,1,1,1} | |
}; | |
creeper.setDeathAnimation(da); | |
// end creeper graphics | |
for (int i=0; i<diamonds.length; i++) { | |
if (diamonds[i].dead) gr2.drawImage(diamonds[i].draw(HEIGHT, WIDTH), null, diamonds[i].x, diamonds[i].y); | |
} | |
for (int i=0; i<deadDiamonds.length; i++) { | |
if (deadDiamonds[i].dead) gr2.drawImage(deadDiamonds[i].draw(HEIGHT, WIDTH), null, deadDiamonds[i].x, deadDiamonds[i].y); | |
} | |
long startTime = System.currentTimeMillis(); | |
long lastTime = startTime; | |
long nextTime = startTime; | |
float f = 1; | |
while (true) | |
{ | |
// only get one random number every second | |
if (lastTime < System.currentTimeMillis()) { | |
f=rand.nextFloat(); | |
lastTime+=1000; | |
} | |
if (keys[37]) creeper.moveLEFT(); // Left arrow | |
if (keys[39]) creeper.moveRIGHT(); // Right arrow | |
if (keys[38]) creeper.moveUP(); // Up arrow | |
if (keys[40]) creeper.moveDOWN(); // Down arrow | |
if (keys[83]) { | |
creeper.decreaseSpeed(); // S | |
if (f < 0.01) creeper.kill(); | |
} | |
if (keys[70]) { | |
creeper.increaseSpeed(); // F | |
if (f < 0.05) creeper.kill(); | |
} | |
if (keys[27]) creeper.kill(); // ESC | |
if (keys[81]) creeper.resurrect(); // Q | |
if (!keys[37] && !keys[39] && !keys[38] && !keys[40]) creeper.moveSTOP(); | |
//for (int i=0;i<50000;i++) { | |
// if (keys[i]) System.out.println(i); | |
//} | |
// render | |
BufferedImage bg2 = new BufferedImage(background.getWidth(), background.getHeight(), background.getType()); | |
bg2.setData(background.getData()); | |
gr2 = (Graphics2D) bg2.getGraphics(); | |
for (int i=0; i<diamonds.length; i++) { | |
// collision detection | |
if ((!diamonds[i].dead) && | |
(creeper.x+creeper.scale*creeper.width >= diamonds[i].x) && | |
(creeper.y+creeper.scale*creeper.height >= diamonds[i].y) && | |
(diamonds[i].x+diamonds[i].width*diamonds[i].scale>=creeper.x) && | |
(diamonds[i].y+diamonds[i].height*diamonds[i].scale>=creeper.y)) { | |
diamonds[i].dead = true; | |
score+=5; | |
} | |
if (!diamonds[i].dead) gr2.drawImage(diamonds[i].draw(HEIGHT, WIDTH), null, diamonds[i].x, diamonds[i].y); | |
} | |
for (int i=0; i<deadDiamonds.length; i++) { | |
// collision detection | |
if ((!deadDiamonds[i].dead) && | |
(creeper.x+creeper.scale*creeper.width >= deadDiamonds[i].x) && | |
(creeper.y+creeper.scale*creeper.height >= deadDiamonds[i].y) && | |
(deadDiamonds[i].x+deadDiamonds[i].width*deadDiamonds[i].scale>=creeper.x) && | |
(deadDiamonds[i].y+deadDiamonds[i].height*deadDiamonds[i].scale>=creeper.y)) { | |
creeper.dead = true; | |
} | |
gr2.drawImage(deadDiamonds[i].draw(HEIGHT, WIDTH), null, deadDiamonds[i].x, deadDiamonds[i].y); | |
} | |
gr2.drawImage(creeper.draw(HEIGHT, WIDTH), null, creeper.x, creeper.y); | |
gr.drawImage(bg2, 0, 0, null); | |
// end render | |
Thread.sleep(1000/100); // sleep a bit | |
} | |
} | |
static AShape getDiamond(int x, int y, Color color) { | |
AShape a = new AShape(x,y,1,5,7); | |
int[] v = {0,0,1,0,0, | |
0,0,1,0,0, | |
0,1,1,1,0, | |
0,1,1,1,0, | |
1,1,1,1,1, | |
0,1,1,1,0, | |
0,0,1,0,0}; | |
a.setVector(v); | |
a.color = color; | |
return a; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment