Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- --------------------------------------------------------------------------------
- AssassinIzdari
- v1.0 - ArmageddonMUD Board Game (http://www.armageddon.org)
- --------------------------------------------------------------------------------
- */
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.util.*;
- public class AssassinIzdari extends Applet implements KeyListener {
- private static int cursorX;
- private static int cursorY;
- private static boolean cursorRight;
- private static boolean cursorLeft;
- private static boolean cursorDown;
- private static boolean cursorUp;
- private static boolean pieceSelected;
- private static int pieceIndex;
- private static int pieceTeam;
- private static boolean stopFlicker;
- private static Vector pieces;
- private final Color backgroundColor = new Color(100, 0, 0);
- public void init() {
- this.setBackground(this.backgroundColor);
- this.addKeyListener(this);
- this.requestFocus();
- this.cursorX = 4;
- this.cursorY = 3;
- this.cursorRight = false;
- this.cursorLeft = false;
- this.cursorDown = false;
- this.cursorUp = false;
- this.pieceSelected = false;
- this.pieceIndex = 0;
- this.pieceTeam = 0;
- this.stopFlicker = false;
- this.pieces = new Vector();
- /* Loading the pieces. Done in such a way that wagons are
- on top. */
- Image icon;
- icon = getImage(getCodeBase(), "dune.gif");
- this.pieces.addElement(new Piece(2, 6, icon, 1));
- this.pieces.addElement(new Piece(3, 6, icon, 1));
- this.pieces.addElement(new Piece(6, 6, icon, 1));
- this.pieces.addElement(new Piece(7, 6, icon, 1));
- this.pieces.addElement(new Piece(2, 1, icon, 2));
- this.pieces.addElement(new Piece(3, 1, icon, 2));
- this.pieces.addElement(new Piece(6, 1, icon, 2));
- this.pieces.addElement(new Piece(7, 1, icon, 2));
- icon = getImage(getCodeBase(), "halfling.gif");
- this.pieces.addElement(new Piece(1, 6, icon, 1));
- this.pieces.addElement(new Piece(8, 6, icon, 1));
- this.pieces.addElement(new Piece(1, 1, icon, 2));
- this.pieces.addElement(new Piece(8, 1, icon, 2));
- icon = getImage(getCodeBase(), "archer.gif");
- this.pieces.addElement(new Piece(3, 7, icon, 1));
- this.pieces.addElement(new Piece(6, 7, icon, 1));
- this.pieces.addElement(new Piece(3, 0, icon, 2));
- this.pieces.addElement(new Piece(6, 0, icon, 2));
- icon = getImage(getCodeBase(), "lizard.gif");
- this.pieces.addElement(new Piece(4, 7, icon, 1));
- this.pieces.addElement(new Piece(5, 7, icon, 1));
- this.pieces.addElement(new Piece(4, 0, icon, 2));
- this.pieces.addElement(new Piece(5, 0, icon, 2));
- icon = getImage(getCodeBase(), "templar.gif");
- this.pieces.addElement(new Piece(2, 7, icon, 1));
- this.pieces.addElement(new Piece(7, 0, icon, 2));
- icon = getImage(getCodeBase(), "assassin.gif");
- this.pieces.addElement(new Piece(1, 7, icon, 1));
- this.pieces.addElement(new Piece(8, 7, icon, 1));
- this.pieces.addElement(new Piece(1, 0, icon, 2));
- this.pieces.addElement(new Piece(8, 0, icon, 2));
- icon = getImage(getCodeBase(), "wagon.gif");
- this.pieces.addElement(new Piece(7, 7, icon, 1));
- this.pieces.addElement(new Piece(2, 0, icon, 2));
- repaint();
- }
- public void paint(Graphics graphics) {
- this.update(graphics);
- }
- public void update(Graphics graphics) {
- /* If the "repaint()" is not just a simple case of
- moving a piece or the cursor, then we will want to
- redraw the whole board. This is because the person
- might be switching windows or something similar. */
- if (!this.stopFlicker) {
- this.drawBoard(graphics);
- this.drawPieces(graphics);
- }
- if (this.cursorLeft) {
- this.drawCursor(graphics, true);
- this.cursorX -=1;
- this.cursorLeft = false;
- }
- else if (this.cursorRight) {
- this.drawCursor(graphics, true);
- this.cursorX +=1;
- this.cursorRight = false;
- }
- else if (this.cursorUp) {
- this.drawCursor(graphics, true);
- this.cursorY -=1;
- this.cursorUp = false;
- }
- else if (this.cursorDown) {
- this.drawCursor(graphics, true);
- this.cursorY +=1;
- this.cursorDown = false;
- }
- this.drawCursor(graphics, false);
- this.stopFlicker = false;
- }
- private void drawBoard(Graphics graphics) {
- for (int vertical = 0; vertical < 8; vertical++) {
- for (int horizontal = 0; horizontal < 8; horizontal++) {
- if (vertical % 2 == 0) {
- if (horizontal % 2 == 0)
- graphics.setColor(Color.black);
- else
- graphics.setColor(Color.gray);
- }
- else {
- if (horizontal % 2 == 0)
- graphics.setColor(Color.gray);
- else
- graphics.setColor(Color.black);
- }
- graphics.fillRect(45 + 40 * horizontal, 10 + 40 * vertical, 35, 35);
- }
- }
- }
- private void drawPieces(Graphics graphics) {
- Piece pieceInQuestion;
- for (int index = 0; index < this.pieces.size(); index++) {
- pieceInQuestion = (Piece) this.pieces.elementAt(index);
- graphics.drawImage(pieceInQuestion.returnImage(),
- pieceInQuestion.getDrawX(), pieceInQuestion.getDrawY(), this);
- if (pieceInQuestion.getTeam() == 1)
- graphics.setColor(Color.blue);
- else
- graphics.setColor(Color.red);
- graphics.drawRect(pieceInQuestion.getDrawX() - 1,
- pieceInQuestion.getDrawY() - 1, 30, 30);
- }
- }
- private void drawCursor(Graphics graphics, boolean erase) {
- /* If 'erase' is true, then erase the old cursor. If the
- cursor is dragging a piece, then erase its old version too. */
- if (erase) {
- if (this.pieceSelected) {
- Piece pieceInQuestion = (Piece) this.pieces.elementAt(this.pieceIndex);
- if (pieceInQuestion.getX() == 0
- || pieceInQuestion.getX() == 9)
- graphics.setColor(this.backgroundColor);
- else if (pieceInQuestion.getX() % 2 == 0) {
- if (pieceInQuestion.getY() % 2 == 0)
- graphics.setColor(Color.gray);
- else
- graphics.setColor(Color.black);
- }
- else {
- if (pieceInQuestion.getY() % 2 == 0)
- graphics.setColor(Color.black);
- else
- graphics.setColor(Color.gray);
- }
- graphics.fillRect(pieceInQuestion.getDrawX() - 1, pieceInQuestion.getDrawY() - 1,
- 31, 31);
- }
- graphics.setColor(this.backgroundColor);
- }
- /* If we're not erasing, and no piece if selected, just
- draw the cursor normally. */
- else if (!this.pieceSelected)
- graphics.setColor(Color.white);
- /* In all other scenarios - ie - we're dragging a piece, but
- we're not erasing anything - we need to draw the piece and
- the cursor with an indicitive color. */
- else {
- Piece pieceInQuestion = (Piece) this.pieces.elementAt(this.pieceIndex);
- pieceInQuestion.setX(this.cursorX);
- pieceInQuestion.setY(this.cursorY);
- this.drawPieces(graphics);
- if (this.pieceTeam == 1)
- graphics.setColor(Color.blue);
- else
- graphics.setColor(Color.red);
- }
- graphics.drawRect(4 + 40 * cursorX, 9 + 40 * cursorY, 36, 36);
- }
- public void keyPressed(KeyEvent event) {
- int keyCode = event.getKeyCode();
- if (keyCode == 37 && this.cursorX > 0) // left
- this.cursorLeft = true;
- else if (keyCode == 39 && this.cursorX < 9) // right
- this.cursorRight = true;
- else if (keyCode == 38 && this.cursorY > 0) // up
- this.cursorUp = true;
- else if (keyCode == 40 && this.cursorY < 7) // down
- this.cursorDown = true;
- else if (keyCode == 32) { // space
- if (!this.pieceSelected) {
- Piece pieceInQuestion;
- for (int index = 0; index < this.pieces.size(); index++) {
- pieceInQuestion = (Piece) this.pieces.elementAt(index);
- if (this.cursorX == pieceInQuestion.getX()
- && this.cursorY == pieceInQuestion.getY()) {
- this.pieceSelected = true;
- this.pieceIndex = index;
- this.pieceTeam = pieceInQuestion.getTeam();
- }
- }
- }
- else
- this.pieceSelected = false;
- }
- this.stopFlicker = true;
- repaint();
- }
- public void keyReleased(KeyEvent event) {
- }
- public void keyTyped(KeyEvent event) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement