Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- */
- package com.programming11.snake2;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Arrays;
- import javax.swing.*;
- public class Snake2 extends JPanel implements KeyListener {
- private final int rows = 14;
- private final int cols = 20;
- private final int squareSize = 40;
- int SnakeHeadRow = 0;
- int SnakeHeadCol = 0;
- int SnakeLength=5; //initial length
- boolean SnakeBody [] [] = new boolean [rows][cols];
- int SnakeBodyPos [] [] = new int [rows][cols];
- int SnakeDirection =2; //starts going left. 1 is north, 2 is east, 3 is south, 4 is west
- int SnakeMoves=10;//the limit on moves is so i dont move infinietly and probably crash
- // start top-left
- public Snake2() {
- // constructor if you want to add setup later
- SnakeBody[0][0] = true;
- for (int r = 0; r < SnakeBodyPos.length; r++) {
- for (int c = 0; c < SnakeBodyPos[r].length; c++) {
- SnakeBodyPos[r][c]=SnakeLength;
- }
- }
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- // draw the grid
- for (int r = 0; r < rows; r++) {
- for (int c = 0; c < cols; c++) {
- g.setColor(Color.black);
- g.fillRect(c * squareSize, r * squareSize, squareSize, squareSize);
- g.setColor(Color.white);
- g.drawRect(rows * squareSize-10, cols * squareSize-10, squareSize, squareSize);
- }
- }
- // draw the snake head
- g.setColor(Color.white);
- g.fillRect(SnakeHeadCol* squareSize, SnakeHeadRow * squareSize,
- squareSize, squareSize);
- //draw the body
- for (int r = 0; r < SnakeBody.length; r++) {
- for (int c = 0; c < SnakeBody[r].length; c++) {
- if (SnakeBody[r][c]){
- if (SnakeBodyPos[r][c]>0){
- g.fillRect(c* squareSize, r* squareSize,
- squareSize, squareSize);
- SnakeBodyPos[r][c] -=1;
- }
- else { SnakeBody[r][c]=false;
- SnakeBodyPos[r][c]=SnakeLength;
- }
- }
- }
- System.out.println();
- }
- //System.out.println(Arrays.deepToString(SnakeBody));
- //printing the array for debugging
- for (int r = 0; r < SnakeBody.length; r++) {
- System.out.print("row "+r+": ");
- for (int c = 0; c < SnakeBody[r].length; c++) {
- System.out.print(SnakeBody[r][c]+", ");
- }
- System.out.println();
- }
- //move the snake goes here?
- SnakeMove();
- }
- public void SnakeMove(){
- SnakeMoves -=1;
- if (SnakeMoves>0){
- if (SnakeDirection==1){
- SnakeHeadRow -=1;
- SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
- repaint();
- }
- if (SnakeDirection==2){
- SnakeHeadCol+=1;
- SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
- repaint();
- }
- if (SnakeDirection==3){
- SnakeHeadRow +=1;
- SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
- repaint();
- }
- if (SnakeDirection==4){
- SnakeHeadCol -=1;
- SnakeBody[SnakeHeadRow][SnakeHeadCol] = true;
- repaint();
- }
- }
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_W) {
- SnakeDirection=1;
- SnakeMoves=10;
- SnakeMove();
- }
- if (e.getKeyCode() == KeyEvent.VK_D) {
- SnakeDirection=2;
- SnakeMoves=10;
- SnakeMove();
- }
- if (e.getKeyCode() == KeyEvent.VK_S) {
- SnakeDirection=3;
- SnakeMoves=10;
- SnakeMove();
- }
- if (e.getKeyCode() == KeyEvent.VK_A) {
- SnakeDirection=4;
- SnakeMoves=10;
- SnakeMove();
- }
- }
- @Override public void keyReleased(KeyEvent e) {}
- @Override public void keyTyped(KeyEvent e) {}
- public static void main(String[] args) {
- JFrame frame = new JFrame("Grid");
- Snake2 panel = new Snake2 ();
- frame.add(panel);
- frame.setSize(815, 800);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.addKeyListener(panel);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment