Snake/Java: Difference between revisions
Content deleted Content added
m let's just make x and y fields rather than an xy array |
No edit summary |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 1:
An good Snake Game by IZzSaken
edited on 21th May 2023
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
private static final int UNIT_SIZE = 10;
private static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
private static final int DELAY = 75;
private final
private final int[]
private int bodyParts =
private int applesEaten;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(new MyKeyAdapter());
startGame();
}
private void startGame() {
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
private void draw(Graphics g) {
if (running) {
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.GREEN);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 14));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: " + applesEaten, (WIDTH - metrics.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize());
} else {
}
}
private void
appleX = (int) (Math.random() * (WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = (int) (Math.random() * (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
private void
}
case 'D':
y[0] =
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
private void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
// Check if head touches left border
}
// Check if head touches right border
}
// Check if head touches top border
// Check if head touches bottom border
if (
running = false;
}
if (!running) {
}
}
private void
g.setColor(Color.
FontMetrics metrics1 = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics1.stringWidth("Game Over")) / 2, HEIGHT / 2);
g.setColor(
FontMetrics metrics2 = getFontMetrics(g.getFont());
g.drawString("Score: " + applesEaten, (WIDTH - metrics2.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize());
}
@Override
public void actionPerformed(ActionEvent e) {
checkApple();
checkCollisions();
}
repaint();
}
private class MyKeyAdapter extends KeyAdapter {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
SnakeGame
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|