public void keyPressed(KeyEvent e) { if (screen.equals("menu")) { } else if (screen.equals("gamepage")) { if (!start) { if (e.getKeyCode() == KeyEvent.VK_S) { start = true; } } else if (start) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { keys[0] = true; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { keys[1] = true; } if (e.getKeyCode() == KeyEvent.VK_UP) { keys[2] = true; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { keys[3] = true; } if (e.getKeyCode() == KeyEvent.VK_SPACE) { // keys[4] = true; } if (e.getKeyCode() == KeyEvent.VK_S) // S = "Stats" -ship info, alien info, ammo info { PrintStats(); } if (e.getKeyCode() == KeyEvent.VK_P) { start = false; Graphics g = getGraphics(); m.drawMenu(g); } repaint(); } } }
public void paint(Graphics window) { if (screen.equals("menu")) { } else if (screen.equals("gamepage")) { // set up the double buffering to make the game animation nice and smooth Graphics2D twoDGraph = (Graphics2D) window; // take a snap shop of the current screen and same it as an image // that is the exact same width and height as the current screen if (back == null) back = (BufferedImage) (createImage(getWidth(), getHeight())); // create a graphics reference to the back ground image // we will draw all changes on the background image Graphics graphToBack = back.createGraphics(); graphToBack.setColor(Color.BLUE); graphToBack.drawString("StarFighter ", 25, 50); graphToBack.setColor(Color.BLACK); graphToBack.fillRect(0, 0, 800, 600); m.drawgamescreen(graphToBack); if (keys[0] == true && ship.getX() > 0 - 20) { ship.move("LEFT"); } if (keys[1] == true && ship.getX() < 800 - 80) { ship.move("RIGHT"); } if (keys[2] == true && ship.getY() > 0 - 10) { ship.move("UP"); } if (keys[3] == true && ship.getY() < 600 - 100) { ship.move("DOWN"); } if (keys[4] == true) { shots.add(new Ammo(ship.getX() + 40, ship.getY() - 80, 1)); // orig speed 25 keys[4] = false; } // add code to move stuff // update ship movement ship.draw(graphToBack); // update alien movement for (Alien a : aliens) { a.draw(graphToBack); } // update Ammo movement for (Ammo ammo : shots) { if (ammo.isalive()) { ammo.draw(graphToBack); } } if (false) // { int highy = 0; int xcord = 0; for (Alien a : aliens) { if (a.getY() > highy && a.getY() < 500) { highy = a.getY(); xcord = a.getX(); } } ship.setX(xcord); keys[4] = true; } if (Threadnum % 100 == 0) { // if (canshoot) keys[4] = true; // canshoot = !canshoot; } if (Threadnum % 1 == 0) { // if (canshoot) { for (Ammo ammo : shots) { if (ammo.isalive()) ammo.setY(ammo.getY() - 10); } } // canshoot = !canshoot; } if (Threadnum % ((random(1, 6) * 300)) == 0) // every 2-5 second add a new wave of aliens { addmorealiens(); } if (Threadnum % 10 == 0) // if (Threadnum%100==0) { for (Alien alien : aliens) { if (alien.getresistance() != 0) { alien.scootback(2); alien.setY(alien.getY() - 2); } else { alien.setY(alien.getY() + (random(0, 2) * 1)); int randx = (random(0, 3)); int dir = random(1, 2); if (dir == 1) { if (alien.getX() - randx > 0) { alien.setX(alien.getX() - randx); } } if (dir == 2) { if (alien.getX() + randx < xlimit) { alien.setX(alien.getX() + randx); } } // alien.setY(alien.getY() + 2); } } } if (aliens.size() == 0) { // addmorealiens(); } // add in collision detection for (Ammo am : shots) // remove dead aliens { if (am.isdead()) { shots.remove(am); } } for (Alien a : aliens) // remove dead aliens { if (a.isdead()) { aliens.remove(a); } } for (Alien a : aliens) // remove past aliens (collsion detection between aliens and wall) { if (a.getY() > 600) { a.setY(100); // aliens.remove(a); } } for (Alien alien : aliens) // remove dead aliens and hit ammo(collision detection between aliens and ammo) { for (Ammo ammo : shots) { if (Math.abs(ammo.getX() - alien.getX()) < 80 && Math.abs(ammo.getY() - alien.getY()) < 80 && ammo.isalive()) // &&alien.getY()==ammo.getY()) { ammo.kill(); hitnum++; ammosupply--; alien.movebackmore(); alien.kill(); System.out.println("alien hit! " + gethitnum()); // alien.setY(alien.getY()-2); } // removedead(); } } if (aliens.size() == 0) { // addmorealiens(); } // m.drawgamescreen (graphToBack); // twoDGraph.drawImage(back, null, 0, 0); } }