- package snake;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.File;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class PanelSnake extends JPanel implements ActionListener , KeyListener{
- ImageIcon ico , snake;
- int lengthofsnake = 3;
- int moves = 0;
- int score = 0 ;
- int[] snakexlungth = new int[750];
- int[] snakeylungth = new int[750];
- int[] emtxpos = {40,70,100,130,160,190,220,250,280,310,340,370,400,430,460,490,520};
- int[] emtypos = {130,160,190,220,250,280,310,340,370,400,430,460,490,520,550,580,610};
- int emtx = (int) (Math.random()*17);
- int emty = (int) (Math.random()*17);
- boolean right = false ;
- boolean left = false ;
- boolean up = false ;
- boolean down = false ;
- ImageIcon rightmouth;
- ImageIcon leftmouth;
- ImageIcon upmouth;
- ImageIcon downmouth;
- Timer timer ;
- int delay = 350 ;
- public PanelSnake(){
- addKeyListener(this);
- this.setFocusable(true);//設置可透過鍵盤獲得值
- this.setFocusTraversalKeysEnabled(true);//設置焦點穿越鍵啟用
- timer = new Timer(delay, this);
- timer.start();
- }
- public void paint(Graphics G){
- if(moves == 0 ){
- snakexlungth[2] = 10;
- snakexlungth[1] = 40;
- snakexlungth[0] = 70;
- snakeylungth[2] = 100;
- snakeylungth[1] = 100;
- snakeylungth[0] = 100;
- }
- //背景
- G.setColor(Color.PINK);
- G.fillRect(0, 0, 600, 700);
- ico = new ImageIcon("SANKE.png");
- ico.paintIcon(this, G, 1, 2);
- //邊界
- G.setColor(Color.orange);
- draw((Graphics2D) G);
- //地板
- G.setColor(Color.GREEN);
- G.fillRect(1,86, 581, 569);
- //
- G.setColor(Color.BLACK);
- G.setFont(new Font("標楷體", 3, 16));
- G.drawString("分數 :"+score, 500, 600);
- G.drawString("步數 :"+moves, 500, 620);
- //蛇頭
- if(moves == 0){
- rightmouth = new ImageIcon("right.png");
- rightmouth.paintIcon(this, G, snakexlungth[0] , snakeylungth[0]);
- }
- //方向
- for(int i = 0 ; i < lengthofsnake ; i++){
- //頭
- if(i == 0 && right){
- rightmouth = new ImageIcon("right.png");
- rightmouth.paintIcon(this, G, snakexlungth[0] , snakeylungth[0]);
- }
- if(i == 0 && left){
- leftmouth = new ImageIcon("left.png");
- leftmouth.paintIcon(this, G, snakexlungth[0] , snakeylungth[0]);
- }
- if(i == 0 && down){
- downmouth = new ImageIcon("down.png");
- downmouth.paintIcon(this, G, snakexlungth[0] , snakeylungth[0]);
- }
- if(i == 0 && up){
- upmouth = new ImageIcon("up.png");
- upmouth.paintIcon(this, G, snakexlungth[0] , snakeylungth[0]);
- }
- //身體
- if(i != 0 ){
- snake = new ImageIcon("1.png");
- snake.paintIcon(this, G, snakexlungth[i] , snakeylungth[i]);
- }
- }
- if(!timer.isRunning()) {
- G.setColor(Color.BLACK);
- G.setFont(new Font("標楷體", 3, 48));
- G.drawString("最高紀錄 :" +score, 300, 400);
- }
- if(new Rectangle(emtxpos[emtx], emtypos[emty], 15, 15).intersects(new Rectangle(snakexlungth[0], snakeylungth[0], 15, 15))){
- lengthofsnake++;
- score++;
- delay-= 4;
- PlayMusic(new File("small_drum1.wav"));
- timer.setDelay(delay);
- emtx = (int) (Math.random()*17);
- emty = (int) (Math.random()*17);
- /* if(lengthofsnake == 25){
- G.setColor(Color.BLACK);
- G.setFont(new Font("標楷體", 3, 48));
- G.drawString("YOU WIN", 310, 400);
- timer.stop() ;
- }*/
- }
- G.setColor(Color.red);
- G.fillOval(emtxpos[emtx], emtypos[emty], 15, 15);
- G.dispose();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(right){
- moves ++;
- for(int i = lengthofsnake-1 ; i >=0 ; i--){
- snakeylungth[i+1] = snakeylungth[i];
- }
- for(int i = lengthofsnake ; i >=0 ; i--){
- if(i == 0){
- snakexlungth[i] = snakexlungth[i]+30;
- }else{
- snakexlungth[i] = snakexlungth[i-1];
- }
- if( snakexlungth[i] > 560)
- timer.stop() ;
- }
- }
- if(left){
- moves ++;
- for(int i = lengthofsnake-1 ; i >=0 ; i--){
- snakeylungth[i+1] = snakeylungth[i];
- }
- for(int i = lengthofsnake ; i >=0 ; i--){
- if(i == 0){
- snakexlungth[i] = snakexlungth[i]-30;
- }else{
- snakexlungth[i] = snakexlungth[i-1];
- }
- if( snakexlungth[i] < 10 )
- timer.stop() ;
- }
- }
- if(up){
- moves++;
- for(int i = lengthofsnake-1 ; i >=0 ; i--){
- snakexlungth[i+1] = snakexlungth[i];
- }
- for(int i = lengthofsnake ; i >=0 ; i--){
- if(i == 0){
- snakeylungth[i] = snakeylungth[i]-30;
- }else{
- snakeylungth[i] = snakeylungth[i-1];
- }
- if( snakeylungth[i] < 100 )
- timer.stop() ;
- }
- }
- if(down){
- moves++;
- for(int i = lengthofsnake-1 ; i >=0 ; i--){
- snakexlungth[i+1] = snakexlungth[i];
- }
- for(int i = lengthofsnake ; i >=0 ; i--){
- if(i == 0){
- snakeylungth[i] = snakeylungth[i]+30;
- }else{
- snakeylungth[i] = snakeylungth[i-1];
- }
- if( snakeylungth[i] > 620 )
- timer.stop() ;
- }
- }
- repaint();
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- right = true ;
- if(!left){
- right = true ;
- }else{
- right = false ;
- }
- up = false ;
- down = false ;
- }
- if(e.getKeyCode() == KeyEvent.VK_LEFT){
- left = true ;
- if(!right){
- left = true ;
- }else{
- left = false ;
- }
- up = false ;
- down = false ;
- }
- if(e.getKeyCode() == KeyEvent.VK_UP){
- up = true ;
- if(!down){
- up = true ;
- }else{
- up = false ;
- }
- right = false ;
- left = false ;
- }
- if(e.getKeyCode() == KeyEvent.VK_DOWN){
- down = true ;
- if(!up){
- down = true ;
- }else{
- down = false ;
- }
- right = false ;
- left = false ;
- }
- if(e.getKeyCode() == KeyEvent.VK_ENTER && !timer.isRunning()){
- moves = 0;
- score = 0 ;
- lengthofsnake = 3 ;
- up = false ;left = false ;
- down = false ;right= false ;
- timer.start();
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- public static void PlayMusic(File Cli) {
- try {
- Clip a = AudioSystem.getClip();
- a.open(AudioSystem.getAudioInputStream(Cli));
- a.start();
- Thread.sleep(0);//間隔
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- private void draw(Graphics2D G) {
- G.setStroke(new BasicStroke(10));
- G.drawRect(0, 85, 582,570);
- G.drawRect(0, 0, 582, 75);
- }
- }
2017年9月16日 星期六
貪食蛇
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。