![]() |
|
[Source] Space Warrior - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE) +--- Thread: [Source] Space Warrior (/Thread-Source-Space-Warrior) |
[Source] Space Warrior - Solixious - 07-29-2013 I made this game of Space Warrior in java recently and would like to share with you guys.. Please do provide feedback on it. Screenshot : ![]() Source : SpaceWarrior.java Code: /*Made by : Solixious Klein*/
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SpaceWarrior extends JPanel implements ActionListener
{
private static final long serialVersionUID = -1995860998543589555L;
private JFrame fr;
private Image spaceCraft,missile;
private SpaceCraft sc;
private Timer timer;
private ArrayList<Boulder> boulders;
private ArrayList<Shine> shines;
private int boulderRate=150;
private int ctr=0,score=0,highScore=0;
private boolean gameOver;
private int bouldersDestroyed,bouldersPopulation;
private boolean paused,startScreen;
public SpaceWarrior()
{
startScreen=true;
paused=false;
bouldersDestroyed=0;
bouldersPopulation=0;
highScore=10000;
gameOver=false;
boulders=new ArrayList<Boulder>();
shines=new ArrayList<Shine>();
sc=new SpaceCraft();
spaceCraft=sc.getImage();
missile=new Missile(sc).getImage();
fr=new JFrame("Space Warrior");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(new TAdapter());
fr.add(this);
fr.setSize(500,500);
fr.setResizable(false);
fr.setLocationRelativeTo(null);
fr.setVisible(true);
setFocusable(true);
setDoubleBuffered(true);
timer=new Timer(9,this);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SpaceWarrior();
}
});
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.black);
g2d.fillRect(0, 0, 500, 500);
ArrayList<Missile> missiles=sc.getMissiles();
int size=shines.size();
for(int i=0;i<shines.size();i++) //draw the shining stars
{
Shine s=shines.get(i);
g2d.drawImage(s.getImage(), s.getXPos(), s.getYPos(), this);
}
size=missiles.size();
for(int i=0;i<size;i++) //draw the missiles fired on screen
{
Missile m=missiles.get(i);
g2d.drawImage(missile, m.getXPos(), m.getYPos(), this);
g2d.drawImage(missile, m.getXPos()+20, m.getYPos(), this);
}
size=boulders.size();
for(int i=0;i<size;i++)
{
Boulder b=boulders.get(i);
if(b.isVisible())
{
g2d.drawImage(b.getImage(), b.getXPos(), b.getYPos(), this);
}
}
g2d.drawImage(spaceCraft, sc.getXPos(), sc.getYPos(), this);
g.setColor(Color.white);
g2d.drawString("Score : "+score, 10, 20);
g2d.drawString("High Score : "+highScore,350,20);
if(startScreen)
{
g.drawString("Press Enter To Start", 200, 200);
}
if(gameOver)
{
g.drawString("Game Over", 220, 200);
g.drawString("Hit Enter To Play Again", 190, 230);
g.drawString("Your Stats :-", 70,300);
g.drawString("Boulders Encountered : "+ bouldersPopulation,70,330);
g.drawString("Boulders Destroyed : " + bouldersDestroyed, 70, 360);
}
if(paused)
{
g.drawString("Paused", 220, 200);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
@Override
public void update(Graphics g)
{
paint(g);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(!gameOver)
{
ctr++;
if(ctr%300==0)
shines.add(new Shine());
for(int i=0;i<shines.size();i++)
{
Shine s=shines.get(i);
s.move();
if(!s.isVisible())
shines.remove(s);
}
ArrayList<Missile> missiles=sc.getMissiles();
for(int i=0;i<missiles.size() && !missiles.isEmpty();i++)
{
Missile m=missiles.get(i);
if(m.isVisible())
{
m.move();
}
else
{
missiles.remove(m);
}
}
if(ctr%boulderRate==0)
{
Boulder b=new Boulder(sc);
boulders.add(b);
bouldersPopulation++;
}
for(int i=0;i<boulders.size() && !boulders.isEmpty();i++)
{
Boulder b=boulders.get(i);
if(b.isVisible())
{
b.move();
}
else
{
boulders.remove(b); //remove the boulder from list if it is not on screen
}
for(int j=0;j<missiles.size();j++)
{
Missile m=missiles.get(j);
if(m.getXPos()-b.getXPos()<=40 && m.getXPos()-b.getXPos()>=-20 && m.getYPos()-b.getYPos()<=20 && m.getYPos()-b.getYPos()>=-20) //check if a missile hit a boulder
{
b.hitMissile();//missile hit the boulder, so reduce its health
missiles.remove(m);
if(b.getHealth()<=0) //check if the boulder hit is destroyed
{
if(b.getHealth()>-5)
{
bouldersDestroyed++;
}
b.explode();
score+=b.getScore();
}
}
}
if(b.getXPos()-sc.getXPos()<=20 && b.getXPos()-sc.getXPos()>=-40 && b.getYPos()-sc.getYPos()<=20 && b.getYPos()-sc.getYPos()>=-20 && b.getHealth()>0) //check for collision of boulders and space craft
{
gameOver=true;
}
}
sc.move(); //moves the space craft
if(score>highScore) //update highScore if necessary
highScore=score;
/*updates level of play according to current score*/
if(score>=500)
boulderRate=100;
if(score>=10000)
boulderRate=75;
if(score>=25000)
boulderRate=50;
if(score>=50000)
boulderRate=25;
repaint();
}
}
private class TAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if(gameOver && e.getKeyCode()==KeyEvent.VK_ENTER)
{
gameOver=false;
score=0;
boulderRate=200;
bouldersPopulation=0;
bouldersDestroyed=0;
boulders=new ArrayList<Boulder>();
shines=new ArrayList<Shine>();
sc=new SpaceCraft();
}
else if(startScreen && e.getKeyCode()==KeyEvent.VK_ENTER)
{
startScreen=false;
timer.start();
}
else if(!gameOver && e.getKeyCode()==KeyEvent.VK_P)
{
if(paused)
{
paused=false;
repaint();
timer.restart();
}
else
{
paused=true;
repaint();
timer.stop();
}
}
else
{
sc.keyPressed(e);
}
}
@Override
public void keyReleased(KeyEvent e) {
sc.KeyReleased(e);
}
}
}SpaceCraft.java Code: import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class SpaceCraft {
private int xPos,yPos;
private String imageFile="craft.png";
private Image image;
private int dx=0,dy=0;
private ArrayList<Missile> missiles;
public SpaceCraft()
{
xPos=232;
yPos=220;
image=new ImageIcon(this.getClass().getResource(imageFile)).getImage();
missiles=new ArrayList<Missile>();
}
public Image getImage()
{
return image;
}
public int getXPos()
{
return xPos;
}
public int getYPos()
{
return yPos;
}
public void move()
{
if(xPos+dx>=0 && xPos+dx<=465)
xPos=xPos+dx;
if(yPos+dy>=0 && yPos+dy<=440)
yPos=yPos+dy;
}
public void fire()
{
missiles.add(new Missile(this));
}
public ArrayList<Missile> getMissiles()
{
return missiles;
}
public void keyPressed(KeyEvent e)
{
int key=e.getKeyCode();
if(key==KeyEvent.VK_UP)
dy=-2;
if(key==KeyEvent.VK_DOWN)
dy=2;
if(key==KeyEvent.VK_RIGHT)
dx=2;
if(key==KeyEvent.VK_LEFT)
dx=-2;
if(key==KeyEvent.VK_SPACE)
fire();
}
public void KeyReleased(KeyEvent e)
{
int key=e.getKeyCode();
if(key==KeyEvent.VK_UP || key== KeyEvent.VK_DOWN)
dy=0;
if(key==KeyEvent.VK_LEFT || key== KeyEvent.VK_RIGHT)
dx=0;
}
}Missile.java Code: import java.awt.Image;
import javax.swing.ImageIcon;
public class Missile
{
private String missile="missile.png";
private Image image;
private int SPEED=10;
private int x,y;
public Missile(SpaceCraft sc)
{
x=sc.getXPos();
y=sc.getYPos();
image=new ImageIcon(this.getClass().getResource(missile)).getImage();
}
public Image getImage()
{
return image;
}
public void move()
{
y-=SPEED;
}
public int getXPos()
{
return x;
}
public int getYPos()
{
return y;
}
public boolean isVisible()
{
return (y>=0);
}
public void setSpeed(int speed)
{
SPEED=speed;
}
}Shine.java Code: import java.awt.Image;
import javax.swing.ImageIcon;
public class Shine
{
private String[] fileNames={"shine1.png","shine2.png","shine3.png"};
private Image image;
private int x,y;
private int SPEED=1;
private boolean visible;
private int ctr=0;
public Shine()
{
SPEED=(int)(Math.random()*4+1);
x=(int)(Math.random()*440);
y=0;
image=new ImageIcon(this.getClass().getResource(fileNames[(int)(Math.random()*3)])).getImage();
visible=true;
}
public void move()
{
ctr++;
if(ctr==10)
{
y+=SPEED;
ctr=0;
}
if(y>465)
visible=false;
}
public Image getImage()
{
return image;
}
public int getXPos()
{
return x;
}
public int getYPos()
{
return y;
}
public boolean isVisible()
{
return visible;
}
}Download Runnable Jar File and Images Here : SpaceWarrior.rar (new link) Space Warrior.rar (old link) RE: [Source] Space Warrior - Psycho_Coder - 07-29-2013 Hi @Solixious, The game looks good and I played and it works good. But I have some comments. In SpaceWarrior.java Here the modifiers are missing. You can use private. Code: ArrayList<Boulder> boulders;
ArrayList<Shine> shines;Java initializes int variables with 0 so no need of initializing them with 0 again. However there isn't a problem if you do so. Code: private int ctr=0,score=0,highScore=0;Now in your constructor SpaceWarrior you have initialized the variables but I would prefer using the this keyword. I think it must be used. Also you have created the basic frame of the game inside it. I would prefer creating a separate method and initializing it there and then calling that method from the constructor. Doing it this way increases readability. Here is what I was expecting :- Code: public SpaceWarrior()
{
this.highScore=5000;
this.gameOver=false;
this.boulders=new ArrayList<Boulder>();
this.shines=new ArrayList<Shine>();
sc=new SpaceCraft();
this.spaceCraft=sc.getImage();
this.missile=new Missile(sc).getImage();
initializeframe();
this.timer=new Timer(10,this);
timer.start();
}
private void initializeframe(){
fr=new JFrame("Space Warrior");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(new TAdapter());
fr.add(this);
fr.setSize(500,500);
fr.setResizable(false);
fr.setLocationRelativeTo(null);
fr.setVisible(true);
setFocusable(true);
setDoubleBuffered(true);
}@Override notation must be added to the following :- paint, update, actionPerformed, keyPressed, and keyReleased In SpaceCraft.java Same as mention earlier. Use a modifier for ArrayList<Missile> and fire method (Instead of public). In the keyPressed method, why not use switch I think using switch would be a better option. Something like :- Code: public void keyPressed(KeyEvent e)
{
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_UP : dy=-2;break;
case KeyEvent.VK_DOWN : dy=+2;break;
case KeyEvent.VK_RIGHT : dx=+2;break;
case KeyEvent.VK_LEFT : dx=-2;break;
case KeyEvent.VK_SPACE : fire();
}
}Others :- I see that you have used an option of health in your code. Then why not display your health. If the player sees that his health has decreased he will take more precautions and will play carefully. So you must display the health. All the above comments are meant for my own understanding of your code. I said these with a view to learn something new and if I am wrong then please don't hesitate and tell me. It will help me improve. Thank you, Sincerely, Psycho_Coder. RE: [Source] Space Warrior - Solixious - 07-29-2013 (07-29-2013, 09:58 AM)Psycho_Coder Wrote: Hi @Solixious, Oops. I guess I missed it. I had plans of modifying changing them later, but it got out of my mind.. ![]() Quote:Java initializes int variables with 0 so no need of initializing them with 0 again. However there isn't a problem if you do so. An old habit from C. I don't see the problem in it though. ![]() Quote:Now in your constructor SpaceWarrior you have initialized the variables but I would prefer using the this keyword. I think it must be used. Also you have created the basic frame of the game inside it. I would prefer creating a separate method and initializing it there and then calling that method from the constructor. Doing it this way increases readability. I guess 'this' keyword is a necessity when their are two variables with same name in the very scope? Quote:@Override notation must be added to the following :- paint, update, actionPerformed, keyPressed, and keyReleased I missed @Override again.. Sorry for that ![]() Quote:Others :- It isn't the player for whom I've used health, it is the boulders.. That is to determine how many hits of bullet would break the boulder. Cheers
RE: [Source] Space Warrior - Psycho_Coder - 07-29-2013 @Solixious I made a mistake as well regarding that health sorry I overlooked it hehe. RE: [Source] Space Warrior - Solixious - 07-29-2013 (07-29-2013, 10:29 AM)Psycho_Coder Wrote: @Solixious I made a mistake as well regarding that health sorry I overlooked it hehe. Don't sweat on it.. Anyone can make mistakes :Smile: RE: [Source] Space Warrior - Deque - 07-30-2013 Sweet game. I suggest you read this article: http://www.java-gaming.org/index.php?topic=24220.0 It is about game loops and matches my observation that most people use the Timer class for a game loop. I think it can be sufficient for a little game like this. But maybe you want to do something bigger some day. Game engines can also be a great help. I.e. Slick2d, JMonkey RE: [Source] Space Warrior - Solixious - 07-30-2013 (07-30-2013, 07:07 PM)Deque Wrote: Sweet game. Hi.. thanks a lot for the link. I'm reading it now and it seems to be wonderful. :Smile: RE: [Source] Space Warrior - noize - 07-30-2013 The game looks good, although, there's something I'd like to bring to your attention. 1. I just easily (and boredly) got 3962400 points. How? I merely blasted a few asteroids and then hid myself in the warm bottom left corner keeping my finger on the whitespace bar. I can't say how much I've spent like that, I was really hoping for something to hit me as soon as possible. You might maybe fix this by temporarily increasingly increasing the number of asteroids spawning in that row, just until the user presses the left/right key (check for the user to actually move (i.e. it's not valid if he's on the right side and presses the right key)). Don't get instantly back to the same number of asteroids as in the other rows, just start decreasing. So, to sum up, slowly increase the number of asteroids in the row the ship is (right from the moment when the user gets in a position) and start decreasing if he moves away. 2. If you do as above mentioned, people won't be able to get more than 20000-30000 points in any way at all. At that point asteroids start becoming too many and there is no way to survive. Well done, really. RE: [Source] Space Warrior - Solixious - 07-31-2013 (07-30-2013, 10:18 PM)noize Wrote: The game looks good, although, there's something I'd like to bring to your attention. Thank you for your feedback. I'll work on fixing that glitch soon enough :Smile: RE: [Source] Space Warrior - unnamed - 07-31-2013 Hey, I just downloaded your game and although it is really fun I have some input to make
Overall a great mini-game , keep up your good work. |