First Commit

This commit is contained in:
Sagi Dayan 2015-12-21 19:54:44 +02:00
commit 0d27c26c0f
56 changed files with 2370 additions and 0 deletions

1
.idea/.name Normal file
View file

@ -0,0 +1 @@
Astroids2D

View file

@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="Astroids2D:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Astroids2D_jar</output-path>
<root id="archive" name="Astroids2D.jar">
<element id="module-output" name="Astroids2D" />
</root>
</artifact>
</component>

22
.idea/compiler.xml Normal file
View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>

View file

@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>

6
.idea/encodings.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

26
.idea/misc.xml Normal file
View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="mavenHome" value="Bundled (Maven 3)" />
</MavenGeneralSettings>
</option>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Astroids2D.iml" filepath="$PROJECT_DIR$/Astroids2D.iml" />
</modules>
</component>
</project>

124
.idea/uiDesigner.xml Normal file
View file

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

1261
.idea/workspace.xml Normal file

File diff suppressed because it is too large Load diff

11
Astroids2D.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 859 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
src.zip Normal file

Binary file not shown.

View file

@ -0,0 +1,107 @@
package Engine;
import Sprites.Sprite;
import java.awt.*;
import java.awt.image.PixelGrabber;
/**
* Created by sagi on 12/19/15.
*/
public class CollisionUtil {
/**
* Checks if a Sprite is colliding with another Sprite.
* @param otherSprite The Sprite to check a collission with.
* @param pixelPerfect If true, it will use a pixel-perfect algorithm. If
* false, it only checks its bounding box.
* @return true if the Sprites collide, otherwise false.
*/
public static boolean collidesWith(Sprite thisSprite, Sprite otherSprite) {
boolean isColliding=false;
Rectangle r1 = thisSprite.getBounds();
Rectangle r2 = otherSprite.getBounds();
r1.intersection(r2);
if(intersection(r1, r2)) {
isColliding = pixelPerfectCollision(thisSprite, otherSprite, r1, r2);
}
return isColliding;
}
private static boolean intersection(Rectangle r, Rectangle d) {
int rect1x = r.x;
int rect1y = r.y;
int rect1w = r.width;
int rect1h = r.height;
int rect2x = d.x;
int rect2y = d.y;
int rect2w = d.width;
int rect2h = d.height;
return (rect1x + rect1w >= rect2x &&
rect1y + rect1h >= rect2y &&
rect1x <= rect2x + rect2w &&
rect1y <= rect2y + rect2h);
}
/*
* pixelPerfectCollision(); first determines the area where the sprites collides
* AKA the collision-rectangle. It then grabs the pixels from both sprites
* which are inside the rectangle. It then checks every pixel from the arrays
* given by grabPixels();, and if 2 pixels at the same position are opaque,
* (alpha value over 0) it will return true. Otherwise it will return false.
*/
private static boolean pixelPerfectCollision(Sprite sprite1,Sprite sprite2, Rectangle r1, Rectangle r2) {
/*
* Get the X-values and Y-values for the two coordinates where the sprites collide
*/
int cornerTopX = (r1.x>r2.x)?r1.x:r2.x;
int cornerBottomX = ((r1.x+r1.width) < (r2.x+r2.width))?(r1.x+r1.width):(r2.x+r2.width);
int cornerTopY = (r1.y>r2.y)?r1.y:r2.y;
int cornerBottomY = ((r1.y+r1.height) < (r2.y+r2.height))?(r1.y+r1.height):(r2.y+r2.height);
//Determine the width and height of the collision rectangle
int width=cornerBottomX-cornerTopX;
int height=cornerBottomY-cornerTopY;
//Create arrays to hold the pixels
int[] pixels1 = new int[width*height];
int[] pixels2 = new int[width*height];
//Create the pixelgrabber and fill the arrays
PixelGrabber pg1 = new PixelGrabber(sprite1.getbImage(), cornerTopX-(int)sprite1.getLocX(), cornerTopY-(int)sprite1.getLocY(), width, height, pixels1, 0, width);
PixelGrabber pg2 = new PixelGrabber(sprite2.getbImage(), cornerTopX-(int)sprite2.getLocX(), cornerTopY-(int)sprite2.getLocY(), width, height, pixels2, 0, width);
//Grab the pixels
try {
pg1.grabPixels();
pg2.grabPixels();
} catch (InterruptedException ex) {
//Logger.getLogger(Sprite.class.getName()).log(Level.SEVERE, null, ex);
}
//Check if pixels at the same spot from both arrays are not transparent.
for(int i=0;i<pixels1.length;i++) {
int a = (pixels1[i] >>> 24) & 0xff;
int a2 = (pixels2[i] >>> 24) & 0xff;
/* Awesome, we found two pixels in the same spot that aren't
* completely transparent! Thus the sprites are colliding!
*/
if(a > 0 && a2 > 0) return true;
}
return false;
}
}

319
src/Engine/GameEngine.java Normal file
View file

@ -0,0 +1,319 @@
package Engine;
import Sprites.*;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Random;
import java.util.Vector;
/**
* Created by sagi on 12/18/15.
*/
public class GameEngine implements KeyListener {
private final int LARGE=100, MEDIUM=50, SMALL=25, NUM_OF_LIVES = 3, LASER_SPEED = 10, ASTROID_SPEED = 3;
private final int STOP=0, UP=1, DOWN=-1;
public boolean gameOn,fireLock, gameOver, isFirstGame;
private Ship player;
private Vector<Sprite> astroidsSprites,laserSprites;
private int pWidth, pHeight;
private Timer astroidTimer, fireTimer, startTime;
private Random r;
private int score, countDown;
private Vector<Sprite> lives, fogs;
private AudioClip laserAudioClip;
private BufferedImage sceneImage;
private AudioClip themeAudioClip;
private final URL laserURL= getClass().getResource("/Sounds/laser.wav");
private final URL themeURL= getClass().getResource("/Sounds/theme.wav");
public GameEngine(int width, int height){
this.isFirstGame = true;
this.gameOver = true;
this.pWidth = width;
this.pHeight = height;
try {
laserAudioClip = Applet.newAudioClip(laserURL);
themeAudioClip = Applet.newAudioClip(themeURL);
}catch (Exception e){
laserAudioClip = null;
themeAudioClip = null;
}
if(themeAudioClip != null)
themeAudioClip.loop();
fogs = new Vector<>();
r = new Random();
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
initFogs();
startNewGame();
}
private void initFogs(){
for(int i = 0 ; i < 4 ; i++){
fogs.add(new Fog((-1)*r.nextInt(),(-1)*r.nextInt(),pWidth,pHeight,1,"Fog.png",r.nextInt(360),pWidth));
}
}
private void startNewGame(){
score = 0;
lives = new Vector<>();
setupLives(NUM_OF_LIVES);
astroidTimer = new Timer(5000, new astroidTimerListener()) ;
fireTimer = new Timer(500, new fireTimerListener()) ;
startTime = new Timer(1000, new countDownListener());
fogs.remove(fogs.size()-1);
initGame();
}
private void initGame(){
this.countDown = 3;
astroidsSprites = new Vector<>();
laserSprites = new Vector<>();
this.player = new Ship(pWidth/2, pHeight/2,pWidth,pHeight, MEDIUM);
gameOn = true;
fireTimer.start();
astroidTimer.start();
for (int i =0 ; i<4 ; i++)
createAstroid();
}
private void setupLives(int num){
int size = 30;
for(int i = 0 ; i < num ; i++){
lives.add(new Ship(pWidth - ((i+1)*size), 15, pWidth, pHeight, size));
}
}
public int getScore(){
return score;
}
public boolean isGameOver(){
return this.gameOver;
}
public int getCountDown(){
return countDown;
}
private void createAstroid(){
astroidsSprites.add(new Astroid((-1)*r.nextInt(),(-1)*r.nextInt(),pWidth,pHeight,ASTROID_SPEED,"astroid.png",r.nextInt(360),LARGE));
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
if(gameOn) {
switch (keyEvent.getKeyCode()) {
case KeyEvent.VK_UP:
player.setDirection(UP);
break;
case KeyEvent.VK_DOWN:
player.setDirection(DOWN);
break;
case KeyEvent.VK_LEFT:
player.turnShip(DOWN);
break;
case KeyEvent.VK_RIGHT:
player.turnShip(UP);
break;
case KeyEvent.VK_SPACE:
if (!fireLock && countDown <= 0) {
if(laserAudioClip != null)
laserAudioClip.loop();
laserSprites.add(new LaserBlast((int) player.getLocX() + (player.getImageWidth() / 2),
(int) player.getLocY() + (player.getImageHeight() / 2),
pWidth,
pHeight,
LASER_SPEED,
"laser2.gif",
player.getAngle(),
SMALL));
fireLock = true;
}
break;
case KeyEvent.VK_F2:
this.isFirstGame = false;
gameOver = false;
startNewGame();
startTime.start();
break;
default:
break;
}
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {
if(gameOn){
switch (keyEvent.getKeyCode()){
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
player.setDirection(STOP);
break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_LEFT:
player.turnShip(STOP);
break;
case KeyEvent.VK_SPACE:
laserAudioClip.stop();
break;
default:
break;
}
}
}
public void update(){
for (Sprite sprite : fogs) {
sprite.update();
}
if(!gameOver) {
if (countDown > 0)
return;
}
else
return;
player.update();
for (Sprite sprite : astroidsSprites) {
sprite.update();
}
for (Sprite sprite : laserSprites) {
sprite.update();
}
collisionHandler();
}
private void collisionHandler(){
Vector<Sprite> newAstroids = new Vector<>();
Vector<Sprite> removeLasers = new Vector<>();
Vector<Sprite> removeAstroids = new Vector<>();
for (int i = 0; i<laserSprites.size(); i++){
for (int j = 0; j<astroidsSprites.size(); j++){
if(CollisionUtil.collidesWith(laserSprites.get(i), astroidsSprites.get(j))) {
score+=100;
if (astroidsSprites.get(j).getSize() == LARGE) {
for (int k = 0; k < 2; k++) {
newAstroids.add(new Astroid((int) astroidsSprites.get(j).getLocX(), (int) astroidsSprites.get(j).getLocY(), pWidth, pHeight, astroidsSprites.get(j).getAcceleration(), "astroid.png", r.nextInt(360), MEDIUM));
}
} else if (astroidsSprites.get(j).getSize() == MEDIUM) {
for (int h = 0; h < 2; h++) {
newAstroids.add(new Astroid((int) astroidsSprites.get(j).getLocX(), (int) astroidsSprites.get(j).getLocY(), pWidth, pHeight, astroidsSprites.get(j).getAcceleration(), "astroid.png", r.nextInt(360), SMALL));
}
score+=100;
}else{
score+=200;
}
removeLasers.add(laserSprites.get(i));
removeAstroids.add(astroidsSprites.get(j));
}
}
}
for(int i = 0 ; i < astroidsSprites.size() ; i++){
if(CollisionUtil.collidesWith(astroidsSprites.get(i), player)){
if(lives.size() == 1){
gameOver = true;
fogs.add(new Fog(pWidth,0,pWidth,pHeight,1,"Fog.png",180,pWidth));
}
if(lives.size() > 0)
lives.remove(lives.size()-1);
initGame();
System.out.println("==============GAME OVER==============");
}
}
astroidsSprites.addAll(newAstroids);
laserSprites.removeAll(removeLasers);
astroidsSprites.removeAll(removeAstroids);
}
public void render(JPanel panel){
sceneImage = new BufferedImage(this.pWidth, this.pHeight, Image.SCALE_FAST);
renderScene(sceneImage.getGraphics(), panel);
}
public void renderScene(Graphics g, JPanel panel){
if(!gameOver)
player.drawSprite(g, panel);
try {
for (Sprite sprite : astroidsSprites) {
sprite.drawSprite(g, panel);
}
for (Sprite sprite : lives) {
sprite.drawSprite(g, panel);
}
for (Sprite sprite : laserSprites) {
sprite.drawSprite(g, panel);
}
for (Sprite sprite : fogs) {
sprite.drawSprite(g, panel);
}
}catch (Exception e)
{
}
}
private class astroidTimerListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent actionEvent) {
createAstroid();
}
}
private class fireTimerListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent actionEvent) {
fireLock = false;
}
}
private class countDownListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent actionEvent) {
countDown--;
}
}
public BufferedImage getScene(){
return this.sceneImage;
}
}

View file

@ -0,0 +1,139 @@
package GamePanels;
import Engine.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
/**
* Created by sagi on 12/18/15.
*/
public class GamePanel extends JPanel implements Runnable{
// private final String BASE_PATH = System.getProperty("user.dir") + File.separator +"src"+ File.separator;
// private final String bgPath = BASE_PATH + "Images" + File.separator + "bg.gif";
// private final String gameOverPath = BASE_PATH + "Images" + File.separator + "gameOver.png";
// private final String startPath = BASE_PATH + "Images" + File.separator + "start.png";
private final URL bgURL= getClass().getResource("/Images/bg2.gif");
private final URL gameOverURL= getClass().getResource("/Images/gameOver.png");
private final URL startURL= getClass().getResource("/Images/start.png");
private int width, height;
private GameEngine engine;
private JLabel lbl_score, lbl_countDown, lbl_gameOver, lbl_bg, lbl_start;
private ImageIcon img_bg, img_go, img_start;
private Image bg_image;
public GamePanel(int width, int height){
this.setLayout(null);
this.width = width;
this.height = height;
this.engine = new GameEngine(width, height);
this.addKeyListener(engine);
this.setFocusable(true);
this.requestFocus();
this.lbl_score = new JLabel("");
lbl_score.setBounds(15,15,width,30);
this.lbl_countDown = new JLabel("");
lbl_countDown.setBounds(width/2,0,width,height);
System.out.println("URL = " +bgURL);
img_bg = new ImageIcon(bgURL);
Image tmp_BG = img_bg.getImage();
bg_image = tmp_BG.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
img_bg = new ImageIcon(tmp_BG);
lbl_bg = new JLabel(img_bg);
lbl_bg.setBounds(0,0,width,height);
lbl_countDown.setFont(new Font("Ariel", Font.BOLD, 100));
lbl_countDown.setForeground(Color.WHITE);
this.add(lbl_countDown);
img_start = new ImageIcon(startURL);
lbl_start = new JLabel(img_start);
lbl_start.setVisible(true);
lbl_start.setBounds(0,0,width,height);
this.add(lbl_start);
img_go = new ImageIcon(gameOverURL);
lbl_gameOver = new JLabel(img_go);
lbl_gameOver.setVisible(false);
lbl_gameOver.setBounds(0,0,width,height);
this.add(lbl_gameOver);
lbl_score.setFont(new Font("Ariel", Font.BOLD, 24));
lbl_score.setForeground(Color.GREEN);
this.add(lbl_score);
repaint();
}
@Override
public void run() {
while(engine.gameOn){
engine.update();
if(engine.isGameOver()){
this.lbl_countDown.setVisible(false);
if(!engine.isFirstGame)
this.lbl_gameOver.setVisible(true);
}else{
this.lbl_gameOver.setVisible(false);
this.lbl_countDown.setVisible(true);
lbl_start.setVisible(false);
}
engine.render(this);
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void addNotify(){
super.addNotify();
(new Thread(this)).start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g.drawImage(bg_image, 0, 0, this); //draw the background
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
//Some labels - Score & countDown
lbl_score.setText("SCORE : " + engine.getScore());
if(engine.getCountDown() > 0){
lbl_countDown.setText(engine.getCountDown()+"");
}else if(engine.getCountDown() == 0){
lbl_countDown.setText("GO!");
}else{
lbl_countDown.setText("");
}
}
}

BIN
src/Images/Fog.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
src/Images/astroid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 KiB

BIN
src/Images/bg2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 MiB

BIN
src/Images/gameOver.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
src/Images/laser2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
src/Images/ship.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 859 B

BIN
src/Images/ship2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
src/Images/start.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

3
src/META-INF/MANIFEST.MF Normal file
View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

18
src/Main.java Normal file
View file

@ -0,0 +1,18 @@
import javax.swing.*;
import GamePanels.*;
/**
* Created by sagi on 12/18/15.
*/
public class Main {
public static void main(String args[]){
JFrame frame = new JFrame("Asteroids 2D - Star Wars Edition");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setResizable(false);
frame.add(new GamePanel(800,600));
frame.setVisible(true);
}
}

BIN
src/Sounds/laser.wav Normal file

Binary file not shown.

BIN
src/Sounds/theme.wav Normal file

Binary file not shown.

38
src/Sprites/Astroid.java Normal file
View file

@ -0,0 +1,38 @@
package Sprites;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
/**
* Created by sagi on 12/18/15.
*/
public class Astroid extends Sprite {
private double selfAngle, rotationSpeed;
private Random r;
public Astroid(int x, int y, int w, int h, int acc, String imgName, double angle, int size) {
super(x, y, w, h, acc, imgName, angle, size);
selfAngle = 0;
r= new Random();
rotationSpeed = ((r.nextDouble()*10) % 3) + 0.01;
}
@Override
public void update() {
locX += acceleration * Math.cos(Math.toRadians(angle));
locY -= acceleration * (-1 * Math.sin(Math.toRadians(angle)));
outOfScreeFix();
selfAngle+=rotationSpeed;
}
@Override
public void drawSprite(Graphics g, JPanel p) {
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
g2d.drawImage(bImage, (int)locX, (int)locY, p);
g2d.rotate(-1 * Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
}
}

44
src/Sprites/Fog.java Normal file
View file

@ -0,0 +1,44 @@
package Sprites;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Fog extends Sprite {
private double selfAngle, rotationSpeed;
private float alphaChannel;
private Random r;
public Fog(int x, int y, int w, int h, int acc, String imgName, double angle, int size) {
super(x, y, w, h, acc, imgName, angle, size);
selfAngle = 0;
r= new Random();
rotationSpeed = 0;
alphaChannel = r.nextFloat()-0.04f;
}
@Override
public void update() {
locX += acceleration * Math.cos(Math.toRadians(angle));
locY -= acceleration * (-1 * Math.sin(Math.toRadians(angle)));
outOfScreeFix();
selfAngle+=rotationSpeed;
}
@Override
public void drawSprite(Graphics g, JPanel p) {
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaChannel));
g2d.drawImage(bImage, (int)locX, (int)locY, p);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
g2d.rotate(-1 * Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
}
}

View file

@ -0,0 +1,32 @@
package Sprites;
import javax.swing.*;
import java.awt.*;
/**
* Created by sagi on 12/18/15.
*/
public class LaserBlast extends Sprite {
public LaserBlast(int x, int y, int w, int h, int acc, String imgName, double angle, int size) {
super(x, y, w, h, acc, imgName, angle, size);
}
@Override
public void update() {
locX += acceleration * Math.cos(Math.toRadians(angle));
locY -= acceleration * (-1 * Math.sin(Math.toRadians(angle)));
outOfScreeFix();
}
@Override
public void drawSprite(Graphics g, JPanel p) {
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
g2d.drawImage(bImage, (int)locX, (int)locY, p);
g2d.rotate(-1 * Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
}
}

80
src/Sprites/Ship.java Normal file
View file

@ -0,0 +1,80 @@
package Sprites;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* Created by sagi on 12/18/15.
*/
public class Ship extends Sprite {
private final int STOP=0, UP=1, DOWN=-1, TURN_SPEED=10;
private final double MAX_SPEED = 6, SLOWING_FACTOR = 0.1;
private int direction, turnDirection;
private BufferedImage bi;
private double selfAccel;
public Ship(int x, int y, int w, int h, int size) {
super(x, y, w, h, 0, "ship2.png", 0, size);
direction = STOP;
turnDirection = STOP;
selfAccel = acceleration;
}
@Override
public void update() {
setSpeed();
this.angle+=TURN_SPEED*turnDirection;
locX += selfAccel * Math.cos(Math.toRadians(angle));
locY -= selfAccel * (-1 * Math.sin(Math.toRadians(angle)));
outOfScreeFix();
}
private void setSpeed(){
if (direction == UP && !(selfAccel > MAX_SPEED)){
selfAccel+=SLOWING_FACTOR*2;
}
else if (direction == DOWN && (selfAccel > MAX_SPEED*(-1))){
selfAccel-=SLOWING_FACTOR*2;
}
else { //slowing down
if (selfAccel > 0) {
selfAccel -= SLOWING_FACTOR;
if (selfAccel < 0) {
selfAccel = 0;
}
}
if (selfAccel < 0) {
selfAccel += SLOWING_FACTOR;
if (selfAccel > 0) {
selfAccel = 0;
}
}
}
}
public void setDirection(int direction){
this.direction = direction;
}
public void drawSprite(Graphics g, JPanel panel) {
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
g2d.drawImage(bImage, (int)locX, (int)locY, panel);
g2d.rotate(-1*Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
}
public void turnShip(int direction){
turnDirection=direction;
}
}

117
src/Sprites/Sprite.java Normal file
View file

@ -0,0 +1,117 @@
package Sprites;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public abstract class Sprite {
protected BufferedImage bImage;
protected int imageWidth, imageHeight; // image dimensions
protected double locX, locY;
protected int acceleration;
protected int pWidth, pHeight, size; // panel's dimensions
protected double angle;
private final String BASE_PATH = System.getProperty("user.dir") + File.separator +"src"+ File.separator;
public Sprite(int x, int y, int w, int h, int acc, String imgName, double angle, int size)
{
this.size = size;
locX = x;
locY = y;
acceleration = acc;
pWidth = w;
pHeight = h;
try {///home/sagi/Development/JCE/Astroids2D/src/Images/ship.gif
bImage = ImageIO.read(getClass().getResource("/Images/"+imgName));
}catch (IOException pin){
pin.printStackTrace();
bImage = null;
}
this.angle = angle;
setImageDimensions();
}
public void setImageDimensions()
{
Image tmp = bImage.getScaledInstance(size,size, Image.SCALE_SMOOTH);
BufferedImage bi = new BufferedImage(size,size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(tmp,0,0,null);
g2d.dispose();
bImage = bi;
}
public abstract void update();
// public Rectangle getBoundingBox()
// {
// return new Rectangle(getLocX(), getLocY(), imageWidth, imageHeight);
// }
public double getLocX() {
return locX;
}
public double getLocY() {
return locY;
}
public int getAcceleration(){
return acceleration;
}
public BufferedImage getbImage()
{
return bImage;
}
public int getImageWidth() {
return imageWidth;
}
public int getImageHeight()
{
return imageHeight;
}
public double getAngle(){
return angle;
}
protected void outOfScreeFix(){ //its not a bug it's a feature
if(locX < 0 - size)
locX = pWidth;
else if (locX > pWidth+size)
locX = 0-size;
if(locY < 0 - size)
locY = pHeight;
else if(locY > pHeight+size)
locY = 0-size ;
}
public Rectangle getBounds() {
Rectangle r = new Rectangle((int)locX, (int)locY, size, size);
return r;
}
public int getSize(){
return size;
}
public abstract void drawSprite(Graphics g, JPanel p);
}