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游戏开发杂谈 - 游戏物体
现实生活中,有很多物体,每个物体的长相.行为都不同. 物体存在于不同的空间内,它只在这个空间内发生作用. 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理. 需要它的时候,就把它造出来,不需 ...
随机推荐
- LeetCode OJ:Best Time to Buy and Sell Stock II(股票买入卖出最佳实际II)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 向重复劳动说不!——GMExplorer 1.0 Beta 发布
下载: GMExplorer 1.0 Beta 计算机网络诞生以来,各项技术日新月异.在浏览器器能做和看到的,早已不止网页了! 特别是近几年HTML5.云服务等技术的兴起,涌向大批优秀的Web应用.人 ...
- Android平台下渗透测试工具大集合
Android平台下渗透测试工具大集合 分享一个google的项目,各种Android下的渗透测试工具. Ad Network Detector (1.2): http://market.androi ...
- tf随笔-6
import tensorflow as tfx=tf.constant([-0.2,0.5,43.98,-23.1,26.58])y=tf.clip_by_value(x,1e-10,1.0)ses ...
- 深入理解UE4宏定义—— GENERATED_BODY
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/72834164 作者:car ...
- java网络编程TCP传输—流操作—拿到源后的写入动作
在网络编程中的TCP传输里,拿到Socket的源后,应该怎么进行读写操作呢,下面我列举了两种方法,希望大家帮忙补充···· 1.利用byte数组作为一个缓冲区进行读写 客户端上传 //获取socket ...
- 最简单的记录程序运行时间的方法:[记录PHP程序运行消耗时间]
比较实用的debug方法:具体参考地址已经记不清了,这里重写成类方便调用 /** * @author logonmy@126.com * @Date: 13-7-23 * @desc example ...
- POJ 2029 Palindromes _easy version
#include<cstdio> #include<cstring> using namespace std; int main() { int n; ]; scanf(&qu ...
- 初识ADO.NET
摘要 作为.NET框架最重要的组件之一,ADO.NET扮演着应用程序与数据交互的重要的角色.本文将从宏观的角度来探讨ADO.NET,和大家一起了解ADO.NET来龙去脉以及ADO.NET的主要组成部分 ...
- noip济南清北冲刺班DAY2
题解: 贪心+dp 30% N<=5 5!枚举一下 20% 高度没有的时候,高度花费就不存在了,将ci排序, 从小到大挨个跳.另外,20% 准备跳楼没有花费,那么跳 楼的高度一定是从小到大, ...