@java编写小游戏进阶之路——贪吃蛇
(若有疑问,可咨询:)
其中有两个需要提前准备(下载也可,后面会提供),那就是图片以及bgm,(大家可以到我的资源中自行下载)
#创建两个类:
代码如下:
类一:
/*** @param args*/public static void main(String[] args) {JFrame frame=new JFrame();frame.setBounds(10,10,900,720);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add(new MMpanel());frame.setVisible(true);}//(主方法类:)
类二:
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.IOException;import java.io.InputStream;import java.util.Random;import javax.imageio.*;import javax.swing.*;import javax.sound.sampled.*;public class MMpanel extends JPanel implements KeyListener, ActionListener{ImageIcon title1 ;ImageIcon food ;ImageIcon body ;ImageIcon up ;ImageIcon down ;ImageIcon left ;ImageIcon right ;int len=3;int score=0;int[] snakex=new int[750];int[] snakey=new int[750];int foodx,foody;Random rand=new Random();//背景音乐Clip bgm;String fx="R";//方向:U,D,R,Lboolean isStarted=false;boolean isFailed=false;Timer timer=new Timer(150, this);public MMpanel(){loadImages();loadBGM();initSnake();this.setFocusable(true);this.addKeyListener(this);timer.start();}public void paintComponent(Graphics g){super.paintComponent(g);this.setBackground(Color.WHITE);title1.paintIcon(this,g,25,11);g.fillRect(25,75,850,600);g.setColor(Color.WHITE);g.drawString("长度:"+len, 750, 35);g.drawString("得分:"+score,750,50);if(fx=="R"){right.paintIcon(this,g,snakex[0],snakey[0]);}else if(fx=="L"){left.paintIcon(this,g,snakex[0],snakey[0]);}else if(fx=="U"){up.paintIcon(this,g,snakex[0],snakey[0]);}else if(fx=="D"){down.paintIcon(this,g,snakex[0],snakey[0]);}for(int i=1;i<len;i++){body.paintIcon(this,g, snakex[i],snakey[i]);}food.paintIcon(this,g,foodx,foody);if(isStarted==false){g.setColor(Color.WHITE);g.setFont(new Font("arial",Font.BOLD,40));g.drawString("Press Space To Start", 300, 300);}if(isFailed){g.setColor(Color.RED);g.setFont(new Font("arial",Font.BOLD,40));g.drawString("Failed:Press Space To Restart", 200, 300);}}public void initSnake(){len=3;snakex[0]=100;snakey[0]=100;snakex[1]=75;snakey[1]=100;snakex[2]=50;snakey[2]=100;foodx=25+25*rand.nextInt(34);foody=75+25*rand.nextInt(24);fx="R";score=0;}public void keyPressed(KeyEvent e) {int keyCode=e.getKeyCode();if(keyCode==KeyEvent.VK_SPACE){if(isFailed){isFailed=false;initSnake();}else{isStarted=!isStarted;}repaint();if(isStarted){playBGM();}else{stopBGM();}}else if(keyCode==KeyEvent.VK_LEFT){fx="L";}else if(keyCode==KeyEvent.VK_UP){fx="U";}else if(keyCode==KeyEvent.VK_RIGHT){fx="R";}else if(keyCode==KeyEvent.VK_DOWN){fx="D";}}public void keyReleased(KeyEvent e) {}public void keyTyped(KeyEvent e) {}public void actionPerformed(ActionEvent e) {if(isStarted&&!isFailed){for(int i=len-1;i>0;i--){snakex[i]=snakex[i-1];snakey[i]=snakey[i-1];}if(fx=="R"){snakex[0]=snakex[0]+25;if(snakex[0]>850)snakex[0]=25;}else if(fx=="L"){snakex[0]=snakex[0]-25;if(snakex[0]<25)snakex[0]=850;}else if(fx=="U"){snakey[0]=snakey[0]-25;if(snakey[0]<75)snakey[0]=650;}else if(fx=="D"){snakey[0]=snakey[0]+25;if(snakey[0]>650)snakey[0]=75;}if(snakex[0]==foodx&&snakey[0]==foody){len++;score++;foodx=25+25*rand.nextInt(34);foody=75+25*rand.nextInt(24);}for(int i=1;i<len;i++){if(snakex[i]==snakex[0]&&snakey[i]==snakey[0]){isFailed=true;}}repaint();}timer.start();}private void loadBGM(){try{bgm=AudioSystem.getClip();InputStream is=this.getClass().getClassLoader().getResourceAsStream("sounds/bgm.wav");AudioInputStream ais=AudioSystem.getAudioInputStream(is);bgm.open(ais);}catch(LineUnavailableException e){e.printStackTrace();} catch (UnsupportedAudioFileException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}private void playBGM(){bgm.loop(Clip.LOOP_CONTINUOUSLY);}private void stopBGM(){bgm.stop();}private void loadImages(){InputStream is;try{is= getClass().getClassLoader().getResourceAsStream("image/P2.png");title1=new ImageIcon(ImageIO.read(is));is= getClass().getClassLoader().getResourceAsStream("image/food.png");food=new ImageIcon(ImageIO.read(is));is= getClass().getClassLoader().getResourceAsStream("image/body1.png");body=new ImageIcon(ImageIO.read(is));is= getClass().getClassLoader().getResourceAsStream("image/headup.png");up=new ImageIcon(ImageIO.read(is));is= getClass().getClassLoader().getResourceAsStream("image/headdown.png");down=new ImageIcon(ImageIO.read(is));is= getClass().getClassLoader().getResourceAsStream("image/headleft.png");left=new ImageIcon(ImageIO.read(is));is= getClass().getClassLoader().getResourceAsStream("image/headright.png");right=new ImageIcon(ImageIO.read(is));}catch(IOException e){e.printStackTrace();}}}