贪吃蛇代码
Posted bangetangchao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了贪吃蛇代码相关的知识,希望对你有一定的参考价值。
1 package moonsnake; 2 3 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.awt.event.KeyEvent; 10 import java.awt.event.KeyListener; 11 import java.util.Random; 12 13 import javax.swing.ImageIcon; 14 import javax.swing.JPanel; 15 import javax.swing.Timer; 16 17 public class MPanel extends JPanel implements KeyListener, ActionListener { 18 ImageIcon title = new ImageIcon("title.png"); 19 ImageIcon body = new ImageIcon("body.png"); 20 ImageIcon up = new ImageIcon("up.png"); 21 ImageIcon down = new ImageIcon("down.png"); 22 ImageIcon left = new ImageIcon("left.png"); 23 ImageIcon right = new ImageIcon("right.png"); 24 ImageIcon food = new ImageIcon("food.png"); 25 26 int len = 3; 27 int score = 0; 28 int[] snakex = new int[750]; 29 int[] snakey = new int[750]; 30 String fx = "R";//方向 R,L,U,D 31 boolean isStarted = false; 32 boolean isFailed = false; 33 Timer timer = new Timer(200,this); 34 int foodx; 35 int foody; 36 Random rand = new Random(); 37 38 public MPanel() { 39 initSnake(); 40 this.setFocusable(true); 41 this.addKeyListener(this); 42 timer.start(); 43 } 44 45 public void paintComponent(Graphics g) { 46 super.paintComponent(g); 47 this.setBackground(Color.WHITE); 48 title.paintIcon(this, g, 25, 11); 49 g.fillRect(25, 75, 850, 600); 50 g.setColor(Color.WHITE); 51 g.drawString("len " + len, 750, 35); 52 g.drawString("score " + score, 750, 50); 53 54 if(fx == "R") { 55 right.paintIcon(this, g, snakex[0], snakey[0]); 56 }else if(fx == "L") { 57 left.paintIcon(this, g, snakex[0], snakey[0]); 58 }else if(fx == "D") { 59 down.paintIcon(this, g, snakex[0], snakey[0]); 60 }else if(fx == "U") { 61 up.paintIcon(this, g, snakex[0], snakey[0]); 62 } 63 64 65 for(int i=1; i< len; i++) { 66 body.paintIcon(this, g, snakex[i], snakey[i]); 67 } 68 69 food.paintIcon(this, g, foodx, foody); 70 71 if(isStarted == false) { 72 g.setFont(new Font("arial", Font.BOLD, 40)); 73 g.setColor(Color.WHITE); 74 g.drawString("Press Space to Start", 300, 300); 75 } 76 if(isFailed) { 77 g.setFont(new Font("arial", Font.BOLD, 40)); 78 g.setColor(Color.RED); 79 g.drawString("Filed: Press Space to Restart", 200, 300); 80 } 81 } 82 83 public void initSnake() { 84 len = 3; 85 snakex[0] = 100; 86 snakey[0] = 100; 87 snakex[1] = 75; 88 snakey[1] = 100; 89 snakex[2] = 50; 90 snakey[2] = 100; 91 foodx = 25 + 25 * rand.nextInt(34); 92 foody = 75 + 25 * rand.nextInt(24); 93 fx = "R"; 94 score = 0; 95 } 96 97 @Override 98 public void keyPressed(KeyEvent e) { 99 100 } 101 102 @Override 103 public void keyReleased(KeyEvent e) { 104 int keyCode = e.getKeyCode(); 105 if(keyCode == KeyEvent.VK_SPACE) { 106 if(isFailed) { 107 isFailed = false; 108 initSnake(); 109 }else { 110 isStarted = !isStarted; 111 } 112 repaint(); 113 }else if(keyCode == KeyEvent.VK_LEFT) { 114 fx = "L"; 115 }else if(keyCode == KeyEvent.VK_RIGHT) { 116 fx = "R"; 117 }else if(keyCode == KeyEvent.VK_UP) { 118 fx = "U"; 119 }else if(keyCode == KeyEvent.VK_DOWN) { 120 fx = "D"; 121 } 122 123 } 124 125 @Override 126 public void keyTyped(KeyEvent e) { 127 128 } 129 130 @Override 131 public void actionPerformed(ActionEvent e) { 132 if(isStarted && !isFailed) { 133 for(int i=len-1; i>0; i--) { 134 snakex[i] = snakex[i-1]; 135 snakey[i] = snakey[i-1]; 136 } 137 if(fx == "R") { 138 snakex[0] = snakex[0]+25; 139 if(snakex[0] > 850)snakex[0] = 25; 140 }else if(fx == "L") { 141 snakex[0] = snakex[0]-25; 142 if(snakex[0] < 25)snakex[0] = 850; 143 }else if(fx == "U") { 144 snakey[0] = snakey[0]-25; 145 if(snakey[0] < 75)snakey[0] = 650; 146 }else if(fx == "D") { 147 snakey[0] = snakey[0]+25; 148 if(snakey[0] > 650)snakey[0] = 75; 149 } 150 151 if(snakex[0] == foodx && snakey[0] == foody) { 152 len++; 153 score = score + 10; 154 foodx = 25 + 25 * rand.nextInt(34); 155 foody = 75 + 25 * rand.nextInt(24); 156 } 157 158 for(int i=1; i<len; i++) { 159 if(snakex[i] == snakex[0] && snakey[i] == snakey[0]) { 160 isFailed = true; 161 } 162 } 163 164 repaint(); 165 } 166 167 timer.start(); 168 } 169 }
1 package moonsnake; 2 3 import javax.swing.JFrame; 4 5 public class Msnake { 6 7 public static void main(String[] args) { 8 JFrame frame = new JFrame(); 9 frame.setBounds(10, 10, 900, 720); 10 frame.setResizable(false); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 frame.add(new MPanel()); 13 14 frame.setVisible(true); 15 16 } 17 18 }
以上是关于贪吃蛇代码的主要内容,如果未能解决你的问题,请参考以下文章