Java怎么实现贪吃蛇游戏

要实现贪吃蛇游戏,可以使用Java语言编写。下面是一个简单的示例代码:import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Image;import java.aw

要实现贪吃蛇游戏,可以使用Java语言编写。下面是一个简单的示例代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GameBoard extends JPanel implements ActionListener {
private final int B_WIDTH = 300;
private final int B_HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public GameBoard() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
loadImages();
initGame();
}
private void loadImages() {
ImageIcon iid = new ImageIcon("src/resources/dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("src/resources/apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("src/resources/head.png");
head = iih.getImage();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}        
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
private void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] >= B_HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= B_WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/942520.html

(0)
派派
上一篇 2024-03-19
下一篇 2024-03-19

相关推荐

  • 阿里云如何挂载数据盘(数据盘怎么挂载)

    阿里云如何挂载数据盘,数据盘怎么挂载内容导航:阿里云windows数据盘怎么挂载阿里云云服务器挂载数据盘怎么使用阿里云linux服务器挂载数据盘有什么用阿里云数据能迁移吗一、阿里云windows数据盘怎么挂载右键单击“计算机”选择“管理”然后在左侧栏选择“

    2022-05-01
    0
  • java如何取数字的第几位

    要取数字的第几位,可以使用以下方法:public int getDigit(int number, int position) {String numStr = String.valueOf(number);if(position numStr.length()) {throw new IllegalArgumentException(“Position is

    2024-04-03
    0
  • Linux日志文件管理和分析的方法是什么

    Linux日志文件管理和分析的方法包括以下几个步骤:确保日志文件的记录功能已经开启:在Linux系统中,一般由rsyslogd服务来负责日志的记录和管理。确保该服务已经正确配置并运行。配置日志文件的存储位置和大小:可以在rsyslogd的配置文件中设置日志文件的存储位置和大小限制,以确保日志文件不会无限增长占满磁盘空间。定期清理日志文件:可以通过定时任务或者手动清理的方式,定期清理过期的日志文件,

    2024-04-28
    0
  • 网站如何快速被百度收录(如何让网站被百度快速收录)

    网站如何快速被百度收录,如何让网站被百度快速收录内容导航:网站如何被百度快速收录怎样才能让自己网站的文章快速被百度收录网站的内链如何快速被百度收录!自己做的网站如何让百度快速收录一、网站如何被百度快速收录第一步:使用百度链接提交代码,这是百度官方提供的代码,可以去百度站长平台获取,代码如图所示,很管用的,只要你的网页质量过得去,都能很快的被收录。(放在网站所有页面

    2022-04-23
    0
  • C#中的非空引用类型是什么

    在C#中,非空引用类型是Nullable类型。Nullable类型允许变量存储null值以及特定类型的值,即使该特定类型本身不能存储null值。Nullable类型可以通过在类型名称后面添加一个问号(?)来定义,例如int?表示一个可以存储整数或null值的变量。

    2024-04-26
    0
  • 「域名信息模板是什么」域名信息模板是什么意思

    域名信息模板是什么,域名信息模板是什么意思 内容导航: 域名模板名称怎么填 域名模板和域名过户模板有什么区别 域名信息报备的具体要求是什么 域名 空间 模版分别是什么 一、域名模板…

    2022-08-28
    0

发表回复

登录后才能评论