Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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]--;


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;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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, 10, 10);
Timer t = new Timer(1000 / SPEED, this);

public SnakeGame() {
t.start();
addKeyListener(new Keyboard());
setFocusable(true);
}

public void paint(Graphics g) {
g.setColor(color(5, 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;
}
}
}