




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 extends 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 ) );this.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 ) );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;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, 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 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 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 eatedBean;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 tempMenuItem;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 = 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, 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( this );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 KeyAdapterpublic 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 )/leftdirection = 4;else if( e.getKeyCode() = 39 & mainSnake.snakeDirection != 4 )/rightdirection = 2;else if( e.getKeyCode() = 38 & mainSnake.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();configMenu.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 = (Point)snakeSq.next();if( temp.equals( tempPoint ) & snakeSq.hasNext() != false )snakeTimer.stop();stopGame();JOptionPane.showMessageDialog( null,Your Score is + score + nnYou Loss! );System.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! );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.getGraphics();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();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( this.bean ) )this.score += 100;this.scoreLabel.setText( + 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( eatedBean.size() != 0 )if( eatedBean.getFirst().equals( temp ) )eatedBean.remove( 0 );elsemainSnake.clearEndSnakePiece( getGraphics(), temp.x, 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.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.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.hasNext() )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, modelItem, 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_namei );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.setFont( 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 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, Speed-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( 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.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.length;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 = UIManager.getInstalledLookAndFeels();private class ActionHandler implements ActionListenerpublic voi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- NB/T 11316-2023变电站电能质量现场测试技术规范
- JJF(烟草)4.2-2010烟草及烟草制品连续流动法测定常规化学成分测量不确定度评定指南第2部分:总植物碱
- 2001年上海市中考数学试题【含答案、解析】
- 考研复习-风景园林基础考研试题【轻巧夺冠】附答案详解
- 风景园林基础考研资料试题及答案详解(全优)
- 《风景园林招投标与概预算》试题A附参考答案详解【夺分金卷】
- 2025年济南四建集团有限责任公司招聘笔试备考题库及答案详解(网校专用)
- Rhino+KeyShot产品设计 教案全套 第1-10章 认识 Rhino - 渲染综合案例
- 2025年黑龙江省五常市辅警招聘考试试题题库及1套参考答案详解
- 2025年河北省定州市辅警招聘考试试题题库及答案详解(有一套)
- 延迟退休人员协议书
- 辽宁2025年三支一扶考试真题
- 人工智能在单片机教学中的应用与创新
- 历史教学新视角:学科核心素养“历史解释”实施策略
- 井下作业施工方案
- 2025年小学一年级语文考试趣味试题及答案
- 社会科学领域课题研究报告范文
- 成人脓毒症患者医学营养治疗指南(2025版)
- 生物工程细胞培养技术试题
- 2025年房地产开发经营服务项目投资风险评估报告
- EPC项目全流程咨询管理的核心要点与优化策略
评论
0/150
提交评论