
新建一个project,新建一个类
把代码贴进去,找到运行(run)这个按钮,按了就能运行,找不到的话快捷键是Ctrl + F11
import javaawtColor;
import javaawtComponent;
import javaawtGraphics;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventKeyEvent;
import javaawteventKeyListener;
import javautilArrayList;
import javaxswingBorderFactory;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJMenu;
import javaxswingJMenuBar;
import javaxswingJMenuItem;
import javaxswingJPanel;
public class SnakeGame {
public static void main(String[] args) {
SnakeFrame frame = new SnakeFrame();
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
}
// ----------记录状态的线程
class StatusRunnable implements Runnable {
public StatusRunnable(Snake snake, JLabel statusLabel, JLabel scoreLabel) {
thisstatusLabel = statusLabel;
thisscoreLabel = scoreLabel;
thissnake = snake;
}
public void run() {
String sta = "";
String spe = "";
while (true) {
switch (snakestatus) {
case SnakeRUNNING:
sta = "Running";
break;
case SnakePAUSED:
sta = "Paused";
break;
case SnakeGAMEOVER:
sta = "GameOver";
break;
}
statusLabelsetText(sta);
scoreLabelsetText("" + snakescore);
try {
Threadsleep(100);
} catch (Exception e) {
}
}
}
private JLabel scoreLabel;
private JLabel statusLabel;
private Snake snake;
}
// ----------蛇运动以及记录分数的线程
class SnakeRunnable implements Runnable {
public SnakeRunnable(Snake snake, Component component) {
thissnake = snake;
thiscomponent = component;
}
public void run() {
while (true) {
try {
snakemove();
componentrepaint();
Threadsleep(snakespeed);
} catch (Exception e) {
}
}
}
private Snake snake;
private Component component;
}
class Snake {
boolean isRun;// ---------是否运动中
ArrayList<Node> body;// -----蛇体
Node food;// --------食物
int derection;// --------方向
int score;
int status;
int speed;
public static final int SLOW = 500;
public static final int MID = 300;
public static final int FAST = 100;
public static final int RUNNING = 1;
public static final int PAUSED = 2;
public static final int GAMEOVER = 3;
public static final int LEFT = 1;
public static final int UP = 2;
public static final int RIGHT = 3;
public static final int DOWN = 4;
public Snake() {
speed = SnakeSLOW;
score = 0;
isRun = false;
status = SnakePAUSED;
derection = SnakeRIGHT;
body = new ArrayList<Node>();
bodyadd(new Node(60, 20));
bodyadd(new Node(40, 20));
bodyadd(new Node(20, 20));
makeFood();
}
// ------------判断食物是否被蛇吃掉
// -------如果食物在蛇运行方向的正前方,并且与蛇头接触,则被吃掉
private boolean isEaten() {
Node head = bodyget(0);
if (derection == SnakeRIGHT && (headx + NodeW) == foodx
&& heady == foody)
return true;
if (derection == SnakeLEFT && (headx - NodeW) == foodx
&& heady == foody)
return true;
if (derection == SnakeUP && headx == foodx
&& (heady - NodeH) == foody)
return true;
if (derection == SnakeDOWN && headx == foodx
&& (heady + NodeH) == foody)
return true;
else
return false;
}
// ----------是否碰撞
private boolean isCollsion() {
Node node = bodyget(0);
// ------------碰壁
if (derection == SnakeRIGHT && nodex == 280)
return true;
if (derection == SnakeUP && nodey == 0)
return true;
if (derection == SnakeLEFT && nodex == 0)
return true;
if (derection == SnakeDOWN && nodey == 380)
return true;
// --------------蛇头碰到蛇身
Node temp = null;
int i = 0;
for (i = 3; i < bodysize(); i++) {
temp = bodyget(i);
if (tempx == nodex && tempy == nodey)
break;
}
if (i < bodysize())
return true;
else
return false;
}
// -------在随机的地方产生食物
public void makeFood() {
Node node = new Node(0, 0);
boolean isInBody = true;
int x = 0, y = 0;
int X = 0, Y = 0;
int i = 0;
while (isInBody) {
x = (int) (Mathrandom() 15);
y = (int) (Mathrandom() 20);
X = x NodeW;
Y = y NodeH;
for (i = 0; i < bodysize(); i++) {
if (X == bodyget(i)x && Y == bodyget(i)y)
break;
}
if (i < bodysize())
isInBody = true;
else
isInBody = false;
}
food = new Node(X, Y);
}
// ---------改变运行方向
public void changeDerection(int newDer) {
if (derection % 2 != newDer % 2)// -------如果与原来方向相同或相反,则无法改变
derection = newDer;
}
public void move() {
if (isEaten()) {// -----如果食物被吃掉
bodyadd(0, food);// --------把食物当成蛇头成为新的蛇体
score += 10;
makeFood();// --------产生食物
} else if (isCollsion())// ---------如果碰壁或自身
{
isRun = false;
status = SnakeGAMEOVER;// -----结束
} else if (isRun) {// ----正常运行(不吃食物,不碰壁,不碰自身)
Node node = bodyget(0);
int X = nodex;
int Y = nodey;
// ------------蛇头按运行方向前进一个单位
switch (derection) {
case 1:
X -= NodeW;
break;
case 2:
Y -= NodeH;
break;
case 3:
X += NodeW;
break;
case 4:
Y += NodeH;
break;
}
bodyadd(0, new Node(X, Y));
// ---------------去掉蛇尾
bodyremove(bodysize() - 1);
}
}
}
// ---------组成蛇身的单位,食物
class Node {
public static final int W = 20;
public static final int H = 20;
int x;
int y;
public Node(int x, int y) {
thisx = x;
thisy = y;
}
}
// ------画板
class SnakePanel extends JPanel {
Snake snake;
public SnakePanel(Snake snake) {
thissnake = snake;
}
public void paintComponent(Graphics g) {
superpaintComponent(g);
Node node = null;
for (int i = 0; i < snakebodysize(); i++) {// ---红蓝间隔画蛇身
if (i % 2 == 0)
gsetColor(Colorblue);
else
gsetColor(Coloryellow);
node = snakebodyget(i);
gfillRect(nodex, nodey, nodeH, nodeW);// 试用
}
node = snakefood;
gsetColor(Colorred);
gfillRect(nodex, nodey, nodeH, nodeW);
}
}
class SnakeFrame extends JFrame {
private JLabel statusLabel;
private JLabel speedLabel;
private JLabel scoreLabel;
private JPanel snakePanel;
private Snake snake;
private JMenuBar bar;
JMenu gameMenu;
JMenu helpMenu;
JMenu speedMenu;
JMenuItem newItem;
JMenuItem pauseItem;
JMenuItem beginItem;
JMenuItem helpItem;
JMenuItem aboutItem;
JMenuItem slowItem;
JMenuItem midItem;
JMenuItem fastItem;
public SnakeFrame() {
init();
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (egetSource() == pauseItem)
snakeisRun = false;
if (egetSource() == beginItem)
snakeisRun = true;
if (egetSource() == newItem) {
newGame();
}
// ------------菜单控制运行速度
if (egetSource() == slowItem) {
snakespeed = SnakeSLOW;
speedLabelsetText("Slow");
}
if (egetSource() == midItem) {
snakespeed = SnakeMID;
speedLabelsetText("Mid");
}
if (egetSource() == fastItem) {
snakespeed = SnakeFAST;
speedLabelsetText("Fast");
}
}
};
pauseItemaddActionListener(l);
beginItemaddActionListener(l);
newItemaddActionListener(l);
aboutItemaddActionListener(l);
slowItemaddActionListener(l);
midItemaddActionListener(l);
fastItemaddActionListener(l);
addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
switch (egetKeyCode()) {
// ------------方向键改变蛇运行方向
case KeyEventVK_DOWN://
snakechangeDerection(SnakeDOWN);
break;
case KeyEventVK_UP://
snakechangeDerection(SnakeUP);
break;
case KeyEventVK_LEFT://
snakechangeDerection(SnakeLEFT);
break;
case KeyEventVK_RIGHT://
snakechangeDerection(SnakeRIGHT);
break;
// 空格键,游戏暂停或继续
case KeyEventVK_SPACE://
if (snakeisRun == true) {
snakeisRun = false;
snakestatus = SnakePAUSED;
break;
}
if (snakeisRun == false) {
snakeisRun = true;
snakestatus = SnakeRUNNING;
break;
}
}
}
public void keyReleased(KeyEvent k) {
}
public void keyTyped(KeyEvent k) {
}
});
}
private void init() {
speedLabel = new JLabel();
snake = new Snake();
setSize(380, 460);
setLayout(null);
thissetResizable(false);
bar = new JMenuBar();
gameMenu = new JMenu("Game");
newItem = new JMenuItem("New Game");
gameMenuadd(newItem);
pauseItem = new JMenuItem("Pause");
gameMenuadd(pauseItem);
beginItem = new JMenuItem("Continue");
gameMenuadd(beginItem);
helpMenu = new JMenu("Help");
aboutItem = new JMenuItem("About");
helpMenuadd(aboutItem);
speedMenu = new JMenu("Speed");
slowItem = new JMenuItem("Slow");
fastItem = new JMenuItem("Fast");
midItem = new JMenuItem("Middle");
speedMenuadd(slowItem);
speedMenuadd(midItem);
speedMenuadd(fastItem);
baradd(gameMenu);
baradd(helpMenu);
baradd(speedMenu);
setJMenuBar(bar);
statusLabel = new JLabel();
scoreLabel = new JLabel();
snakePanel = new JPanel();
snakePanelsetBounds(0, 0, 300, 400);
snakePanelsetBorder(BorderFactorycreateLineBorder(ColordarkGray));
add(snakePanel);
statusLabelsetBounds(300, 25, 60, 20);
add(statusLabel);
scoreLabelsetBounds(300, 20, 60, 20);
add(scoreLabel);
JLabel temp = new JLabel("状态");
tempsetBounds(310, 5, 60, 20);
add(temp);
temp = new JLabel("分数");
tempsetBounds(310, 105, 60, 20);
add(temp);
temp = new JLabel("速度");
tempsetBounds(310, 55, 60, 20);
add(temp);
speedLabelsetBounds(310, 75, 60, 20);
add(speedLabel);
}
private void newGame() {
thisremove(snakePanel);
thisremove(statusLabel);
thisremove(scoreLabel);
speedLabelsetText("Slow");
statusLabel = new JLabel();
scoreLabel = new JLabel();
snakePanel = new JPanel();
snake = new Snake();
snakePanel = new SnakePanel(snake);
snakePanelsetBounds(0, 0, 300, 400);
snakePanelsetBorder(BorderFactorycreateLineBorder(ColordarkGray));
Runnable r1 = new SnakeRunnable(snake, snakePanel);
Runnable r2 = new StatusRunnable(snake, statusLabel, scoreLabel);
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1start();
t2start();
add(snakePanel);
statusLabelsetBounds(310, 25, 60, 20);
add(statusLabel);
scoreLabelsetBounds(310, 125, 60, 20);
add(scoreLabel);
}
}
package snake;
import javaxswingUIManager;
import javaawt;
/
<p>Title: 贪食蛇游戏</p>
<p>Description: </p>
<p>Copyright: Copyright (c) 2005</p>
<p>Company: </p>
@author zsb
@version 10
/
public class Snake {
private boolean packFrame = false;
//Construct the application
public Snake() {
MainFrame frame = new MainFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, eg from their layout
if (packFrame) {
framepack();
}
else {
framevalidate();
}
//Center the window
Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();
Dimension frameSize = framegetSize();
if (frameSizeheight > screenSizeheight) {
frameSizeheight = screenSizeheight;
}
if (frameSizewidth > screenSizewidth) {
frameSizewidth = screenSizewidth;
}
framesetLocation((screenSizewidth - frameSizewidth) / 2, (screenSizeheight - frameSizeheight) / 2);
framesetVisible(true);
}
//Main method
public static void main(String[] args) {
/ try {
UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());
}
catch(Exception e) {
eprintStackTrace();
}/
new Snake();
}
}
package snake;
import javaawt;
import javaxswing;
/
<p>Title: 贪食蛇游戏</p>
<p>Description: </p>
<p>Copyright: Copyright (c) 2005</p>
<p>Company: </p>
@author zsb
@version 10
/
public class Help_Dialog extends JDialog {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JTextArea jTextArea1 = new JTextArea();
private String help = "游戏说明:\n1 :方向键控制蛇移动的方向"+
"\n2 :按开始键开始游戏"+
"\n3 :按暂停键可以暂停游戏,再按暂停键能继续玩游戏"+
"\n4 :**为普通食物,吃一个得100分"+
"\n5 :青色为穿身宝物,吃一个得100分,该宝物允许玩家穿过一次蛇身"+
"\n6 :红色为穿墙宝物,吃一个得200分,该宝物允许玩家穿过一次墙壁"+
"\n7 :当分数到达一定时,会自动升级当到达最高级别时就没有升级了";
GridBagLayout gridBagLayout1 = new GridBagLayout();
public Help_Dialog(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
exprintStackTrace();
}
}
public Help_Dialog() {
this(null, "", false);
}
private void jbInit() throws Exception {
panel1setLayout(borderLayout1);
jTextArea1setBackground(SystemColorcontrol);
jTextArea1setEditable(false);
jTextArea1setText("游戏说明:\n1 :方向键控制蛇移动的方向\n2 :按开始键开始游戏\n3 :按暂停键可以暂停游戏,再按暂停键能继续玩游戏\n4 :**为普通食物,吃一个得100分\n" +
"5 :青色为穿身宝物,吃一个得100分,该宝物允许玩家穿过一次蛇身\n6 :红色为穿墙宝物,吃一个得100分,该宝物允许玩家穿过一次墙壁\n" +
"7 :当分数到达一定时,会自动升级当到达最高级别时就没有升级了");
thissetResizable(false);
thissetTitle("帮助");
thisgetContentPane()setLayout(gridBagLayout1);
getContentPane()add(panel1, new GridBagConstraints(0, 0, 1, 1, 10, 10
,GridBagConstraintsCENTER, GridBagConstraintsBOTH, new Insets(0, 0, 0, 0), 45, 83));
panel1add(jTextArea1, BorderLayoutCENTER);
}
}
package snake;
import javaawt;
import javaawtevent;
import javaxswing;
import comborlandjbcllayout;
/
<p>Title: 贪食蛇游戏</p>
<p>Description: </p>
<p>Copyright: Copyright (c) 2005</p>
<p>Company: </p>
@author zsb
@version 10
/
public class MainFrame extends JFrame {
private JPanel contentPane;//窗体内容网格
private JToolBar jToolBar1 = new JToolBar();//工具栏
private JButton jButton1 = new JButton();//游戏开始按钮
private JButton jButton2 = new JButton();//游戏暂停按钮
private JButton jButton3 = new JButton();//游戏退出按钮
private JButton jButton4 = new JButton();//帮助按钮
private JPanel jPanel1 = new JPanel();//游戏主体面板容器
private JPanel jPanel_PlayArea = new JPanel();//游戏区面板容器
private XYLayout xYLayout1 = new XYLayout();//容器布局管理器
private XYLayout xYLayout2 = new XYLayout();
private XYLayout xYLayout3 = new XYLayout();
private static final int UP = 1, LEFT = 2, DOWN = 3, RIGHT = 4;//贪食蛇运动方向
private static final int BEGINNER = 1, MIDDLE = 2, EXPERT = 3;//游戏级别常量
private static final int ROWS = 30;//游戏区行数
private static final int COLS = 50;//游戏区列数
private boolean isPause = false;//游戏暂停标志
private boolean isEnd;//游戏结束标志
private SnakeBody snake ;//贪食蛇
private int score = 0;//当前得分
private int level = BEGINNER;//当前游戏级别
SnakeThread thread = new SnakeThread();//游戏主线程
private GridLayout gridLayout1 = new GridLayout(ROWS, COLS, 0, 0);//游戏区布局
private JButton[][] playBlocks;//游戏区的所有方块
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JRadioButton jRadioButton3 = new JRadioButton();
//Construct the frame
public MainFrame() {
enableEvents(AWTEventWINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
eprintStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(ToolkitgetDefaultToolkit()createImage(MainFrameclassgetResource("[Your Icon]")));
contentPane = (JPanel)thisgetContentPane();
contentPanesetLayout(xYLayout2);
thissetResizable(false);
thissetSize(new Dimension(512, 414));
thissetTitle("贪食蛇");
thisaddKeyListener(new MainFrame_this_keyAdapter(this));
jButton1setFont(new javaawtFont("DialogInput", 0, 12));
jButton1setMaximumSize(new Dimension(79, 39));
jButton1setMinimumSize(new Dimension(79, 39));
jButton1setPreferredSize(new Dimension(79, 39));
jButton1setFocusPainted(false);
jButton1setText("开始");
jButton1addActionListener(new MainFrame_jButton1_actionAdapter(this));
jButton1addKeyListener(new MainFrame_this_keyAdapter(this));
jButton2setText("暂停");
jButton2addActionListener(new MainFrame_jButton2_actionAdapter(this));
jButton2setPreferredSize(new Dimension(79, 39));
jButton2setFocusPainted(false);
jButton2setMinimumSize(new Dimension(79, 39));
jButton2setMaximumSize(new Dimension(79, 39));
jButton2setFont(new javaawtFont("DialogInput", 0, 12));
jButton2addKeyListener(new MainFrame_this_keyAdapter(this));
jButton3setText("退出");
jButton3addActionListener(new MainFrame_jButton3_actionAdapter(this));
jButton3setPreferredSize(new Dimension(79, 39));
jButton3setFocusPainted(false);
jButton3setMinimumSize(new Dimension(79, 39));
jButton3setMaximumSize(new Dimension(79, 39));
jButton3setFont(new javaawtFont("DialogInput", 0, 12));
jButton3addKeyListener(new MainFrame_this_keyAdapter(this));
jButton4setText("帮助");
jButton4addActionListener(new MainFrame_jButton4_actionAdapter(this));
jButton4setPreferredSize(new Dimension(79, 39));
jButton4setFocusPainted(false);
jButton4setMinimumSize(new Dimension(79, 39));
jButton4setMaximumSize(new Dimension(79, 39));
jButton4setFont(new javaawtFont("DialogInput", 0, 12));
jButton4addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel1setLayout(xYLayout1);
jPanel1addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel_PlayAreasetLayout(gridLayout1);
jPanel_PlayAreasetBorder(BorderFactorycreateEtchedBorder());
jPanel_PlayAreaaddKeyListener(new MainFrame_this_keyAdapter(this));
jLabel1setFont(new javaawtFont("DialogInput", 0, 12));
jLabel1setText("得分:");
jPanel2setLayout(xYLayout3);
jLabel2setFont(new javaawtFont("DialogInput", 0, 12));
jLabel2setToolTipText("");
jLabel2setText("0");
jLabel3setText("穿身:");
jLabel3setFont(new javaawtFont("DialogInput", 0, 12));
jLabel4setFont(new javaawtFont("DialogInput", 0, 12));
jLabel4setText("0");
jLabel5setText("穿墙:");
jLabel5setFont(new javaawtFont("DialogInput", 0, 12));
jLabel5setToolTipText("");
jLabel6setText("0");
jLabel6setFont(new javaawtFont("DialogInput", 0, 12));
jLabel6setToolTipText("");
jRadioButton1setFont(new javaawtFont("DialogInput", 0, 12));
jRadioButton1setText("初级");
jRadioButton1addActionListener(new MainFrame_jRadioButton1_actionAdapter(this));
jRadioButton1addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2setText("中级");
jRadioButton2addActionListener(new MainFrame_jRadioButton2_actionAdapter(this));
jRadioButton2addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2setFont(new javaawtFont("DialogInput", 0, 12));
jRadioButton3setText("高级");
jRadioButton3addActionListener(new MainFrame_jRadioButton3_actionAdapter(this));
jRadioButton3setFont(new javaawtFont("DialogInput", 0, 12));
jRadioButton3addKeyListener(new MainFrame_this_keyAdapter(this));
buttonGroup1add(jRadioButton1);
buttonGroup1add(jRadioButton2);
buttonGroup1add(jRadioButton3);
jRadioButton1setSelected(true);
jLabel7setFont(new javaawtFont("DialogInput", 0, 12));
jLabel7setText("等级:");
contentPaneadd(jToolBar1, new XYConstraints(0, 0, 512, -1));
jToolBar1add(jButton1, null);
jToolBar1add(jButton2, null);
jToolBar1add(jButton3, null);
jToolBar1add(jButton4, null);
jToolBar1addKeyListener(new MainFrame_this_keyAdapter(this));
contentPaneadd(jPanel1, new XYConstraints(0, 43, 512, 371));
jPanel1add(jPanel_PlayArea, new XYConstraints(0, 33, 524, 314));
jPanel1add(jPanel2, new XYConstraints(0, 0, 498, 30));
jPanel2add(jRadioButton1, new XYConstraints(331, 1, -1, -1));
jPanel2add(jRadioButton3, new XYConstraints(443, 1, -1, -1));
jPanel2add(jRadioButton2, new XYConstraints(390, 1, -1, -1));
jPanel2add(jLabel6, new XYConstraints(245, 6, 30, -1));
jPanel2add(jLabel7, new XYConstraints(287, 5, 47, -1));
jPanel2add(jLabel5, new XYConstraints(197, 6, 47, -1));
jPanel2add(jLabel4, new XYConstraints(166, 7, 26, -1));
jPanel2add(jLabel3, new XYConstraints(120, 6, 38, -1));
jPanel2add(jLabel1, new XYConstraints(23, 6, 39, -1));
jPanel2add(jLabel2, new XYConstraints(62, 6, 54, -1));
//创建并初始化游戏区方块
playBlocks = new JButton[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j] = new JButton();
playBlocks[i][j]setBackground(ColorlightGray);
playBlocks[i][j]setVisible(false);
jPanel_PlayAreaadd(playBlocks[i][j]);
}
}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
superprocessWindowEvent(e);
if (egetID() == WindowEventWINDOW_CLOSING) {
Systemexit(0);
}
}
public void start() {
//初始化游戏参数
score = 0;
isPause = false;
isEnd = false; //
jButton2setText("暂停");
//初始化游戏区
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j]setBackground(ColorlightGray);
playBlocks[i][j]setVisible(false);
}
}
//创建蛇身
snake = new SnakeBody();
//在游戏区内随机放置食物
int x = (int) (Mathrandom() ROWS);
int y = (int) (Mathrandom() COLS);
while (playBlocks[x][y]isVisible()) {
x = (int) (Mathrandom() ROWS);
y = (int) (Mathrandom() COLS);
}
playBlocks[x][y]setBackground(Coloryellow);
playBlocks[x][y]setVisible(true);
//初始化状态提示区
jLabel2setText(IntegertoString(score));
jLabel4setText(IntegertoString(snakethroughbody));
jLabel2setText(IntegertoString(snakethroughwall));
try {
//启动游戏
threadstart();
}
catch (IllegalThreadStateException illegalThreadStateException) {}
}
//完整的>
有。贪吃蛇自行编辑java代码使其具有交互功能是可以的。实现贪吃蛇游戏基本功能,屏幕上随机出现一个“食物”,称为豆子。玩家能利用上下左右键控制“蛇”的移动,“蛇”吃到“豆子”后“蛇”身体加长一节,得分增加,“蛇”碰到边界或蛇头与蛇身相撞,“蛇”死亡,游戏结束。
以上就是关于急需用eclipse写的小游戏代码 比如贪吃蛇,五子棋,猜数字,俄罗斯方块等的小游戏代码全部的内容,包括:急需用eclipse写的小游戏代码 比如贪吃蛇,五子棋,猜数字,俄罗斯方块等的小游戏代码、谁帮我写个java贪吃蛇游戏全面的包括得分等功能!谢谢!希望大家帮帮忙!、贪吃蛇java代码具有交互功能吗等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)