- 步骤 :
- 1、创建窗口对象Ui
- 2、背景的绘制
- 3、键盘的监听事件
-
-显示接口
-
-添加监听事件
- 4、将己方鱼放上去
- 5、让小鱼移动
- 6、其他的鱼并引导(先创建集合将鱼装起来,获得每个对象,画图)
- 7、吃鱼
- 8、被吃
package 大鱼吃小鱼;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Ui extends JFrame implements KeyListener {
FishPanel fishPanel = new FishPanel();
public Ui() {
this.setTitle( "大鱼吃小鱼" );
this.setSize( 1440 , 900 );
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setLocationRelativeTo( null );
this.add(fishPanel);
fishPanel.action();
this.setVisible( true );
this.addKeyListener( this );
}
public static void main(String [] args) {
new Ui();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(0);
fishPanel.move(e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
}
}
package 大鱼吃小鱼;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class FishPanel extends JPanel {
int state = 0;
Image bkImg = new ImageIcon("images/bg.jpg").getImage();
Image over = new ImageIcon("images/bg.jpg").getImage();
Image zan = new ImageIcon("images/bg.jpg").getImage();
Image start = new ImageIcon("images/start.png").getImage();
ArrayList<Fish> fishs = new ArrayList<Fish>();
int miniFish = 0 , middleFish = 0 , bigFish = 0 ,superFish = 0 ;
int score = 0;
public FishPanel() {
myFish = new MyFish();
oldzhanLi=myFish.zhanLi;
}
MyFish myFish = new MyFish();
int oldzhanLi = myFish.zhanLi;
public void paint(Graphics g) {
super.paint(g);
System.out.println("zzzzz");
switch(state) {
case 0://游戏加载
g.drawImage(start, 0, 0, 1440, 900, 0, 0, 500,300, null);
break;
case 1://游戏开始
drawBackGround(g);
drawMyFish(g);
drawFishs(g);
g.setColor(Color.white);
Font f = new Font("黑体",Font.BOLD,20);
g.setFont(f);
g.drawString("当前得分:"+score, 15, 20);
g.drawString("战斗力:"+myFish.zhanLi, 200, 20);
break;
case 2://游戏暂停
g.drawImage(zan, 0, 0, null);
break;
case 3://游戏结束
drawBackGround(g);
drawMyFish(g);
drawFishs(g);
g.drawString("当前得分:"+score, 15, 20);
g.drawString("战斗力:"+myFish.zhanLi, 200, 20);
g.setColor(Color.white);
Font ff = new Font("黑体",Font.BOLD,50);
g.setFont(ff);
g.drawString("游戏结束",600 ,450);
break;
}
}
private void drawFishs(Graphics g) {
for(int i = 0 ; i < fishs.size() ; i ++ ) {
Fish fish = fishs.get(i);
fish.draw(g);
}
}
private void drawBackGround(Graphics g) {
g.drawImage(bkImg, 0, 0, 1440, 900, 0, 0, 2048, 1024, null);
}
private void drawMyFish(Graphics g) {
myFish.draw(g);
}
public void move(int keycode) {
switch(keycode) {
case KeyEvent.VK_SPACE:
if(state == 0) {
state = 1;
}
break;
case KeyEvent.VK_LEFT:
if(state == 1) {
myFish.moveLeft();
repaint();
}
break;
case KeyEvent.VK_RIGHT:
if(state == 1) {
myFish.moveRight();
repaint();
}
break;
case KeyEvent.VK_UP:
if(state == 1) {
myFish.moveUp();
repaint();
}
break;
case KeyEvent.VK_DOWN:
if(state == 1) {
myFish.moveDown();
repaint();
}
break;
}
}
int index=1;
public void action() {
Timer timer=new Timer();
timer.schedule(new TimerTask() {
public void run() {
if(state == 1) {
createFishs();
hitFish();
repaint();
}
}
},20,10);
}
private void hitFish() {
for( int i = 0 ; i < fishs.size() ; i ++ ) {
Fish fish = fishs.get(i);
if( hit(fish) ) {
if(myFish.zhanLi >= fish.zhanLi) {
switch(fish.zhanLi) {
case 1 ://吃最小鱼加一分
score ++;
break;
case 2 ://吃中下等鱼加二分
score += 2;
break;
case 3 ://吃中上等鱼加五分
score += 5;
break;
case 4 ://吃大鱼加十分
score += 10;
break;
}
if(score >= 5) {
myFish.zhanLi = 2;
}
if(score >= 25) {
myFish.zhanLi = 3;
}
if(score >= 45) {
myFish.zhanLi = 4;
}
fishs.remove(i);
if(myFish.zhanLi > oldzhanLi) {
myFish.x1 = (int)(myFish.x1*1.1);
myFish.y1 = (int)(myFish.y1*1.1);
oldzhanLi = myFish.zhanLi;
}
}else {
state = 3;
}
}
}
}
private boolean hit(Fish fish) {
int x = myFish.x + (myFish.x1 - myFish.x)/2;
int y = myFish.y + (myFish.y1 - myFish.y)/2;
if(x >= fish.x && x <= fish.x + fish.length) {
if(y >= fish.y && y <= fish.y + fish.height) {
return true;
}
}
return false;
}
private void createFishs() {
if(miniFish ++ % 200 == 0) {
fishs.add(new MiniFish());
}
if(middleFish ++ % 400 == 0) {
fishs.add(new MiddleFish());
}
if((bigFish ++ % 1000) == 0) {
fishs.add(new bigFish());
}
if((superFish++%5000)==0) {
fishs.add(new superFish());
}
}
}
鱼的父类
package 大鱼吃小鱼;
import java.awt.Graphics;
import java.awt.Image;
public class Fish {
int x,y;
int length,height;
boolean isMoveLeft = true;
Image image;
Image[] img = new Image[10];
int sx1,sy1;
int sx2,sy2;
int zhanLi;
int step;
int count;
public void draw(Graphics g) {
}
public void move() {
}
}
绘制各种鱼 有mini鱼、middle鱼、big鱼、super鱼
- mini鱼
package 大鱼吃小鱼;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class MiniFish extends Fish{
public MiniFish() {
int r = (int)(Math.random()*2);
if(r == 0) {
isMoveLeft = false;
x = -68;
}else {
isMoveLeft = true;
x = 1440;
}
y = (int)(Math.random()*900);
length = 68;
height = 55;
zhanLi = 1;
step = 0;
image = new ImageIcon("images/fish08.png").getImage();
}
public void draw(Graphics g) {
super.draw(g);
if( isMoveLeft ) {
sy1=height*step;
sx1=0;
sy2=height*step+height;
sx2=length;
g.drawImage(image, x--, y, x+length, y+height, sx1, sy1, sx2, sy2, null);
}else {
sx2=0;
sy2=height*step;
sx1=length;
sy1=height*step+height;
g.drawImage(image, x++, y, x+length, y+height, sx1, sy1, sx2, sy2, null);
}
}
public void move() {
if(step<1){
step++;
}else
step=0;
}
}
- middle鱼
package 大鱼吃小鱼;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class MiddleFish extends Fish {
public MiddleFish() {
int r = (int)(Math.random()*2);
if(r == 0) {
isMoveLeft = false;
x = -68;
}else {
isMoveLeft = true;
x = 1440;
}
y = (int)(Math.random()*900);
length = 96;
height = 29;
zhanLi = 2;
step = 0;
image = new ImageIcon("images/fish12.png").getImage();
}
public void draw(Graphics g) {
super.draw(g);
if( isMoveLeft ) {
sy1=height*step;
sx1=0;
sy2=height*step+height;
sx2=length;
g.drawImage(image, x--, y, x+length, y+height, sx1, sy1, sx2, sy2, null);
}else {
sx2=0;
sy2=height*step;
sx1=length;
sy1=height*step+height;
g.drawImage(image, x++, y, x+length, y+height, sx1, sy1, sx2, sy2, null);
}
}
public void move() {
int step = 0;
if(step<2) {
step++;
}else
step=0;
}
}
- big鱼
package 大鱼吃小鱼;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class bigFish extends Fish {
public bigFish() {
int r = (int)(Math.random()*2);
if(r == 0) {
isMoveLeft=false;
x=-310;
}else {
isMoveLeft=true;
x=1440;
}
y=(int)(Math.random()*900);
length= 310;
height= 104;
zhanLi=3;
step=0;
image = new ImageIcon("images/fish03.png").getImage();
}
public void draw(Graphics g){
if(isMoveLeft) {
sy1=height*step;
sx1=0;
sy2=height*step+height;
sx2=length;
g.drawImage(image, x--, y, x+length, y+height, sx1, sy1, sx2,sy2, null);
}
else {
sx2=0;
sy2=height*step;
sx1=length;
sy1=height*step+height;
g.drawImage(image, x++, y, x+length, y+height, sx1, sy1, sx2,sy2, null);
}
}
public void move() {
if(step<8){
step++;
}else
step=0;
}
}
- super鱼
package 大鱼吃小鱼;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class superFish extends Fish {
public superFish() {
int r = (int)(Math.random()*2);
if(r == 0) {
isMoveLeft=false;
x=-380;
}else {
isMoveLeft=true;
x=1440;
}
y=(int)(Math.random()*900);
length= 380;
height= 210;
zhanLi=4;
step=0;
image = new ImageIcon("images/fish17.png").getImage();
}
public void draw(Graphics g){
if(isMoveLeft) {
sy1=height*step;
sx1=0;
sy2=height*step+height;
sx2=length;
g.drawImage(image, x--, y, x+length, y+height, sx1, sy1, sx2,sy2, null);
}
else {
sx2=0;
sy2=height*step;
sx1=length;
sy1=height*step+height;
g.drawImage(image, x++, y, x+length, y+height, sx1, sy1, sx2,sy2, null);
}
}
public void move() {
if(step<1) {
step++;
}else
step=0;
}
}
代码所需要的图片
- 游戏界面
images/start.png
-
游戏运行界面 images/bg.jpg -
super鱼 images/fish17.png -
big鱼 images/fish03.png -
middle鱼 images/fish12.png -
自己鱼和mini鱼 images/fish08.png
|