Ich versuche, ein Snake-Spiel zu machen, indem Sie ein Tutorial mit Java beobachten. Mein Problem ist, dass die Schlange von Anfang an mit sich selbst kollidiert.Snake Game Bewegung Problem
Hier ist meine Hauptklasse "Main.java"
import java.awt.Color;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame obj = new JFrame();
Gameplay gameplay = new Gameplay();
obj.setBounds(10, 10, 905, 700);
obj.setBackground(Color.DARK_GRAY);
obj.setResizable(false);
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(gameplay);
}
}
meine Spielklasse Hier namens "Gameplay.java"
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Gameplay extends JPanel implements KeyListener, ActionListener {
private int[] snakexlength = new int[750];
private int[] snakeylength = new int[750];
private boolean left = false;
private boolean right = false;
private boolean up = false;
private boolean down = false;
private ImageIcon rightmouth;
private ImageIcon upmouth;
private ImageIcon downmouth;
private ImageIcon leftmouth;
private int lengthofsnake = 3;
private Timer timer;
private int delay = 100;
private ImageIcon snakeimage;
private ImageIcon titleImage;
private ImageIcon enemyimage;
private int[] enemyxpos = {
25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425,
450, 475, 500, 525, 550, 575, 600, 625, 650, 675, 700, 725, 750, 775, 800, 825, 850 };
private int[] enemyypos = {
75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475,
500, 525, 550, 575, 600, 625 };
private Random random = new Random();
private int xpos = random.nextInt(34);
private int ypos = random.nextInt(23);
private int score = 0;
private int moves = 0;
public Gameplay() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g) {
if (moves == 0) {
snakexlength[2] = 50;
snakexlength[1] = 75;
snakexlength[0] = 100;
snakeylength[2] = 100;
snakeylength[1] = 100;
snakeylength[0] = 100;
}
// draw title image border
g.setColor(Color.WHITE);
g.drawRect(24, 10, 851, 55);
// draw the title image
titleImage = new ImageIcon("snaketitle.jpg");
titleImage.paintIcon(this, g, 25, 11);
// draw border for gameplay
g.setColor(Color.WHITE);
g.drawRect(24, 74, 851, 577);
// draw background for the gameplay
g.setColor(Color.BLACK);
g.fillRect(25, 75, 850, 575);
// draw scores
g.setColor(Color.red);
g.setFont(new Font("arial", Font.BOLD, 20));
g.drawString("Score: " + score, 780, 30);
// draw length
g.setColor(Color.red);
g.setFont(new Font("arial", Font.BOLD, 20));
g.drawString("Length: " + lengthofsnake, 780, 50);
rightmouth = new ImageIcon("rightmouth.png");
rightmouth.paintIcon(this, g, snakexlength[0], snakeylength[0]);
for (int a = 0; a < lengthofsnake; a++)
{
if (a == 0 && right == true) {
rightmouth = new ImageIcon("rightmouth.png");
rightmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if (a == 0 && left == true) {
leftmouth = new ImageIcon("leftmouth.png");
leftmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if (a == 0 && up == true) {
upmouth = new ImageIcon("upmouth.png");
upmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if (a == 0 && down == true) {
downmouth = new ImageIcon("downmouth.png");
downmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if (a != 0) {
snakeimage = new ImageIcon("snakeimage.png");
snakeimage.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
}
enemyimage = new ImageIcon("enemy.png");
if ((enemyxpos[xpos] == snakexlength[0] && enemyypos[ypos] == snakeylength[0])) {
scorelength();
ratrandom();
/*
* score++; lengthofsnake++; xpos = random.nextInt(34); ypos =
* random.nextInt(23);
*/
}
enemyimage.paintIcon(this, g, enemyxpos[xpos], enemyypos[xpos]);
for (int b = 1; b < lengthofsnake; b++) {
if (snakexlength[b] == snakexlength[0] && snakeylength[0] == snakeylength[0]) {
right = false;
left = false;
up = false;
down = false;
g.setColor(Color.white);
g.setFont(new Font("arial", Font.BOLD, 50));
g.drawString("Game Overs", 300, 300);
g.setFont(new Font("arial", Font.BOLD, 20));
g.drawString("Spacebar to AGAIN play", 350, 340);
}
}
g.dispose();
}
public void scorelength() {
score += 5;
lengthofsnake++;
}
public void ratrandom() {
xpos = random.nextInt(34);
ypos = random.nextInt(23);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
lengthofsnake = 3;
score = 0;
moves = 0;
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
moves++;
right = true;
if (left == false) {
right = true;
} else {
left = true;
right = false;
}
up = false;
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
moves++;
left = true;
if (right == false) {
left = true;
} else {
left = false;
right = true;
}
up = false;
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
moves++;
up = true;
if (down == false) {
up = true;
} else {
down = true;
up = false;
}
left = false;
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
moves++;
down = true;
if (up == false) {
down = true;
} else {
up = true;
down = false;
}
left = false;
right = false;
}
}
public void actionPerformed(ActionEvent e) {
timer.start();
if (right == true) {
for (int r = lengthofsnake - 1; r >= 0; r--) {
snakeylength[r + 1] = snakeylength[r];
}
for (int r = lengthofsnake; r >= 0; r--) {
if (r == 0) {
snakexlength[r] = snakeylength[r] + 25;
} else {
snakexlength[r] = snakeylength[r - 1];
}
if (snakexlength[r] > 850) {
snakexlength[r] = 25;
}
}
repaint();
}
if (left == true) {
for (int r = lengthofsnake - 1; r >= 0; r--) {
snakeylength[r + 1] = snakeylength[r];
}
for (int r = lengthofsnake; r >= 0; r--) {
if (r == 0) {
snakexlength[r] = snakeylength[r] - 25;
} else {
snakexlength[r] = snakeylength[r - 1];
}
if (snakexlength[r] < 25) {
snakexlength[r] = 850;
}
}
repaint();
}
if (up) {
for (int r = lengthofsnake - 1; r >= 0; r--) {
snakexlength[r + 1] = snakexlength[r];
}
for (int r = lengthofsnake; r >= 0; r--) {
if (r == 0) {
snakeylength[r] = snakeylength[r] - 25;
} else {
snakeylength[r] = snakeylength[r - 1];
}
if (snakeylength[r] < 75) {
snakeylength[r] = 625;
}
}
repaint();
}
if (down == true) {
for (int r = lengthofsnake - 1; r >= 0; r--) {
snakexlength[r + 1] = snakexlength[r];
}
for (int r = lengthofsnake; r >= 0; r--) {
if (r == 0) {
snakeylength[r] = snakexlength[r] + 25;
} else {
snakeylength[r] = snakeylength[r - 1];
}
if (snakeylength[r] > 625) {
snakeylength[r] = 75;
}
}
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }
}
eine Hilfe nötig
Jetzt wäre wahrscheinlich eine gute Zeit zu lernen, wie man einen Debugger verwendet. – assylias
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – slim
Verwende 'paintComponent (g)', nicht 'paint (g)' ... Lade auch nicht neu die Bilder jedes Mal, wenn Sie malen. Laden Sie sie nur einmal. –