Advertisement
Guest User

AssassinIzdari v1.0 - ArmageddonMUD Board Game

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.93 KB | None | 0 0
  1.     /*
  2. --------------------------------------------------------------------------------
  3.         AssassinIzdari
  4.        
  5.         v1.0 - ArmageddonMUD Board Game (http://www.armageddon.org)
  6. --------------------------------------------------------------------------------
  7.     */
  8.  
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.applet.Applet;
  12. import java.util.*;
  13.  
  14. public class AssassinIzdari extends Applet implements KeyListener {
  15.  
  16.     private static int cursorX;
  17.     private static int cursorY;
  18.  
  19.     private static boolean cursorRight;
  20.     private static boolean cursorLeft;
  21.     private static boolean cursorDown;
  22.     private static boolean cursorUp;
  23.  
  24.     private static boolean pieceSelected;
  25.     private static int pieceIndex;
  26.     private static int pieceTeam;
  27.  
  28.     private static boolean stopFlicker;
  29.  
  30.     private static Vector pieces;
  31.  
  32.     private final Color backgroundColor = new Color(100, 0, 0);
  33.  
  34.     public void init() {
  35.  
  36.         this.setBackground(this.backgroundColor);
  37.  
  38.         this.addKeyListener(this);
  39.         this.requestFocus();
  40.  
  41.         this.cursorX = 4;
  42.         this.cursorY = 3;
  43.  
  44.         this.cursorRight = false;
  45.         this.cursorLeft = false;
  46.         this.cursorDown = false;
  47.         this.cursorUp = false;
  48.  
  49.         this.pieceSelected = false;
  50.         this.pieceIndex = 0;
  51.         this.pieceTeam = 0;
  52.  
  53.         this.stopFlicker = false;
  54.  
  55.         this.pieces = new Vector();
  56.  
  57.         /* Loading the pieces.  Done in such a way that wagons are
  58.         on top. */
  59.  
  60.         Image icon;
  61.  
  62.         icon = getImage(getCodeBase(), "dune.gif");
  63.         this.pieces.addElement(new Piece(2, 6, icon, 1));
  64.         this.pieces.addElement(new Piece(3, 6, icon, 1));
  65.         this.pieces.addElement(new Piece(6, 6, icon, 1));
  66.         this.pieces.addElement(new Piece(7, 6, icon, 1));
  67.         this.pieces.addElement(new Piece(2, 1, icon, 2));
  68.         this.pieces.addElement(new Piece(3, 1, icon, 2));
  69.         this.pieces.addElement(new Piece(6, 1, icon, 2));
  70.         this.pieces.addElement(new Piece(7, 1, icon, 2));
  71.         icon = getImage(getCodeBase(), "halfling.gif");
  72.         this.pieces.addElement(new Piece(1, 6, icon, 1));
  73.         this.pieces.addElement(new Piece(8, 6, icon, 1));
  74.         this.pieces.addElement(new Piece(1, 1, icon, 2));
  75.         this.pieces.addElement(new Piece(8, 1, icon, 2));
  76.         icon = getImage(getCodeBase(), "archer.gif");
  77.         this.pieces.addElement(new Piece(3, 7, icon, 1));
  78.         this.pieces.addElement(new Piece(6, 7, icon, 1));
  79.         this.pieces.addElement(new Piece(3, 0, icon, 2));
  80.         this.pieces.addElement(new Piece(6, 0, icon, 2));
  81.         icon = getImage(getCodeBase(), "lizard.gif");
  82.         this.pieces.addElement(new Piece(4, 7, icon, 1));
  83.         this.pieces.addElement(new Piece(5, 7, icon, 1));
  84.         this.pieces.addElement(new Piece(4, 0, icon, 2));
  85.         this.pieces.addElement(new Piece(5, 0, icon, 2));
  86.         icon = getImage(getCodeBase(), "templar.gif");
  87.         this.pieces.addElement(new Piece(2, 7, icon, 1));
  88.         this.pieces.addElement(new Piece(7, 0, icon, 2));
  89.         icon = getImage(getCodeBase(), "assassin.gif");
  90.         this.pieces.addElement(new Piece(1, 7, icon, 1));
  91.         this.pieces.addElement(new Piece(8, 7, icon, 1));
  92.         this.pieces.addElement(new Piece(1, 0, icon, 2));
  93.         this.pieces.addElement(new Piece(8, 0, icon, 2));
  94.         icon = getImage(getCodeBase(), "wagon.gif");
  95.         this.pieces.addElement(new Piece(7, 7, icon, 1));
  96.         this.pieces.addElement(new Piece(2, 0, icon, 2));
  97.  
  98.         repaint();
  99.     }
  100.    
  101.     public void paint(Graphics graphics) {
  102.  
  103.         this.update(graphics);
  104.     }
  105.    
  106.     public void update(Graphics graphics) {
  107.  
  108.         /* If the "repaint()" is not just a simple case of
  109.         moving a piece or the cursor, then we will want to
  110.         redraw the whole board.  This is because the person
  111.         might be switching windows or something similar. */
  112.  
  113.         if (!this.stopFlicker) {
  114.             this.drawBoard(graphics);
  115.             this.drawPieces(graphics);
  116.         }
  117.  
  118.         if (this.cursorLeft) {
  119.             this.drawCursor(graphics, true);
  120.             this.cursorX -=1;
  121.             this.cursorLeft = false;
  122.         }
  123.         else if (this.cursorRight) {
  124.             this.drawCursor(graphics, true);
  125.             this.cursorX +=1;
  126.             this.cursorRight = false;
  127.         }
  128.         else if (this.cursorUp) {
  129.             this.drawCursor(graphics, true);
  130.             this.cursorY -=1;
  131.             this.cursorUp = false;
  132.         }
  133.         else if (this.cursorDown) {
  134.             this.drawCursor(graphics, true);
  135.             this.cursorY +=1;
  136.             this.cursorDown = false;
  137.         }
  138.  
  139.         this.drawCursor(graphics, false);
  140.  
  141.         this.stopFlicker = false;
  142.     }
  143.    
  144.     private void drawBoard(Graphics graphics) {
  145.  
  146.         for (int vertical = 0; vertical < 8; vertical++) {
  147.             for (int horizontal = 0; horizontal < 8; horizontal++) {
  148.                 if (vertical % 2 == 0) {
  149.                     if (horizontal % 2 == 0)
  150.                         graphics.setColor(Color.black);
  151.                     else
  152.                         graphics.setColor(Color.gray);
  153.                 }
  154.                 else {
  155.                     if (horizontal % 2 == 0)
  156.                         graphics.setColor(Color.gray);
  157.                     else
  158.                         graphics.setColor(Color.black);
  159.                 }
  160.                
  161.                 graphics.fillRect(45 + 40 * horizontal, 10 + 40 * vertical, 35, 35);
  162.             }
  163.         }
  164.     }
  165.  
  166.     private void drawPieces(Graphics graphics) {
  167.  
  168.         Piece pieceInQuestion;
  169.  
  170.         for (int index = 0; index < this.pieces.size(); index++) {
  171.             pieceInQuestion = (Piece) this.pieces.elementAt(index);
  172.             graphics.drawImage(pieceInQuestion.returnImage(),
  173.             pieceInQuestion.getDrawX(), pieceInQuestion.getDrawY(), this);
  174.            
  175.             if (pieceInQuestion.getTeam() == 1)
  176.                 graphics.setColor(Color.blue);
  177.             else
  178.                 graphics.setColor(Color.red);
  179.  
  180.             graphics.drawRect(pieceInQuestion.getDrawX() - 1,
  181.                 pieceInQuestion.getDrawY() - 1, 30, 30);
  182.         }
  183.     }
  184.  
  185.     private void drawCursor(Graphics graphics, boolean erase) {
  186.  
  187.         /* If 'erase' is true, then erase the old cursor.  If the
  188.         cursor is dragging a piece, then erase its old version too. */
  189.  
  190.         if (erase) {
  191.  
  192.             if (this.pieceSelected) {
  193.                 Piece pieceInQuestion = (Piece) this.pieces.elementAt(this.pieceIndex);
  194.  
  195.                 if (pieceInQuestion.getX() == 0
  196.                     || pieceInQuestion.getX() == 9)
  197.                     graphics.setColor(this.backgroundColor);
  198.                 else if (pieceInQuestion.getX() % 2 == 0) {
  199.                     if (pieceInQuestion.getY() % 2 == 0)
  200.                         graphics.setColor(Color.gray);
  201.                     else
  202.                         graphics.setColor(Color.black);
  203.                 }
  204.                 else {
  205.                     if (pieceInQuestion.getY() % 2 == 0)
  206.                         graphics.setColor(Color.black);
  207.                     else
  208.                         graphics.setColor(Color.gray);
  209.                 }
  210.  
  211.             graphics.fillRect(pieceInQuestion.getDrawX() - 1, pieceInQuestion.getDrawY() - 1,
  212.                 31, 31);
  213.             }
  214.  
  215.             graphics.setColor(this.backgroundColor);
  216.         }
  217.  
  218.         /* If we're not erasing, and no piece if selected, just
  219.         draw the cursor normally. */
  220.  
  221.         else if (!this.pieceSelected)
  222.             graphics.setColor(Color.white);
  223.  
  224.         /* In all other scenarios - ie - we're dragging a piece, but
  225.         we're not erasing anything - we need to draw the piece and
  226.         the cursor with an indicitive color. */
  227.  
  228.         else {
  229.  
  230.             Piece pieceInQuestion = (Piece) this.pieces.elementAt(this.pieceIndex);
  231.             pieceInQuestion.setX(this.cursorX);
  232.             pieceInQuestion.setY(this.cursorY);
  233.             this.drawPieces(graphics);
  234.  
  235.             if (this.pieceTeam == 1)
  236.                 graphics.setColor(Color.blue);
  237.             else
  238.                 graphics.setColor(Color.red);
  239.         }
  240.  
  241.         graphics.drawRect(4 + 40 * cursorX, 9 + 40 * cursorY, 36, 36);
  242.     }      
  243.  
  244.     public void keyPressed(KeyEvent event) {
  245.    
  246.         int keyCode = event.getKeyCode();
  247.        
  248.         if (keyCode == 37 && this.cursorX > 0) // left
  249.             this.cursorLeft = true;
  250.         else if (keyCode == 39 && this.cursorX < 9) // right
  251.             this.cursorRight = true;
  252.         else if (keyCode == 38 && this.cursorY > 0) // up
  253.             this.cursorUp = true;
  254.         else if (keyCode == 40 && this.cursorY < 7) // down
  255.             this.cursorDown = true;
  256.         else if (keyCode == 32) { // space
  257.             if (!this.pieceSelected) {
  258.  
  259.                 Piece pieceInQuestion;
  260.                
  261.                 for (int index = 0; index < this.pieces.size(); index++) {
  262.                     pieceInQuestion = (Piece) this.pieces.elementAt(index);
  263.                     if (this.cursorX == pieceInQuestion.getX()
  264.                         && this.cursorY == pieceInQuestion.getY()) {
  265.                         this.pieceSelected = true;
  266.                         this.pieceIndex = index;
  267.                         this.pieceTeam = pieceInQuestion.getTeam();
  268.                     }
  269.                 }
  270.             }
  271.             else
  272.                 this.pieceSelected = false;
  273.         }
  274.  
  275.         this.stopFlicker = true;
  276.  
  277.         repaint();
  278.     }
  279.    
  280.     public void keyReleased(KeyEvent event) {
  281.     }
  282.        
  283.     public void keyTyped(KeyEvent event) {
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement