我有这个任务:实现一个小游戏,其中:
海中有两个岛。
一艘船需要从一个岛屿航行到另一个岛屿,以收集尽可能多的黄金并返回。
船可以使用箭头键导航。
船不能驶过岛屿。
沿途有水雷随意移动。
地雷在四处漂流时不会穿过岛屿。
如果船撞到地雷,它就会被摧毁。每条船有三个生命。
黄金完全装载到船上需要时间。这个时间大约是三秒。如果一艘船不等这么久,它就会部分装载黄金。
卸载也是如此。
在你完全卸载五次后游戏结束,或者你的生命结束。
- 游戏共有三个难度:简单、中等和困难。简单游戏的地雷比中等游戏少,中等游戏的地雷比困难游戏少。
这就是我到目前为止所拥有的:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class GoldRush extends Applet {
int boatx=125;
int boaty= 270;
int lifes = 3;
int gold1=0;
int gold2=20;
int option;
/**
* @param args the command line arguments
*/
public void init()
{
setSize(900, 500);
Object[] options = {"Easy", "Meduim","Difficult"};
option = JOptionPane.showOptionDialog(this,"Choose a difficulty ","Gold Rush",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,
null,options,options[2]);
addKeyListener(new KeyListener());
setFocusable(true);
}
public void paint(Graphics g) {
Image back,boat,bomb,gold;
back = getImage("back.jpg");
boat = getImage("boat.jpg");
bomb = getImage("bomb.jpg");
gold = getImage("gold.jpg");
g.drawImage(back, 0, 0, null);
g.setColor(Color.BLACK);
g.drawString("Lifes: "+ lifes, 10, 20);
g.drawString("Gold on Your ISland: "+ gold1, 10, 40);
g.drawString("Gold on the other Island: "+ gold2, 10, 60);
if(boatx >=125 && boatx <= 640&& boaty >= 2 && boaty <= 450){
g.drawImage(boat, boatx, boaty, null);
}else {
if(boatx<125) boatx=125;
if(boatx>640) boatx=640;
if(boaty<2) boaty=2;
if(boaty>450) boaty=450;
g.drawImage(boat, boatx, boaty, null);
}
if (option == 0){
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
}
else if (option == 1){
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
}
else if (option == 2){
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
g.drawImage(bomb, (int)(145 + (Math.random() * (601 - 145))),(int)(190 + (Math.random() * (700 - 190))) , null);
}
}
public Image getImage(String name){
URL imgUrl = getClass().getClassLoader().getResource("resources/"+name);
ImageIcon icon = new ImageIcon(imgUrl);
return icon.getImage();
}
public void up(){
boaty= boaty-5;
repaint();
}
public void down(){
boaty= boaty+5;
repaint();
}
public void left(){
if(boatx >= 125)
boatx= boatx-5;
repaint();
}
public void right(){
boatx= boatx+5;
repaint();
}
class KeyListener extends KeyAdapter{
public KeyListener(){
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
super.keyPressed(e);
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT : left();break;
case KeyEvent.VK_RIGHT: right();break;
case KeyEvent.VK_UP: up();break;
case KeyEvent.VK_DOWN: down(); break;
default:
System.out.println("You pressed: " + e.getKeyCode());
}
}
}
}
我不知道怎么做第 8 项,我检查船是否停留 3 秒以完全装载黄金。你能帮我解决这个问题吗?
我会很感激 ;)