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游戏开发杂谈 - 游戏物体
现实生活中,有很多物体,每个物体的长相.行为都不同. 物体存在于不同的空间内,它只在这个空间内发生作用. 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理. 需要它的时候,就把它造出来,不需 ...
随机推荐
- NODE 性能优化
五个手段 “如果你的 node 服务器前面没有 nginx, 那么你可能做错了.”—Bryan Hughes Node.js 是使用 最流行的语言— JavaScript 构建服务器端应用的领先工具 ...
- 记录个人数组、字符串自己常忘记的方法,以及ES常用处理方式
记录自己在工作中,时不时使用,每次都要去查一下的基础方法.以及ES6经常使用的方法 一.Array 1.concat 合并数组 2.shift 获取数组第一个元素 unshift 向数组首位添加一个元 ...
- Flask 上下文管理-- (session,request,current_app的传递)--类似本地线程实现,以及多app应用
Flask session,request,current_app的传递 请求上下文的作用 -- 封装请求相关得数据(request,session) 请求上下文 request session re ...
- 数据库基础 非关系型数据库 MongoDB 和 redis
数据库基础 非关系型数据库 MongoDB 和 redis 1 NoSQL简介 访问量增加,频繁的读写 直接访问(硬盘)物理级别的数据,会很慢 ,关系型数据库的压力会很大 所以,需要内存级的读写操作, ...
- 在 Ubuntu 上搭建 Hadoop 分布式集群 Eclipse 开发环境
一直在忙Android FrameWork,终于闲了一点,利用空余时间研究了一下Hadoop,并且在自己和同事的电脑上搭建了分布式集群,现在更新一下blog,分享自己的成果. 一 .环境 1.操作系统 ...
- Android Broadcast 和 BroadcastReceiver的权限机制
在Android应用开发中,有时会遇到以下两种情况, 1. 一些敏感的广播并不想让第三方的应用收到 : 2. 要限制自己的Receiver接收某广播来源,避免被恶意的同样的ACTION的广播所干扰. ...
- 3天学习完AngularJS基础内容小结
简介:AngularJS 是一个 JavaScript 框架.它是一个以 JavaScript 编写的库. 一.AngularJS大致功能模块 二.页面交互变得简单 1.示例:计算价格 <htm ...
- ng 指令的自定义、使用
1.创建和使用var app = angular.module('myApp',['ng']);app.directive('指令名称',func); 自定义指令的命名:驼峰式,有两部分构成,前缀一般 ...
- Java进阶知识点5:服务端高并发的基石 - NIO与Reactor模式以及AIO与Proactor模式
一.背景 要提升服务器的并发处理能力,通常有两大方向的思路. 1.系统架构层面.比如负载均衡.多级缓存.单元化部署等等. 2.单节点优化层面.比如修复代码级别的性能Bug.JVM参数调优.IO优化等等 ...
- HDU3507Print Article (斜率优化DP)
Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...