diff --git a/G group Project evaluation 1/aayush saini 1710991020/bin/objects/Apple.class b/G group Project evaluation 1/aayush saini 1710991020/bin/objects/Apple.class new file mode 100644 index 0000000..ee4e33c Binary files /dev/null and b/G group Project evaluation 1/aayush saini 1710991020/bin/objects/Apple.class differ diff --git a/G group Project evaluation 1/aayush saini 1710991020/bin/objects/Snake.class b/G group Project evaluation 1/aayush saini 1710991020/bin/objects/Snake.class new file mode 100644 index 0000000..86ee150 Binary files /dev/null and b/G group Project evaluation 1/aayush saini 1710991020/bin/objects/Snake.class differ diff --git a/G group Project evaluation 1/aayush saini 1710991020/bin/snakeGame/SnakeGame$Keyboard.class b/G group Project evaluation 1/aayush saini 1710991020/bin/snakeGame/SnakeGame$Keyboard.class new file mode 100644 index 0000000..d6d77f4 Binary files /dev/null and b/G group Project evaluation 1/aayush saini 1710991020/bin/snakeGame/SnakeGame$Keyboard.class differ diff --git a/G group Project evaluation 1/aayush saini 1710991020/bin/snakeGame/SnakeGame.class b/G group Project evaluation 1/aayush saini 1710991020/bin/snakeGame/SnakeGame.class new file mode 100644 index 0000000..6641e34 Binary files /dev/null and b/G group Project evaluation 1/aayush saini 1710991020/bin/snakeGame/SnakeGame.class differ diff --git a/G group Project evaluation 1/aayush saini 1710991020/src/objects/Apple.java b/G group Project evaluation 1/aayush saini 1710991020/src/objects/Apple.java new file mode 100644 index 0000000..e080c91 --- /dev/null +++ b/G group Project evaluation 1/aayush saini 1710991020/src/objects/Apple.java @@ -0,0 +1,23 @@ + package objects; + +import snakeGame.SnakeGame; + +public class Apple { + + SnakeGame main; + + public int posX; + public int posY; + + public Apple(int startX, int startY) { + posX = startX; + posY = startY; + + } + + @SuppressWarnings("static-access") + public void setRandomPosition() { + posX = (int) (Math.random() * main.WIDTH); + posY = (int) (Math.random() * main.HEIGHT); + } +} \ No newline at end of file diff --git a/G group Project evaluation 1/aayush saini 1710991020/src/objects/Snake.java b/G group Project evaluation 1/aayush saini 1710991020/src/objects/Snake.java new file mode 100644 index 0000000..198adbe --- /dev/null +++ b/G group Project evaluation 1/aayush saini 1710991020/src/objects/Snake.java @@ -0,0 +1,47 @@ + + package objects; + +import snakeGame.SnakeGame; + +public class Snake { + + SnakeGame main; + + public int direction = 0; + public int length = 2; + + public int snakeX[] = new int[main.WIDTH * main.HEIGHT]; + public int snakeY[] = new int[main.WIDTH * main.HEIGHT]; + + public Snake(int x0, int y0, int x1, int y1) { + snakeX[0] = x0; + snakeY[0] = y0; + snakeX[1] = x1; + snakeY[1] = y1; + } + + @SuppressWarnings("static-access") + public void move() { + + for (int d = length; d > 0; d--) { + snakeX[d] = snakeX[d - 1]; + snakeY[d] = snakeY[d - 1]; + } + + if (direction == 0) snakeX[0]++; + if (direction == 1) snakeY[0]++; + if (direction == 2) snakeX[0]--; + if (direction == 3) snakeY[0]--; + + for (int d = length - 1; d > 0; d--) { + if ((snakeX[0] == snakeX[d]) & (snakeX[0] == snakeY[d])) length = d - 2; + } + + if (snakeX[0] > main.WIDTH) snakeX[0] = 0; + if (snakeX[0] < 0) snakeX[0] = main.WIDTH - 1; + if (snakeY[0] > main.HEIGHT - 1) snakeY[0] = 0; + if (snakeY[0] < 0) snakeY[0] = main.HEIGHT - 1; + + if (length < 2) length = 2; + } +} \ No newline at end of file diff --git a/G group Project evaluation 1/aayush saini 1710991020/src/snakeGame/SnakeGame.java b/G group Project evaluation 1/aayush saini 1710991020/src/snakeGame/SnakeGame.java new file mode 100644 index 0000000..5d1a26f --- /dev/null +++ b/G group Project evaluation 1/aayush saini 1710991020/src/snakeGame/SnakeGame.java @@ -0,0 +1,93 @@ +package snakeGame; + +import objects.Apple; +import objects.Snake; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +public class SnakeGame extends JPanel implements ActionListener { + + public static final int SCALE = 32; + public static final int WIDTH = 20; + public static final int HEIGHT = 20; + public static final int SPEED = 4; + + Apple a = new Apple((int) (Math.random() * WIDTH), (int) (Math.random() * HEIGHT)); + Snake s = new Snake(10, 10, 9, 10); + Timer t = new Timer(500/ SPEED, this); + + public SnakeGame() { + t.start(); + addKeyListener(new Keyboard()); + setFocusable(true); + } + + public void paint(Graphics g) { + g.setColor(color(10, 50, 10)); + g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE); + g.setColor(color(255, 216, 0)); + + for (int xx = 0; xx <= WIDTH * SCALE; xx += SCALE) { + g.drawLine(xx, 0, xx, HEIGHT * SCALE); + } + + for (int yy = 0; yy <= HEIGHT * SCALE; yy += SCALE) { + g.drawLine(0, yy, WIDTH * SCALE, yy); + } + + for (int d = 0; d < s.length; d++) { + g.setColor(color(0, 0, 255)); + g.fillRect(s.snakeX[d] * SCALE + 1, s.snakeY[d] * SCALE + 1, SCALE - 1, SCALE - 1); + } + + g.setColor(color(255, 0, 0)); + g.fillRect(a.posX * SCALE + 1, a.posY * SCALE + 1, SCALE - 1, SCALE - 1); + } + + public Color color(int red, int green, int blue) { + return new Color(red, green, blue); + } + + public static void main(String[] args) { + JFrame f = new JFrame(); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setResizable(false); + f.setSize(WIDTH * SCALE + 7, HEIGHT * SCALE + 29); + f.setLocationRelativeTo(null); + f.add(new SnakeGame()); + f.setVisible(true); + } + + public void actionPerformed(ActionEvent e) { + s.move(); + + if ((s.snakeX[0] == a.posX) & (s.snakeY[0] == a.posY)) { + a.setRandomPosition(); + s.length++; + } + + for (int k = 1; k < s.length; k++) { + if ((s.snakeX[k] == a.posX) & (s.snakeY[k] == a.posY)) { + a.setRandomPosition(); + } + } + + repaint(); + } + + private class Keyboard extends KeyAdapter { + public void keyPressed(KeyEvent kEve) { + int key = kEve.getKeyCode(); + + if ((key == KeyEvent.VK_RIGHT) & s.direction != 2) s.direction = 0; + if ((key == KeyEvent.VK_DOWN) & s.direction != 3) s.direction = 1; + if ((key == KeyEvent.VK_LEFT) & s.direction != 0) s.direction = 2; + if ((key == KeyEvent.VK_UP) & s.direction != 1) s.direction = 3; + } + } +} \ No newline at end of file