《贪吃蛇源代码JavaApplet小程序》由会员分享,可在线阅读,更多相关《贪吃蛇源代码JavaApplet小程序(20页珍藏版)》请在金锄头文库上搜索。
1、SnakeGame.java package SnakeGame; import javax.swing.*; public class SnakeGame public static void main( String args ) JDialog.setDefaultLookAndFeelDecorated( true ); GameFrame temp = new GameFrame(); Snake.java package SnakeGame; import java.awt.*; import java.util.*; class Snake extends LinkedList
2、public intsnakeDirection = 2; public intsnakeReDirection = 4; public Snake() this.add( new Point( 3, 3 ) ); this.add( new Point( 4, 3 ) ); this.add( new Point( 5, 3 ) ); this.add( new Point( 6, 3 ) ); this.add( new Point( 7, 3 ) ); this.add( new Point( 8, 3 ) ); this.add( new Point( 9, 3 ) ); this.a
3、dd( new Point( 10, 3 ) ); public void changeDirection( Point temp, int direction ) this.snakeDirection = direction; switch( direction ) case 1:/up this.snakeReDirection = 3; this.add( new Point( temp.x, temp.y - 1 ) ); break; case 2:/right this.snakeReDirection = 4; this.add( new Point( temp.x + 1,
4、temp.y ) ); break; case 3:/down this.snakeReDirection = 1; this.add( new Point( temp.x, temp.y + 1 ) ); break; case 4:/left this.snakeReDirection = 2; this.add( new Point( temp.x - 1, temp.y ) ); break; public booleancheckBeanIn( Point bean ) Point temp = (Point) this.getLast(); if( temp.equals( bea
5、n ) ) return true; return false; public void removeTail() this.remove( 0 ); public void drawSnake( Graphics g, intsingleWidthX, intsingleHeightY, intcooPos ) g.setColor( ColorGroup.COLOR_SNAKE ); Iterator snakeSq = this.iterator(); while ( snakeSq.hasNext() ) Point tempPoint = (Point)snakeSq.next();
6、 this.drawSnakePiece( g, tempPoint.x, tempPoint.y, singleWidthX, singleHeightY, cooPos ); public void drawSnakePiece( Graphics g, int temp1, int temp2, intsingleWidthX, intsingleHeightY, intcooPos ) g.fillRoundRect( singleWidthX * temp1 + 1, singleHeightY * temp2 + 1, singleWidthX - 2, singleHeightY
7、 - 2, cooPos, cooPos ); public void clearEndSnakePiece( Graphics g, int temp1, int temp2, intsingleWidthX, intsingleHeightY, intcooPos ) g.setColor( ColorGroup.COLOR_BACK ); g.fillRoundRect( singleWidthX * temp1 + 1, singleHeightY * temp2 + 1, singleWidthX - 2, singleHeightY - 2, cooPos, cooPos ); G
8、ameFrame.java package SnakeGame; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.awt.geom.*; class GameFrame extends JFrame private Toolkit tempKit; private inthorizontalGrid, verticalGrid; private intsingleWidthX, singleHeightY; private intcooPos; p
9、rivate Snake mainSnake; private LinkedListeatedBean; eatedBean = new LinkedList(); private Iterator snakeSq; public javax.swing.TimersnakeTimer; private int direction = 2; private int score; private String info; private Point bean, eatBean; bean = new Point(); private boolean flag; private JMenuBari
10、nfoMenu; private JMenu tempMenu; private JMenuItem tempMenuItem; private JRadioButtonMenuItem levelMenuItem, versionMenuItem; private JLabelscoreLabel; scoreLabel = new JLabel(); private Graphics2D g; private ImageIconsnakeHead; snakeHead = new ImageIcon( LOGO.gif ); private ConfigMenuconfigMenu; pr
11、ivate booleanhasStoped = true; public GameFrame() this.tempKit = this.getToolkit(); this.setSize( tempKit.getScreenSize() ); this.setGrid( 60, 40, 5 ); this.getContentPane().setBackground( ColorGroup.COLOR_BACK ); this.setUndecorated( true ); this.setResizable( false ); this.addKeyListener( new KeyH
12、andler() ); GameFrame.this.snakeTimer = new javax.swing.Timer( 80, new TimerHandler() ); this.getContentPane().add( scoreLabel, BorderLayout.SOUTH ); this.scoreLabel.setFont( new Font( Fixedsys, Font.BOLD, 15 ) ); this.scoreLabel.setText( PauseSPACE - ExitESC ); this.configMenu = new ConfigMenu( thi
13、s ); this.setVisible( true ); public void setGrid( int temp1, int temp2, int temp3 ) this.horizontalGrid = temp1; this.verticalGrid = temp2; this.singleWidthX = this.getWidth() / temp1; this.singleHeightY = this.getHeight() / temp2; this.cooPos = temp3; private class KeyHandler extends KeyAdapter pu
14、blic void keyPressed( KeyEvent e ) if( e.getKeyCode() = 27 ) snakeTimer.stop(); if( JOptionPane.showConfirmDialog( null, Are you sure to exit? ) = 0 ) System.exit( 0 ); snakeTimer.start(); else if( e.getKeyCode() = 37 &mainSnake.snakeDirection != 2 )/left direction = 4; else if( e.getKeyCode() = 39 &mainSnake.snakeDirection != 4 )/right direction = 2; 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.getKeyCod