求大神帮忙完善一个java小游戏,急急急!!!!
2013-06-05 19:39
489 查看
新手一个,请大神。。。。。
在下面程序下添加实现如下功能:
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
}
}
角色图片
相关文章推荐
- 如何成为一个牛掰的Java大神
- 那位大神帮忙JAVA解析JSON
- 关于一个Adb connection Error,请各路大神帮忙解决下,不胜感激!
- 一个实现用户登录注销的小程序,求大神帮忙解救 ...
- Java获取随机数的3种方法和总结(正在做一个小游戏 用到了 想总结一下 保存了 以后自己看)
- 智能排班,求大神来帮忙给一个完整的思路!!!
- spark-submit提交程序遇到一个很奇葩的错误,求大神帮忙解决
- 用java写一个小游戏
- 构建UIautomatortest环境时,运行报错。请各位大神帮忙看一下撒,小白一个~
- 求大神帮忙解决一个网络设备互联的考试题,谢谢啦。
- 【求助】一个菜鸟java作业,帮忙看一下错在哪儿,题目是判断回文数
- 贪吃蛇源代码『最近写的一个JAVA小游戏』,整体构思过程如文
- 使用Java写一个猜数字小游戏
- 一个GUI的简单练习-------- Java记事本 简单实现 陆续完善中……
- 一个GUI的简单练习-------- Java记事本 简单实现 陆续完善中……
- Java 多线程学习之生产者消费者模型:一个较完善的实现
- JAVA实战:一个桌球小游戏
- 自己写了一个拍照的页面,但是保存的图片太模糊了。求大神帮忙看看
- 刚刚安装好的Eclipse,导入一个Maven项目之后POM文件的第一行一直报错,看了好多帖子也没解决,会是版本问题吗,请各路大神帮忙解决一下,万分感谢!!!
- 用java写一个电子书阅读器,怎么进行自动内码识别,求大神指教