电子版馈线开放游戏代码

创建一个新项目和一个新类。

粘贴代码,找到run按钮,按下运行。如果找不到,快捷键是Ctrl+F11。

导入Java . awt . color;

导入Java . awt . component;

导入Java . awt . graphics;

导入Java . awt . event . action event;

导入Java . awt . event . action listener;

导入Java . awt . event . key event;

导入Java . awt . event . key listener;

导入Java . util . ArrayList;

导入javax . swing . border factory;

导入javax . swing . jframe;

导入javax . swing . jlabel;

导入javax . swing . jmenu;

导入javax . swing . jmenubar;

导入javax . swing . jmenuitem;

导入javax . swing . jpanel;

公共类蛇游戏{

公共静态void main(String[] args) {

snake frame frame = new snake frame();

frame . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);

frame . set visible(true);

}

}

// -记录状态的线程。

类StatusRunnable实现Runnable {

公共状态运行(Snake snake,JLabel statusLabel,JLabel scoreLabel) {

this . status label = status label;

this . score label = score label;

this.snake = snake

}

公共无效运行(){

String sta =

字符串spe =

while (true) {

开关(snake.status) {

案例蛇。跑步:

sta = "正在运行";

打破;

案例蛇。暂停:

sta = "暂停";

打破;

案例蛇。游戏结束:

sta = " GameOver

打破;

}

status label . settext(sta);

score label . settext("+snake . score);

尝试{

thread . sleep(100);

} catch(异常e) {

}

}

}

私有JLabel scoreLabel

私有JLabel状态标签;

私蛇蛇;

}

// -蛇的运动和记录分数的线。

SnakeRunnable类实现Runnable {

public SnakeRunnable(Snake snake,Component component) {

this.snake = snake

this.component =组件;

}

公共无效运行(){

while (true) {

尝试{

snake . move();

component . repaint();

Thread.sleep(蛇.速);

} catch(异常e) {

}

}

}

私蛇蛇;

私有组件组件;

}

蛇类{

布尔isRun// -是否在运动?

ArrayList & ltNode & gt身体;//-蛇身

节点食物;//-食物

交互方向;//-方向

int分数;

int状态;

int速度;

公共静态final int SLOW = 500

公共静态final int MID = 300

公共静态final int FAST = 100;

public static final int RUNNING = 1;

public static final int PAUSED = 2;

public static final int game over = 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;

公共蛇(){

速度=蛇。慢;

得分= 0;

isRun = false

地位=蛇。暂停;

方向=蛇。对;

body = new ArrayList & ltNode & gt();

body.add(新节点(60,20));

body.add(新节点(40,20));

body.add(new Node(20,20));

make food();

}

// -判断食物是否被蛇吃掉。

// -如果食物正好在蛇奔跑方向的前方,与蛇头接触,就会被吃掉。

私有布尔值isEaten() {

node head = body . get(0);

if(direction = = Snake。右& amp& amp(head.x + Node。W) == food.x

& amp& amphead.y == food.y)

返回true

if(direction = = Snake。左& amp& amp(head.x - Node。W) == food.x

& amp& amphead.y == food.y)

返回true

if(direction = = Snake。UP & amp& amphead.x == food.x

& amp& amp(head.y节点。H) == food.y)

返回true

if(direction = = Snake。羽绒& amp& amphead.x == food.x

& amp& amp(head.y + Node。H) == food.y)

返回true

其他

返回false

}

// -是否碰撞。

私有布尔isCollsion() {

node node = body . get(0);

// -撞到了墙上。

if(direction = = Snake。右& amp& ampnode.x == 280)

返回true

if(direction = = Snake。UP & amp& ampnode.y == 0)

返回true

if(direction = = Snake。左& amp& ampnode.x == 0)

返回true

if(direction = = Snake。羽绒& amp& ampnode.y == 380)

返回true

// -蛇头遇到了蛇。

节点温度= null

int I = 0;

for(I = 3;我& ltbody . size();i++) {

temp = body . get(I);

if(temp . x = = node . x & amp;& amptemp.y == node.y)

打破;

}

如果(我& ltbody.size())

返回true

其他

返回false

}

//-在随机地点生产食物。

public void makeFood() {

Node node =新节点(0,0);

boolean isInBody = true

int x = 0,y = 0;

int X = 0,Y = 0;

int I = 0;

while (isInBody) {

x =(int)(math . random()* 15);

y =(int)(math . random()* 20);

X = x *节点。w;

Y = y *节点。h;

for(I = 0;我& ltbody . size();i++) {

if (X == body.get(i)。x & amp& ampY == body.get(i)。y)

打破;

}

如果(我& ltbody.size())

isInBody = true

其他

isInBody = false

}

食物=新节点(X,Y);

}

//-改变运行方向

public void change direction(int newDer){

if(方向% 2!= newDer% 2)// -如果方向与原始方向相同或相反,则不能更改。

derection = newDer

}

公共void移动(){

If(iseaten()){//-如果吃了食物。

body.add(0,食物);// -把食物当成蛇头,变成新的蛇身。

得分+= 10;

make food();//-生产食物

} else if (iscollision ())// -如果撞墙或者自己。

{

isRun = false

地位=蛇。GAMEOVER//-结束

} else if(is run){//-正常操作(无食物、无墙壁、无触摸)

node node = body . get(0);

int X = node.x

int Y = node.y

// -蛇头根据运行方向向前移动一个单位。

开关(方向){

案例1:

X -=节点。w;

打破;

案例二:

Y -=节点。h;

打破;

案例三:

X +=节点。w;

打破;

案例4:

Y +=节点。h;

打破;

}

body.add(0,新节点(X,Y));

// - .

body . remove(body . size()-1);

}

}

}

// -组成蛇身的单位,食物。

类节点{

公共静态final int W = 20

公共静态final int H = 20

int x;

int y;

公共节点(int x,int y) {

this.x = x

this.y = y

}

}

//-画板

类SnakePanel扩展JPanel {

蛇蛇;

公共蛇板(蛇蛇){

this.snake = snake

}

public void paint component(Graphics g){

super . paint component(g);

Node node = null

for(int I = 0;我& ltsnake . body . size();I+i++) {// -以红色和蓝色间隔绘制蛇体。

如果(i % 2 == 0)

g . set color(color . blue);

其他

g . set color(color . yellow);

node = snake . body . get(I);

g.fillRect(node.x,node.y,node。h节点。w);//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

}

node = snake.food

g . set color(color . red);

g.fillRect(node.x,node.y,node。h节点。w);

}

}

类SnakeFrame扩展JFrame {

私有JLabel状态标签;

私有JLabel speedLabel

私有JLabel scoreLabel

私人JPanel snakePanel

私蛇蛇;

私人JMenuBar酒吧;

JMenu游戏菜单;

JMenu helpMenu

JMenu speedMenu

JMenuItem newItem

JMenuItem pauseItem

JMenuItem beginItem

JMenuItem helpItem

JMenuItem关于item;

JMenuItem slowItem

JMenuItem midItem

JMenuItem fastItem

公共蛇形框架(){

init();

action listener l = new action listener(){

public void action performed(action event e){

if (e.getSource() == pauseItem)

snake.isRun = false

if (e.getSource() == beginItem)

snake.isRun = true

if (e.getSource() == newItem) {

new game();

}

// -菜单控制运行速度。

if (e.getSource() == slowItem) {

蛇。速度=蛇。慢;

speed label . settext(" Slow ");

}

if (e.getSource() == midItem) {

蛇。速度=蛇。MID

speed label . settext(" Mid ");

}

if (e.getSource() == fastItem) {

蛇。速度=蛇。快;

speed label . settext(" Fast ");

}

}

};

pause item . addactionlistener(l);

begin item . addactionlistener(l);

new item . addactionlistener(l);

about item . addactionlistener(l);

slowitem . addactionlistener(l);

miditem . addactionlistener(l);

fastitem . addactionlistener(l);

addkey listener(new key listener(){

公共void按键(按键事件e) {

switch (e.getKeyCode()) {

// -方向键改变蛇的运行方向。

案例关键事件。VK _唐://

snake.changeDerection(Snake。向下);

打破;

案例关键事件。VK_UP://

snake.changeDerection(Snake。UP);

打破;

案例关键事件。VK _左://

snake.changeDerection(Snake。左);

打破;

案例关键事件。VK _右://

snake.changeDerection(Snake。对);

打破;

//空格键,暂停或继续游戏。

案例关键事件。VK空间://

if (snake.isRun == true) {

snake.isRun = false

snake.status = Snake。暂停;

打破;

}

if (snake.isRun == false) {

snake.isRun = true

snake.status = Snake。跑步;

打破;

}

}

}

public void key released(key event k){

}

public void keyTyped(KeyEvent k) {

}

});

}

私有void init() {

speed label = new JLabel();

Snake = new Snake();

setSize(380,460);

set layout(null);

this . setresizable(false);

bar = new JMenuBar();

gameMenu = new JMenu("游戏");

newItem = new JMenuItem("新游戏");

game menu . add(new item);

Pause item = new JMenuItem(" Pause ");

game menu . add(pause item);

begin item = new JMenuItem(" Continue ");

game menu . add(begin item);

Help menu = new JMenu(" Help ");

About item = new JMenuItem(" About ");

help menu . add(about item);

Speed menu = new JMenu(" Speed ");

slowItem = new JMenuItem(" Slow ");

Fast item = new JMenuItem(" Fast ");

midItem = new JMenuItem(" Middle ");

speed menu . add(slowItem);

speed menu . add(midItem);

speed menu . add(fast item);

bar . add(game menu);

bar . add(help menu);

bar . add(speed menu);

setJMenuBar(bar);

status label = new JLabel();

score label = new JLabel();

snakePanel = new JPanel();

snakePanel.setBounds(0,0,300,400);

snake panel . set border(border factory . create line border(color . dark gray));

添加(snakePanel);

statusLabel.setBounds(300,25,60,20);

add(status label);

scoreLabel.setBounds(300,20,60,20);

add(score label);

JLabel temp = new JLabel(" status ");

temp.setBounds(310,5,60,20);

add(temp);

temp = new JLabel(" score ");

temp.setBounds(310,105,60,20);

add(temp);

temp = new JLabel(" speed ");

temp.setBounds(310,55,60,20);

add(temp);

speedLabel.setBounds(310,75,60,20);

添加(speed label);

}

私有void newGame() {

this . remove(snakePanel);

this . remove(status label);

this . remove(score label);

speed label . settext(" Slow ");

status label = new JLabel();

score label = new JLabel();

snakePanel = new JPanel();

Snake = new Snake();

snakePanel = new SnakePanel(蛇);

snakePanel.setBounds(0,0,300,400);

snake panel . set border(border factory . create line border(color . dark gray));

runnable r 1 = new snake runnable(snake,snake panel);

runnable R2 = new status runnable(snake,statusLabel,score label);

螺纹t1 =新螺纹(r 1);

线程t2 =新线程(R2);

t 1 . start();

T2 . start();

添加(snakePanel);

status label . set bounds(310,25,60,20);

add(status label);

scoreLabel.setBounds(310,125,60,20);

add(score label);

}

}