Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. package simpleGame;
  2.  
  3. public class TestGame {
  4.  
  5.     public static void main(String[] args) {
  6.         Player p=new Player(1,1);
  7.         MapPanel mp=new MapPanel(p);
  8.         GameFrame frame=new GameFrame(mp, p);
  9.     }
  10.  
  11. }
  12.  
  13.  
  14. package simpleGame;
  15.  
  16. import javax.swing.*;
  17. import java.awt.event.KeyEvent;
  18. import java.awt.event.KeyListener;
  19.  
  20. public class GameFrame extends JFrame implements KeyListener {
  21.     private MapPanel panel;
  22.     private Player player;
  23.  
  24.     public GameFrame(MapPanel p, Player pl){
  25.         panel=p;
  26.         player=pl;
  27.         add(panel);
  28.  
  29.         this.pack();
  30.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  31.         this.setVisible(true);
  32.  
  33.         this.addKeyListener(this);
  34.     }
  35.  
  36.     @Override
  37.     public void keyTyped(KeyEvent keyEvent) {
  38.  
  39.     }
  40.  
  41.     @Override
  42.     public void keyPressed(KeyEvent keyEvent) {
  43.         int key=keyEvent.getKeyCode();
  44.         System.out.println(key);
  45.  
  46.         switch(key){
  47.             case KeyEvent.VK_UP:
  48.                 System.out.println("UP");
  49.                 player.move(-1, 0);
  50.                 panel.repaint();
  51.                 break;
  52.             case KeyEvent.VK_DOWN:
  53.                 System.out.println("DOWN");
  54.                 player.move(1, 0);
  55.                 panel.repaint();
  56.                 break;
  57.             case KeyEvent.VK_LEFT:
  58.                 System.out.println("LEFT");
  59.                 player.move(0, -1);
  60.                 panel.repaint();
  61.                 break;
  62.             case KeyEvent.VK_RIGHT:
  63.                 System.out.println("RIGHT");
  64.                 player.move(0, 1);
  65.                 panel.repaint();
  66.                 break;
  67.         }
  68.  
  69.     }
  70.  
  71.     @Override
  72.     public void keyReleased(KeyEvent keyEvent) {
  73.  
  74.     }
  75. }
  76.  
  77.  
  78. package simpleGame;
  79.  
  80. import javax.swing.*;
  81. import java.awt.*;
  82.  
  83. public class MapPanel extends JPanel {
  84.     private Player p;
  85.     private int PANEL_WIDTH=800, PANEL_HEIGHT=800;
  86.  
  87.  
  88.     public MapPanel(Player play){
  89.         p=play;
  90.         setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
  91.     }
  92.  
  93.     public void paintComponent(Graphics g){
  94.         super.paintComponent(g);
  95.         g.fillRect(p.getLocCol(), p.getLocRow(), p.getCELL_SIZE(),p.getCELL_SIZE());
  96.     }
  97.  
  98. }
  99.  
  100. package simpleGame;
  101.  
  102. public class Player {
  103.     private int locRow, locCol;
  104.     private final int CELL_SIZE=20;
  105.  
  106.     public Player(int row, int col){
  107.         locRow=row*CELL_SIZE;
  108.         locCol=col*CELL_SIZE;
  109.     }
  110.  
  111.     public boolean move(int rowShift, int colShift){
  112.         boolean moved=true;
  113.  
  114.         locRow=locRow+(rowShift*CELL_SIZE);
  115.         locCol=locCol+(colShift*CELL_SIZE);
  116.  
  117.         return moved;
  118.     }
  119.  
  120.     public int getCELL_SIZE() {
  121.         return CELL_SIZE;
  122.     }
  123.  
  124.     public int getLocRow() {
  125.         return locRow;
  126.     }
  127.  
  128.     public void setLocRow(int locRow) {
  129.         this.locRow = locRow;
  130.     }
  131.  
  132.     public int getLocCol() {
  133.         return locCol;
  134.     }
  135.  
  136.     public void setLocCol(int locCol) {
  137.         this.locCol = locCol;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement