Guest User

Untitled

a guest
Dec 16th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. /*
  2. * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3. */
  4.  
  5. package com.programming11.snake2;
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.util.Arrays;
  10. import javax.swing.*;
  11.  
  12. public class Snake2 extends JPanel implements KeyListener {
  13.  
  14. private final int rows = 14;
  15. private final int cols = 20;
  16. private final int squareSize = 40;
  17. int SnakeHeadRow = 0;
  18. int SnakeHeadCol = 0;
  19. int SnakeLength=5; //initial length
  20. boolean SnakeBody [] [] = new boolean [rows][cols];
  21. int SnakeBodyPos [] [] = new int [rows][cols];
  22. int SnakeDirection =2; //starts going left. 1 is north, 2 is east, 3 is south, 4 is west
  23. int SnakeMoves=10;//the limit on moves is so i dont move infinietly and probably crash
  24.  
  25.  
  26.  
  27. // start top-left
  28.  
  29.  
  30. public Snake2() {
  31. // constructor if you want to add setup later
  32. SnakeBody[0][0] = true;
  33. for (int r = 0; r < SnakeBodyPos.length; r++) {
  34. for (int c = 0; c < SnakeBodyPos[r].length; c++) {
  35. SnakeBodyPos[r][c]=SnakeLength;
  36.  
  37. }
  38.  
  39. }
  40.  
  41. }
  42.  
  43. @Override
  44. public void paintComponent(Graphics g) {
  45. super.paintComponent(g);
  46.  
  47. // draw the grid
  48. for (int r = 0; r < rows; r++) {
  49. for (int c = 0; c < cols; c++) {
  50. g.setColor(Color.black);
  51. g.fillRect(c * squareSize, r * squareSize, squareSize, squareSize);
  52.  
  53. g.setColor(Color.white);
  54. g.drawRect(rows * squareSize-10, cols * squareSize-10, squareSize, squareSize);
  55. }
  56. }
  57.  
  58. // draw the snake head
  59.  
  60.  
  61. g.setColor(Color.white);
  62. g.fillRect(SnakeHeadCol* squareSize, SnakeHeadRow * squareSize,
  63. squareSize, squareSize);
  64.  
  65. //draw the body
  66. for (int r = 0; r < SnakeBody.length; r++) {
  67. for (int c = 0; c < SnakeBody[r].length; c++) {
  68. if (SnakeBody[r][c]){
  69. if (SnakeBodyPos[r][c]>0){
  70. g.fillRect(c* squareSize, r* squareSize,
  71. squareSize, squareSize);
  72. SnakeBodyPos[r][c] -=1;
  73. }
  74. else { SnakeBody[r][c]=false;
  75. SnakeBodyPos[r][c]=SnakeLength;
  76. }
  77. }
  78. }
  79. System.out.println();
  80. }
  81.  
  82.  
  83. //System.out.println(Arrays.deepToString(SnakeBody));
  84.  
  85. //printing the array for debugging
  86. for (int r = 0; r < SnakeBody.length; r++) {
  87. System.out.print("row "+r+": ");
  88. for (int c = 0; c < SnakeBody[r].length; c++) {
  89. System.out.print(SnakeBody[r][c]+", ");
  90. }
  91. System.out.println();
  92. }
  93.  
  94. //move the snake goes here?
  95. SnakeMove();
  96.  
  97. }
  98. public void SnakeMove(){
  99.  
  100. SnakeMoves -=1;
  101. if (SnakeMoves>0){
  102.  
  103. if (SnakeDirection==1){
  104. SnakeHeadRow -=1;
  105. SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
  106. repaint();
  107.  
  108. }
  109. if (SnakeDirection==2){
  110. SnakeHeadCol+=1;
  111. SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
  112. repaint();
  113.  
  114. }
  115. if (SnakeDirection==3){
  116. SnakeHeadRow +=1;
  117. SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
  118. repaint();
  119.  
  120. }
  121. if (SnakeDirection==4){
  122. SnakeHeadCol -=1;
  123. SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
  124. repaint();
  125.  
  126. }
  127.  
  128. }
  129.  
  130. }
  131.  
  132. @Override
  133. public void keyPressed(KeyEvent e) {
  134. if (e.getKeyCode() == KeyEvent.VK_W) {
  135. SnakeDirection=1;
  136. SnakeMoves=10;
  137. SnakeMove();
  138. }
  139. if (e.getKeyCode() == KeyEvent.VK_D) {
  140. SnakeDirection=2;
  141. SnakeMoves=10;
  142. SnakeMove();
  143.  
  144. }
  145. if (e.getKeyCode() == KeyEvent.VK_S) {
  146. SnakeDirection=3;
  147. SnakeMoves=10;
  148. SnakeMove();
  149.  
  150. }
  151.  
  152.  
  153. if (e.getKeyCode() == KeyEvent.VK_A) {
  154. SnakeDirection=4;
  155. SnakeMoves=10;
  156. SnakeMove();
  157.  
  158. }
  159.  
  160.  
  161.  
  162. }
  163.  
  164. @Override public void keyReleased(KeyEvent e) {}
  165. @Override public void keyTyped(KeyEvent e) {}
  166.  
  167.  
  168. public static void main(String[] args) {
  169. JFrame frame = new JFrame("Grid");
  170. Snake2 panel = new Snake2 ();
  171.  
  172. frame.add(panel);
  173. frame.setSize(815, 800);
  174. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  175. frame.setVisible(true);
  176.  
  177.  
  178. frame.addKeyListener(panel);
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment