按关键词阅读:
else if( e.getKeyCode() = 38 &mainSnake.snakeDirection != 3 )/up direction = 1;
else if( e.getKeyCode() = 40 &mainSnake.snakeDirection != 1 )/down direction = 3;
else if( e.getKeyCode() = 32 ) if( !hasStoped ) if( !flag ) snakeTimer.stop();
confi 。
16、gMenu.setVisible( true );
configMenu.setMenuEnable( false );
flag = true;
else snakeTimer.start();
configMenu.setVisible( false );
configMenu.setMenuEnable( true );
flag = false;
private class TimerHandler implements ActionListener public synchronized void actionPerformed( ActionEvent e ) Point temp 。
17、 = (Point) mainSnake.getLast();
snakeSq = mainSnake.iterator();
while ( snakeSq.hasNext() ) Point tempPoint = (Point)snakeSq.next();
if( temp.equals( tempPoint ) &snakeSq.hasNext() != false ) snakeTimer.stop();
stopGame();
JOptionPane.showMessageDialog( null, Your Score is + score + nnYou Loss! );
S 。
18、ystem.out.println( temp.x + + temp.y );
if( (temp.x = 0 & direction = 4) | (temp.x = horizontalGrid-1 & direction = 2) | (temp.y = 0 & direction = 1) | (temp.y = verticalGrid-1 & direction = 3) ) snakeTimer.stop();
stopGame();
JOptionPane.showMessageDialog( null, Your Score is + score + nnYou Loss!。
19、);
if( direction != mainSnake.snakeReDirection ) moveSnake( direction );
mainSnake.drawSnake( getGraphics(), singleWidthX, singleHeightY, cooPos );
drawBeanAndEBean( getGraphics() );
public void stopGame() this.hasStoped = true;
this.snakeTimer.stop();
Graphics2D g = (Graphics2D) GameFrame.this.getG 。
20、raphics();
g.setColor( ColorGroup.COLOR_BACK );
super.paint( g );
configMenu.setVisible( true );
public void resetGame() System.gc();
this.hasStoped = false;
Graphics2D g = (Graphics2D) GameFrame.this.getGraphics();
g.setColor( ColorGroup.COLOR_BACK );
super.paint( g );
this.mainSnake = new Snake();
。
21、 this.createBean( bean );
this.eatedBean.clear();
mainSnake.drawSnake( getGraphics(), singleWidthX, singleHeightY, cooPos );
this.snakeTimer.start();
this.direction = 2;
this.score = 0;
this.scoreLabel.setText( PauseSPACE - ExitESC );
private void moveSnake( int direction ) if( mainSnake.checkBeanIn 。
22、( this.bean ) ) this.score += 100;
this.scoreLabel.setText( this.info + Current Score: + this.score );
this.eatedBean.add( new Point(this.bean) );
this.createBean( this.bean );
mainSnake.changeDirection( (Point) mainSnake.getLast(), direction );
Point temp = (Point) mainSnake.getFirst();
if( eatedBe 。
23、an.size() != 0 ) if( eatedBean.getFirst().equals( temp ) ) eatedBean.remove( 0 );
else mainSnake.clearEndSnakePiece( getGraphics(), temp.x, temp.y, singleWidthX, singleHeightY, cooPos );
mainSnake.removeTail();
else mainSnake.clearEndSnakePiece( getGraphics(), temp.x, temp.y, singleWidthX, singleHei 。
24、ghtY, cooPos );
mainSnake.removeTail();
private void drawBeanAndEBean( Graphics g ) g.setColor( ColorGroup.COLOR_BEAN );
this.drawPiece( g, this.bean.x, this.bean.y );
g.setColor( ColorGroup.COLOR_EATEDBEAN );
snakeSq = eatedBean.iterator();
while ( snakeSq.hasNext() ) Point tempPoint = (Point)snake 。
25、Sq.next();
this.drawPiece( g, tempPoint.x, tempPoint.y );
private void drawPiece( Graphics g, int x, int y ) g.fillRoundRect( this.singleWidthX * x + 1, this.singleHeightY * y + 1, this.singleWidthX - 2, this.singleHeightY - 2, this.cooPos, this.cooPos );
private void createBean( Point temp ) LP: wh 。
26、ile( true ) temp.x = (int) (Math.random() * this.horizontalGrid);
temp.y = (int) (Math.random() * this.verticalGrid);
snakeSq = mainSnake.iterator();
while ( snakeSq.hasNext() ) if( snakeSq.next().equals( new Point( temp.x, temp.y ) ) ) continue LP;
break;
ConfigMenu.java package SnakeGame;
import j 。
27、ava.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ConfigMenu extends JMenuBar GameFrame owner;
JMenu menu;
JMenuItem menuItem;
JRadioButtonMenuItem speedItem, modelItem, standardItem;
private UIManager.LookAndFeelInfo looks;
public ConfigMenu( GameFrame owner ) this.owner = owne 。
28、r;
owner.setJMenuBar( this );
String menu_name = Snake Game, Game Configure, Game Help;
menu = new JMenumenu_name.length;
for( inti = 0;
imenu_name.length;
i+ ) menui = new JMenu( menu_namei );
menui.setFont( new Font( Courier, Font.PLAIN, 12 ) );
this.add( menui );
String menuItem_name = Start Game 。
29、, Stop Game, Exit Game, Game Color, About.;
menuItem = new JMenuItemmenuItem_name.length;
for( inti = 0;
imenuItem_name.length;
i+ ) menuItemi = new JMenuItem( menuItem_namei );
稿源:(未知)
【傻大方】网址:/a/2021/0711/0022845681.html
标题:贪吃|贪吃蛇源代码JavaApplet小程序( 二 )