`
MouseLearnJava
  • 浏览: 465999 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Swing贪吃蛇游戏(一):基本功能实现

阅读更多

本文将提供一个Swing版本的贪吃蛇游戏,游戏包括最基本的功能:

1. 用Timer来管理贪吃蛇线程。
2. 实现按钮,键盘的事件响应。
3. 随机产生食物。
4. 游戏结束的判断:蛇头触碰到蛇身或者蛇头触碰到边界。
5. 实现游戏过程中的暂停以及贪吃蛇运行速度调整。
6. … …


程序界面如下:左边是贪吃蛇运行的范围,右边暂时只有分数信息,当蛇吃到食物的时候分数加10.



暂停,调整蛇体运行速度界面如下:



主要的代码如下:



package my.games.snake.model;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import my.games.snake.contants.SnakeGameConstant;

/**
 * 
 * 贪吃蛇游戏用到的格子类
 * 
 * @author Eric
 * 
 */
public class Grid implements Serializable {

	private static final long serialVersionUID = 5105993927776028563L;

	private int x; // x location

	private int y; // y location

	private Color color; // color for square

	public Grid() {
	}

	public Grid(int x, int y, Color color) {
		this.x = x;
		this.y = y;
		this.color = color;
	}

	/**
	 * Draw Grid
	 * 
	 * @param g2
	 */
	public void draw(Graphics2D g2) {
		int clientX = SnakeGameConstant.SNAKE_GAME_PANEL_LEFT + x
				* SnakeGameConstant.GRID_SIZE;
		int clientY = SnakeGameConstant.SNAKE_GAME_PANEL_TOP + y
				* SnakeGameConstant.GRID_SIZE;
		Rectangle2D.Double rect = new Rectangle2D.Double(clientX, clientY,
				SnakeGameConstant.GRID_SIZE, SnakeGameConstant.GRID_SIZE);
		g2.setPaint(color);
		g2.fill(rect);
		g2.setPaint(Color.BLACK);
		g2.draw(rect);
	}

	/**
	 * @return the color
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * @param color
	 *            the color to set
	 */
	public void setColor(Color color) {
		this.color = color;
	}

	/**
	 * @return the x
	 */
	public int getX() {
		return x;
	}

	/**
	 * @param x
	 *            the x to set
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * @return the y
	 */
	public int getY() {
		return y;
	}

	/**
	 * @param y
	 *            the y to set
	 */
	public void setY(int y) {
		this.y = y;
	}

}


package my.games.snake.model;

import java.awt.Graphics2D;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import my.games.snake.contants.SnakeGameConstant;
import my.games.snake.enums.Direction;

public class Snake implements Serializable {

	private static final long serialVersionUID = -1064622816984550631L;

	private List<Grid> list = null;

	private Direction direction = Direction.RIGHT;

	public Snake() {
		this.list = new LinkedList<Grid>();
	}

	public void changeDirection(Direction direction) {
		if (direction.isUpDirection()) {
			if (!this.direction.isUpDirection()
					&& !this.direction.isDownDirection()) {
				this.direction = direction;
			}
		} else if (direction.isRightDirection()) {
			if (!this.direction.isRightDirection()
					&& !this.direction.isLeftDirection()) {
				this.direction = direction;
			}
		} else if (direction.isDownDirection()) {
			if (!this.direction.isUpDirection()
					&& !this.direction.isDownDirection()) {
				this.direction = direction;
			}
		} else if (direction.isLeftDirection()) {
			if (!this.direction.isRightDirection()
					&& !this.direction.isLeftDirection()) {
				this.direction = direction;
			}
		}
	}

	public void draw(Graphics2D g2) {
		for (Grid grid : list) {
			grid.draw(g2);
		}
	}

	/**
	 * @return the list
	 */
	public List<Grid> getList() {
		return list;
	}

	/**
	 * @param list
	 *            the list to set
	 */
	public void setList(List<Grid> list) {
		this.list = list;
	}

	/**
	 * @return the direction
	 */
	public Direction getDirection() {
		return direction;
	}

	/**
	 * @param direction
	 *            the direction to set
	 */
	public void setDirection(Direction direction) {
		this.direction = direction;
	}

	public void move() {
		Grid currentHead = list.get(0);
		int headX = currentHead.getX();
		int headY = currentHead.getY();
		currentHead.setColor(SnakeGameConstant.SNAKE_BODY_COLOR);

		if (direction.isDownDirection()) {
			list.add(0, new Grid(headX, headY + 1,
					SnakeGameConstant.SNAKE_HEADER_COLOR));
			list.remove(list.size() - 1);
		} else if (direction.isUpDirection()) {
			list.add(0, new Grid(headX, headY - 1,
					SnakeGameConstant.SNAKE_HEADER_COLOR));
			list.remove(list.size() - 1);
		} else if (direction.isRightDirection()) {
			list.add(0, new Grid(headX + 1, headY,
					SnakeGameConstant.SNAKE_HEADER_COLOR));
			list.remove(list.size() - 1);
		} else if (direction.isLeftDirection()) {
			list.add(0, new Grid(headX - 1, headY,
					SnakeGameConstant.SNAKE_HEADER_COLOR));
			list.remove(list.size() - 1);
		}

	}

}


package my.games.snake.ui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import my.games.snake.contants.SnakeGameConstant;
import my.games.snake.enums.GameState;

public class SnakeGameFrame extends JFrame {

	private static final long serialVersionUID = 998014032682506026L;

	private SnakeGamePanel panel;

	private Container contentPane;

	private JMenuItem startMI = new JMenuItem("Start");

	private JMenuItem pauseMI = new JMenuItem("Pause");

	private JMenu speedMenu = new JMenu("Speed");

	private JMenuItem exitMI = new JMenuItem("Exit");

	private JMenuItem aboutMI = new JMenuItem("About");

	private JRadioButtonMenuItem speedMI1 = new JRadioButtonMenuItem("Speed1",
			true);

	private JRadioButtonMenuItem speedMI2 = new JRadioButtonMenuItem("Speed2",
			false);

	private JRadioButtonMenuItem speedMI3 = new JRadioButtonMenuItem("Speed3",
			false);

	private JRadioButtonMenuItem speedMI4 = new JRadioButtonMenuItem("Speed4",
			false);

	private JRadioButtonMenuItem speedMI5 = new JRadioButtonMenuItem("Speed5",
			false);

	public int speedFlag = 1;

	public SnakeGameFrame() {
		setTitle(SnakeGameConstant.SNAKE_GAME);
		setSize(SnakeGameConstant.SNAKE_GAME_FRAME_WIDTH,
				SnakeGameConstant.SNAKE_GAME_FRAME_HEIGHT);
		setResizable(false);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu setMenu = new JMenu("Set");
		JMenu helpMenu = new JMenu("Help");

		setMenu.setMnemonic('s');
		setMenu.setMnemonic('H');

		menuBar.add(setMenu);
		menuBar.add(helpMenu);

		setMenu.add(startMI);
		setMenu.add(pauseMI);
		setMenu.addSeparator();

		setMenu.addSeparator();
		setMenu.add(speedMenu);
		setMenu.addSeparator();
		setMenu.add(exitMI);

		ButtonGroup group = new ButtonGroup();
		group.add(speedMI1);
		group.add(speedMI2);
		group.add(speedMI3);
		group.add(speedMI4);
		group.add(speedMI5);

		speedMenu.add(speedMI1);
		speedMenu.add(speedMI2);
		speedMenu.add(speedMI3);
		speedMenu.add(speedMI4);
		speedMenu.add(speedMI5);

		startMI.addActionListener(new StartAction());
		pauseMI.addActionListener(new PauseAction());
		exitMI.addActionListener(new ExitAction());
		speedMI1.addActionListener(new SpeedAction());
		speedMI2.addActionListener(new SpeedAction());
		speedMI3.addActionListener(new SpeedAction());
		speedMI4.addActionListener(new SpeedAction());
		speedMI5.addActionListener(new SpeedAction());

		helpMenu.add(aboutMI);
		aboutMI.addActionListener(new AboutAction());

		contentPane = getContentPane();
		panel = new SnakeGamePanel(this);
		contentPane.add(panel);

		startMI.setEnabled(true);
		pauseMI.setEnabled(false);

		// 设置游戏状态是初始化状态
		panel.setGameState(GameState.INITIALIZE);
	}

	private class StartAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			startMI.setEnabled(false);
			pauseMI.setEnabled(true);
			panel.setGameState(GameState.RUN);
			panel.getTimer().start();
		}
	}

	private class PauseAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			pauseMI.setEnabled(false);
			startMI.setEnabled(true);
			panel.setGameState(GameState.PAUSE);
			if (panel.getTimer().isRunning()) {
				panel.getTimer().stop();
			}

		}
	}

	private class SpeedAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			Object speed = event.getSource();
			if (speed == speedMI1) {
				speedFlag = 1;
			} else if (speed == speedMI2) {
				speedFlag = 2;
			} else if (speed == speedMI3) {
				speedFlag = 3;
			} else if (speed == speedMI4) {
				speedFlag = 4;
			} else if (speed == speedMI5) {
				speedFlag = 5;
			}

			panel.getTimer().setDelay(1000 - 200 * (speedFlag - 1));
		}
	}

	private class ExitAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			int result = JOptionPane.showConfirmDialog(SnakeGameFrame.this,
					SnakeGameConstant.QUIT_GAME, SnakeGameConstant.SNAKE_GAME,
					JOptionPane.YES_NO_OPTION);
			if (result == JOptionPane.YES_OPTION) {
				System.exit(0);
			}
		}
	}

	private class AboutAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String string = SnakeGameConstant.KEYBOARDS_DESCRIPTION;
			JOptionPane.showMessageDialog(SnakeGameFrame.this, string);
		}
	}

}


package my.games.snake.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import my.games.snake.contants.SnakeGameConstant;
import my.games.snake.enums.Direction;
import my.games.snake.enums.GameState;
import my.games.snake.model.Grid;
import my.games.snake.model.Snake;

public class SnakeGamePanel extends JPanel {

	private static final long serialVersionUID = -4173775119881265176L;

	private int flag[][] = new int[SnakeGameConstant.GRID_COLUMN_NUMBER][SnakeGameConstant.GRID_ROW_NUMBER];// 在一个20*30的界面中,设置每个方块的flag

	private Color color[][] = new Color[SnakeGameConstant.GRID_COLUMN_NUMBER][SnakeGameConstant.GRID_ROW_NUMBER];// 在一个20*30的界面中,设置每个方块的颜色

	private Snake snake;

	private Grid food;

	public TimerAction timerAction;

	private int score;

	private SnakeGameFrame frame;

	private Timer timer;

	private Grid grid;

	private GameState gameState = GameState.INITIALIZE;

	//private GameOverType gameOverType = GameOverType.TOUCH_EDGE;

	private boolean needToGenerateFood = false;

	public SnakeGamePanel(SnakeGameFrame frame) {
		for (int i = SnakeGameConstant.LEFT; i <= SnakeGameConstant.RIGHT; i++) {
			for (int j = SnakeGameConstant.UP; j <= SnakeGameConstant.DOWN; j++) {
				flag[i][j] = 0;
			}
		}
		addKeyListener(new KeyHandler());
		setFocusable(true);
		init();

		timerAction = new TimerAction();
		timer = new Timer(1000, timerAction);
		score = 0;
		this.frame = frame;
		grid = new Grid();
	}

	private void init() {
		initSnake();
		initFood();
	}

	private void initSnake() {
		snake = new Snake();
		List<Grid> list = new LinkedList<Grid>();
		list.add(new Grid(4, 1, SnakeGameConstant.SNAKE_BODY_COLOR));
		list.add(0, new Grid(5, 1, SnakeGameConstant.SNAKE_HEADER_COLOR));
		snake.setList(list);
	}

	private void initFood() {
		food = new Grid();
		needToGenerateFood = true;
		this.generateFoodByRandom();
	}

	public void setGameState(GameState state) {
		gameState = state;
	}

	private void judgeGameOver() {
		if (isSnakeHeadTouchEdge() || isSnakeHeadTouchBody()) {
			gameState = GameState.OVER;
			int result = JOptionPane.showConfirmDialog(frame,
					SnakeGameConstant.GAME_OVER, SnakeGameConstant.SNAKE_GAME,
					JOptionPane.YES_NO_OPTION);
			if (result == JOptionPane.YES_OPTION) {
				for (int i = SnakeGameConstant.LEFT; i <= SnakeGameConstant.RIGHT; i++) {
					for (int j = SnakeGameConstant.UP; j <= SnakeGameConstant.DOWN; j++) {
						flag[i][j] = 0;
					}
				}

				gameState = GameState.RUN;
				score = 0;
				init();
				timer.start();
			} else {
				System.exit(0);
			}
		}
	}

	public void drawGameFrame(Graphics2D g2) {

	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;

		g2.draw(new Rectangle2D.Double(SnakeGameConstant.SNAKE_GAME_PANEL_LEFT,
				SnakeGameConstant.SNAKE_GAME_PANEL_TOP,
				SnakeGameConstant.SNAKE_GAME_PANEL_RIGHT,
				SnakeGameConstant.SNAKEGAME_PANEL_BOTTOM));

		if (gameState.isInitializedState()) {
			return;
		}

		draw(g2);
		drawScore(g);
	}

	private void draw(Graphics2D g2) {
		drawSnake(g2);
		drawFood(g2);
		for (int i = SnakeGameConstant.LEFT; i <= SnakeGameConstant.RIGHT; i++) {
			for (int j = SnakeGameConstant.UP; j <= SnakeGameConstant.DOWN; j++) {
				if (flag[i][j] == 1) {
					grid.setX(i);
					grid.setY(j);
					grid.setColor(color[i][j]);
					grid.draw(g2);
				}
			}
		}
	}

	private void drawScore(Graphics g) {
		g.drawString("Score: " + score,
				SnakeGameConstant.SNAKE_GAME_PANEL_RIGHT + 20, 200);
	}

	private void drawSnake(Graphics2D g2) {
		snake.draw(g2);
	}

	private void drawFood(Graphics2D g2) {
		food.draw(g2);
	}

	private class KeyHandler implements KeyListener {
		public void keyPressed(KeyEvent event) {
			if (!gameState.isRunState()) {
				return;
			}
			int keyCode = event.getKeyCode();
			switch (keyCode) {
			case KeyEvent.VK_LEFT:
				snake.changeDirection(Direction.LEFT);
				break;

			case KeyEvent.VK_RIGHT:
				snake.changeDirection(Direction.RIGHT);
				break;

			case KeyEvent.VK_UP:
				snake.changeDirection(Direction.UP);
				break;

			case KeyEvent.VK_DOWN:
				snake.changeDirection(Direction.DOWN);
				break;
			default:
				break;
			}
			repaint();
		}

		public void keyReleased(KeyEvent event) {
		}

		public void keyTyped(KeyEvent event) {
		}
	}

	private class TimerAction implements ActionListener, Serializable {

		private static final long serialVersionUID = 749074368549207272L;

		public void actionPerformed(ActionEvent e) {
			if (!gameState.isRunState()) {
				return;
			}

			generateFoodByRandom();
			snake.move();
			eatFood();
			judgeGameOver();

			repaint();
		}
	}

	private boolean isFoodAvailable(int x, int y) {
		for (Grid grid : snake.getList()) {
			if (x == grid.getX() && y == grid.getY()) {
				return false;
			}
		}
		return true;
	}

	private void generateFoodByRandom() {

		if (needToGenerateFood) {
			Random r = new Random();
			int randomX = r.nextInt(SnakeGameConstant.GRID_COLUMN_NUMBER);
			int randomY = r.nextInt(SnakeGameConstant.GRID_ROW_NUMBER);

			if (isFoodAvailable(randomX, randomX)) {
				food = new Grid(randomX, randomY, SnakeGameConstant.FOOD_COLOR);

				needToGenerateFood = false;
			} else {
				generateFoodByRandom();
			}
		}
	}

	private boolean isSnakeHeadTouchEdge() {
		Grid head = this.snake.getList().get(0);
		if ((head.getX() >= SnakeGameConstant.GRID_COLUMN_NUMBER)
				|| (head.getX() < 0)) {
			//this.gameOverType = GameOverType.TOUCH_EDGE;
			return true;
		}
		if ((head.getY() >= SnakeGameConstant.GRID_ROW_NUMBER)
				|| (head.getY() < 0)) {
			//this.gameOverType = GameOverType.TOUCH_EDGE;
			return true;
		}

		return false;
	}

	private boolean isSnakeHeadTouchBody() {
		Grid head = this.snake.getList().get(0);
		int length = snake.getList().size();

		for (int i = 1; i < length; i++) {
			if (head.getX() == snake.getList().get(i).getX()
					&& head.getY() == snake.getList().get(i).getY()) {
				//this.gameOverType = GameOverType.TOUCH_BODY;
				return true;
			}
		}

		return false;
	}

	private boolean isFoodTouched() {
		Grid head = snake.getList().get(0);
		return head.getX() == food.getX() && head.getY() == food.getY();
	}

	private void eatFood() {
		if (isFoodTouched()) {
			Grid tail = snake.getList().get(snake.getList().size() - 1);
			snake.getList().add(tail);
			this.needToGenerateFood = true;
			this.score += 10;

		}
	}

	/**
	 * @return the timer
	 */
	public Timer getTimer() {
		return timer;
	}

	/**
	 * @param timer
	 *            the timer to set
	 */
	public void setTimer(Timer timer) {
		this.timer = timer;
	}

}


完整的代码,请参考附件MySnakeGame.7z,有需要的朋友可以下载。

后续的博文将添加如下功能:

(二)添加随机障碍物。
(三)添加游戏进度的存储和读取
(四)完成游戏排行榜
... ...
  • 大小: 20.3 KB
  • 大小: 23 KB
  • 大小: 20.3 KB
3
7
分享到:
评论

相关推荐

    JavaSwing贪吃蛇小游戏

    在Swing贪吃蛇游戏中,主要涉及以下核心知识点: 1. **Swing组件**:游戏界面通常由多个Swing组件构成,如JFrame作为主窗口,JPanel作为游戏面板,以及可能的JLabel用于显示分数或游戏状态。这些组件通过继承自...

    Swing贪吃蛇游戏(二):增加随机产生障碍物功能

    在本项目中,我们探讨的是如何在Java Swing中创建一个贪吃蛇游戏,并且增加了随机产生障碍物的功能。Swing是Java的一个图形用户界面(GUI)工具包,它提供了丰富的组件和事件处理机制,使得开发桌面应用程序变得更加...

    Swing版贪吃蛇游戏

    Swing版贪吃蛇游戏是一款基于Java Swing图形用户界面库开发的经典小游戏,它不仅保留了传统贪吃蛇的基本玩法,还增加了许多现代化的功能,如进度保存与加载、得分排行榜和随机生成的障碍物等,提升了游戏的趣味性和...

    java swing 贪吃蛇游戏.zip

    这个“java swing 贪吃蛇游戏.zip”压缩包包含了一个使用Java Swing开发的贪吃蛇游戏的完整源代码,非常适合初学者进行学习、实践,或者作为毕业设计或课程设计的项目。 贪吃蛇游戏是一个经典的计算机游戏,玩家...

    Java swing 贪吃蛇游戏.zip

    1. **Swing组件**:贪吃蛇游戏的核心是游戏面板,这通常会由`JPanel`类来实现。在`GamePanel.java`中,我们可能会看到如何自定义`JPanel`,添加绘图功能,以显示蛇、食物和边界。 2. **多线程**:为了实现动态的...

    (源码+视频) swing贪吃蛇游戏开发教程+源码.rar

    【Swing贪吃蛇游戏开发教程】是一套针对Java初学者的编程教学资源,它涵盖了使用Java Swing库创建一个经典游戏——贪吃蛇的过程。Swing是Java AWT(抽象窗口工具包)的一个扩展,提供了丰富的图形用户界面(GUI)...

    java_swing完整版贪吃蛇

    在本项目“java_swing完整版贪吃蛇”中,开发者利用Java Swing的技术,实现了一个经典的贪吃蛇游戏。 贪吃蛇游戏是一个非常基础且受欢迎的游戏,玩家控制一个不断移动的蛇,通过吃食物来增长蛇的身体长度。当蛇头...

    基于Java Swing的贪吃蛇游戏源代码和可运行文件

    本项目利用Java Swing构建了一个经典的【贪吃蛇游戏】,这款游戏简单易懂,是学习Swing和游戏编程的绝佳案例。 在Java Swing中,我们首先会接触到`JFrame`,它是窗口的基础类,用于展示应用程序的主要界面。在...

    Java项目:贪吃蛇游戏(java+swing)

    总的来说,Java Swing结合贪吃蛇游戏的实现,不仅展示了Swing的基础用法,也涵盖了游戏开发中的基本逻辑处理和图形绘制,是学习Java GUI编程的一个很好的实践项目。通过这个项目,开发者可以深入理解事件处理、线程...

    贪吃蛇游戏(java实现)

    在贪吃蛇游戏中,通常有两个主要的线程:一个是主游戏循环线程,负责游戏的逻辑更新;另一个是渲染线程,用于绘制游戏画面。使用Thread类或者Runnable接口可以创建并管理这些线程。 **4. 坐标系统和碰撞检测** 游戏...

    基于java-swing的贪吃蛇游戏

    总结,通过Java Swing和基本的面向对象编程,我们可以构建出一款具有趣味性的贪吃蛇游戏。这个过程不仅锻炼了我们的编程技能,也让我们对Java GUI和事件处理有了更深入的理解。实际开发过程中,还可能遇到更多挑战,...

    贪吃蛇游戏java版源码

    贪吃蛇游戏是一款经典的计算机游戏,它源自早期的诺基亚手机,并且在全球范围内广受欢迎。这个Java版本的贪吃蛇游戏源码是为那些希望深入理解游戏开发和Java GUI编程,特别是AWT(Abstract Window Toolkit)和Swing...

    基于Swing开发的贪吃蛇小游戏.zip

    这个基于Swing的贪吃蛇游戏项目不仅展示了Swing组件的使用,还涵盖了基本的游戏编程概念,如定时器、绘图、事件处理和游戏逻辑。通过研究和实践这个项目,开发者可以深入理解Java GUI编程,并提升自己的编程技能。

    java swing的贪吃蛇代码,自己写的,没有用到线程 只用到Swing的timer类控制游戏运行

    在这个项目中,开发者使用Swing来实现了一个经典的贪吃蛇游戏,而没有使用多线程技术。通常,游戏开发中会使用线程来实现游戏循环,以便在处理用户输入的同时更新游戏状态。然而,此代码通过Swing的`Timer`类实现了...

    儿时回忆,贪吃蛇,Java swing编写,完美运行。myeclipes导入即可

    在Java Swing中实现贪吃蛇游戏,主要涉及以下几个关键知识点: 1. **图形用户界面(GUI)**:使用JFrame作为主窗口,放置JPanel作为游戏面板。Swing提供了各种组件,如JButton、JLabel等,但在这里主要是自定义组件以...

    Java swing框架实现的贪吃蛇游戏完整示例

    Java swing框架实现的贪吃蛇游戏完整示例 本文主要介绍了使用Java swing框架实现的贪吃蛇游戏的完整示例,通过结合完整实例形式分析了Java使用swing框架结合awt图形绘制实现贪吃蛇游戏的具体步骤与相关实现技巧。 ...

    自己写的贪吃蛇游戏源码(swing版本)

    在这个特定的项目中,"自己写的贪吃蛇游戏源码(swing版本)"是一个利用Swing实现的经典游戏贪吃蛇的示例代码。对于初学者来说,这是一个很好的实践案例,可以帮助他们理解和掌握Swing的基本用法以及游戏开发的基础...

    java贪吃蛇小游戏程序课程设计.pdf

    1. 贪吃蛇游戏:实现一个基本的贪吃蛇游戏,包括蛇的移动、食物的随机产生和游戏的结束判断。 2. 游戏升级:游戏可以进行升级,增加吃食物的难度,以便使游戏更具有挑战性。 3. 游戏界面可调整:游戏界面的大小可以...

    一个小小的贪吃蛇游戏,基于Java Swing .zip

    标题中的“一个小小的贪吃蛇游戏,基于Java Swing .zip”揭示了这是一个使用Java编程语言,特别是Java Swing库开发的简单贪吃蛇游戏。Java Swing是Java AWT(抽象窗口工具包)的一个扩展,提供了丰富的图形用户界面...

Global site tag (gtag.js) - Google Analytics