Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.20 KB | None | 0 0
  1. package com.RGuSnake;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5.  
  6. Snake.getInstance().createBoard();
  7.  
  8. }
  9. }
  10.  
  11. package com.RGuSnake;
  12.  
  13. import javax.swing.*;
  14. import java.awt.*;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Random;
  20.  
  21.  
  22. public class Snake extends JPanel implements ActionListener {
  23.  
  24.  
  25. private int sizeWidth;
  26. private int sizeHeight;
  27. private int offsetWidth;
  28. private int offsetHeight;
  29. private int scale;
  30. private ArrayList<Point> snakeLocation;
  31. private static Point food;
  32. private String direction = "RIGHT";
  33. private String tmpDirection = "RIGHT";
  34.  
  35. private static final Snake snake = new Snake();
  36.  
  37. private Integer delay;
  38. private Boolean isPaused = false;
  39. private Boolean isAlive = false;
  40. private Timer timer;
  41. private Board board;
  42. private Buttons buttons;
  43. private JFrame frame;
  44.  
  45. private Integer score=0;
  46. private int speed=5;
  47.  
  48. private Snake() {
  49.  
  50. }
  51.  
  52. public static Snake getInstance() {
  53. return snake;
  54. }
  55.  
  56. public void createBoard() {
  57. frame = new JFrame("Typical Snake Game");
  58. snakeLocation = new ArrayList<>();
  59. snakeLocation.add(new Point(-100,-100));
  60. food=new Point(-100,-100);
  61. board = new Board();
  62. sizeWidth=board.getSizeWidth();
  63. sizeHeight=board.getSizeHeight();
  64. offsetHeight=board.getOffsetHeight();
  65. offsetWidth=board.getOffsetWidth();
  66. scale=board.getScale();
  67.  
  68. buttons = new Buttons();
  69. frame.getContentPane().add(BorderLayout.CENTER, board);
  70. frame.getContentPane().add(BorderLayout.SOUTH, buttons);
  71. frame.setPreferredSize(new Dimension(sizeWidth + 2 * offsetWidth, sizeHeight + 2 * offsetHeight + 50));
  72.  
  73. frame.setResizable(false);
  74. frame.setVisible(true);
  75. frame.pack();
  76. frame.setLocationRelativeTo(null);
  77. frame.setFocusable(true);
  78. frame.requestFocus();
  79. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80.  
  81. }
  82.  
  83.  
  84. public void startGame() {
  85. delay=100+(5-speed)*15;
  86. System.out.println(delay);
  87. timer = new Timer(delay, this);
  88. System.out.println(delay);
  89. if(frame==null){
  90. snake.createBoard();
  91. }
  92. score=0;
  93. direction="RIGHT";
  94. snakeLocation.clear();
  95.  
  96. for(int i=0;i<6;i++){
  97. snakeLocation.add(new Point(Math.round((sizeWidth+offsetWidth)/(2*10))*10-i*10, Math.round((sizeHeight+offsetHeight)/(2*10))*10));
  98. }
  99.  
  100. newFood();
  101.  
  102. buttons.blockButtons();
  103. isAlive = true;
  104. isPaused = false;
  105. timer.start();
  106. }
  107.  
  108.  
  109.  
  110. public ArrayList<Point> getSnakeLocation() {
  111. return snakeLocation;
  112. }
  113.  
  114. public Point getFoodLocation() {
  115. return food;
  116. }
  117. public Boolean getIsAlive() { return isAlive;}
  118.  
  119. public void setDirection(String dir) {
  120. snake.direction = dir;
  121. }
  122. public String getDirection() {
  123. return snake.direction;
  124. }
  125.  
  126. public String getTmpDirection(){
  127. return snake.tmpDirection;
  128. }
  129.  
  130. public void spacePressed(){
  131.  
  132. if(!isAlive) {
  133. snake.startGame();
  134. }
  135. else {
  136. isPaused^=true;
  137. }
  138. }
  139.  
  140. public Boolean getPause(){
  141. return isPaused;
  142. }
  143.  
  144. public void move() {
  145. if (direction.equals("RIGHT")) {
  146. snakeLocation.add(0, new Point(snakeLocation.get(0).x + 10, snakeLocation.get(0).y + 0));
  147. } else if (direction.equals("LEFT")) {
  148. snakeLocation.add(0, new Point(snakeLocation.get(0).x - 10, snakeLocation.get(0).y + 0));
  149. } else if (direction.equals("UP")) {
  150. snakeLocation.add(0, new Point(snakeLocation.get(0).x, snakeLocation.get(0).y - 10));
  151. } else if (direction.equals("DOWN")) {
  152. snakeLocation.add(0, new Point(snakeLocation.get(0).x, snakeLocation.get(0).y + 10));
  153. }
  154. }
  155.  
  156.  
  157. public void actionPerformed(ActionEvent arg0) {
  158. if(!isPaused && isAlive) {
  159. tmpDirection = direction;
  160. snake.move();
  161.  
  162. snake.checkPosition();
  163.  
  164. //refresh();
  165. board.repaint();
  166.  
  167. } else if(!isAlive) {
  168. timer.stop();
  169. buttons.enableButtons();
  170. }
  171.  
  172.  
  173. }
  174.  
  175. public void newFood() {
  176. Random random = new Random();
  177. Point point;
  178. point = new Point(random.nextInt(sizeWidth / scale) * scale + offsetWidth, random.nextInt(sizeHeight / scale) * scale + offsetHeight);
  179.  
  180. while (Arrays.asList(getSnakeLocation()).contains(point)) {
  181. point = new Point(random.nextInt(sizeWidth / scale) * scale + offsetWidth, random.nextInt(sizeHeight / scale) * scale + offsetHeight);
  182. }
  183.  
  184. food = point;
  185. }
  186.  
  187. public void increaseScore() {
  188. score=score+speed;
  189. }
  190. public int getScore(){
  191. return score;
  192. }
  193.  
  194. public void increaseSpeed(){
  195. if(speed<10) {
  196. speed += 1;
  197. }
  198. }
  199.  
  200. public void decreaseSpeed(){
  201. if(speed>1) {
  202. speed -= 1;
  203. }
  204. }
  205.  
  206. public int getSpeed(){
  207. return speed;
  208. }
  209.  
  210. public void refresh(){
  211.  
  212. board.repaint();
  213. }
  214.  
  215.  
  216. public void checkPosition(){
  217. for (int j = 1; j < snakeLocation.size()-1; j++) {
  218. if (snakeLocation.get(0).equals(snakeLocation.get(j))) {
  219. isAlive = false;
  220. }
  221. }
  222.  
  223.  
  224. if (snakeLocation.get(0).x==offsetWidth-scale || snakeLocation.get(0).x==sizeWidth+offsetWidth ||snakeLocation.get(0).y==offsetHeight-scale || snakeLocation.get(0).y==sizeHeight+offsetHeight) {
  225. isAlive = false;
  226. }
  227.  
  228.  
  229. if (snakeLocation.get(0).equals(food)) {
  230. newFood();
  231. increaseScore();
  232. }
  233. else {
  234. snakeLocation.remove(snakeLocation.size() - 1);
  235. }
  236.  
  237. }
  238. }
  239.  
  240. package com.RGuSnake;
  241.  
  242. import javax.swing.*;
  243. import java.awt.*;
  244. import java.util.ArrayList;
  245.  
  246.  
  247. public class Board extends JPanel {
  248.  
  249. private int sizeWidth = 300;
  250. private int sizeHeight = 300;
  251. private int offsetWidth = 30;
  252. private int offsetHeight = 30;
  253. private int scale = 10;
  254. Snake snake=Snake.getInstance();
  255.  
  256. public void paintComponent(Graphics g) {
  257.  
  258.  
  259. super.paintComponent(g);
  260.  
  261. //super.update(g);
  262. ArrayList<Point> points = Snake.getInstance().getSnakeLocation();
  263. g.setColor(Color.BLACK);
  264. g.fillRect(offsetWidth - scale, offsetHeight - scale, sizeWidth + 2 * scale, sizeHeight + 2 * scale);
  265. g.setColor(Color.LIGHT_GRAY);
  266. g.fillRect(offsetWidth, offsetHeight, sizeWidth, sizeHeight);
  267.  
  268. g.setColor(Color.BLACK);
  269. for (int i = 1; i < points.size(); i++) {
  270. g.fillRect(snake.getSnakeLocation().get(i).x, snake.getSnakeLocation().get(i).y, scale, scale);
  271. }
  272. g.setColor(Color.RED);
  273. g.fillRect(snake.getSnakeLocation().get(0).x, snake.getSnakeLocation().get(0).y, scale, scale);
  274.  
  275. g.setColor(Color.BLUE);
  276. g.fillRect(snake.getFoodLocation().x, snake.getFoodLocation().y, scale, scale);
  277.  
  278. g.setColor(Color.RED);
  279. Font font = new Font("Verdana", Font.BOLD, 12);
  280. g.setFont(font);
  281. FontMetrics fm = g.getFontMetrics();
  282. String score = "Score: " + snake.getScore() + " Speed: " + snake.getSpeed();
  283. g.drawString(score, (offsetWidth * 2 + sizeWidth) / 2 - fm.stringWidth(score) / 2, offsetHeight / 2);
  284.  
  285.  
  286. if (!snake.getIsAlive()) {
  287.  
  288.  
  289. font = new Font("Verdana", Font.BOLD, 12);
  290. g.setFont(font);
  291. String gameOver1 = "CHOOSE THE SPEED";
  292. String gameOver2 = "BEFORE STARTING NEW GAME";
  293. fm = g.getFontMetrics();
  294.  
  295. g.setColor(Color.red);
  296. g.drawString(gameOver1, (offsetWidth * 2 + sizeWidth) / 2 - fm.stringWidth(gameOver1) / 2, (offsetHeight + sizeHeight) / 2);
  297. g.drawString(gameOver2, (offsetWidth * 2 + sizeWidth) / 2 - fm.stringWidth(gameOver2) / 2, (offsetHeight + sizeHeight + 40) / 2);
  298. //String speed = "Game speed: " + snake.getSpeed();
  299. //g.drawString(speed,(offsetWidth*2+sizeWidth)/2-fm.stringWidth(speed)/2,(offsetHeight+sizeHeight)/2+70);
  300. }
  301. }
  302.  
  303.  
  304. public int getSizeWidth() {
  305. return sizeWidth;
  306. }
  307.  
  308. public int getOffsetWidth() {
  309. return offsetWidth;
  310. }
  311.  
  312. public int getSizeHeight(){
  313. return sizeHeight;
  314. }
  315.  
  316. public int getOffsetHeight() {
  317. return offsetHeight;
  318. }
  319.  
  320. public int getScale(){
  321. return scale;
  322. }
  323. }
  324.  
  325. package com.RGuSnake;
  326.  
  327. import javax.swing.*;
  328. import java.awt.*;
  329. import java.awt.event.ActionEvent;
  330. import java.awt.event.ActionListener;
  331. import java.awt.event.KeyEvent;
  332.  
  333. public class Buttons extends JPanel {
  334.  
  335. Snake snake=Snake.getInstance();
  336. Board board=new Board();
  337. JButton startGame;
  338. JButton speedDown;
  339. JButton speedUp;
  340.  
  341. public Buttons(){
  342. JPanel buttonsPanel = new JPanel();
  343. buttonsPanel.setPreferredSize(new Dimension(board.getSizeWidth()+board.getOffsetWidth(), 20));
  344.  
  345. startGame = new JButton("START!");
  346. startGame.setBackground(Color.red);
  347.  
  348. speedDown = new JButton("Speed Down");
  349. speedUp = new JButton("Speed Up");
  350. startGame.addActionListener(new startGame());
  351. speedUp.addActionListener(new SpeedUp());
  352. speedDown.addActionListener(new SpeedDown());
  353.  
  354. startGame.setFocusPainted(false);
  355. startGame.setFocusable(false);
  356. speedDown.setFocusPainted(false);
  357. speedDown.setFocusable(false);
  358. speedUp.setFocusPainted(false);
  359. speedUp.setFocusable(false);
  360. buttonsPanel.setLayout(new GridLayout());
  361. buttonsPanel.add(startGame);
  362. buttonsPanel.add(speedDown);
  363. buttonsPanel.add(speedUp);
  364.  
  365. InputMap im = buttonsPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
  366. ActionMap am = buttonsPanel.getActionMap();
  367.  
  368. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
  369. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
  370. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");
  371. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DownArrow");
  372. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "Space");
  373.  
  374. am.put("RightArrow", new ArrowAction("RightArrow"));
  375. am.put("LeftArrow", new ArrowAction("LeftArrow"));
  376. am.put("UpArrow", new ArrowAction("UpArrow"));
  377. am.put("DownArrow", new ArrowAction("DownArrow"));
  378. am.put("Space", new ArrowAction("Space"));
  379. add(buttonsPanel);
  380.  
  381. }
  382.  
  383. public void blockButtons() {
  384. startGame.setText("PAUSE");
  385. speedUp.setEnabled(false);
  386. speedDown.setEnabled(false);
  387.  
  388. }
  389.  
  390. public void enableButtons() {
  391. startGame.setText("START!");
  392. speedUp.setEnabled(true);
  393. speedDown.setEnabled(true);
  394.  
  395. }
  396.  
  397. private class startGame implements ActionListener {
  398. @Override
  399. public void actionPerformed(ActionEvent e) {
  400. snake.spacePressed();
  401. }
  402. }
  403.  
  404. private class SpeedUp implements ActionListener {
  405. @Override
  406. public void actionPerformed(ActionEvent e) {
  407. snake.increaseSpeed();
  408. snake.refresh();
  409. }
  410. }
  411. private class SpeedDown implements ActionListener {
  412. @Override
  413. public void actionPerformed(ActionEvent e) {
  414. snake.decreaseSpeed();
  415. snake.refresh();
  416. }
  417. }
  418. }
  419.  
  420. package com.RGuSnake;
  421.  
  422. import javax.swing.*;
  423. import java.awt.event.ActionEvent;
  424.  
  425. public class ArrowAction extends AbstractAction {
  426. Snake snake=Snake.getInstance();
  427. private String cmd;
  428.  
  429. public ArrowAction(String cmd) {
  430. this.cmd = cmd;
  431. }
  432.  
  433. @Override
  434. public void actionPerformed(ActionEvent e) {
  435. if ((cmd.equalsIgnoreCase("LeftArrow")) && !snake.getDirection().equals("RIGHT") && !snake.getTmpDirection().equals("RIGHT") && !snake.getPause()) {
  436. snake.setDirection("LEFT");
  437. } else if (cmd.equalsIgnoreCase("RightArrow") && !snake.getDirection().equals("LEFT") && !snake.getTmpDirection().equals("LEFT")&& !snake.getPause()) {
  438. snake.setDirection("RIGHT");
  439. } else if (cmd.equalsIgnoreCase("UpArrow")&& !snake.getDirection().equals("DOWN") && !snake.getTmpDirection().equals("DOWN")&& !snake.getPause()) {
  440. snake.setDirection("UP");
  441. } else if (cmd.equalsIgnoreCase("DownArrow")&& !snake.getDirection().equals("UP") && !snake.getTmpDirection().equals("UP")&& !snake.getPause()) {
  442. snake.setDirection("DOWN");
  443. } else if (cmd.equalsIgnoreCase("Space")) {
  444. snake.spacePressed();
  445. }
  446. }
  447.  
  448. }
  449.  
  450. private int sizeWidth;
  451. private int sizeHeight;
  452. private int offsetWidth;
  453. private int offsetHeight;
  454. private int scale;
  455.  
  456. board.getWidth()
  457.  
  458. private static Point food;
  459.  
  460. private String currentDirection = "RIGHT";
  461. private String nextDirection= "RIGHT";
  462.  
  463. public void move() {
  464. currentDirection = nextDirection;
  465.  
  466. if (direction.equals("RIGHT")) {
  467. snakeLocation.add(0, new Point(snakeLocation.get(0).x + 10, snakeLocation.get(0).y + 0));
  468. } else
  469. ...
  470. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement