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游戏制作之水果忍者的更多相关文章
- JavaScript实现的水果忍者游戏,支持鼠标操作
智能手机刚刚普及时,水果忍者这款小游戏可谓风靡一时.几年过去了,现在,让我们用纯JavaScript来实现这个水果忍者游戏,就算是为了锤炼我们的JavaScript开发技能吧. 大家可以通过这个链接在 ...
- 前端优秀作品展示,JavaScript 版水果忍者
<水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版的切水果游戏由百度 JS 小组开发,采用 vml + svg 绘图,使用了 Rapha ...
- 作品展示,JavaScript 版水果忍者
点这里 <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版的切水果游戏由百度 JS 小组开发,采用 vml + svg 绘图,使用了 R ...
- 基于html5 canvas和js实现的水果忍者网页版
今天爱编程小编给大家分享一款基于html5 canvas和js实现的水果忍者网页版. <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版 ...
- L3-012 水果忍者 (30 分)
2010年风靡全球的“水果忍者”游戏,想必大家肯定都玩过吧?(没玩过也没关系啦~)在游戏当中,画面里会随机地弹射出一系列的水果与炸弹,玩家尽可能砍掉所有的水果而避免砍中炸弹,就可以完成游戏规定的任务. ...
- 游戏制作之路:一个对我来说可实现的High-end的Mac/iOS游戏制作大概计划
对于学习一些东西,我比较习惯任务驱动式的学习,也就是说,要事先订好一个目标,要做什么东西,达到什么效果,然后根据自己了解的知识作一个可以实现这个目标的计划. 现在要学的是游戏制作,而且是High-en ...
- 【开源java游戏框架libgdx专题】-01-libgdx介绍
libgdx是一款开源的java游戏框架,而且还实现了Desktop/Android/BlackBerry/iOS/HTML5这些些平台的跨平台开发.官方网址:https://libgdx.badlo ...
- Cocos2D:塔防游戏制作之旅(十八)
在Enemy.m的getDamaged:方法只给你添加如下1行(在if条件内): [theGame awardGold:200]; 现在运行游戏你将注意到你不能放置超出你资源金币的炮塔了.当然杀死敌人 ...
- java游戏开发杂谈 - 游戏物体
现实生活中,有很多物体,每个物体的长相.行为都不同. 物体存在于不同的空间内,它只在这个空间内发生作用. 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理. 需要它的时候,就把它造出来,不需 ...
随机推荐
- UI- Layer的使用总结(附动画)
#pargma mark - Layer 1. 设置当前视图的背景颜色 self.view.backgroundColor = [UIColor lightGrayColor]; 2. 创建一个视图, ...
- 视图框架:Spring MVC 4.0(2)
在<springMVC4(7)模型视图方法源码综合分析>一文中,我们介绍了ModelAndView的用法,它会在控制层方法调用完毕后作为返回值返回,里面封装好了我们的业务逻辑数据和视图对象 ...
- Photon Cloud Networking: OnPhotonSerializeView Not Firing
Photon Cloud Networking: OnPhotonSerializeView Not Firing http://answers.unity3d.com/questions/31305 ...
- CodeForces - 662A:Gambling Nim (求有多少个子集其异或为S)(占位)
As you know, the game of "Nim" is played with n piles of stones, where the i-th pile initi ...
- CentOS系统内核、操作系统位数以及系统参数查看
2016-07-29 一.系统内核的查看方法 1.uname -a 显示详细的内核信息 Linux localhost.localdomain 3.10.0-229.el7.x86_64 #1 SMP ...
- git撤销各种状态下的操作
使用Git时会出现各种各样的问题,下面是几种情况下怎么反悔的操作 一,未加入缓存区,撤销文件修改 git checkout -- file 二,已加入缓存区,撤销文件提交 git reset HEAD ...
- openfaas 架构介绍
此为官方介绍 Overview of OpenFaaS Function Watchdog You can make any Docker image into a serverless fun ...
- new JSONObject()报错
如果缺少下列依赖包会报错: 1.commons-beanutils-1.7.0.jar 2.commons-collections-3.2.1.jar 3.commons-lang-2.3.jar 4 ...
- emqtt 4 (我要publish消息了)
这次,分析处理publish msg的流程. 由protocol开始 publish 类型的packet的处理是: process(Packet = ?PUBLISH_PACKET(_Qos, Top ...
- Docker Toolbox on Windows 7
1,下载DockerToolbox,可选daocloud或官网下载 https://get.daocloud.io/toolbox/ 2,配置虚拟机路径环境变量,避免系统C盘,很占空间 MACHINE ...