21xrx.com
2024-12-23 00:42:19 Monday
登录
文章检索 我的文章 写文章
Java小游戏代码大全:让编程初学者轻松上手游戏开发
2023-06-19 20:45:46 深夜i     --     --
Java 小游戏 代码

Java作为一门广受欢迎的编程语言,不仅在企业级应用开发中得到了广泛应用,也在游戏开发领域逐渐崭露头角。针对那些对Java编程有一定了解,想要尝试开发自己的小游戏的初学者,本文为大家提供了一份Java小游戏代码大全,其中包含了多个不同类型的小游戏,如扫雷、贪食蛇等。

下面给出一个贪食蛇的代码示例:


import java.awt.Color;

import java.awt.Graphics;

import java.awt.Point;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.util.LinkedList;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Snake extends JPanel implements KeyListener {

  private static final long serialVersionUID = -6209907017780465069L;

  private int width = 40;

  private int height = 30;

  private int size = 10;

  private LinkedList snake = new LinkedList ();

  private Point food = null;

  private Random random = new Random();

  private int direction = KeyEvent.VK_RIGHT;

  public Snake() {

    JFrame frame = new JFrame("Snake");

    frame.setSize(width * size + 6, height * size + 28);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(this);

    frame.addKeyListener(this);

    frame.setResizable(false);

    initSnake();

    initFood();

    frame.setVisible(true);

  }

  public void initSnake() {

    snake.clear();

    snake.add(new Point(5, 0));

    snake.add(new Point(4, 0));

    snake.add(new Point(3, 0));

    snake.add(new Point(2, 0));

  }

  public void initFood() {

    food = new Point(random.nextInt(width), random.nextInt(height));

  }

  public void move() {

    Point head = snake.getFirst();

    Point next = new Point(head.x, head.y);

    switch (direction) {

    case KeyEvent.VK_UP:

      next.y--;

      break;

    case KeyEvent.VK_DOWN:

      next.y++;

      break;

    case KeyEvent.VK_LEFT:

      next.x--;

      break;

    case KeyEvent.VK_RIGHT:

      next.x++;

      break;

    }

    if (next.x < 0 || next.x >= width || next.y < 0 || next.y >= height || snake.contains(next)) {

      initSnake();

      return;

    }

    snake.addFirst(next);

    if (next.equals(food)) {

      initFood();

    } else {

      snake.removeLast();

    }

  }

  @Override

  public void paint(Graphics g) {

    g.setColor(Color.BLACK);

    g.fillRect(0, 0, width * size, height * size);

    g.setColor(Color.WHITE);

    for (Point point : snake) {

      g.fillRect(point.x * size, point.y * size, size, size);

    }

    g.setColor(Color.RED);

    g.fillRect(food.x * size, food.y * size, size, size);

  }

  public static void main(String[] args) {

    new Snake().run();

  }

  public void run() {

    while (true) {

      try {

        Thread.sleep(100);

      } catch (InterruptedException e) {

        e.printStackTrace();

      }

      move();

      repaint();

    }

  }

  @Override

  public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if ((key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN || key == KeyEvent.VK_LEFT

        || key == KeyEvent.VK_RIGHT) && Math.abs(key - direction) != 2)

      direction = key;

    

  }

  @Override

  public void keyReleased(KeyEvent e)

  

  @Override

  public void keyTyped(KeyEvent e)

  

}

此处为贪食蛇的核心代码实现部分,具体效果请见运行结果。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复