Advertisement
Hansikk

Game

Apr 18th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.31 KB | None | 0 0
  1. //---------------------------------MAPMAKE
  2.  
  3. package game;
  4.  
  5. public class MapMake{
  6.     static int[][] discovered = new int[10][10];
  7.     static int[][] content = new int[10][10];
  8.     static int heroX = 0;
  9.     static int heroY = 0;
  10.     public void MapMake(){
  11.         discovered[0][0] = 1;
  12.         for (int row = 0; row < 10; row++) {
  13.             for (int col = 0; col < 10; col++) {
  14.                 discovered[row][col] = 0;
  15.             }
  16.         }
  17.         discovered[0][0] = 1;
  18.         int dragon1 = 3;
  19.         int dragon2 = 5;
  20.         int dragon3 = 7;
  21.         int dragon4 = 9;
  22.         int dragon5 = 1;
  23.         int food = 25; // id 6
  24.  
  25.         while (dragon1 != 0) {
  26.             for (int i = 0; i < 3; i++) {
  27.                 for (int j = 0; j < 3; j++) {
  28.                     int x = (int) (Math.random() * 2);
  29.                     if (x != 1 && dragon1 != 0 && content[i][j] == 0
  30.                             && (i > 0 && i < 3 || j > 0 && j < 3)) {
  31.                         dragon1--;
  32.                         content[i][j] = 1;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.         while (dragon2 != 0 && food != 0) {
  38.             for (int i = 0; i < 5; i++) {
  39.                 for (int j = 0; j < 5; j++) {
  40.                     int x = (int) (Math.random() * 2);
  41.                     if (x != 1 && dragon2 != 0 && content[i][j] == 0
  42.                             && (i > 2 && i < 5 || j > 3 && j < 5)) {
  43.                         dragon2--;
  44.                         content[i][j] = 2;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         while (dragon3 != 0) {
  50.             for (int i = 0; i < 7; i++) {
  51.                 for (int j = 0; j < 7; j++) {
  52.                     int x = (int) (Math.random() * 2);
  53.                     if (x != 1 && dragon3 != 0 && content[i][j] == 0
  54.                             && (i > 4 && i < 7 || j > 4 && j < 7)) {
  55.                         dragon3--;
  56.                         content[i][j] = 3;
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         while (dragon4 != 0) {
  62.             for (int i = 0; i < 9; i++) {
  63.                 for (int j = 0; j < 9; j++) {
  64.                     int x = (int) (Math.random() * 2);
  65.                     if (x != 1 && dragon4 != 0 && content[i][j] == 0
  66.                             && (i > 6 && i < 9 || j > 6 && j < 9)) {
  67.                         dragon4--;
  68.                         content[i][j] = 4;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.         while (dragon5 != 0) {
  74.             for (int i = 0; i < 10; i++) {
  75.                 for (int j = 0; j < 10; j++) {
  76.                     int x = (int) (Math.random() * 2);
  77.                     if (x != 1 && dragon5 != 0 && content[i][j] == 0
  78.                             && (i > 8 && i < 10 || j > 8 && j < 10)) {
  79.                         dragon5--;
  80.                         content[i][j] = 5;
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         while (food != 0) {
  86.             for (int i = 0; i < 10; i++) {
  87.                 for (int j = 0; j < 10; j++) {
  88.                     int x = (int) (Math.random() * 2);
  89.                     if (x != 1 && food != 0 && content[i][j] == 0) {
  90.                         food--;
  91.                         content[i][j] = 6;
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.         discovered[0][0] = 1;
  97.     }
  98.     public int getDiscovered(int i, int j) {
  99.         return discovered[i][j];
  100.     }
  101.     public int getContent(int i, int j) {
  102.         return content[i][j];
  103.     }
  104.     public int getHeroX() {
  105.         return heroX;
  106.     }
  107.     public int getHeroY() {
  108.         return heroY;
  109.     }
  110.     public void setDiscovered(int i, int j, int k) {
  111.         MapMake.discovered[i][j] = k;
  112.     }
  113.     public void setContent(int i, int j, int k) {
  114.         MapMake.content[i][j] = k;
  115.     }
  116.     public void setHeroX(int heroX) {
  117.         this.heroX = heroX;
  118.     }
  119.     public void setHeroY(int heroY) {
  120.         this.heroY = heroY;
  121.     }
  122. }
  123.  
  124. //---------------------------------GUI
  125. package game;
  126.  
  127. import java.awt.GridLayout;
  128. import java.io.IOException;
  129.  
  130. import javax.swing.JFrame;
  131. import javax.swing.JPanel;
  132.  
  133. public class Gui extends JFrame{
  134.     /**
  135.      *
  136.      */
  137.     private static final long serialVersionUID = 1L;
  138.     int n = 0;
  139.     int m = 0;
  140.     static MapMake mapMake = new MapMake();
  141.     static MapRefresh mapRefresh = new MapRefresh();
  142.     static JPanel panel = new JPanel();
  143.     static Fighting fighting = null;
  144.    
  145.     public static void main(String[] args) throws IOException {
  146.         Gui frame = new Gui();
  147.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  148.         frame.setTitle("Dungeons and Dragons(and some food)");
  149.         frame.setSize(1280, 720);
  150.  
  151.         mapMake.setDiscovered(0, 0, 1);
  152.         panel.setLayout(new GridLayout(10, 10, 2, 2));
  153.         mapRefresh.MapRefresh();
  154.         frame.add(panel);
  155.         frame.addKeyListener(new Listener());
  156.         frame.setVisible(true);
  157.     }
  158. }
  159.  
  160. //---------------------------------MAPREFRESH
  161. package game;
  162.  
  163. import java.awt.image.BufferedImage;
  164. import java.io.File;
  165. import java.io.IOException;
  166.  
  167. import javax.imageio.ImageIO;
  168. import javax.swing.ImageIcon;
  169. import javax.swing.JLabel;
  170. import javax.swing.JPanel;
  171.  
  172. public class MapRefresh extends Gui{
  173.     static MapMake mapMake = Gui.mapMake;
  174.    
  175.     @SuppressWarnings("unused")
  176.     public void MapRefresh() {
  177.         BufferedImage hall = null;
  178.         BufferedImage valge = null;
  179.         JPanel panel = Gui.panel;
  180.         for (int i = 0; i < 10; i++) {
  181.             for (int j = 0; j < 10; j++) {
  182.                 //              panel.add(new JButton("" + content[i][j]));
  183.                 if (mapMake.getDiscovered(i, j) == 1) {
  184.                     panel.add(new JLabel("hero on siin peal"));
  185.                 } else if (mapMake.getDiscovered(i, j) == 0) {
  186.                     try {
  187.                         panel.add(new JLabel(new ImageIcon(hall = ImageIO.read(new File("C:/hall.jpg")))));
  188.                     } catch (IOException e) {
  189.                     } catch (NullPointerException e){
  190.                        
  191.                     }
  192.                 } else {
  193.                     try {
  194.                         panel.add(new JLabel(new ImageIcon(valge = ImageIO.read(new File("C:/valge.jpg")))));
  195.                     } catch (IOException e) {
  196.                     } catch (NullPointerException e){
  197.                        
  198.                     }
  199.                 }
  200.             }
  201.         }
  202.     }
  203. }
  204.  
  205. //---------------------------------LISTENER
  206. package game;
  207.  
  208. import java.awt.event.*;
  209.  
  210. import javax.swing.JPanel;
  211.  
  212. public class Listener extends Gui implements KeyListener{
  213.     static MapRefresh mapRefresh = new MapRefresh();
  214.     static MapMake mapMake = Gui.mapMake;
  215.     static JPanel panel = Gui.panel;
  216.     static Fighting fighting = null;
  217.     @Override
  218.     public void keyPressed(KeyEvent e) {
  219.     }
  220.  
  221.     @Override
  222.     public void keyReleased(KeyEvent e) {
  223.         int x = mapMake.getHeroX();
  224.         int y = mapMake.getHeroY();
  225.         if (e.getKeyCode() == 37 && y > 0){
  226.             mapMake.setDiscovered(x, y-1, 1);
  227.             mapMake.setDiscovered(x, y, 2);
  228.             mapMake.setHeroY(y - 1);
  229.         } else if(e.getKeyCode() == 38 && x > 0){
  230.             mapMake.setDiscovered(x-1, y, 1);
  231.             mapMake.setDiscovered(x, y, 2);
  232.             mapMake.setHeroX(x - 1);
  233.         } else if (e.getKeyCode() == 39 && y < 9){
  234.             mapMake.setDiscovered(x, y+1, 1);
  235.             mapMake.setDiscovered(x, y, 2);
  236.             mapMake.setHeroY(y + 1);
  237.         } else if (e.getKeyCode() == 40 && x < 9){
  238.             mapMake.setDiscovered(x+1, y, 1);
  239.             mapMake.setDiscovered(x, y, 2);
  240.             mapMake.setHeroX(x + 1);
  241.         }
  242.         int z = mapMake.getContent(x, y);
  243. //      if (z != 6){
  244. //          fighting = new Fighting(z);
  245. //      }
  246.         panel.removeAll();
  247.         System.out.println(e.getKeyCode());
  248.         mapRefresh.MapRefresh();
  249.         panel.validate();
  250.         panel.repaint();
  251.        
  252.     }
  253.  
  254.     @Override
  255.     public void keyTyped(KeyEvent e) {     
  256.     }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement