private void spawnRandomers() { for (int i = 0; i < randomN; i++) { float x = (float) Math.random() * width; float y = (float) Math.random() * height; float r = (float) Math.sqrt( Math.pow(((Player) players.get(0)).getX() - x, 2) + Math.pow(((Player) players.get(0)).getY() - x, 2)); while (r < distanceLimit) { x = (float) Math.random() * width; y = (float) Math.random() * height; r = (float) Math.sqrt( Math.pow(((Player) players.get(0)).getX() - x, 2) + Math.pow(((Player) players.get(0)).getY() - y, 2)); } enemies.add(new EnemyTypes.Random(x, y, 0.5f, borders)); } spawnRandomersB = false; }
public void paintComponent(Graphics g) { super.paintComponent(g); height = this.getHeight(); width = this.getWidth(); drawPlayers(g); g.setColor(Color.WHITE); g.drawString("Score", width - 60, 25); g.drawString(String.valueOf(score), width - 60, 40); if (countdownF) { g.drawString(String.valueOf(counterN), width / 2 - 10, height / 2); } else { if ((spawnCircleB) && (spawnIncrease)) { spawnCircles(); } if (spawnMonsterB) { spawnMonsters(); } if (spawnRandomersB) { spawnRandomers(); } if (spawnRainB) { spawnRain(); } if (spawnBombB()) { spawnBomb(); } try { Iterator i = enemies.iterator(); while (i.hasNext()) { Enemy e = (Enemy) i.next(); Iterator j = players.iterator(); while (j.hasNext()) { Player p = (Player) j.next(); e.move(players, programSpeedAdjust /*/players.size()*/); if ((e.collidesWith(p.getX(), p.getY())) && (!p.getImmunity())) { p.decLives(p.getX(), p.getY()); p.setImmunity(true); } } e.paint(g); } } catch (Exception e) { } } drawLayout(g); }
private void movePlayers() { onePlayerAlive = false; Iterator i = players.iterator(); while (i.hasNext()) { Player p = (Player) i.next(); if (p.getLives() == 0) { p.setActive(false); } if (p.isActive()) { onePlayerAlive = true; p.move(); } } }
private void setup() { playerSetup(); deathLocation = new int[4]; Arrays.fill(deathLocation, -1); menu.setVisible(false); this.revalidate(); audioSetup(); Iterator i = players.iterator(); Player p; while (i.hasNext()) { p = (Player) i.next(); p.resetLives(3); p.setActive(true); } BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "BLANK"); this.setCursor(blank); reset(); ballN = 1; level = 1; timeLast = 0; score = 0; counterN = 10; timeCircle = 0; timeCircleSwitch = 0; programLoopCounter = 1; programSpeedAdjust = 1; onePlayerAlive = true; countdownF = true; circular = true; spawnIncrease = true; spawnCircleB = false; spawnMonsterB = false; spawnRandomersB = false; levelSetup(); countdown(); animate(); }
private void drawPlayers(Graphics g) { Iterator i = players.iterator(); Player p; while (i.hasNext()) { p = (Player) i.next(); if (p.isActive()) { if (p.getImmunity()) { g.setColor(Color.red.brighter()); g.drawOval(p.getX() - 30, p.getY() - 30, 60, 60); } g.setColor(Color.WHITE); g.fillOval(p.getX() - 5, p.getY() - 5, 10, 10); } p.paint(g); } }
private void spawnCircles() { if (circular) { for (int i = 0; i < ballN; i++) { double degree = Math.random() * 2 * Math.PI; float x = ((Player) players.get(0)).getX() + distance * (float) Math.sin(degree * i); float y = ((Player) players.get(0)).getY() + distance * (float) Math.cos(degree * i); enemies.add(new EnemyTypes.Circle(x, y, invSpeed)); } } else { for (int i = 1; i < (ballN / 2); i++) { float x = (i * 2 * width) / (ballN); float y = 0; enemies.add(new EnemyTypes.Circle(x, y, invSpeed)); } for (int i = (ballN / 2) + 1; i < ballN; i++) { float x = ((i - ballN / 2) * 2 * width) / ballN; float y = height; enemies.add(new EnemyTypes.Circle(x, y, invSpeed)); } } spawnIncrease = false; }
private void playerSetup() { players = new ArrayList(twoPlayerRB.isSelected() ? 2 : 1); if (mouseRB.isSelected()) { MouseControlledPlayer p1 = new MouseControlledPlayer(width / 2, height / 2, 3, true, this, 3, 1, width); addMouseMotionListener(p1); addMouseListener(p1); players.add(p1); } else { KeyboardControlledPlayer p1 = new KeyboardControlledPlayer( width / 2, height / 2, 3, true, this, 3, KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_S, KeyEvent.VK_D, keyboardSpeedS1.getValue(), KeyEvent.VK_SPACE, KeyEvent.VK_F, 1, width); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p1); players.add(p1); } if (twoPlayerRB.isSelected()) { if (mouseRB.isSelected()) { KeyboardControlledPlayer p2 = new KeyboardControlledPlayer( width / 2, height / 2, 3, true, this, 3, KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_S, KeyEvent.VK_D, keyboardSpeedS2.getValue(), KeyEvent.VK_SPACE, KeyEvent.VK_F, 2, width); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p2); players.add(p2); } else { KeyboardControlledPlayer p2 = new KeyboardControlledPlayer( width / 2, height / 2, 3, true, this, 3, KeyEvent.VK_NUMPAD8, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD5, KeyEvent.VK_NUMPAD6, keyboardSpeedS2.getValue(), KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD1, 2, width); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p2); players.add(p2); } } }
private void drawLayout(Graphics g) { g.setColor(Color.blue); g.drawRect(10, 10, width - 20, height - 20); g.setColor(Color.red.brighter()); Iterator f = players.iterator(); int lifeDisplayPosition = 0; while (f.hasNext()) { Player h = (Player) f.next(); if (h.isActive()) { lifeDisplayPosition++; String lifeInformation = "Player " + lifeDisplayPosition + ": "; for (int i = 0; i < h.getLives(); i++) { lifeInformation += "\u2606"; } g.drawString(lifeInformation, 20, lifeDisplayPosition * 40); } } borders[2] = width - 20; borders[3] = height - 20; Iterator i = players.iterator(); Player p; while (i.hasNext()) { p = (Player) i.next(); if (p.isActive() && !countdownF && (p.getX() < borders[0] || p.getY() < borders[1] || p.getX() > borders[2] || p.getY() > borders[3]) && (!p.getImmunity())) { p.decLives(width / 2, height / 2); p.setImmunity(true); } if (p.getLives() <= 0) { if (p instanceof MouseControlledPlayer) { MouseControlledPlayer m = (MouseControlledPlayer) p; removeMouseListener(m); removeMouseMotionListener(m); } else if (p instanceof KeyboardControlledPlayer) { KeyboardFocusManager.getCurrentKeyboardFocusManager() .removeKeyEventDispatcher((KeyboardControlledPlayer) p); } i.remove(); for (int h = 0; h < 4; h++) { if (deathLocation[h] == -1) { deathLocation[h] = p.getX(); deathLocation[h + 1] = p.getY(); break; } } } } if ((deathLocation[0] != -1) && (deathLocation[1] != -1)) { g.drawLine( deathLocation[0] - 15, deathLocation[1] - 15, deathLocation[0] + 15, deathLocation[1] + 15); g.drawLine( deathLocation[0] + 15, deathLocation[1] - 15, deathLocation[0] - 15, deathLocation[1] + 15); } if ((deathLocation[2] != -1) && (deathLocation[3] != -1)) { g.drawLine( deathLocation[2] - 15, deathLocation[3] - 15, deathLocation[2] + 15, deathLocation[3] + 15); g.drawLine( deathLocation[2] + 15, deathLocation[3] - 15, deathLocation[2] - 15, deathLocation[3] + 15); } if (!onePlayerAlive) { Font old = g.getFont(); g.setFont(new Font("monospaced", Font.BOLD, 20)); g.setColor(Color.red.darker()); g.drawString("GAME OVER", width / 2 - 40, height / 2); g.setFont(old); g.setColor(Color.WHITE); g.drawString("Score", width - 60, 25); g.drawString(String.valueOf(score), width - 60, 40); } }