@Override public void keyPressed(KeyEvent ke) { if (ke.getKeyCode() == KeyEvent.VK_LEFT) { ship.rotateLeft(); } else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) { ship.rotateRight(); } else if (ke.getKeyCode() == KeyEvent.VK_UP) { ship.accelerate(); } repaint(); }
@Override public void mouseClicked(MouseEvent me) { if (gameOver == true) { startWave(wave); } else { ship.accelerate(); } repaint(); }
public void startWave(int nextWave) { gameOver = false; gameWon = false; wave = nextWave; waveMessageCounter = 30; if (wave == 1) { ship.y = 720; aliens = new Alien[25]; for (int i = 0; i < aliens.length; i++) { aliens[i] = new PlainAlien(); } } if (wave == 2) { ship.y = 720; aliens = new Alien[25]; for (int i = 0; i < aliens.length; i++) { aliens[i] = new RedAlien(); } } if (wave == 3) { ship.y = 720; aliens = new Alien[25]; for (int i = 0; i < aliens.length; i++) { aliens[i] = new Alien3(); } } if (wave == 4) { ship.y = 720; aliens = new Alien[25]; for (int i = 0; i < aliens.length; i++) { aliens[i] = new Alien4(); } } if (wave == 5) gameWon = true; }
@Override public void actionPerformed(ActionEvent ae) { // Update the ship ship.update(ship); if (gameOver || gameWon) { repaint(); return; } waveMessageCounter -= 1; // Update the aliens for (int i = 0; i < aliens.length; i++) { aliens[i].update(ship); } // Count the visible aliens int visibleAliens = 0; for (int i = 0; i < aliens.length; i++) { aliens[i].update(ship); if (aliens[i].visible) { visibleAliens++; } } // If there are no more aliens visible, start the next wave if (ship.y <= 0) { startWave(wave + 1); } // Check if the ship has collided with any of the aliens for (int i = 0; i < aliens.length; i++) { // Check for a collision of the alien with the ship if (aliens[i].visible && ship.x < aliens[i].x + aliens[i].w && aliens[i].x < ship.x + ship.w && ship.y < aliens[i].y + aliens[i].h && aliens[i].y < ship.y + ship.h) { gameOver = true; } } repaint(); }
@Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; background.paint(g2); ship.paint(g2); for (int i = 0; i < aliens.length; i++) { aliens[i].paint(g2); } if (gameOver) { drawMessage(g2, "You've been hit!"); } else if (gameWon) { drawMessage(g2, "Congratulations, you've escaped!"); } else if (waveMessageCounter > 0) { drawMessage(g2, "Wave " + wave); } }
void turnOnTurretReceiver() throws IOException { Global.log("Turning on turret receiver..."); while (true) { if (isDisposed()) throw new IOException("Client game disposed"); Global.log("Connecting to port " + Global.turretPort() + "..."); try { turretStream = new ClientByteStream(ip, Global.turretPort(), Ship.bufferSize()); break; } catch (IOException ex) { } } Global.log("Connected!"); timers.add( new FixedTimer( new FixedTask() { public boolean fixedRate() { return false; } public float FPS() { return Global.ReceiveFPS; } public void run() { byte[] data = null; String name = null; try { data = turretStream.read(); name = turretStream.readLine(); } catch (IOException ex) { System.err.println("Cannot read info from turret stream"); Global.onException(); stop(); return; } if (data == null) return; Ship s = new Ship(name, Global.transparent); s.setDesign(new Design.Turret(s)); s.fromBytes(data, false); addShip(s); } })); }
void createBytePorts() throws IOException { Global.log("Creating byte ports..."); int n = numPlayers(); playerByteStreams = new ClientByteStream[n]; for (int i = 0; i < n; ++i) { ClientByteStream stream = null; while (true) { if (isDisposed()) throw new IOException("Client game disposed"); Global.log("Connecting to port " + Global.playerPort(i) + "..."); try { stream = playerByteStreams[i] = new ClientByteStream(ip, Global.playerPort(i), Ship.bufferSize()); break; } catch (IOException ex) { } } Global.log("Connected!"); Global.log("Sending client information..."); // first byte is player ID stream.out.write((byte) playerID); stream.out.flush(); Global.log("Done!"); } Global.log("Done creating byte ports!"); }
private void majgrid1(Player player) { gridtest = new Grid(); gridtest = player.getGridJoueur(); ShipType type = ShipType.AIRCRAFT_CARRIER; Ship ship = new Ship(type); Orientation orient = Orientation.HORIZONTAL; for (int i = 0; i < 10; i++) // ligne { for (int j = 0; j < 10; j++) // colonne { if (gridtest.getCell(i, j) instanceof OceanCell) { tabbout1[i][j].setIcon(iconeau); } // else if(gridtest.getCell(i, j) instanceof TouchOceanCell) // { // tabbout1[i][j].setIcon(iconeautouch); // } // else if(gridtest.getCell(i, j) instanceof TouchOceanCell) // { // tabbout1[i][j].setIcon(icondebristouch); // } else if (gridtest.getCell(i, j) instanceof ShipCell) { ship = ((ShipCell) gridtest.getCell(i, j)).getShip(); int size = ship.getSize(); orient = ((ShipCell) gridtest.getCell(i, j)).getOrient(); if (orient.isHorizontal()) { if (ship.getSize() == 2) { tabbout1[i][j].setIcon(iconsub1h); tabbout1[i][j + 1].setIcon(iconsub2h); tabbout1[i][j + 2].setIcon(iconsub3h); j = j + 2; } else if (ship.getSize() == 3) // battleship { tabbout1[i][j].setIcon(iconbattle1h); tabbout1[i][j + 1].setIcon(iconbattle2h); tabbout1[i][j + 2].setIcon(iconbattle3h); tabbout1[i][j + 3].setIcon(iconbattle4h); j = j + 3; } else { tabbout1[i][j].setIcon(iconair1h); tabbout1[i][j + 1].setIcon(iconair2h); tabbout1[i][j + 2].setIcon(iconair3h); tabbout1[i][j + 3].setIcon(iconair4h); tabbout1[i][j + 4].setIcon(iconair5h); j = j + 4; } } } } for (int i2 = 0; i2 < 10; i2++) // ligne { for (int j2 = 0; j2 < 10; j2++) // colonne { if (gridtest.getCell(i2, j2) instanceof ShipCell) { ship = ((ShipCell) gridtest.getCell(i2, j2)).getShip(); orient = ((ShipCell) gridtest.getCell(i2, j2)).getOrient(); if (!orient.isHorizontal()) { if (ship.getSize() == 2) { tabbout1[i2][j2].setIcon(iconsub1v); tabbout1[i2 + 1][j2].setIcon(iconsub2v); tabbout1[i2 + 2][j2].setIcon(iconsub3v); i2 = i2 + 2; } else if (ship.getSize() == 3) // battleship { tabbout1[i2][j2].setIcon(iconbattle1v); tabbout1[i2 + 1][j2].setIcon(iconbattle2v); tabbout1[i2 + 2][j2].setIcon(iconbattle3v); tabbout1[i2 + 3][j2].setIcon(iconbattle4v); i2 = i2 + 3; } else { tabbout1[i2][j2].setIcon(iconair1v); tabbout1[i2 + 1][j2].setIcon(iconair2v); tabbout1[i2 + 2][j2].setIcon(iconair3v); tabbout1[i2 + 3][j2].setIcon(iconair4v); tabbout1[i2 + 4][j2].setIcon(iconair5v); i2 = i2 + 4; } } } } } } }