/** Checks if the player collides with a blip, power blip, or a ghost. */ public void checkCollision() { Rectangle playerRect = new Rectangle(player.getCenter().x - 8, player.getCenter().y - 8, 13, 13); for (Enemy enemy : enemies) { Rectangle enemyRect = new Rectangle(enemy.getCenter().x - 8, enemy.getCenter().y - 8, 13, 13); if (playerRect.intersects(enemyRect)) { if (!enemy.isAlive()) { continue; } if (Globals.state == ENEMY_HUNTER_STATE) { Globals.game.playerKilled(); player.loseALife(); // player is dead, so we paint the dead player screen paintDeadPlayer(); try { // sleep to make it stay on screen Thread.sleep(1500); } catch (InterruptedException e) { // DO nothing } resetCanvas(); // we return because only one enemy should be able to make a player lose a life return; } else if (Globals.state == ENEMY_HUNTED_STATE) { if (enemy.isAlive()) { enemy.setAlive(false); player.addPoints(POINTS_FOR_KILLING_ENEMY); } } } else { continue; } } }
/** * Creates a new "game" from the current engine.Globals.game variable. While the ANN stays the * same, the speed, actor positions, score, et cetera, are all reset. */ public void newGame() { stopped = true; player.setLives(STARTING_LIVES); player.setScore(0); Graphics g = strategy.getDrawGraphics(); waiting = true; Globals.state = ENEMY_HUNTER_STATE; player.setCenter(board.getPlayerStartPoint()); player.setDirection(Player.DEFAULT_STARTING_DIRECTION); board.reset(); Globals.blipsLeft = Globals.game.getBoard().getBlipCount(); for (Enemy enemy : enemies) { enemy.reset(); } GamePainters.drawBoard(board, g, this); GamePainters.drawEnemies(enemies, g, this); GamePainters.drawPlayer(player, g, this); GamePainters.paintBottomBar(player, board, g, this); strategy.show(); }
public void actionPerformed(ActionEvent action) { player.Update(); viewScreen.CenterOnObject(player.getBoundingBox()); map.centerOnWorldCoordinates( new Point((int) player.getBoundingBox().x, (int) player.getBoundingBox().y)); collisionManager.handleCollisions(); this.repaint(); this.requestFocus(); }
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(); }
/* applet init event. Should initialize the applet. */ public void init() { backbuffer = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB); g2d = backbuffer.createGraphics(); iconSheet = getImage(this.getClass().getResource("data/icons2.png")); wateringCanImage = getImageFromIconSheet(0, 0); wateringSplashImage = getImageFromIconSheet(1, 0); flowerImage = getImageFromIconSheet(2, 0); seedImage = getImageFromIconSheet(3, 0); skullImage = getImageFromIconSheet(4, 0); tapImage = getImageFromIconSheet(5, 0); playerImage = getImageFromIconSheet(0, 1); flowerStalkImage = getImageFromIconSheet(2, 1); clockImage = getImageFromIconSheet(4, 1); tapSplashImage = getImageFromIconSheet(5, 1); dirtImage = getImageFromIconSheet(0, 2); brickImage = getImageFromIconSheet(0, 3); player.image = playerImage; pickupAudio = getAudioClip(this.getClass().getResource("data/Pickup.wav")); for (int i = 0; i < 14; i++) { flowerList[i] = new Flower(); flowerList[i].height = rand.nextInt(14); } addKeyListener(this); }
public void captureImage() { String savepath = this.saveDirectory + "\\cam" + this.getDateFormatNow("yyyyMMdd_HHmmss-S") + ".jpg"; System.out.println("Capturing current image to " + savepath); // Grab a frame FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl"); buf = fgc.grabFrame(); // Convert it to an image btoi = new BufferToImage((VideoFormat) buf.getFormat()); img = btoi.createImage(buf); // save image saveJPG(img, savepath); // show the image // imgpanel.setImage(img); // images.add(img); images.add(savepath); if (images_lastadded.size() >= lastadded_max) { // Remove last images_lastadded.remove(images_lastadded.size() - 1); } images_lastadded.add(0, images.size() - 1); images_nevershown.add(0, images.size() - 1); forceNewImage(); }
public void keyPressed(KeyEvent k) { int keycode = k.getKeyCode(); switch (keycode) { case KeyEvent.VK_LEFT: player.moveLeft(); break; case KeyEvent.VK_RIGHT: player.moveRight(); case KeyEvent.VK_SPACE: player.water(); break; case KeyEvent.VK_ENTER: player.plantSeed(); pickupAudio.play(); break; } }
/** * Causes the ghosts and player to go back to their original starting positions. Used when a * player dies, for example. */ public void resetCanvas() { Graphics g = strategy.getDrawGraphics(); waiting = true; Globals.state = ENEMY_HUNTER_STATE; player.setCenter(board.getPlayerStartPoint()); player.setDirection(Player.DEFAULT_STARTING_DIRECTION); for (Enemy enemy : enemies) { enemy.reset(); } GamePainters.drawBoard(board, g, this); GamePainters.drawEnemies(enemies, g, this); GamePainters.drawPlayer(player, g, this); GamePainters.paintBottomBar(player, board, g, this); strategy.show(); }
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 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); } }
protected void layout1024v2() { // Resolution of the camera pictures divided by 2 // 320x240, Creative camera for layout1280 size_x = 320; size_y = 240; sizeCaptureWindow_x = size_x; sizeCaptureWindow_y = size_y; imagepanels = new WebcamCaptureAndFadeImagePanel[1]; imagepanels[0] = new WebcamCaptureAndFadeImagePanel(1, 1, size_x, size_y); setLayout(new BorderLayout()); setSize((4 + 2 + 4) * size_x, (6) * size_y); add(imagepanels[0], BorderLayout.WEST); if ((comp = player.getVisualComponent()) != null) { add(comp, BorderLayout.EAST); } enable_datetext = false; enable_forceNewImage = true; enable_forceNewImageNow = true; captureWindow = false; txt_location_x = txt_location_y = 0; txt_size_x = 18; txt_size_y = size_y; number_of_frames_betweencaptures = 0 * fps; // Number of frame to wait between captures number_of_frames_before_cwopen = 0 * fps; // Number of frames to wait before we allow capturewindow to be opened number_of_frames_showimage = 600 * fps; // Number of frames to hold the image before fading to next number_of_frames_redborder = (int) 0.5 * fps; // Number of frames the red border should last, -1 to disable number_of_second_showcapturetext = 0.1; // Number of seconds to show a text after capture, 0 for none fade_per_frame = 0.50f; // Short fading }
protected void layout1280() { // Resolution of the camera pictures divided by 2 // 320x240, Creative camera for layout1280 size_x = 320 / 2; size_y = 240 / 2; sizeCaptureWindow_x = size_x * 2; sizeCaptureWindow_y = size_y * 2; imagepanels = new WebcamCaptureAndFadeImagePanel[4]; imagepanels[0] = new WebcamCaptureAndFadeImagePanel(3, 8, size_x, size_y); imagepanels[1] = new WebcamCaptureAndFadeImagePanel(2, 3, size_x, size_y); imagepanels[2] = new WebcamCaptureAndFadeImagePanel(2, 3, size_x, size_y); imagepanels[3] = new WebcamCaptureAndFadeImagePanel(3, 8, size_x, size_y); setLayout(new BorderLayout()); setSize((4 + 2 + 4) * size_x, (6) * size_y); add(imagepanels[0], BorderLayout.WEST); JPanel middle = new JPanel(new BorderLayout()); middle.add(imagepanels[1], BorderLayout.NORTH); if ((comp = player.getVisualComponent()) != null) { middle.add(comp, BorderLayout.CENTER); } middle.add(imagepanels[2], BorderLayout.SOUTH); middle.setSize(new Dimension(4 * size_x, 2 * size_y)); add(middle, BorderLayout.CENTER); add(imagepanels[3], BorderLayout.EAST); enable_datetext = false; enable_forceNewImage = false; captureWindow = false; txt_location_x = txt_location_y = 0; txt_size_x = 18; txt_size_y = size_y; }
public void updateObjects() { player.tick(); }
/** * This starts the main game loop. This is the guts of the game. When the loop starts it checks to * see if the player has any lives left, if not, it prints "Game Over" and resets the game. If * there are lives left, it decides what the ghosts next move will be, detects any collisions, and * acts accordingly, and also gets input from the player */ public void start() { Globals.state = ENEMY_HUNTER_STATE; Globals.game.setPlayer(new Player()); player = Globals.game.getPlayer(); player.setCenter(board.getPlayerStartPoint()); newGame(); paused = false; waiting = true; Point regenDoorPoint = board.getRegenDoor(); Point regenPoint = board.getRegenPoint(); Point playerPoint = player.getCenter(); while ((this.isVisible()) && (player.getLives() >= 0)) { if (paused) { paintPauseScreen(); } while (paused || stopped || waiting) { Thread.yield(); } if (Globals.blipsLeft <= 0) { Globals.speed *= SPEED_MOD; resetCanvas(); board.reset(); Globals.blipsLeft = board.getBlipCount(); } // now we need to see if the current state is enemy hunted if (Globals.state == ENEMY_HUNTED_STATE) { // now we need to see if we should go out of it if (Globals.timeUntilLeaveingHuntedState <= 0) { Globals.state = ENEMY_HUNTER_STATE; Globals.timeUntilLeaveingHuntedState = TIME_IN_HUNTED_STATE; } else { // we shouldn't, so we decrement the counter Globals.timeUntilLeaveingHuntedState -= Globals.speed; } } // move the player player.move(board.getMoveOptions(player), playerChoice); if (Globals.aiMode == FSA_AI_MODE) { // now updated the enemy movements for (int i = 0; i < enemies.length; i++) { // these are put here to reduce program jumping and cache misses as well Enemy enemy = enemies[i]; int moveOptions = board.getMoveOptions(enemy); // first, we need to take apporpriate actions if we're on a regen tile if (board.getCurrentTile(enemy).isRegen()) { // first check to see if the enemy is alive if (!enemy.isAlive()) { // it's not, so we make it alive enemy.setAlive(true); } // now we tell the enemy to move towards the the door enemy.move(moveOptions, regenDoorPoint, true); } else { // We're not in the regen pen, so now we check if the enemy is alive if (!enemy.isAlive()) { // it's not, so we want to go towards the regen point enemy.move(moveOptions, regenPoint, false); } else { // the enemy is alive, so we tell it towards/away from the player enemy.move(moveOptions, playerPoint, false); } } } } else if (Globals.aiMode == ANN_AI_MODE) { // Globals.game.getNet().process(ANNTools.getBoardCondition(board, enemies, player)); player.setInputValues(board); for (Enemy enemy : enemies) { enemy.setInputValues(board); } Globals.game.getNet().generateActivations(); for (int i = 0; i < enemies.length; i++) { // Last 2 values don't matter here enemies[i].move( board.getMoveOptions(enemies[i]), board.getRegenDoor(), board.getCurrentTile(enemies[i]).isRegen()); } } // no we check for collisions and act appropriately checkCollision(); // check to see if we should notify the game that we have stepped if (Globals.trainingMode != TRAINING_MODE_OFF) { // we should, so we do. Globals.game.step(); } // And now we sleep. The sleep time is the current speed try { Thread.sleep(Globals.speed); } catch (InterruptedException e) { break; } // no update the screen update(); } if (player.getLives() <= 0) { paintGameOver(); try { Thread.sleep(2500); } catch (InterruptedException e) { // DO nothing } newGame(); start(); } }
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); } }
public WebcamCaptureAndFadePanel(String saveDir, String layout) { System.out.println("Using " + saveDir + " as directory for the images."); saveDirectory = saveDir; getImages(); images_used = new ArrayList<Integer>(); images_lastadded = new ArrayList<Integer>(); images_nevershown = new ArrayList<Integer>(); Vector devices = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); Enumeration enumeration = devices.elements(); System.out.println("- Available cameras -"); ArrayList<String> names = new ArrayList<String>(); while (enumeration.hasMoreElements()) { CaptureDeviceInfo cdi = (CaptureDeviceInfo) enumeration.nextElement(); String name = cdi.getName(); if (name.startsWith("vfw:")) { names.add(name); System.out.println(name); } } // String str1 = "vfw:Logitech USB Video Camera:0"; // String str2 = "vfw:Microsoft WDM Image Capture (Win32):0"; if (names.size() == 0) { JOptionPane.showMessageDialog( null, "Ingen kamera funnet. " + "Du må koble til et kamera for å kjøre programmet.", "Feil", JOptionPane.ERROR_MESSAGE); System.exit(0); } else if (names.size() > 1) { JOptionPane.showMessageDialog( null, "Fant mer enn 1 kamera. " + "Velger da:\n" + names.get(0), "Advarsel", JOptionPane.WARNING_MESSAGE); } String str2 = names.get(0); di = CaptureDeviceManager.getDevice(str2); ml = di.getLocator(); try { player = Manager.createRealizedPlayer(ml); formatControl = (FormatControl) player.getControl("javax.media.control.FormatControl"); /* Format[] formats = formatControl.getSupportedFormats(); for (int i=0; i<formats.length; i++) System.out.println(formats[i].toString()); */ player.start(); } catch (javax.media.NoPlayerException e) { JOptionPane.showMessageDialog( null, "Klarer ikke å starte" + " programmet pga. feil med kamera. Sjekk at det er koblet til.", "IOException", JOptionPane.ERROR_MESSAGE); System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(0); } /* * Layout * * Add * - comp * - imagepanels */ if (layout.equals("1024v2")) { layout1024v2(); } else if (layout.equals("1280")) { layout1280(); } else { layout1024(); } // Capture Window if (captureWindow) { cw = new JFrame("Capture from webcam"); cw.setAlwaysOnTop(true); cw.setSize(sizeCaptureWindow_x, sizeCaptureWindow_y); cw.addKeyListener(new captureWindowKeyListner()); cw.setUndecorated(true); // Add webcam if ((comp = player.getVisualComponent()) != null) { cw.add(comp); } // Add panel to window and set location of window cw.setLocation(cwLocation_x, cwLocation_y); } // Text window cwText = new rotatedText(""); /* * Timer for update */ Timer thread = new Timer(); thread.schedule(new frameUpdateTask(), 0, (1000 / fps)); }
public void paintEntities() { player.Draw(backBuffer, viewScreen.getScreen()); }
public static void playerclose() { player.close(); player.deallocate(); }