Results 1 to 3 of 3
  1. #1
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default Sprites - My Game (Sort of)

    I'm new here and I'd like to start things off right by letting you have my game. It's kind of like space invaders. All the enemy sprites are stored in an ArrayList. All the missiles are stored in a separate ArrayList. The player has a rectangular boundary that he can't go outside of. This keeps him in the JFrame. Upon reaching the edge of the boundary the player will stop. Enemies will bounce off the side of the JFrame and reverse their velocity.

    I developed my own sprite class for the game. Missiles could be implemented as sprites but I used a separate missile class for them.

    I used the same image for the missiles of the enemy and the player so it looks like the enemy is shooting the missile backwards. You can change it simply by using a parameter for the image or a setImage() method. That's up to you.

    Arrow keys move, space fires. If the player holds space the missiles fire in a very rapid succession. I'll probably change that eventually to have a slower fire-rate.

    If you die the window closes. If you kill all the enemies the window closes.

    Feel free to use the source in whatever way you like. Edit, copy, whatever. The source code is partially commented and it's pretty easy to understand. If you want something explained or expanded upon or if you have any questions feel free to ask them here.


    If you like the source then please let me know.

    Screenshot:


    Download
    Last edited by David M.; 04-23-2011 at 06:42 PM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Nice game, but how about Levels and a Level Editor rather than exiting the program once all the enemies are dead!

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Tidied up your Sprite.java
    Java Code:
    import java.awt.event.KeyEvent;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    
    public class Sprite {
    
     /** used to determine sprite position */
     private double xVel, yVel;
     private int xPos, yPos, boundAction;
     private Rectangle bounds;
     /** sprite display */
     private java.awt.Image image;
     private boolean visibility;
     /** missiles */
     private ArrayList<Missile> missiles;
    
     /** constructor */
     Sprite (String imagePath, int x, int y, double velocityX, double velocityY, Rectangle boardBounds, int boundAction) {
         xPos = x;
         yPos = y;
         xVel = velocityX;
         yVel = velocityY;
         bounds = boardBounds;
         this.boundAction = boundAction;
         image = new javax.swing.ImageIcon(getClass().getResource(imagePath)).getImage();
         missiles = new ArrayList<Missile>();
         visibility = true;
     }
    
     public void move() {
       xPos += (int) xVel; //increase x position by x velocity
       yPos += (int) yVel; //increase y position by y velocity
    
       int x1 = (int) bounds.getX(),
           x2 = (int) bounds.getWidth()-getWidth(),
           y1 = (int) bounds.getY(),
           y2 = (int) bounds.getHeight()-getHeight();
    
       boolean hitLeft = xPos < x1,
               hitRight = xPos > x2,
               hitTop = yPos < y1,
               hitBottom = yPos > y2;
    
       switch (boundAction) {
         case 1: //"stop"
           if (hitLeft || hitRight) {
             xVel = 0;
             xPos = hitLeft? x1 : x2;
           }
           if (hitTop || hitBottom) {
             yVel = 0;
             yPos = hitTop? y1 : y2;
           }
           break;
         case 2: //"bounce"
           if (hitLeft || hitRight) {
             xVel = -xVel;
             xPos = hitLeft? x1 : x2;
           }
           if (hitTop || hitBottom) {
             yVel = -yVel;
             yPos = hitTop? y1 : y2;
           }
           break;
         case 3: //"warp"
           if (hitLeft) xPos = x2;
           if (hitRight) xPos = x1;
           if (hitTop) yPos = y2;
           if (hitBottom) yPos = y1;
           break;
       }
     }
    
     public void fire(int ySpeed) {
       Missile m = new Missile(xPos+getWidth()/2, yPos, ySpeed);
       missiles.add(m);
     }
    
     /** getters */
     public int getXPos() {return xPos;}
     public int getYPos() {return yPos;}
     public int getWidth() {return image.getWidth(null);}
     public int getHeight() {return image.getHeight(null);}
     public double getVelocityX() {return xVel;}
     public double getVelocityY() {return yVel;}
     public Rectangle getBounds() {return bounds;}
     public java.awt.Image getImage() {return image;}
     public ArrayList<Missile> getMissiles() {return missiles;}
     public boolean isVisible() {return visibility;}
    
     /** setters */
     public void setVelocityX(double velocityX) {xVel = velocityX;}
     public void setVelocityY(double velocityY) {yVel = velocityY;}
     public void setBounds(Rectangle rectangle) {bounds = rectangle;}
     public void setVisibility(boolean visible) {visibility = visible;}
    
     /** event handlers */
     public void keyPressed(KeyEvent e) {
       int key = e.getKeyCode();
       switch (key) {
         case KeyEvent.VK_SPACE: fire(-4); break;
         case KeyEvent.VK_LEFT: xVel = -2; break;
         case KeyEvent.VK_RIGHT: xVel = 2; break;
         case KeyEvent.VK_UP: yVel = -2; break;
         case KeyEvent.VK_DOWN: yVel = 2; break;
       }
     }
    
     public void keyReleased(KeyEvent e) {
       int key = e.getKeyCode();
       switch (key) {
         case KeyEvent.VK_LEFT:
         case KeyEvent.VK_RIGHT:
           xVel = 0;
           break;
         case KeyEvent.VK_UP:
         case KeyEvent.VK_DOWN:
           yVel = 0;
           break;
       }
     }
    
    }
    GameBoard.java:164
    Java Code:
    enemyCol.setVisibility(false);
    GameBoard.java:180
    Java Code:
    player.setVisibility(false);

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 09:06 AM
  3. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM
  4. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM
  5. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •