Created
May 28, 2019 03:37
-
-
Save ultr7A/fc1c8af3794d2483da74bce60a6fc5ac to your computer and use it in GitHub Desktop.
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 java.awt.*; | |
import java.awt.AlphaComposite; | |
import java.applet.*; | |
import java.awt.event.*; | |
import java.awt.Graphics.*; | |
import java.awt.Graphics2D; | |
import java.net.*; | |
import java.lang.InterruptedException; | |
import java.util.Random; | |
import java.math.*; | |
public class space extends Applet implements Runnable, KeyListener, MouseListener | |
{ | |
//Game Resources | |
//AudioClip explodeSound; | |
//AudioClip playerSpawnSound; | |
//AudioClip shootSound; | |
Random r; | |
Thread gameLoop; | |
double gameSpeed=0.75; | |
boolean running = true; | |
int counter; | |
int delay; | |
boolean gameNotStartedYet=true; | |
boolean dead=false; | |
boolean initialGraphics=true; | |
String debug=" "; | |
static String[] levelStatus = new String[10]; | |
void initStatuses() | |
{ | |
levelStatus[0]= new String("Brace For Carnage"); | |
levelStatus[1]= new String("...Just the beginning..."); | |
levelStatus[2]= new String("...Soon..."); | |
levelStatus[3]= new String("Be Quick Or Be DEAD"); | |
levelStatus[4]= new String("You're the TITS!"); | |
levelStatus[5]= new String("GE-ZUS!"); | |
levelStatus[6]= new String("CHUCK NORRIS"); | |
levelStatus[7]= new String("This is Madness"); | |
levelStatus[8]= new String("Schube Flufleelufsk"); | |
levelStatus[9]= new String("Bi-Winning"); | |
} | |
//Image img; | |
//URL base; | |
//MediaTracker mt; | |
Graphics bG; | |
Image offscreen; | |
Dimension appletSize; | |
//player variables | |
boolean playerAlive=true; | |
int score=0; | |
int multiplier=1; | |
double multiplierProgress=1.0; | |
int levelIndex=0; | |
boolean pauseKey=false; | |
boolean playerUpKey=false; | |
boolean playerDownKey=false; | |
boolean playerLeftKey=false; | |
boolean playerRightKey=false; | |
boolean playerUpKey2=false; | |
boolean playerDownKey2=false; | |
boolean playerLeftKey2=false; | |
boolean playerRightKey2=false; | |
double playerXV; | |
double playerYV; | |
double playerX; | |
double playerY; | |
double theta=0.0; | |
int weaponIndex=0; | |
int bulletWait=1; | |
int maxBullets=75; | |
double bulletDirection=0.0; | |
double[][] playerBullets = new double[maxBullets][5]; | |
int[] bulletShade = new int[maxBullets]; | |
double[] weaponSpreads = new double[10]; | |
double powerShot=0.0; | |
void initWeaponSpreads() | |
{ | |
weaponSpreads[0]=0; | |
weaponSpreads[1]=0.2; | |
weaponSpreads[2]=0.5; | |
weaponSpreads[3]=1; | |
weaponSpreads[4]=0; | |
weaponSpreads[5]=1; | |
weaponSpreads[6]=1.2; | |
weaponSpreads[7]=1.5; | |
weaponSpreads[8]=2.0; | |
weaponSpreads[9]=2.2; | |
} | |
boolean gameOver=false; | |
int lives=3; | |
//enemy variables | |
int numEnemies=0; | |
double[][] enemyPosition = new double[100][2]; | |
double[][] enemyVelocity = new double[100][2]; | |
double[] enemyHealth = new double[100]; | |
int[] enemyType = new int[100]; | |
double[] enemyTheta = new double[100]; | |
double[] enemyExplosionTheta = new double[100]; | |
//graphics variables | |
int stars=100; | |
double [][] starPosition = new double[stars][2]; | |
double [][] starVelocity = new double[stars][2]; | |
int [][] starSize = new int[stars][2]; | |
int [][] starColor = new int[stars][3]; | |
boolean[] starExists=new boolean[stars]; | |
int scrollBG=0; | |
void createStar() | |
{ | |
for(int i=0;i<stars;i++) | |
{ | |
if(!starExists[i]) | |
{ | |
starPosition[i][0]=appletSize.width/2; | |
starPosition[i][1]=appletSize.height/2; | |
starVelocity[i][0]=r.nextDouble()*3-1.5; | |
starVelocity[i][1]=r.nextDouble()*3-1.5; | |
starSize[i][0] = r.nextInt(2); | |
starSize[i][1] = r.nextInt(2); | |
starColor[i][0]=200+r.nextInt(55); | |
starColor[i][1]=200+r.nextInt(55); | |
starColor[i][2]=200+r.nextInt(55); | |
starExists[i]=true; | |
break; | |
} | |
} | |
} | |
void destroyStar(int i) | |
{ | |
starExists[i]=false; | |
} | |
void initStars() | |
{ | |
for(int x=0;x<stars;x++) | |
{ | |
createStar(); | |
if(starExists[x]) | |
{ | |
starPosition[x][0]=Math.sin(x)*180.0+appletSize.width/2; | |
starPosition[x][1]=Math.cos(x)*180.0+appletSize.height/2; | |
} | |
} | |
} | |
void shoot(int x, int y) | |
{ | |
if(score>1000&&weaponIndex==0) | |
{ | |
weaponIndex=1; | |
} | |
if(score>4000&&weaponIndex==1) | |
{ | |
weaponIndex=2; | |
} | |
if(score>9000&&weaponIndex==2) | |
{ | |
weaponIndex=3; | |
} | |
if(score>100000&&weaponIndex==3) | |
{ | |
weaponIndex=4; | |
} | |
if(score>200000&&weaponIndex==4) | |
{ | |
weaponIndex=5; | |
} | |
if(score>400000&&weaponIndex==5) | |
{ | |
weaponIndex=6; | |
} | |
if(score>1000000&&weaponIndex==6) | |
{ | |
weaponIndex=7; | |
} | |
if(score>10000000&&weaponIndex==7) | |
{ | |
weaponIndex=8; | |
} | |
if(score>40000000&&weaponIndex==8) | |
{ | |
weaponIndex=9; | |
} | |
for(int i=0;i<maxBullets;i++) | |
{ | |
if(playerBullets[i][4]==0) | |
{ | |
if(weaponIndex>=4) | |
powerShot=1.0; | |
double variance = (r.nextDouble()*weaponSpreads[weaponIndex])-weaponSpreads[weaponIndex]/2; | |
boolean shoot=false; | |
if(x>0) | |
{ | |
if(bulletDirection<Math.PI/2||bulletDirection>Math.PI*1.5) | |
{ | |
bulletDirection+=Math.abs(bulletDirection-Math.PI/2)/4.0; | |
if(bulletDirection>Math.PI*2) | |
bulletDirection=0.0; | |
} | |
else | |
{ | |
bulletDirection-=Math.abs(bulletDirection-Math.PI/2)/4.0; | |
if(bulletDirection<0) | |
bulletDirection=Math.PI*2; | |
} | |
shoot=true; | |
} | |
else if(x<0) | |
{ | |
if(bulletDirection<Math.PI/2||bulletDirection>Math.PI*1.5) | |
{ | |
bulletDirection-=Math.abs(bulletDirection-Math.PI*1.5)/4.0; | |
if(bulletDirection<0) | |
bulletDirection=Math.PI*2; | |
} | |
else | |
{ | |
bulletDirection+=Math.abs(bulletDirection-Math.PI*1.5)/4.0; | |
if(bulletDirection>Math.PI*2) | |
bulletDirection=0.0; | |
} | |
shoot=true; | |
} | |
if(y<0) | |
{ | |
if(bulletDirection<Math.PI) | |
{ | |
bulletDirection+=Math.abs(bulletDirection-Math.PI)/4.0; | |
if(bulletDirection>Math.PI*2) | |
bulletDirection=0.0; | |
} | |
else | |
{ | |
bulletDirection-=Math.abs(bulletDirection-Math.PI)/4.0; | |
if(bulletDirection<0) | |
bulletDirection=Math.PI*2; | |
} | |
shoot=true; | |
} | |
else if(y>0) | |
{ | |
if(bulletDirection<Math.PI) | |
{ | |
bulletDirection-=Math.abs(bulletDirection)/4.0; | |
if(bulletDirection<0) | |
bulletDirection=Math.PI*2; | |
} | |
else | |
{ | |
bulletDirection+=Math.abs(bulletDirection-Math.PI*2)/4.0; | |
if(bulletDirection>Math.PI*2) | |
bulletDirection=0.0; | |
} | |
shoot=true; | |
} | |
if(shoot) | |
{ | |
playerBullets[i][0]=playerX; | |
playerBullets[i][1]=playerY; | |
playerBullets[i][2]=Math.sin(bulletDirection+variance/10)*6*(1+powerShot)+playerXV*0.5; | |
playerBullets[i][3]=Math.cos(bulletDirection+variance/10)*6*(1+powerShot)+playerYV*0.5; | |
playerBullets[i][4]=1.0; | |
} | |
break; | |
} | |
} | |
} | |
void bulletPhysics() | |
{ | |
for(int i=0;i<maxBullets;i++) | |
{ | |
if(playerBullets[i][4]>0) | |
{ | |
playerBullets[i][0]+=playerBullets[i][2]*gameSpeed; | |
playerBullets[i][1]+=playerBullets[i][3]*gameSpeed; | |
if(playerBullets[i][0]>appletSize.width||playerBullets[i][0]<0|| | |
playerBullets[i][1]>appletSize.height||playerBullets[i][1]<0) | |
{ | |
playerBullets[i][0]=0.0; | |
playerBullets[i][1]=0.0; | |
playerBullets[i][2]=0.0; | |
playerBullets[i][3]=0.0; | |
playerBullets[i][4]=0.0; | |
} | |
} | |
} | |
} | |
void spawnEnemy(int type) | |
{ | |
for(int i=0;i<100;i++) | |
{ | |
if(enemyHealth[i]<=0.0 && enemyExplosionTheta[i]==0) | |
{ | |
double theta=r.nextDouble()*6.28; | |
enemyPosition[i][0]=(appletSize.width/2)+500*Math.sin(theta); | |
enemyPosition[i][1]=(appletSize.height/2)+500*Math.cos(theta); | |
enemyHealth[i]=100.0; | |
enemyType[i]=type; | |
enemyTheta[i]=0.0; | |
numEnemies++; | |
break; | |
} | |
} | |
} | |
void spawnEnemyAt(int type,double x,double y,double t) | |
{ | |
for(int i=0;i<100;i++) | |
{ | |
if(enemyHealth[i]<=0.0 && enemyExplosionTheta[i]==0.0) | |
{ | |
double theta=r.nextDouble()*6.28; | |
enemyPosition[i][0]=x; | |
enemyPosition[i][1]=y; | |
enemyHealth[i]=100.0; | |
enemyType[i]=type; | |
enemyTheta[i]=t; | |
numEnemies++; | |
break; | |
} | |
} | |
} | |
void respawn() | |
{ | |
if(lives<1) | |
{ | |
gameOver=true; | |
} | |
else | |
{ | |
//playerSpawnSound.play(); | |
multiplierProgress=1.0; | |
playerAlive=true; | |
playerX=appletSize.width/2; | |
playerY=appletSize.height/2; | |
} | |
for(int i=0;i<100;i++) | |
{ | |
enemyHealth[i]=0.0; | |
enemyExplosionTheta[i]=0.0; | |
} | |
numEnemies=0; | |
} | |
void restart() | |
{ | |
gameOver=false; | |
score=0; | |
lives=3; | |
//multiplierProgress=1; | |
powerShot=0; | |
weaponIndex=0; | |
respawn(); | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Initialization // | |
/////////////////////////////////////////////////////////////////////// | |
public void init() | |
{ | |
r = new Random(); | |
//explodeSound = getAudioClip(getCodeBase(), "shoot.wav"); | |
//playerSpawnSound = getAudioClip(getCodeBase(), "fuckin_cool.wav"); | |
//shootSound = getAudioClip(getCodeBase(), "shoot.wav"); | |
//initialize Graphics | |
appletSize = this.getSize(); | |
offscreen = createImage(appletSize.width,appletSize.height); | |
bG = offscreen.getGraphics(); | |
initStars(); | |
initStatuses(); | |
//initialize Player | |
playerXV=0.0; | |
playerYV=0.0; | |
playerX=appletSize.width/2; | |
playerY=appletSize.height/2; | |
//initialize arrays | |
for(int i=0;i<100;i++) | |
{ | |
enemyPosition[i][0]=0.0; | |
enemyPosition[i][1]=0.0; | |
enemyVelocity[i][0]=0.0; | |
enemyVelocity[i][1]=0.0; | |
enemyHealth[i]=0.0; | |
enemyType[i]=0; | |
} | |
for(int i=0;i<maxBullets;i++) | |
{ | |
playerBullets[i][0]=0.0; | |
playerBullets[i][1]=0.0; | |
playerBullets[i][2]=0.0; | |
playerBullets[i][3]=0.0; | |
playerBullets[i][4]=0.0; | |
bulletShade[i]=r.nextInt(200)+55; | |
} | |
initWeaponSpreads(); | |
//set up the gui | |
setLayout(null); | |
//set up input | |
addKeyListener( this ); | |
addMouseListener( this ); | |
//start the clock | |
gameLoop= new Thread(this); | |
gameLoop.start(); | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Input // | |
/////////////////////////////////////////////////////////////////////// | |
public void keyPressed( KeyEvent e ) | |
{ | |
char c = e.getKeyChar(); | |
if((c=='p'||c=='P')&&!pauseKey) | |
{ | |
pauseKey=true; | |
} | |
else | |
{ | |
pauseKey=false; | |
} | |
if ( c=='w' ) | |
{ | |
playerUpKey=true; | |
} | |
if ( c=='a' ) | |
{ | |
playerLeftKey=true; | |
} | |
if ( c=='s' ) | |
{ | |
playerDownKey=true; | |
} | |
if ( c=='d' ) | |
{ | |
playerRightKey=true; | |
} | |
int keyCode = e.getKeyCode(); | |
switch( keyCode ) { | |
case KeyEvent.VK_UP: | |
// handle up | |
playerUpKey2=true; | |
break; | |
case KeyEvent.VK_DOWN: | |
// handle down | |
playerDownKey2=true; | |
break; | |
case KeyEvent.VK_LEFT: | |
// handle left | |
playerLeftKey2=true; | |
break; | |
case KeyEvent.VK_RIGHT : | |
// handle right | |
playerRightKey2=true; | |
break; | |
} | |
//e.consume(); | |
} | |
public void keyReleased( KeyEvent e ) | |
{ | |
char c = e.getKeyChar(); | |
if ( c=='w' ) | |
{ | |
playerUpKey=false; | |
} | |
if ( c=='a' ) | |
{ | |
playerLeftKey=false; | |
} | |
if ( c=='s' ) | |
{ | |
playerDownKey=false; | |
} | |
if ( c=='d' ) | |
{ | |
playerRightKey=false; | |
} | |
int keyCode = e.getKeyCode(); | |
switch( keyCode ) { | |
case KeyEvent.VK_UP: | |
// handle up | |
playerUpKey2=false; | |
break; | |
case KeyEvent.VK_DOWN: | |
// handle down | |
playerDownKey2=false; | |
break; | |
case KeyEvent.VK_LEFT: | |
// handle left | |
playerLeftKey2=false; | |
break; | |
case KeyEvent.VK_RIGHT : | |
// handle right | |
playerRightKey2=false; | |
break; | |
} | |
//e.consume(); | |
} | |
public void keyTyped( KeyEvent e ) { | |
//repaint(); | |
e.consume(); | |
} | |
public void mouseEntered( MouseEvent e ) { } | |
public void mouseExited( MouseEvent e ) { } | |
public void mousePressed( MouseEvent e ) { } | |
public void mouseReleased( MouseEvent e ) { } | |
public void mouseClicked( MouseEvent e ) | |
{ | |
initialGraphics=false; | |
if(gameOver) | |
{ | |
restart(); | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Game Loop // | |
/////////////////////////////////////////////////////////////////////// | |
public void destroy() | |
{ | |
// will cause thread to stop looping | |
running = false; | |
// destroy it. | |
gameLoop = null; | |
} | |
public void run() | |
{ | |
while (running) | |
{ | |
if(!initialGraphics&&!pauseKey) | |
{ | |
if(gameNotStartedYet) | |
{ | |
gameNotStartedYet=false; | |
//playerSpawnSound.play(); | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Background Spawning // | |
/////////////////////////////////////////////////////////////////////// | |
if(r.nextInt(5)==0) | |
{ | |
createStar(); | |
} | |
for(int i=0;i<stars;i++) | |
{ | |
if(starExists[i]) | |
{ | |
starPosition[i][0]+=starVelocity[i][0]; | |
starPosition[i][1]+=starVelocity[i][1]; | |
starVelocity[i][0]=(starPosition[i][0]-appletSize.width/2)/36; | |
starVelocity[i][1]=(starPosition[i][1]-appletSize.height/2)/36; | |
if(starPosition[i][0]<0||starPosition[i][0]>appletSize.width||starPosition[i][1]<0||starPosition[i][1]>appletSize.height) | |
{ | |
destroyStar(i); | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Player Motion // | |
/////////////////////////////////////////////////////////////////////// | |
if(playerAlive) | |
{ | |
levelIndex=(int)(Math.sqrt(score/2000)); | |
if(levelIndex>9) | |
levelIndex=9; | |
if(playerUpKey) | |
playerYV-=0.35; | |
if(playerDownKey) | |
playerYV+=0.35; | |
if(playerLeftKey) | |
playerXV-=0.35; | |
if(playerRightKey) | |
playerXV+=0.35; | |
playerXV*=0.97; | |
playerYV*=0.97; | |
playerX+=playerXV*gameSpeed; | |
playerY+=playerYV*gameSpeed; | |
/////////////////////////////////////////////////////////////////////// | |
// Player Shooting // | |
/////////////////////////////////////////////////////////////////////// | |
multiplier=(int)multiplierProgress; | |
int xs=0,ys=0; | |
if(playerUpKey2) | |
{ | |
ys--; | |
//playerUpKey2=false; | |
} | |
if(playerDownKey2) | |
{ | |
ys++; | |
//playerDownKey2=false; | |
} | |
if(playerLeftKey2) | |
{ | |
xs--; | |
//playerLeftKey2=false; | |
} | |
if(playerRightKey2) | |
{ | |
xs++; | |
//playerRightKey2=false; | |
} | |
if(bulletWait==0) | |
{ | |
for(int i=0;i<(int)weaponSpreads[weaponIndex]+1;i++) | |
{ | |
shoot(xs,ys); | |
} | |
//shootSound.play(); | |
bulletWait=(int)weaponSpreads[weaponIndex]*2+3; | |
} | |
else | |
{ | |
bulletWait--; | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Player Bounds Checking // | |
/////////////////////////////////////////////////////////////////////// | |
if(playerX<0) | |
{ | |
playerX=0; | |
} | |
if(playerX>appletSize.width) | |
{ | |
playerX=appletSize.width; | |
} | |
if(playerY>appletSize.height) | |
{ | |
playerY=appletSize.height; | |
} | |
if(playerY<0) | |
{ | |
playerY=0; | |
} | |
if(playerXV>4.0) | |
playerXV=4.0; | |
if(playerXV<-4.0) | |
playerXV=-4.0; | |
if(playerYV>4.0) | |
playerYV=4.0; | |
if(playerYV<-4.0) | |
playerYV=-4.0; | |
bulletPhysics(); | |
/////////////////////////////////////////////////////////////////////// | |
// Enemy spawning // | |
/////////////////////////////////////////////////////////////////////// | |
if(r.nextInt(110)<2+levelIndex && numEnemies<100 && playerAlive) | |
{ | |
if(r.nextInt(110)<2+levelIndex*2) | |
{ | |
if(r.nextInt(110)<2+levelIndex*2) | |
spawnEnemy(2); //bad ass kind | |
else | |
spawnEnemy(1); | |
} | |
else | |
{ | |
spawnEnemy(0); //spiraling homing type | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Enemy Moving // | |
/////////////////////////////////////////////////////////////////////// | |
for(int i=0;i<100;i++) | |
{ | |
if(enemyHealth[i]>0) | |
{ | |
if(enemyType[i]==0) | |
{ | |
// homing type | |
if(enemyPosition[i][0]<playerX) | |
{ | |
enemyVelocity[i][0]+=0.077; | |
} | |
else if(enemyPosition[i][0]>playerX) | |
{ | |
enemyVelocity[i][0]-=0.077; | |
} | |
if(enemyPosition[i][1]<playerY) | |
{ | |
enemyVelocity[i][1]+=0.077; | |
} | |
else if(enemyPosition[i][1]>playerY) | |
{ | |
enemyVelocity[i][1]-=0.077; | |
} | |
} | |
if(enemyType[i]==1) | |
{ | |
// homing spiraling type | |
enemyVelocity[i][0]+=Math.sin(enemyTheta[i])*0.3; | |
enemyVelocity[i][1]+=Math.cos(enemyTheta[i])*0.3; | |
if(enemyPosition[i][0]<playerX) | |
{ | |
enemyVelocity[i][0]+=0.07; | |
} | |
else if(enemyPosition[i][0]>playerX) | |
{ | |
enemyVelocity[i][0]-=0.07; | |
} | |
if(enemyPosition[i][1]<playerY) | |
{ | |
enemyVelocity[i][1]+=0.07; | |
} | |
else if(enemyPosition[i][1]>playerY) | |
{ | |
enemyVelocity[i][1]-=0.07; | |
} | |
} | |
if(enemyType[i]==2) | |
{ | |
// homing armoured type | |
//enemyVelocity[i][0]+=Math.sin(enemyTheta[i])*0.3; | |
//enemyVelocity[i][1]+=Math.cos(enemyTheta[i])*0.3; | |
if(enemyPosition[i][0]<playerX) | |
{ | |
enemyVelocity[i][0]+=0.02; | |
} | |
else if(enemyPosition[i][0]>playerX) | |
{ | |
enemyVelocity[i][0]-=0.02; | |
} | |
if(enemyPosition[i][1]<playerY) | |
{ | |
enemyVelocity[i][1]+=0.02; | |
} | |
else if(enemyPosition[i][1]>playerY) | |
{ | |
enemyVelocity[i][1]-=0.02; | |
} | |
} | |
if(enemyType[i]==3) | |
{ | |
// homing spiraling type part III | |
enemyVelocity[i][0]+=Math.sin(enemyTheta[i])*0.2; | |
enemyVelocity[i][1]+=Math.cos(enemyTheta[i])*0.2; | |
if(enemyPosition[i][0]<playerX) | |
{ | |
enemyVelocity[i][0]+=0.02; | |
} | |
else if(enemyPosition[i][0]>playerX) | |
{ | |
enemyVelocity[i][0]-=0.02; | |
} | |
if(enemyPosition[i][1]<playerY) | |
{ | |
enemyVelocity[i][1]+=0.02; | |
} | |
else if(enemyPosition[i][1]>playerY) | |
{ | |
enemyVelocity[i][1]-=0.02; | |
} | |
} | |
if(enemyType[i]==4) | |
{ | |
// homing spiraling type part III | |
enemyVelocity[i][0]+=Math.sin(enemyTheta[i])*0.1; | |
enemyVelocity[i][1]+=Math.cos(enemyTheta[i])*0.1; | |
if(enemyPosition[i][0]<playerX) | |
{ | |
enemyVelocity[i][0]+=0.01; | |
} | |
else if(enemyPosition[i][0]>playerX) | |
{ | |
enemyVelocity[i][0]-=0.01; | |
} | |
if(enemyPosition[i][1]<playerY) | |
{ | |
enemyVelocity[i][1]+=0.01; | |
} | |
else if(enemyPosition[i][1]>playerY) | |
{ | |
enemyVelocity[i][1]-=0.01; | |
} | |
} | |
enemyTheta[i]+=0.1*gameSpeed; | |
if(enemyTheta[i]>6.28) | |
{ | |
enemyTheta[i]=0.0; | |
} | |
enemyVelocity[i][0]*=0.97; | |
enemyVelocity[i][1]*=0.97; | |
enemyPosition[i][0]+=enemyVelocity[i][0]*gameSpeed; | |
enemyPosition[i][1]+=enemyVelocity[i][1]*gameSpeed; | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Enemy Collisions // | |
/////////////////////////////////////////////////////////////////////// | |
for(int i=0;i<100;i++) | |
{ | |
if(enemyExplosionTheta[i]>0.0) | |
{ | |
enemyExplosionTheta[i]+=10; | |
if(enemyExplosionTheta[i]>100) | |
{ | |
numEnemies--; | |
enemyExplosionTheta[i]=0.0; | |
multiplierProgress+=0.05; | |
//if(multiplierProgress>10.0) | |
// multiplierProgress=10.0; | |
if(enemyType[i]==0) | |
{ | |
score+=10*multiplier; | |
} | |
if(enemyType[i]==1) | |
{ | |
score+=50*multiplier; | |
spawnEnemyAt(3,enemyPosition[i][0],enemyPosition[i][1],0.0); | |
spawnEnemyAt(3,enemyPosition[i][0],enemyPosition[i][1],3.14); | |
} | |
if(enemyType[i]==2) | |
{ | |
score+=500*multiplier; | |
} | |
if(enemyType[i]==3) | |
{ | |
score+=100*multiplier; | |
spawnEnemyAt(4,enemyPosition[i][0],enemyPosition[i][1],0.0); | |
spawnEnemyAt(4,enemyPosition[i][0],enemyPosition[i][1],3.14); | |
} | |
if(enemyType[i]==4) | |
{ | |
score+=100*multiplier; | |
} | |
//break; | |
} | |
} | |
if(enemyHealth[i]>0.0) | |
{ | |
double x=enemyPosition[i][0]; | |
double y=enemyPosition[i][1]; | |
if(Math.sqrt(Math.pow(playerX-x,2)+Math.pow(playerY-y,2))<14) | |
{ | |
playerAlive=false; | |
lives--; | |
respawn(); | |
} | |
for(int a=0;a<maxBullets;a++) | |
{ | |
if(playerBullets[a][4]==1.0) | |
{ | |
double bx=playerBullets[a][0]; | |
double by=playerBullets[a][1]; | |
if(Math.sqrt(Math.pow(bx-x,2)+Math.pow(by-y,2))<20) | |
{ | |
if(powerShot==0) // if it's not a laser beem, the bullet dies. | |
{ | |
playerBullets[a][4]=0.0; | |
} | |
switch(enemyType[i]) | |
{ | |
case 1: | |
enemyHealth[i]-=25; | |
break; | |
case 2: | |
enemyHealth[i]-=5; | |
break; | |
case 3: | |
enemyHealth[i]-=50; | |
break; | |
case 4: | |
enemyHealth[i]-=50; | |
break; | |
default: | |
enemyHealth[i]=0; | |
break; | |
} | |
if(enemyHealth[i]<=0) | |
{ | |
//explodeSound.play(); | |
enemyExplosionTheta[i]=1; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
repaint(); | |
try | |
{ | |
//try for 60fps? | |
gameLoop.sleep(12); | |
} | |
catch (InterruptedException e) | |
{ | |
System.out.println(e); | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Graphics // | |
/////////////////////////////////////////////////////////////////////// | |
public void paint(Graphics g) | |
{ | |
//Graphics2D g2d = (Graphics2D)g; | |
//Graphics2D bG = (Graphics2D) g; | |
bG.setColor(Color.black); | |
bG.fillRect(0,0,appletSize.width,appletSize.height); | |
//bG.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f)); | |
for(int i=0;i<stars;i++) | |
{ | |
if(starExists[i]) | |
{ | |
bG.setColor(new Color(starColor[i][0],starColor[i][1],starColor[i][2])); | |
bG.fillRect((int)starPosition[i][0],(int)starPosition[i][1],starSize[i][0]+1,starSize[i][1]+1); | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Player Graphics // | |
/////////////////////////////////////////////////////////////////////// | |
if(playerAlive) | |
{ | |
int x,y,z,x2,y2; | |
for(int i=0;i<4;i++) | |
{ | |
//x=(int)(playerX+Math.sin(theta+i*Math.PI/4)*20.0); | |
//y=(int)(playerY+Math.cos(theta+i*Math.PI/4)*20.0); | |
//x2=(int)(playerX+Math.sin(theta+i*Math.PI/4)*10.0); | |
//y2=(int)(playerY+Math.cos(theta+i*Math.PI/4)*10.0); | |
//z=255; | |
bG.setColor(new Color((int)(Math.abs(Math.sin(theta/i)*255)),0,0)); | |
//bG.drawLine(x,y,x2,y2); | |
bG.drawOval((int)playerX-i*2,(int)playerY-i*2,i*4,i*4); | |
theta+=0.01*gameSpeed; | |
if(theta>6.28) | |
theta=0.0; | |
} | |
bG.setColor(Color.red); | |
bG.drawOval((int)playerX-10,(int)playerY-10,20,20); | |
} | |
else | |
{ | |
bG.drawString("Game Over",appletSize.width/2-30,appletSize.height/2); | |
bG.drawString("Click to Restart",appletSize.width/2-40,appletSize.height/2+20); | |
//death animation | |
} | |
//bG.drawString("* Debug Console... *"+debug,0,100); | |
/////////////////////////////////////////////////////////////////////// | |
// Player Weapon Graphics // | |
/////////////////////////////////////////////////////////////////////// | |
for(int i=0;i<maxBullets;i++) | |
{ | |
if(playerBullets[i][4]>0) | |
{ | |
if(powerShot==0.0) | |
{ | |
bG.setColor(new Color(0,255,0)); | |
bG.drawLine((int)playerBullets[i][0],(int)playerBullets[i][1],(int)playerBullets[i][0]+ | |
(int)playerBullets[i][2],(int)playerBullets[i][1]+(int)playerBullets[i][3]); | |
} | |
else | |
{ | |
bG.setColor(new Color(255,0,255)); | |
bG.drawLine((int)playerBullets[i][0],(int)playerBullets[i][1],(int)playerBullets[i][0]+ | |
(int)playerBullets[i][2],(int)playerBullets[i][1]+(int)playerBullets[i][3]); | |
bG.setColor(new Color(255,90,255)); | |
bG.drawLine((int)playerBullets[i][0]-1,(int)playerBullets[i][1]-1,(int)playerBullets[i][0]+ | |
(int)playerBullets[i][2]-1,(int)playerBullets[i][1]+(int)playerBullets[i][3]-1); | |
} | |
} | |
} | |
//draw the image(s) | |
//bG.drawImage(img,0,70,200,200,this); | |
/////////////////////////////////////////////////////////////////////// | |
// Enemy Graphics // | |
/////////////////////////////////////////////////////////////////////// | |
for(int i=0;i<100;i++) | |
{ | |
if(enemyHealth[i]>0||enemyExplosionTheta[i]>0) | |
{ | |
if(enemyType[i]==0) | |
{ | |
bG.setColor(new Color(0,120,255)); | |
//bG.fillRect((int)enemyPosition[i][0]-10,(int)enemyPosition[i][1]-10,20,20); | |
bG.drawLine( | |
(int)(enemyPosition[i][0]-10-Math.sin(enemyExplosionTheta[i]/33)*10), | |
(int)enemyPosition[i][1], | |
(int)enemyPosition[i][0], | |
(int)enemyPosition[i][1]+10); | |
bG.drawLine( | |
(int)enemyPosition[i][0], | |
(int)enemyPosition[i][1]+10, | |
(int)(enemyPosition[i][0]+10+Math.sin(enemyExplosionTheta[i]/33)*10), | |
(int)enemyPosition[i][1]); | |
bG.drawLine( | |
(int)(enemyPosition[i][0]+10+Math.sin(enemyExplosionTheta[i]/33)*10), | |
(int)enemyPosition[i][1], | |
(int)enemyPosition[i][0], | |
(int)enemyPosition[i][1]-10); | |
bG.drawLine( | |
(int)enemyPosition[i][0], | |
(int)enemyPosition[i][1]-10, | |
(int)(enemyPosition[i][0]-10-Math.sin(enemyExplosionTheta[i]/33)*10), | |
(int)enemyPosition[i][1]); | |
bG.setColor(new Color(60,180,255)); | |
bG.drawLine((int)enemyPosition[i][0]-11,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]+11); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]+11,(int)enemyPosition[i][0]+11,(int)enemyPosition[i][1]); | |
bG.drawLine((int)enemyPosition[i][0]+11,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]-11); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]-11,(int)enemyPosition[i][0]-11,(int)enemyPosition[i][1]); | |
} | |
if(enemyType[i]==1) | |
{ | |
bG.setColor(new Color(255,100,255)); | |
//bG.fillRect((int)enemyPosition[i][0]-10,(int)enemyPosition[i][1]-10,20,20); | |
bG.drawLine( | |
(int)(enemyPosition[i][0]-10-enemyExplosionTheta[i]/2), | |
(int)(enemyPosition[i][1]), | |
(int)(enemyPosition[i][0]), | |
(int)(enemyPosition[i][1]+10+enemyExplosionTheta[i]/2)); | |
bG.drawLine( | |
(int)enemyPosition[i][0], | |
(int)(enemyPosition[i][1]+10+enemyExplosionTheta[i]/2), | |
(int)(enemyPosition[i][0]+10+enemyExplosionTheta[i]/2), | |
(int)enemyPosition[i][1]); | |
bG.drawLine( | |
(int)(enemyPosition[i][0]+10+enemyExplosionTheta[i]/2), | |
(int)enemyPosition[i][1], | |
(int)enemyPosition[i][0], | |
(int)(enemyPosition[i][1]-10-enemyExplosionTheta[i]/2)); | |
bG.drawLine( | |
(int)enemyPosition[i][0], | |
(int)(enemyPosition[i][1]-10-enemyExplosionTheta[i]/2), | |
(int)(enemyPosition[i][0]-10-enemyExplosionTheta[i]/2), | |
(int)enemyPosition[i][1]); | |
bG.setColor(new Color(255,180,255)); | |
bG.drawLine((int)enemyPosition[i][0]-11,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]+11); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]+11,(int)enemyPosition[i][0]+11,(int)enemyPosition[i][1]); | |
bG.drawLine((int)enemyPosition[i][0]+11,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]-11); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]-11,(int)enemyPosition[i][0]-11,(int)enemyPosition[i][1]); | |
} | |
if(enemyType[i]==2) | |
{ | |
bG.setColor(new Color(255,0,0)); | |
//bG.fillRect((int)enemyPosition[i][0]-10,(int)enemyPosition[i][1]-10,20,20); | |
bG.drawLine( | |
(int)(enemyPosition[i][0]-10-enemyExplosionTheta[i]/2), | |
(int)enemyPosition[i][1], | |
(int)enemyPosition[i][0], | |
(int)(enemyPosition[i][1]+10+enemyExplosionTheta[i]/2)); | |
bG.drawLine( | |
(int)enemyPosition[i][0], | |
(int)(enemyPosition[i][1]+10+enemyExplosionTheta[i]/2), | |
(int)(enemyPosition[i][0]+10+enemyExplosionTheta[i]/2), | |
(int)enemyPosition[i][1]); | |
bG.drawLine( | |
(int)(enemyPosition[i][0]+10+enemyExplosionTheta[i]/2), | |
(int)enemyPosition[i][1], | |
(int)enemyPosition[i][0], | |
(int)(enemyPosition[i][1]-10-enemyExplosionTheta[i]/2)); | |
bG.drawLine( | |
(int)enemyPosition[i][0], | |
(int)(enemyPosition[i][1]-10-enemyExplosionTheta[i]/2), | |
(int)(enemyPosition[i][0]-10-enemyExplosionTheta[i]/2), | |
(int)enemyPosition[i][1]); | |
bG.setColor(new Color(255,220,220)); | |
bG.drawLine((int)enemyPosition[i][0]-15-r.nextInt(2),(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]+15+r.nextInt(2)); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]+15+r.nextInt(2),(int)enemyPosition[i][0]+15+r.nextInt(2),(int)enemyPosition[i][1]); | |
bG.drawLine((int)enemyPosition[i][0]+15+r.nextInt(2),(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]-15-r.nextInt(2)); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]-15-r.nextInt(2),(int)enemyPosition[i][0]-15-r.nextInt(2),(int)enemyPosition[i][1]); | |
} | |
if(enemyType[i]==3) | |
{ | |
bG.setColor(new Color(255,100,255)); | |
//bG.fillRect((int)enemyPosition[i][0]-10,(int)enemyPosition[i][1]-10,20,20); | |
bG.drawLine((int)enemyPosition[i][0]-5,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]+5); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]+10,(int)enemyPosition[i][0]+10,(int)enemyPosition[i][1]); | |
bG.drawLine((int)enemyPosition[i][0]+5,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]-5); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]-5,(int)enemyPosition[i][0]-5,(int)enemyPosition[i][1]); | |
bG.setColor(new Color(255,180,255)); | |
bG.drawLine((int)enemyPosition[i][0]-11,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]+11); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]+11,(int)enemyPosition[i][0]+11,(int)enemyPosition[i][1]); | |
bG.drawLine((int)enemyPosition[i][0]+11,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]-11); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]-11,(int)enemyPosition[i][0]-11,(int)enemyPosition[i][1]); | |
} | |
if(enemyType[i]==4) | |
{ | |
bG.setColor(new Color(255,100,255)); | |
//bG.fillRect((int)enemyPosition[i][0]-10,(int)enemyPosition[i][1]-10,20,20); | |
bG.drawLine((int)enemyPosition[i][0]-5,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]+5); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]+5,(int)enemyPosition[i][0]+5,(int)enemyPosition[i][1]); | |
bG.drawLine((int)enemyPosition[i][0]+5,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]-5); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]-5,(int)enemyPosition[i][0]-5,(int)enemyPosition[i][1]); | |
bG.setColor(new Color(255,180,255)); | |
bG.drawLine((int)enemyPosition[i][0]-4,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]+4); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]+4,(int)enemyPosition[i][0]+4,(int)enemyPosition[i][1]); | |
bG.drawLine((int)enemyPosition[i][0]+4,(int)enemyPosition[i][1],(int)enemyPosition[i][0],(int)enemyPosition[i][1]-4); | |
bG.drawLine((int)enemyPosition[i][0],(int)enemyPosition[i][1]-4,(int)enemyPosition[i][0]-4,(int)enemyPosition[i][1]); | |
} | |
} | |
} | |
if(initialGraphics) | |
{ | |
/////////////////////////////////////////////////////////////////////// | |
// Initial graphics // | |
/////////////////////////////////////////////////////////////////////// | |
bG.setColor(Color.green); | |
bG.drawString("* Click on this, use WASD and ARROWS to play and press P to pause *",appletSize.width/2-185,(appletSize.height/2)+65); | |
bG.drawString("S P A C E - S H O O T E R - one.six.one ",appletSize.width/2-100,(appletSize.height/2)-65); | |
bG.drawString("NEW: graphics, red enemies spawn more frequently",appletSize.width/2-190,(appletSize.height/2)+100); | |
} | |
else | |
{ | |
bG.setColor(new Color(220,220,255)); | |
bG.drawString("Score "+Integer.toString(score),0,20); | |
bG.drawString("Multiplier "+Integer.toString(multiplier)+"X",0,40); | |
bG.drawString("Weapon "+Integer.toString(weaponIndex),0,60); | |
bG.drawString("Lives ",0,80); | |
bG.setColor(new Color(255,0,0)); | |
for(int i =0; i < 3;i++) | |
{ | |
if(lives-i>0) | |
{ | |
bG.setColor(new Color(255,0,0)); | |
bG.drawString("<3",50+i*25,80); | |
} | |
else | |
{ | |
bG.setColor(new Color(100,0,0)); | |
bG.drawString("</3",50+i*25,80); | |
} | |
} | |
//level status | |
bG.setColor(new Color(220,220,255)); | |
bG.drawString(levelStatus[levelIndex],appletSize.width/2-30,20); | |
//bG.drawString("Bullet Direction "+Double.toString(bulletDirection/Math.PI),0,100); | |
if(pauseKey) | |
{ | |
bG.setColor(new Color(255,50,0)); | |
bG.drawString("Paused",appletSize.width/2-25,appletSize.height/2); | |
} | |
} | |
g.drawImage(offscreen,0,0,this); | |
} | |
public void update(Graphics g) | |
{ | |
paint(g); | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Gui Events // | |
/////////////////////////////////////////////////////////////////////// | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment