Snake Game in Java: A Beginner's Guide
// Press Shift twice to open the Search Everywhere dialog and type 'show whitespaces', // then press Enter. You can now see whitespace characters in your code. package com.firefor01;
import java.awt.; import javax.swing.; import java.awt.event.; import java.util.;
public class test extends JFrame implements ActionListener { String name; JButton jbn = new JButton('开始'); D jpn = new D();
test() { Container cn = getContentPane(); cn.setLayout(new BorderLayout()); cn.add(jbn, BorderLayout.NORTH); cn.add(jpn, BorderLayout.CENTER); jbn.addActionListener(this); setSize(600, 400); setVisible(true); }
public void actionPerformed(ActionEvent e) {
Thread t1 = new Thread(jpn);
t1.start();
jpn.requestFocus(true);
}
public static void main(String args[]) {
new test();
}
}
class D extends JPanel implements Runnable, KeyListener {
int x = 10;
int direction = 0;
Random r = new Random();
Vector
D() {
v.add(new Point(20, 10));
v.add(new Point(20, 10));
v.add(new Point(20, 10));
v.add(new Point(20, 10));
int t = r.nextInt(20);
food.x = t * 10;
food.y = t * 10;
addKeyListener(this);
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < v.size(); i++) {
Point p = v.get(i);
int x = p.x;
int y = p.y;
if (i == v.size() - 1) {
g.setColor(Color.blue);
g.fillOval(x, y, 9, 9);
} else {
g.setColor(Color.black);
g.fillOval(x, y, 9, 9);
}
}
int x = food.x;
int y = food.y;
g.setColor(Color.red);
g.fillRect(x, y, 9, 9);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP && direction != 3) { // Add boundary check
direction = 2;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != 2) { // Add boundary check
direction = 3;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != 0) { // Add boundary check
direction = 1;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != 1) { // Add boundary check
direction = 0;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void run() {
try {
while (true) {
Point head = v.get(v.size() - 1); // Get the head of the snake
Point newHead = new Point(head); // Create a new head point
// Update the new head point based on the direction
if (direction == 0) {
newHead.x += 10;
}
if (direction == 1) {
newHead.x -= 10;
}
if (direction == 2) {
newHead.y -= 10;
}
if (direction == 3) {
newHead.y += 10;
}
// Check if the new head point is hitting the boundary
if (newHead.x < 0 || newHead.x >= getWidth() || newHead.y < 0 || newHead.y >= getHeight()) {
JOptionPane.showMessageDialog(null, 'Game over!'); // Show game over message
System.exit(0); // Exit the program
}
// Check if the new head point is hitting the body
for (int i = 0; i < v.size() - 1; i++) {
Point p = v.get(i);
if (p.equals(newHead)) {
JOptionPane.showMessageDialog(null, 'Game over!'); // Show game over message
System.exit(0); // Exit the program
}
}
v.add(newHead); // Add the new head point to the vector
// Check if the snake is eating the food
if (newHead.equals(food)) {
int t = r.nextInt(20);
food.x = t * 10;
food.y = t * 10;
} else {
v.remove(0); // Remove the tail of the snake
}
repaint(); // Repaint the panel
Thread.sleep(100); // Delay the thread
}
} catch (Exception e) {
}
}
}
原文地址: http://www.cveoy.top/t/topic/oBIn 著作权归作者所有。请勿转载和采集!