java游戏制作之水果忍者

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java游戏制作之水果忍者相关的知识,希望对你有一定的参考价值。

水果忍者的原理很简单,主要就是采用随机的方式是画面上面出现水果。

package Fruitninja;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Backpicture extends JPanel{
    private int x=1,y=1;
    private int line = 100;
    private int[] ti = new int[3];
    private int[] pi = new int[6];
    private int i=0;
    Random ran = new Random();
    private int value;
    ThreadPicture tp = new ThreadPicture(800);
    AddListener al = new AddListener(this);
    ImageIcon background = new ImageIcon(this.getClass().getResource("水果忍者.jpg"));
    ImageIcon apple = new ImageIcon(this.getClass().getResource("apple.png"));
    ImageIcon lemon = new ImageIcon(this.getClass().getResource("lemon.png"));
    ImageIcon orange = new ImageIcon(this.getClass().getResource("orange.png"));
    ImageIcon purple = new ImageIcon(this.getClass().getResource("purple.png"));
    ImageIcon strawberry = new ImageIcon(this.getClass().getResource("strawberry.png"));
    ImageIcon watermelon = new ImageIcon(this.getClass().getResource("watermelon.png"));
    ImageIcon apple1 = new ImageIcon(this.getClass().getResource("apple-1.png"));
    ImageIcon lemon1 = new ImageIcon(this.getClass().getResource("lemon-1.png"));
    ImageIcon orange1 = new ImageIcon(this.getClass().getResource("orange-1.png"));
    ImageIcon purple1 = new ImageIcon(this.getClass().getResource("purple-1.png"));
    ImageIcon strawberry1 = new ImageIcon(this.getClass().getResource("strawberry-1.png"));
    ImageIcon watermelon1 = new ImageIcon(this.getClass().getResource("watermelon-1.png"));
    public static void main(String[] args) {
        Backpicture bp = new Backpicture();
        bp.initUI();
    }
    public Backpicture(){
        ti = tp.setTi();
    }
    public void initUI(){
        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(3);
        jf.setSize(new Dimension(1500,1000));
        jf.setLocationRelativeTo(null);
        jf.setTitle("Fruit ninja");
        jf.add(this);
        this.setSize(new Dimension(500,600));
        jf.addMouseListener(al);
        al.setThreadPicture(tp);
        ranValue();
        tp.getAl(al);tp.getTi(ti);
        jf.setVisible(true);
        
        Thread t = new Thread(al);
        t.start();
        
        
        
    }
    
    
    public void paint(Graphics g){
        super.paint(g);
        g.drawImage(background.getImage(), 0, 0, 1500, 1000, null);
        for(int i=0;i<3;i++){
            al.Cut();
            if(ti[i]==1){
                g.drawImage(apple.getImage(), ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);
            }
            if(ti[i]==2){
                g.drawImage(lemon.getImage(),ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);
            }
            if(ti[i]==3){
                g.drawImage(orange.getImage(), ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);
            }
            if(ti[i]==4){
                g.drawImage(purple.getImage(), ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);
            }
            if(ti[i]==5){
                g.drawImage(strawberry.getImage(),ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);
            }
            if(ti[i]==6){
                g.drawImage(watermelon.getImage(),ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);
            }
            if(ti[i]==7){
                System.out.println("ti[i]"+ti[i]);
                g.drawImage(apple1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);
                
            }
            if(ti[i]==8){
                System.out.println("ti[i]"+ti[i]);
                g.drawImage(lemon1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);
                
            }
            if(ti[i]==9){
                System.out.println("ti[i]"+ti[i]);
                g.drawImage(orange1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);
                
            }
            if(ti[i]==10){
                System.out.println("ti[i]"+ti[i]);
                g.drawImage(purple1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);
                
            }
            if(ti[i]==11){
                System.out.println("ti[i]"+ti[i]);
                g.drawImage(strawberry1.getImage(),(ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);
                
            }
            if(ti[i]==12){
                System.out.println("ti[i]"+ti[i]);
                g.drawImage(watermelon1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);
                
            }
        }
        //System.out.println("al.Cut()="+al.Cut());
//        if(al.Cut()!=0){
//        
//            if(al.Cut()==1){
//                g.drawImage(apple1.getImage(), al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);
//                
//            }
//            if(al.Cut()==2){
//                g.drawImage(lemon1.getImage(),al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);
//            }
//            if(al.Cut()==3){
//                g.drawImage(orange1.getImage(), al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);
//            }
//            if(al.Cut()==4){
//                g.drawImage(purple1.getImage(), al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);
//            }
//            if(al.Cut()==5){
//                g.drawImage(strawberry1.getImage(),al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);
//            }
//            if(al.Cut()==6){
//                g.drawImage(watermelon1.getImage(),al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);
//            }
//        }
        
        }
        

public void ranValue(){
    int pi[] = {1,2,3,4,5,6};
//    for(int t=3;t > 0;t--)
//            { 
//            value = ran.nextInt(6)+1;
//            ti[t-1]=value;
//            }
//             al.getValue(ti);
//             //al.Cut();
    
        while(i<3){
        value = ran.nextInt(6);ti[i]=0;
        if(pi[value]!=0){
            ti[i]=pi[value];pi[value]=0;
            i++;
        }
        al.getValue(ti);
    }
             }


}
package Fruitninja;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JPanel;

public class AddListener extends MouseAdapter implements Runnable{
    private int x1,y1,x2,y2; 
    private JPanel JPanel;
    private ThreadPicture tp;
    private int value;
    private int[] pi = new int[6];
    private int i=0;
    private int j;
    private int[] ti = new int[3];
    private int line = 100;
    public AddListener(JPanel JPanel){
        this.JPanel = JPanel;
        
    }
    public void mousePressed(MouseEvent e) {
         x1 = e.getX();
         y1 = e.getY();
         System.out.println("x1: "+x1+"y1: "+y1);
     }
     public void mouseReleased(MouseEvent e) {
         x2 = e.getX();
         y2 = e.getY();
//         System.out.println("x2: "+x2+"y2:" +y2);
     }
     public void setThreadPicture(ThreadPicture tp){
         this.tp = tp;
     }
     public void run(){
            while(true){
                tp.move();
                //Cut();
                JPanel.repaint();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
     public void getValue(int[] ti){
        
         this.ti = ti;
         
     }
     public void  Cut(){
         for(int g=3;g>0;g--){
         if(ti[g-1]*line+ti[g-1]*100>=x1&&tp.getY()>=y1&&(ti[g-1]*line+ti[g-1]*100+150)<=x2&&(tp.getY()+150)<=y2) 
             {ti[g-1]=ti[g-1]+6;
             x1=0;x2=0;y1=0;y2=0;}
         else {ti[g-1]=ti[g-1];x1=0;x2=0;y1=0;y2=0;}
         }
         
     }
     
    

}
package Fruitninja;

import java.util.Random;

public class ThreadPicture {
    private int y;
    private int[] ti = new int[3];
    private AddListener al;
    private int[] pi = new int[6];
    private int i=0;
    Random ran = new Random();
    private int value;
    private int count=1;
    public ThreadPicture(int y){
        this.y = y;
    }
    public void move(){
        y-=20;
        if(y<=100){
            y=800;
//            for(int t=3;t > 0;t--)
//            { 
//            value = ran.nextInt(5)+1;
//            ti[t-1]=value;
//            }
//             al.getValue(ti);
//             //al.Cut();
            int pi[] = {1,2,3,4,5,6};i=0;
            while(i<3){
                value = ran.nextInt(6);ti[i]=0;
                if(pi[value]!=0){
                    ti[i]=pi[value];pi[value]=0;
                    i++;
                }
                al.getValue(ti);
            }
        }
        }
    public int getY(){
        return y;
    }
    public void getTi(int[] ti){
        this.ti = ti;
    }
    public void getAl(AddListener al){
        this.al = al;
    
    }
    public int[] setTi(){
        return ti;
        }

}

技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享

以上是关于java游戏制作之水果忍者的主要内容,如果未能解决你的问题,请参考以下文章

Python制作经典游戏案例-水果忍者(附源码等文件)

《游戏学习》| 水果忍者HTML5网页版在线游戏 | 源码分析

《游戏学习》| 水果忍者HTML5网页版在线游戏 | 源码分析

JavaScript实现的水果忍者游戏,支持鼠标操作

Pygame实战风靡全球的切水果游戏升级版“水果忍者”上线啦!你敢来PK嘛?!

基于Cocos Creator的水果忍者游戏