首页 【最新】JAVA贪吃蛇源代码

【最新】JAVA贪吃蛇源代码

举报
开通vip

【最新】JAVA贪吃蛇源代码【最新】JAVA贪吃蛇源代码 JAVAÌ??ÔÉßÔ??úÂë??Ò???(2008-10-10 23:50:14)תÔØ?êÇ??ºjavaÌ??ÔÉßÔ?ÂëÔ??úÂëit ?ÖÀà?º??Êõ×?Çø SnakeGame.java package SnakeGame; import javax.swing.*; public class SnakeGame { public static void main( String[] args ) { JDialog.setDefaultLoo...

【最新】JAVA贪吃蛇源代码
【最新】JAVA贪吃蛇源代码 JAVAÌ??ÔÉßÔ??úÂë??Ò???(2008-10-10 23:50:14)תÔØ?êÇ??ºjavaÌ??ÔÉßÔ?ÂëÔ??úÂëit ?ÖÀà?º??Êõ×?Çø 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 { public 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://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, 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 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.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 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( "Pause[SPACE] - Exit[ESC]" ); 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 KeyAdapter { public 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.getKeyCode() == 32 ) { if( !hasStoped ) { if( !flag ) { snakeTimer.stop(); configMenu.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 = (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 + "\n\nYou 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 + "\n\nYou 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( "Pause[SPACE] - Exit[ESC]" ); } 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(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 ); } else { mainSnake.clearEndSnakePiece( getGraphics(), temp.x, temp.y, singleWidthX, singleHeightY, cooPos ); mainSnake.removeTail(); } } else { mainSnake.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.java package SnakeGame; import java.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 = owner; owner.setJMenuBar( this ); String[] menu_name = {"Snake Game", "Game Configure", "Game Help"}; menu = new JMenu[menu_name.length]; for( int i = 0; i < menu_name.length; i++ ) { menu[i] = new JMenu( menu_name[i] ); menu[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) ); this.add( menu[i] ); } String[] menuItem_name = {"Start Game", "Stop Game", "Exit Game", "Game Color", "About..."}; menuItem = new JMenuItem[menuItem_name.length]; for( int i = 0; i < menuItem_name.length; i++ ) { menuItem[i] = new JMenuItem( menuItem_name[i] ); menuItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) ); menuItem[i].addActionListener( new ActionHandler() ); } menu[0].add( menuItem[0] ); menu[0].add( menuItem[1] ); menu[0].addSeparator(); menu[0].add( menuItem[2] ); menu[1].add( menuItem[3] ); menu[2].add( menuItem[4] ); String[] inner_menu_name = {"Game Speed", "Window Model", "Game Standard "}; JMenu[] inner_menu = new JMenu[inner_menu_name.length]; for( int i = 0; i < inner_menu_name.length; i++ ) { inner_menu[i] = new JMenu( inner_menu_name[i] ); inner_menu[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) ); menu[1].add( inner_menu[i] ); } ButtonGroup temp1 = new ButtonGroup(); String[] speedItem_name = {"Speed-1", "Speed-2", "Speed-3", "Speed-4", "Speed-5"}; speedItem = new JRadioButtonMenuItem[speedItem_name.length]; for( int i = 0; i < speedItem_name.length; i++ ) { speedItem[i] = new JRadioButtonMenuItem( speedItem_name[i] ); inner_menu[0].add( speedItem[i] ); speedItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) ); speedItem[i].addItemListener( new ItemHandler() ); temp1.add( speedItem[i] ); } ButtonGroup temp2 = new ButtonGroup(); String[] modelItem_name = { "Linux", "Mac", "Windows" }; modelItem = new JRadioButtonMenuItem[modelItem_name.length]; for( int i = 0; i < modelItem_name.length; i++ ) { modelItem[i] = new JRadioButtonMenuItem( modelItem_name[i] ); inner_menu[1].add( modelItem[i] ); modelItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) ); modelItem[i].addItemListener( new ItemHandler() ); temp2.add( modelItem[i] ); } ButtonGroup temp3 = new ButtonGroup(); String[] standardItem_name = { "60 * 40", "45 * 30", "30 * 20" }; standardItem = new JRadioButtonMenuItem[standardItem_name.length]; for( int i = 0; i < standardItem_name.length; i++ ) { standardItem[i] = new JRadioButtonMenuItem( standardItem_name[i] ); inner_menu[2].add( standardItem[i] ); standardItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) ); standardItem[i].addItemListener( new ItemHandler() ); temp3.add( standardItem[i] ); } looks = UIManager.getInstalledLookAndFeels(); } private class ActionHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { if( e.getSource() == menuItem[0] ) { owner.resetGame(); ConfigMenu.this.setVisible( false ); } else if( e.getSource() == menuItem[1] ) { owner.stopGame(); ConfigMenu.this.setVisible( true ); ConfigMenu.this.setMenuEnable( true ); } else if( e.getSource() == menuItem[2] ) { System.exit( 0 ); } else if( e.getSource() == menuItem[3] ) { ConfigDialog temp = new ConfigDialog( owner ); temp.setVisible( true ); } else if( e.getSource() == menuItem[4] ) { JOptionPane.showMessageDialog( null, "Sanke Game 2.0 Version!\n\n" + "Author: FinalCore\n\n" ); } } } private class ItemHandler implements ItemListener { public void itemStateChanged( ItemEvent e ) { for( int i = 0; i < speedItem.length; i++ ) { if( e.getSource() == speedItem[i] ) { owner.snakeTimer.setDelay( 150 - 30 * i ); } } if( e.getSource() == standardItem[0] ) { owner.setGrid( 60, 40, 5 ); } else if( e.getSource() == standardItem[1] ) { owner.setGrid( 45, 30, 10 ); } else if( e.getSource() == standardItem[2] ) { owner.setGrid( 30, 20, 15 ); } for( int i = 0; i < modelItem.length; i++ ) { if( e.getSource() == modelItem[i] ) { try { UIManager.setLookAndFeel( looks[i].getClassName() ); }catch(Exception ex){} } } } } public void setMenuEnable( boolean temp ) { menu[1].setEnabled( temp ); } }
本文档为【【最新】JAVA贪吃蛇源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_686908
暂无简介~
格式:doc
大小:47KB
软件:Word
页数:0
分类:生活休闲
上传时间:2017-10-15
浏览量:25