当前位置:编程学习 > JAVA >>

急急急!!!求大神帮忙完善一个java小游戏,不胜感激

新手一个,请大神。。。。。

在下面程序下添加实现如下功能:

1.鼠标控制图片角色运动

2.触碰到界跌落

3。跌落后弹出对话框提示游戏结束

4.设置背景

5.设置背景音乐

 


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;

class Ball {
 int coodinateX, coodinateY, width, height, velocityX, velocityY;
 Color color;
 BufferedImage bimg;
 JFrame jf;

 public Ball(int cx, int cy, int w, int h, int vx, int vy, Color c,BufferedImage bimg,JFrame jf) {
  coodinateX = cx;
  coodinateY = cy;
  width = w;
  height = h;
  velocityX = vx;
  velocityY = vy;
  color = c;
  this.bimg=bimg;
  this.jf=jf;
 }

 public void update() {
  coodinateX = coodinateX + velocityX;
  coodinateY = coodinateY + velocityY;
 }

 public void draw(Graphics g){
  //g.setColor(color);
  //g.fillOval(coodinateX, coodinateY, width, height);
  g.drawImage(bimg,coodinateX,coodinateY,jf);
 }

 public void collide(int pW, int pH) {
  if (coodinateX >= pW - width) {
   // 判断是否撞上右边界
   coodinateX = pW - width;
   velocityX = -velocityX; // x速度变向
  }
  if (coodinateX <= 0) {// 判断是否撞上左边界
   coodinateX = 0;
   velocityX = -velocityX; // x速度变向
  }
  if (coodinateY >= pH - height) {// 判断是否撞上下边界
   coodinateY = pH - height;
   velocityY = -velocityY; // y速度变向
  }
  if (coodinateY <= 0) { // 判断是否撞到上边界
   coodinateY = 0;
   velocityY = -velocityY; // y速度变向
  }
 }

 public boolean collide(Ball s) {
  if (coodinateY + height <= s.coodinateY
    && coodinateY + height + velocityY >= s.coodinateY
      + s.velocityY) { // 判断当前精灵是否将要穿越s的上边界
   if (coodinateX + width + velocityX >= s.coodinateX + s.velocityX
     && coodinateX + velocityX <= s.coodinateX + s.width
       + s.velocityX) { // 判断当前精灵是否将在s的x范围内
    velocityY = -velocityY;
    s.velocityY = -s.velocityY;
    return true;
   }
  }
  if (coodinateY >= s.coodinateY + s.height
    && coodinateY + velocityY <= s.coodinateY + s.height
      + s.velocityY) { // 判断当前精灵是否将要穿越s的下边界
   if (coodinateX + width + velocityX >= s.coodinateX + s.velocityX
     && coodinateX + velocityX <= s.coodinateX + s.width
       + s.velocityX) { // 判断当前精灵是否将在s的x范围内
    velocityY = -velocityY;
    s.velocityY = -s.velocityY;
    return true;
   }
  }
  if (coodinateX + width <= s.coodinateX
    && coodinateX + width + velocityX > s.coodinateX + s.velocityX) { // 判断当前精灵是否将要穿越s的左边界
   if (coodinateY + height + velocityY > s.coodinateY
     && coodinateY + velocityY < s.coodinateY + s.height
       + s.velocityY) { // 判断当前精灵是否将在s的y范围内
    velocityX = -velocityX;
    s.velocityX = -s.velocityX;
    return true;
   }
  }
  if (coodinateX >= s.coodinateX + s.width
    && coodinateX + velocityX < s.coodinateX + s.width
      + s.velocityX) { // 判断当前精灵是否将要穿越s的右边界
   if (coodinateY + height + velocityY > s.coodinateY
     && coodinateY + velocityY < s.coodinateY + s.height
       + s.velocityY) { // 判断当前精灵是否将在s的y范围内
    velocityX = -velocityX;
    s.velocityX = -s.velocityX;
    return true;
   }
  }
  return false;
 }
}

 

public class Animation extends JFrame implements ActionListener,KeyListener{

 // private int x = 0, y = 0, dx = 0, dy = 0;
 private Ball b1, b2;
 private int interval = 20;
 private int width, height;
 private int cpWidth, cpHeight;
 private Image offScreen;
 private Graphics offScreenGraphics;
 private MenuBar mb;
 private Menu m1, m2;
 private MenuItem mi1[] = new MenuItem[3], mi2[] = new MenuItem[3];
 private boolean isRun = false;
 private boolean first = true;

 public Animation() {
  File picture = new File("img/role.gif");  
  try {
      BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture));
   b1=new Ball(10, 50,  sourceImg.getWidth(),sourceImg.getHeight(), 10, 10, Color.red,sourceImg,this);
    //  g.drawImage(sourceImg,10, 10, this);
      } 
  catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } 
  
  
 // b2=new Ball(400, 50, 50, 50, -10, 0, Color.blue);
  width = 800;
  height = 400;

  mb = new MenuBar();
  m1 = new Menu("控制");
  m2 = new Menu("interval");
  mb.add(m2 = new Menu("interval"));
  mb.add(m1);
  setMenuBar(mb);
  m1.add(mi1[0] = new MenuItem("run"));
  m1.add(mi1[1] = new MenuItem("stop"));
  m1.add(mi1[2] = new MenuItem("exit"));

  m2.add(mi2[0] = new MenuItem("20ms"));
  m2.add(mi2[1] = new MenuItem("50ms"));
  m2.add(mi2[2] = new MenuItem("100ms"));
  mi1[0].addActionListener(this);
  mi1[1].addActionListener(this);
  mi1[2].addActionListener(this);
  mi2[0].addActionListener(this);
  mi2[1].addActionListener(this);
  mi2[2].addActionListener(this);

  addKeyListener(this);
  setSize(width, height);
  
  setVisible(true);

 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new Animation();

 }

 @Override
 public void paint(Graphics g) {
  // TODO Auto-generated method stub
  if (first) {
   super.paint(g);
   first = false;
  }
  

  if (isRun) {
   cpWidth = getContentPane().getWidth();
   cpHeight = getContentPane().getHeight();
   g = getContentPane().getGraphics();
   g.setColor(Color.white);
   g.fillRect(0, 0, cpWidth, cpHeight);
   b1.draw(g);
  // b2.draw(g);

   try {
    Thread.sleep(interval);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   b1.update();
 //  b2.update();
 //  b1.collide(b2);
   b1.collide(cpWidth,cpHeight);
 //  b2.collide(cpWidth,cpHeight);
   repaint();
  }

 }

 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if (e.getSource() == mi1[0]) {
   if (isRun)
    return;
   else {
    isRun = true;
    repaint();
   }
  } else if (e.getSource() == mi1[1]) {

   if (!isRun)
    return;
   else {
    isRun = false;
    repaint();
   }
  } else if (e.getSource() == mi1[2]) {
   System.exit(0);
  } else if (e.getSource() == mi2[0]) {
   interval = 20;
   repaint();
  } else if (e.getSource() == mi2[1]) {
   interval = 50;
   repaint();
  } else if (e.getSource() == mi2[2]) {
   interval = 100;
   repaint();
  }

 }

 @Override
 public void keyPressed(KeyEvent e) {
  // TODO Auto-generated method stub
  if(e.getKeyCode()==KeyEvent.VK_LEFT){
   b1.coodinateX-=30;
   repaint();
  }
  if(e.getKeyCode()==KeyEvent.VK_RIGHT){
   b1.coodinateX+=30;
   repaint();
  }
  if(e.getKeyCode()==KeyEvent.VK_UP){
   b1.coodinateY-=30;
   repaint();
  }
  if(e.getKeyCode()==KeyEvent.VK_DOWN){
   b1.coodinateY+=30;
   repaint();
  }
 }

 @Override
 public void keyReleased(KeyEvent e) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void keyTyped(KeyEvent e) {
  // TODO Auto-generated method stub
  
 }

}


 

角色图片[img=http://][/img]

Java 游戏 --------------------编程问答-------------------- 真的看出来LZ很急,发了两遍贴。。

嗯,友情帮顶~
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,