public void nudge(int i) { x[i] += (double) rand.nextInt(1000) / 8756; y[i] += (double) rand.nextInt(1000) / 5432; int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10); scale[i] = (double) tmpScale / 10; int nudgeX = (int) (((double) getWidth() / 2) * .8); int nudgeY = (int) (((double) getHeight() / 2) * .60); xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX; yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY; }
public void shuffle() { for (int i = 0; i < 3; i++) { for (int j = 0; j < num_cards; j++) { int k = Math.abs(rand.nextInt() % num_cards); /* swap */ int temp = deck[j]; deck[j] = deck[k]; deck[k] = temp; } } current = 0; }
private GObject checkCorner(double x, double y) { GObject obj = getElementAt(x, y); // check the corner for GObject if (obj == paddle) { vy = -Math.abs(vy); PrecisionPaddle(); } else if (obj != null && obj != lifeCount) { // check if the ball hits a brick remove(obj); vy = -1.05 * vy; vx = 1.05 * vx; brickCount--; AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au"); bounceClip.play(); } return obj; }
public void paint(Graphics g) { // int numDoors=0; int rX = player.getRelativeX(); int rY = player.getRelativeY(); boolean notInBuilding = true; bufferGraphics.clearRect(0, 0, dim.width, dim.width); bufferGraphics.setColor(Color.WHITE); r2.setLocation(20 + rX, 20 + rY); // Iterator<Objects> iterator1 = entities.iterator();//size is the last index, therefore it // starts at the end // while(iterator1.hasNext()){ // iterator1.next().update(rX, rY); // bufferGraphics.drawImage(iterator1.next().image, iterator1.next().blockX+rX, // iterator1.next().blockX+rY, null); // } for (int x = 0; x < buildings.size(); x++) { buildings.get(x).update(rX, rY); player.onDoor(player.r, buildings.get(x).door); player.collideOn(player.r, buildings.get(x)); if (player.inHouse(player.r, buildings.get(x).inside)) { notInBuilding = false; } // if(notInBuilding){ // bufferGraphics.drawImage(buildings.get(x).image, // buildings.get(x).blockX+rX,buildings.get(x).blockY+rY, null); // }else{ // bufferGraphics.drawImage(buildings.get(x).innerImage, // buildings.get(x).blockX+rX,buildings.get(x).blockY+rY, null); // } // bufferGraphics.fillRect(entities.get(x).bounds.x, entities.get(x).bounds.y, // entities.get(x).bounds.width, entities.get(x).bounds.height); } bufferGraphics.setFont(new Font("Arial", Font.PLAIN, 20)); // String m1 = new Boolean(player.canMove('w')).toString(); bufferGraphics.drawString(message, 0, 170); // bufferGraphics.fillRect(r2.x, r2.y, r2.width, r2.height); // player.collideOn(player.r, r2); // ***Image log1 = new ImageIcon(getClass().getResource("logTest.png")).getImage(); // ***bufferGraphics.drawImage(log1, (int)r2.getX(), (int)r2.getY(), null); // bufferGraphics.drawLine((int)player.N.getX1(), (int)player.N.getY1(), (int)player.N.getX2(), // (int)player.N.getY2()); // bufferGraphics.drawLine((int)player.E.getX1(), (int)player.E.getY1(), (int)player.E.getX2(), // (int)player.E.getY2()); // bufferGraphics.drawLine((int)player.S.getX1(), (int)player.S.getY1(), (int)player.S.getX2(), // (int)player.S.getY2()); // bufferGraphics.drawLine((int)player.W.getX1(), (int)player.W.getY1(), (int)player.W.getX2(), // (int)player.W.getY2()); // bufferGraphics.setColor(Color.RED); // bufferGraphics.drawLine((int)player.NI.getX1(), (int)player.NI.getY1(), // (int)player.NI.getX2(), (int)player.NI.getY2()); // bufferGraphics.drawLine((int)player.EI.getX1(), (int)player.EI.getY1(), // (int)player.EI.getX2(), (int)player.EI.getY2()); // bufferGraphics.drawLine((int)player.SI.getX1(), (int)player.SI.getY1(), // (int)player.SI.getX2(), (int)player.SI.getY2()); // bufferGraphics.drawLine((int)player.WI.getX1(), (int)player.WI.getY1(), // (int)player.WI.getX2(), (int)player.WI.getY2()); if (notInBuilding) { Image thebg = new ImageIcon(getClass().getResource("grassbackingdev.png")).getImage(); bufferGraphics.drawImage(thebg, 0 + rX, 0 + rY, null); } player.update(); int setX = Math.abs(player.relativeX); int setY = Math.abs(player.relativeY); if (player.relativeX > 0) { setX = -setX; } if (player.relativeY > 0) { setY = -setY; } if (notInBuilding) { for (int x = 0; x < entities.size(); x++) { entities.get(x).update(rX, rY); bufferGraphics.drawImage( entities.get(x).image, entities.get(x).blockX + rX, entities.get(x).blockY + rY, null); player.collideOn(player.r, entities.get(x)); if (entities.get(x).health <= 0) { if (entities.get(x).material == "Wood") wood += entities.get(x).matsReturn; if (entities.get(x).material == "Stone") stone += entities.get(x).matsReturn; entities.remove(x); } // bufferGraphics.fillRect(entities.get(x).bounds.x, entities.get(x).bounds.y, // entities.get(x).bounds.width, entities.get(x).bounds.height); } } for (int x = 0; x < buildings.size(); x++) { if (notInBuilding) { bufferGraphics.drawImage( buildings.get(x).image, buildings.get(x).blockX + rX, buildings.get(x).blockY + rY, null); } else { bufferGraphics.drawImage( buildings.get(x).innerImage, buildings.get(x).blockX + rX, buildings.get(x).blockY + rY, null); } } if (notInBuilding) { for (int x = 0; x < npcArray.size(); x++) { for (int e = 0; e < entities.size(); e++) { npcArray.get(x).detectEntities(entities.get(e)); if (entities.get(e).health <= 0) { entities.remove(e); } } npcArray.get(x).update(rX, rY, setX + 92, setY + 82); bufferGraphics.drawImage( npcArray.get(x).graphic, npcArray.get(x).npcX + rX, npcArray.get(x).npcY + rY, null); if (npcArray.get(x).isDead()) { npcArray.remove(x); } else if (npcArray.get(x).hitBox.intersects(player.r)) { player.health -= 25; npcArray.remove(x); } } } for (int x = 0; x < playerAttack.size(); x++) { playerAttack.get(x).update(rX, rY); bufferGraphics.drawImage( playerAttack.get(x).graphic, playerAttack.get(x).currentX + rX, playerAttack.get(x).currentY + rY, null); for (int y = 0; y < npcArray.size(); y++) { if (playerAttack.get(x).hitBox.intersects(npcArray.get(y).hitBox)) { npcArray.get(y).health -= 25; playerAttack.get(x).hasHit = true; } } if (playerAttack.get(x).isDead()) { playerAttack.remove(x); } } player.notOnDoor = true; if (player.moving) { bufferGraphics.drawImage( player.getImage().getImage(), player.getCharx(), player.getChary(), null); // TODO } else { bufferGraphics.drawImage(player.getIdle(), player.getCharx(), player.getChary(), null); } if (Ghosting) { if (player.face == 1) { bufferGraphics.drawImage(player.getResource(resource), 104, 87, null); // EAST } if (player.face == 2) { bufferGraphics.drawImage(player.getResource(resource), 94, 106, null); // SOUTH*94,106 } if (player.face == 3) { bufferGraphics.drawImage(player.getResource(resource), 84, 87, null); // WEST84,87 } if (player.face == 4) { bufferGraphics.drawImage(player.getResource(resource), 94, 75, null); // NORTH94,75 } } if (player.health <= 0) { bufferGraphics.drawString("You are dead", 10, 90); stop(); } bufferGraphics.setFont(new Font("Arial", Font.PLAIN, 9)); bufferGraphics.setColor(new Color(160, 82, 45)); bufferGraphics.fillRect(0, 2, 8, 8); bufferGraphics.setColor(new Color(105, 105, 105)); bufferGraphics.fillRect(0, 12, 8, 8); bufferGraphics.drawImage(statusBar, 0, 22, null); bufferGraphics.setColor(Color.WHITE); bufferGraphics.drawString(String.format("%d", wood), 18, 10); bufferGraphics.drawString(String.format("%d", stone), 18, 20); bufferGraphics.drawString(String.format("%d", player.health), 18, 30); // bufferGraphics.fillRect((int)player.r.getX(), (int)player.r.getY(), // (int)player.r.getWidth(),(int)player.r.getHeight()); // bufferGraphics.fillRect(player.r.x, player.r.x, player.r.width, player.r.height); g.drawImage(offScreen, 0, 0, this); // if(drawConsole){ // message = String.format("wood: %d", wood); // } }
@Override public void keyPressed(KeyEvent e) { int index = e.getKeyCode(); switch (index) { case KeyEvent.VK_ESCAPE: stop(); break; case KeyEvent.VK_TAB: stop(); break; case KeyEvent.VK_W: player.moving = true; player.moveN(); break; case KeyEvent.VK_A: player.moving = true; player.moveW(); break; case KeyEvent.VK_S: player.moving = true; player.moveS(); break; case KeyEvent.VK_D: player.moving = true; player.moveE(); break; case KeyEvent.VK_E: player.hasTool = true; break; case KeyEvent.VK_Q: if (getResource(resource) > 0) { int setX = Math.abs(player.relativeX); int setY = Math.abs(player.relativeY); if (player.relativeX > 0) { setX = -setX; } if (player.relativeY > 0) { setY = -setY; } if (resource <= 1) { if (player.face == 1) { entities.add(new basicBlock(104 + setX, 87 + setY, resource, 'e')); // EAST } if (player.face == 2) { entities.add(new basicBlock(94 + setX, 106 + setY, resource, 's')); // SOUTH* } if (player.face == 3) { entities.add(new basicBlock(84 + setX, 87 + setY, resource, 'w')); // WEST } if (player.face == 4) { entities.add(new basicBlock(94 + setX, 75 + setY, resource, 'n')); // NORTH } } else { if (player.face == 1) { entities.add(new attackBlock(104 + setX, 87 + setY, resource)); // EAST } if (player.face == 2) { entities.add(new attackBlock(94 + setX, 106 + setY, resource)); // SOUTH* } if (player.face == 3) { entities.add(new attackBlock(84 + setX, 87 + setY, resource)); // WEST } if (player.face == 4) { entities.add(new attackBlock(94 + setX, 75 + setY, resource)); // NORTH } } // if(drawConsole){ // message = "Block added"; // } takeResource(resource); } break; case KeyEvent.VK_R: if (Ghosting == false) { Ghosting = true; } else { Ghosting = false; } break; case KeyEvent.VK_BACK_QUOTE: if (drawConsole) { drawConsole = false; } else { drawConsole = true; } break; case KeyEvent.VK_1: resource = 0; break; case KeyEvent.VK_2: resource = 1; break; case KeyEvent.VK_3: resource = 2; break; case KeyEvent.VK_SPACE: int setX = Math.abs(player.relativeX); int setY = Math.abs(player.relativeY); if (player.relativeX > 0) { setX = -setX; } if (player.relativeY > 0) { setY = -setY; } playerAttack.add(new Missiles(98 + setX, 85 + setY, 0, player.face)); break; case KeyEvent.VK_ENTER: int i = randometer.nextInt(101); int i2 = randometer.nextInt(101); npcArray.add(new Penguins(250 + i, 250 + i2, penguinAnimation)); break; default: // String.format("pressed %s", KeyEvent.getKeyText(index)); } // message = String.format("pressed %s", KeyEvent.getKeyText(index)); }
private TetrisPiece randomPiece() { int rand = Math.abs(random.nextInt()); return new TetrisPiece(rand % (PIECE_COLORS.length)); }