JAVA小程序—贪吃蛇源代码(共12页).docx

上传人:飞****2 文档编号:13652489 上传时间:2022-04-30 格式:DOCX 页数:12 大小:16.16KB
返回 下载 相关 举报
JAVA小程序—贪吃蛇源代码(共12页).docx_第1页
第1页 / 共12页
JAVA小程序—贪吃蛇源代码(共12页).docx_第2页
第2页 / 共12页
点击查看更多>>
资源描述

《JAVA小程序—贪吃蛇源代码(共12页).docx》由会员分享,可在线阅读,更多相关《JAVA小程序—贪吃蛇源代码(共12页).docx(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上JAVA贪吃蛇源代码 SnakeGame.javapackage SnakeGame;import javax.swing.*;public class SnakeGamepublic static void main( String args )JDialog.setDefaultLookAndFeelDecorated( true );GameFrame temp = new GameFrame();Snake.javapackage SnakeGame;import java.awt.*;import java.util.*;class Snake extend

2、s LinkedListpublic int snakeDirection = 2;public int snakeReDirection = 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 ) );th

3、is.add( new Point( 10, 3 ) );public void changeDirection( Point temp, int direction )this.snakeDirection = direction;switch( direction )case 1:/upthis.snakeReDirection = 3;this.add( new Point( temp.x, temp.y - 1 ) );break;case 2:/rightthis.snakeReDirection = 4;this.add( new Point( temp.x + 1, temp.y

4、 ) );break;case 3:/downthis.snakeReDirection = 1;this.add( new Point( temp.x, temp.y + 1 ) );break;case 4:/leftthis.snakeReDirection = 2;this.add( new Point( temp.x - 1, temp.y ) );break;public boolean checkBeanIn( Point bean )Point temp = (Point) this.getLast();if( temp.equals( bean ) )return true;

5、return false;public void removeTail()this.remove( 0 );public void drawSnake( Graphics g, int singleWidthX, int singleHeightY, int cooPos )g.setColor( ColorGroup.COLOR_SNAKE );Iterator snakeSq = this.iterator();while ( snakeSq.hasNext() )Point tempPoint = (Point)snakeSq.next();this.drawSnakePiece( g,

6、 tempPoint.x, tempPoint.y,singleWidthX, singleHeightY, cooPos );public void drawSnakePiece( Graphics g, int temp1, int temp2,int singleWidthX, int singleHeightY, int cooPos )g.fillRoundRect( singleWidthX * temp1 + 1,singleHeightY * temp2 + 1,singleWidthX - 2,singleHeightY - 2,cooPos,cooPos );public

7、void clearEndSnakePiece( Graphics g, int temp1, int temp2,int singleWidthX, int singleHeightY, int cooPos )g.setColor( ColorGroup.COLOR_BACK );g.fillRoundRect( singleWidthX * temp1 + 1,singleHeightY * temp2 + 1,singleWidthX - 2,singleHeightY - 2,cooPos,cooPos );GameFrame.javapackage SnakeGame;import

8、 java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.awt.geom.*;class GameFrame extends JFrameprivate Toolkit tempKit;private int horizontalGrid, verticalGrid;private int singleWidthX, singleHeightY;private int cooPos;private Snake mainSnake;private LinkedList eate

9、dBean;eatedBean = new LinkedList();private Iterator snakeSq;public javax.swing.Timer snakeTimer;private int direction = 2;private int score;private String info;private Point bean, eatBean;bean = new Point();private boolean flag;private JMenuBar infoMenu;private JMenu tempMenu;private JMenuItem tempM

10、enuItem;private JRadioButtonMenuItem levelMenuItem, versionMenuItem;private JLabel scoreLabel;scoreLabel = new JLabel();private Graphics2D g;private ImageIcon snakeHead;snakeHead = new ImageIcon( LOGO.gif );private ConfigMenu configMenu;private boolean hasStoped = true;public GameFrame()this.tempKit

11、 = 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 KeyHandler() );GameFrame.this.snakeTimer = new javax.swing.Timer( 80, ne

12、w 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( this );this.setVisible( true );public void setGrid( int temp1, int temp2, in

13、t temp3 )this.horizontalGrid = temp1;this.verticalGrid = temp2;this.singleWidthX = this.getWidth() / temp1;this.singleHeightY = this.getHeight() / temp2;this.cooPos = temp3;private class KeyHandler extends KeyAdapterpublic void keyPressed( KeyEvent e )if( e.getKeyCode() = 27 )snakeTimer.stop();if( J

14、OptionPane.showConfirmDialog( null, Are you sure to exit? ) = 0 )System.exit( 0 );snakeTimer.start();else if( e.getKeyCode() = 37 & mainSnake.snakeDirection != 2 )/leftdirection = 4;else if( e.getKeyCode() = 39 & mainSnake.snakeDirection != 4 )/rightdirection = 2;else if( e.getKeyCode() = 38 & mainS

15、nake.snakeDirection != 3 )/updirection = 1;else if( e.getKeyCode() = 40 & mainSnake.snakeDirection != 1 )/downdirection = 3;else if( e.getKeyCode() = 32 )if( !hasStoped )if( !flag )snakeTimer.stop();configMenu.setVisible( true );configMenu.setMenuEnable( false );flag = true;elsesnakeTimer.start();co

16、nfigMenu.setVisible( false );configMenu.setMenuEnable( true );flag = false;private class TimerHandler implements ActionListenerpublic synchronized void actionPerformed( ActionEvent e )Point temp = (Point) mainSnake.getLast();snakeSq = mainSnake.iterator();while ( snakeSq.hasNext() )Point tempPoint =

17、 (Point)snakeSq.next();if( temp.equals( tempPoint ) & snakeSq.hasNext() != false )snakeTimer.stop();stopGame();JOptionPane.showMessageDialog( null,Your Score is + score + \n\nYou Loss! );System.out.println( temp.x + + temp.y );if( (temp.x = 0 & direction = 4) |(temp.x = horizontalGrid-1 & di

18、rection = 2) |(temp.y = 0 & direction = 1) |(temp.y = verticalGrid-1 & direction = 3) )snakeTimer.stop();stopGame();JOptionPane.showMessageDialog( null,Your Score is + score + \n\nYou Loss! );if( direction != mainSnake.snakeReDirection )moveSnake( direction );mainSnake.drawSnake( getGraphics

19、(), singleWidthX, singleHeightY, cooPos );drawBeanAndEBean( getGraphics() );public void stopGame()this.hasStoped = true;this.snakeTimer.stop();Graphics2D g = (Graphics2D) GameFrame.this.getGraphics();g.setColor( ColorGroup.COLOR_BACK );super.paint( g );configMenu.setVisible( true );public void reset

20、Game()System.gc();this.hasStoped = false;Graphics2D g = (Graphics2D) GameFrame.this.getGraphics();g.setColor( ColorGroup.COLOR_BACK );super.paint( g );this.mainSnake = new Snake();this.createBean( bean );this.eatedBean.clear();mainSnake.drawSnake( getGraphics(), singleWidthX, singleHeightY, cooPos )

21、;this.snakeTimer.start();this.direction = 2;this.score = 0;this.scoreLabel.setText( PauseSPACE - ExitESC );private void moveSnake( int direction )if( mainSnake.checkBeanIn( this.bean ) )this.score += 100;this.scoreLabel.setText( this.info + Current Score: + this.score );this.eatedBean.add( new Point

22、(this.bean) );this.createBean( this.bean );mainSnake.changeDirection( (Point) mainSnake.getLast(), direction );Point temp = (Point) mainSnake.getFirst();if( eatedBean.size() != 0 )if( eatedBean.getFirst().equals( temp ) )eatedBean.remove( 0 );elsemainSnake.clearEndSnakePiece( getGraphics(), temp.x,

23、temp.y,singleWidthX, singleHeightY, cooPos );mainSnake.removeTail();elsemainSnake.clearEndSnakePiece( getGraphics(), temp.x, temp.y,singleWidthX, singleHeightY, cooPos );mainSnake.removeTail();private void drawBeanAndEBean( Graphics g )g.setColor( ColorGroup.COLOR_BEAN );this.drawPiece( g, this.bean

24、.x, this.bean.y );g.setColor( ColorGroup.COLOR_EATEDBEAN );snakeSq = eatedBean.iterator();while ( snakeSq.hasNext() )Point tempPoint = (Point)snakeSq.next();this.drawPiece( g, tempPoint.x, tempPoint.y );private void drawPiece( Graphics g, int x, int y )g.fillRoundRect( this.singleWidthX * x + 1,this

25、.singleHeightY * y + 1,this.singleWidthX - 2,this.singleHeightY - 2,this.cooPos,this.cooPos );private void createBean( Point temp )LP:while( true )temp.x = (int) (Math.random() * this.horizontalGrid);temp.y = (int) (Math.random() * this.verticalGrid);snakeSq = mainSnake.iterator();while ( snakeSq.ha

26、sNext() )if( snakeSq.next().equals( new Point( temp.x, temp.y ) ) )continue LP;break;ConfigMenu.javapackage SnakeGame;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ConfigMenu extends JMenuBarGameFrame owner;JMenu menu;JMenuItem menuItem;JRadioButtonMenuItem speedItem, m

27、odelItem, standardItem;private UIManager.LookAndFeelInfo looks;public ConfigMenu( GameFrame owner )this.owner = owner;owner.setJMenuBar( this );String menu_name = Snake Game, Game Configure, Game Help;menu = new JMenumenu_name.length;for( int i = 0; i menu_name.length; i+ )menui = new JMenu( menu_na

28、mei );menui.setFont( new Font( Courier, Font.PLAIN, 12 ) );this.add( menui );String menuItem_name = Start Game, Stop Game, Exit Game,Game Color,About.;menuItem = new JMenuItemmenuItem_name.length;for( int i = 0; i menuItem_name.length; i+ )menuItemi = new JMenuItem( menuItem_namei );menuItemi.setFon

29、t( new Font( Courier, Font.PLAIN, 12 ) );menuItemi.addActionListener( new ActionHandler() );menu0.add( menuItem0 );menu0.add( menuItem1 );menu0.addSeparator();menu0.add( menuItem2 );menu1.add( menuItem3 );menu2.add( menuItem4 );String inner_menu_name = Game Speed, Window Model, Game Standard ;JMenu

30、inner_menu = new JMenuinner_menu_name.length;for( int i = 0; i inner_menu_name.length; i+ )inner_menui = new JMenu( inner_menu_namei );inner_menui.setFont( new Font( Courier, Font.PLAIN, 12 ) );menu1.add( inner_menui );ButtonGroup temp1 = new ButtonGroup();String speedItem_name = Speed-1, Speed-2, S

31、peed-3, Speed-4, Speed-5;speedItem = new JRadioButtonMenuItemspeedItem_name.length;for( int i = 0; i speedItem_name.length; i+ )speedItemi = new JRadioButtonMenuItem( speedItem_namei );inner_menu0.add( speedItemi );speedItemi.setFont( new Font( Courier, Font.PLAIN, 12 ) );speedItemi.addItemListener(

32、 new ItemHandler() );temp1.add( speedItemi );ButtonGroup temp2 = new ButtonGroup();String modelItem_name = Linux, Mac, Windows ;modelItem = new JRadioButtonMenuItemmodelItem_name.length;for( int i = 0; i modelItem_name.length; i+ )modelItemi = new JRadioButtonMenuItem( modelItem_namei );inner_menu1.

33、add( modelItemi );modelItemi.setFont( new Font( Courier, Font.PLAIN, 12 ) );modelItemi.addItemListener( new ItemHandler() );temp2.add( modelItemi );ButtonGroup temp3 = new ButtonGroup();String standardItem_name = 60 * 40, 45 * 30, 30 * 20 ;standardItem = new JRadioButtonMenuItemstandardItem_name.len

34、gth;for( int i = 0; i standardItem_name.length; i+ )standardItemi = new JRadioButtonMenuItem( standardItem_namei );inner_menu2.add( standardItemi );standardItemi.setFont( new Font( Courier, Font.PLAIN, 12 ) );standardItemi.addItemListener( new ItemHandler() );temp3.add( standardItemi );looks = UIMan

35、ager.getInstalledLookAndFeels();private class ActionHandler implements ActionListenerpublic void actionPerformed( ActionEvent e )if( e.getSource() = menuItem0 )owner.resetGame();ConfigMenu.this.setVisible( false );else if( e.getSource() = menuItem1 )owner.stopGame();ConfigMenu.this.setVisible( true

36、);ConfigMenu.this.setMenuEnable( true );else if( e.getSource() = menuItem2 )System.exit( 0 );else if( e.getSource() = menuItem3 )ConfigDialog temp = new ConfigDialog( owner );temp.setVisible( true );else if( e.getSource() = menuItem4 )JOptionPane.showMessageDialog( null, Sanke Game 2.0 Version!\

37、n\n +Author: FinalCore\n\n );private class ItemHandler implements ItemListenerpublic void itemStateChanged( ItemEvent e )for( int i = 0; i speedItem.length; i+ )if( e.getSource() = speedItemi )owner.snakeTimer.setDelay( 150 - 30 * i );if( e.getSource() = standardItem0 )owner.setGrid( 60,

38、 40, 5 );else if( e.getSource() = standardItem1 )owner.setGrid( 45, 30, 10 );else if( e.getSource() = standardItem2 )owner.setGrid( 30, 20, 15 );for( int i = 0; i modelItem.length; i+ )if( e.getSource() = modelItemi )tryUIManager.setLookAndFeel( looksi.getClassName() );catch(Exception ex)public void setMenuEnable( boolean temp )menu1.setEnabled( temp );专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁