1 Replies - 272 Views - Last Post: 27 September 2013 - 07:42 PM Rate Topic: -----

#1 shortbus  Icon User is online

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 163
  • Joined: 26-March 13

trying to make a shooting mechanic in this java asteroids game.

Posted 27 September 2013 - 07:03 PM

ive been working on this basic frame work for this java asteroids game for about a day now and i need help making it so when i press the space bar 10 circles come up and move up the y axis in intervals of 10.
here is my code so far.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import javax.swing.JOptionPane;

/**
 *
 * @author josh
 */
public class Test extends Applet implements KeyListener {
    Mechanics tools = new Mechanics();
    Timer t = new Timer();
    int x = 420; // sets x pos for player
    int y = 450; // sets y pos for player
    int x1 = 900; // sets x pos for rect
    int y1 = 500; 
    int rx = 1;
    int ry = 400;
    int w = 895;
    int h= 600;
    int W = 0;
    int cx = x;
    int cy = y - 30;
    boolean win = false;
    boolean ready, shot = false;
    
    int[] shots = new int[10];
    public void init() {
        setSize(x1,y1);
        this.requestFocus();
        addKeyListener(this);
        
    }
    public void paint(Graphics g){
        setBackground(Color.BLACK);
        g.setColor(Color.WHITE);
        g.fillOval(x,y,20,20);
        g.drawString("Position " + x + " " + y, 40,40 );
        g.drawString("Lives = " + tools.getLives(),30,20);
       
            
        if(shot){
        for(int a = 0; a< shots.length; a++){
            cy = cy += 10;
            g.drawOval(cx, cy, 5, 5);
            repaint();
        }
        }
    
       
    }
  
  
    
    

    @Override
    public void keyPressed(KeyEvent ke) {
       if(ke.getKeyCode() == KeyEvent.VK_SPACE){
            
                 shot = true;
             
           
       }
    
       
     if(ke.getKeyCode() == KeyEvent.VK_A){
         if(x >= 1){ // checks collision with the left wall
         x = x -10;
         }
        
     }
       if(ke.getKeyCode() == KeyEvent.VK_D){
           if(x <= 835){ // need to make variables for these values 
           x = x + 10;
           
           }
          if(x >= 800 - 24){ //no idea why this works!!!
             //tools.win();
             // how do it make it so ican make a new level?
             win = true;
          }
       }
        repaint();
    
      
    }

    @Override
    public void keyReleased(KeyEvent ke) {
          if(ke.getKeyCode() == KeyEvent.VK_SPACE){
            
           
            shot = false;
          }
          repaint();   
       }
          
    
    

    @Override
    public void keyTyped(KeyEvent ke) {
    
    }

}


any help would be great because i dont even know where to begin.

Is This A Good Question/Topic? 0
  • +

Replies To: trying to make a shooting mechanic in this java asteroids game.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: trying to make a shooting mechanic in this java asteroids game.

Posted 27 September 2013 - 07:42 PM

You've been around long enough to know not to duplicate post. It shouldn't come as a surprise that I'm closing this thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1