正如标题所说,我的导弹没有出现。我不确定这是输入错误,还是什么。这是作业所说的:
BlockquotePart 5:拍摄
当按下向上箭头键时,巡逻艇将射击导弹。 使类变量patrolMissileX和patrolMissileY。初始化 patrolMissileY为0.初始化一个新的PATROL_MISSILE_LENGTH 写10.方法:public static void movePatrolMissileAndDraw(Graphics g);移动导弹并拉动 它类似于moveEnemyShipAndDraw:如果没有画任何东西 patrolMissileY是0.否则,导弹作为垂直线 规定的长度和x位置。 patrolMissileY是最顶尖的 线。将导弹画成白色,将导弹向上移动5个像素,然后 再用黑色画出来。如果patrolMissileY为0或为负,请不要 用黑色绘制导弹,但将巡逻导弹Y设为0。
在循环中睡眠之前调用此方法。当向上箭头键 按下并且patrolMissileY为0,将patrolMissileX设置为中心 将巡逻舰设置在导弹顶部以使其底部 是巡逻船顶部上方一个像素(PATROL_Y)。你应该 只能一次发射一枚导弹而上箭不应该发射 如果仍然展示导弹,我会做任何事情。
此外,由于我会继续为每个部分而来,所以最后部分的任何提示都将受到赞赏:
第6部分:检测命中
创建一个布尔类变量,命中,初始化为false;写下来 方法:public static boolean detectHit();如果是,则返回true 目前的导弹击中敌舰,否则就是假的。有一个 如果导弹顶部在敌舰内部,则击中。为了这个 发生两件事必须是真的:导弹顶部的x值 必须在敌人的左右两侧之间的y值 导弹的顶部必须在顶部和底部之间 敌人。在startGame循环结束时,将hit设置为true detectHit()返回true。修改moveEnemyShipAndDraw,以便命中 是的,敌舰是黑色的,不会移动。在这 案例,显示消息:敌人击中了!在下面一行的绿色 巡逻船。如果敌舰离开屏幕或时间运行 out,显示消息:Enemy ship逃走了!在下面一行的红色 巡逻船。
以下是代码:
import java.awt.*;
public class Project2 {
public static final int PATROL_Y = 250;
public static final int PATROL_SIZE = 20;
public static int patrolX = 270;
public static final int ENEMY_Y = 20;
public static final int ENEMY_SIZE = 30;
public static int enemyX = 0;
public static final int RIGHT_ARROW = 39;
public static final int LEFT_ARROW = 37;
public static final int UP_ARROW = 38;
public static int patrolMissileX = 0;
public static int patrolMissileY = 0;
public static final int PATROL_MISSILE_LENGTH = 10;
//exe starts here
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(300, 300);
Graphics g = panel.getGraphics( );
g.drawString("Project 2 by Garrett Griffin", 10, 15);
startGame(panel, g);
}
//Creates a patrol ship at x = patrolX,y=PATROL_Y, with side PATROL_SIZE in the given color
public static void drawPatrol(Graphics g, Color c){
g.setColor(Color.GREEN);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
}
//moveEnemyShipAndDraw: draws the enemy ship first in white, then increment enemyX by 1, then draws the ship in red again.
public static void moveEnemyShipAndDraw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(enemyX, ENEMY_Y, ENEMY_SIZE, ENEMY_SIZE);
enemyX+=1;
g.setColor(Color.RED);
g.fillRect(enemyX, ENEMY_Y, ENEMY_SIZE, ENEMY_SIZE);
if(enemyX>300) {
g.setColor(Color.RED);
g.drawString("The enemy ship got away!", 10, 290);
}
}
//Handles user input: RIGHT_ARROW and LEFT_ARROW moves patrolship by 3 pixels left or right
public static void handleKeys(DrawingPanel panel, Graphics g) {
//Set panel.getKeyCode(); to a variable. Any variable.
int i = panel.getKeyCode();
//keep from going out of bounds on the right side
if (RIGHT_ARROW==i && patrolX>=280) {
g.setColor(Color.WHITE);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
patrolX-=3;
g.setColor(Color.GREEN);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
} else if (LEFT_ARROW==i && patrolX<=0) {
g.setColor(Color.WHITE);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
patrolX+=3;
g.setColor(Color.GREEN);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
//if RA, move right 3 pixels
} else if (RIGHT_ARROW==i) {
g.setColor(Color.WHITE);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
patrolX+=3;
g.setColor(Color.GREEN);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
//if LA, move left 3 pixels
} else if(LEFT_ARROW==i) {
g.setColor(Color.WHITE);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
patrolX-=3;
g.setColor(Color.GREEN);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
//if zero, do nothing
} else if(i==0) {
g.setColor(Color.GREEN);
g.fillRect(patrolX, PATROL_Y, PATROL_SIZE, PATROL_SIZE);
}
}
//Gets uparrow input from user to create missile. Do not draw anything if patrolMissileY is 0.
public static void movePatrolMissileAndDraw(DrawingPanel panel, Graphics g) {
int i = panel.getKeyCode();
if(UP_ARROW==i && patrolMissileY>0){
patrolMissileX=patrolX;
patrolMissileY=PATROL_Y;
g.setColor(Color.WHITE);
g.drawLine(patrolMissileX, patrolMissileY, PATROL_MISSILE_LENGTH, PATROL_MISSILE_LENGTH);
patrolMissileY+=5;
g.setColor(Color.BLACK);
g.drawLine(patrolMissileX, patrolMissileY, PATROL_MISSILE_LENGTH, PATROL_MISSILE_LENGTH);
}
}
public static void startGame(DrawingPanel panel, Graphics g) {
int x = 0;
int y = 270;
int deltaX = 1;
int deltaY = -3;
drawPatrol(g, Color.green);
for (int time = 0; time <= 1000; time++) {
moveEnemyShipAndDraw(g);
handleKeys(panel, g);
movePatrolMissileAndDraw(panel, g);
panel.sleep(50);
}
}
}
我正在使用的DrawingPanel类:
/*
Stuart Reges and Marty Stepp
February 24, 2007
Changes by Tom Bylander in 2010 (no anti-alias, repaint on sleep)
Changes by Tom Bylander in 2012 (track mouse clicks and movement)
Changes by Tom Bylander in 2013 (fix bug in tracking mouse clicks)
Changes by S. Robbins in 2014 (getters for width and height)
Changes by S. Robbins in 2014 (addKeyListener added)
Changes by S. Robbins in 2014 (catch exception on default close so that it works in an applet)
Changes by S. Robbins in 2015 (buffer key events)
Changes by S. Robbins in 2015 (show mouse status by default is off)
The DrawingPanel class provides a simple interface for drawing persistent
images using a Graphics object. An internal BufferedImage object is used
to keep track of what has been drawn. A client of the class simply
constructs a DrawingPanel of a particular size and then draws on it with
the Graphics object, setting the background color if they so choose.
To ensure that the image is always displayed, a timer calls repaint at
regular intervals.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;
public class DrawingPanel implements ActionListener {
private static final String versionMessage =
"Drawing Panel version 1.1, January 25, 2015";
private static final int DELAY = 100; // delay between repaints in millis
private static final boolean PRETTY = false; // true to anti-alias
private static boolean showStatus = false;
private static final int MAX_KEY_BUF_SIZE = 10;
private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position
private volatile MouseEvent click; // stores the last mouse click
private volatile boolean pressed; // true if the mouse is pressed
private volatile MouseEvent move; // stores the position of the mouse
private ArrayList<KeyInfo> keys;
// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
keys = new ArrayList<KeyInfo>();
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
statusBar = new JLabel(" ");
statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
statusBar.setText(versionMessage);
panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(width, height));
panel.add(new JLabel(new ImageIcon(image)));
click = null;
move = null;
pressed = false;
// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
pressed = false;
move = e;
if (showStatus)
statusBar.setText("moved (" + e.getX() + ", " + e.getY() + ")");
}
public void mousePressed(MouseEvent e) {
pressed = true;
move = e;
if (showStatus)
statusBar.setText("pressed (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseDragged(MouseEvent e) {
pressed = true;
move = e;
if (showStatus)
statusBar.setText("dragged (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseReleased(MouseEvent e) {
click = e;
pressed = false;
if (showStatus)
statusBar.setText("released (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseEntered(MouseEvent e) {
// System.out.println("mouse entered");
panel.requestFocus();
}
};
panel.addMouseListener(listener);
panel.addMouseMotionListener(listener);
new DrawingPanelKeyListener();
g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.BLACK);
if (PRETTY) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.1f));
}
frame = new JFrame("Drawing Panel");
frame.setResizable(false);
try {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so that this works in an applet
} catch (Exception e) {}
frame.getContentPane().add(panel);
frame.getContentPane().add(statusBar, "South");
frame.pack();
frame.setVisible(true);
toFront();
frame.requestFocus();
// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}
public void showMouseStatus(boolean f) {
showStatus = f;
}
public void addKeyListener(KeyListener listener) {
panel.addKeyListener(listener);
panel.requestFocus();
}
// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return g2;
}
// set the background color of the drawing panel
public void setBackground(Color c) {
panel.setBackground(c);
}
// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
panel.repaint();
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}
// close the drawing panel
public void close() {
frame.dispose();
}
// makes drawing panel become the frontmost window on the screen
public void toFront() {
frame.toFront();
}
// return panel width
public int getWidth() {
return width;
}
// return panel height
public int getHeight() {
return height;
}
// return the X position of the mouse or -1
public int getMouseX() {
if (move == null) {
return -1;
} else {
return move.getX();
}
}
// return the Y position of the mouse or -1
public int getMouseY() {
if (move == null) {
return -1;
} else {
return move.getY();
}
}
// return the X position of the last click or -1
public int getClickX() {
if (click == null) {
return -1;
} else {
return click.getX();
}
}
// return the Y position of the last click or -1
public int getClickY() {
if (click == null) {
return -1;
} else {
return click.getY();
}
}
// return true if a mouse button is pressed
public boolean mousePressed() {
return pressed;
}
public synchronized int getKeyCode() {
if (keys.size() == 0)
return 0;
return keys.remove(0).keyCode;
}
public synchronized char getKeyChar() {
if (keys.size() == 0)
return 0;
return keys.remove(0).keyChar;
}
public synchronized int getKeysSize() {
return keys.size();
}
private synchronized void insertKeyData(char c, int code) {
keys.add(new KeyInfo(c,code));
if (keys.size() > MAX_KEY_BUF_SIZE) {
keys.remove(0);
// System.out.println("Dropped key");
}
}
private class KeyInfo {
public int keyCode;
public char keyChar;
public KeyInfo(char keyChar, int keyCode) {
this.keyCode = keyCode;
this.keyChar = keyChar;
}
}
private class DrawingPanelKeyListener implements KeyListener {
int repeatCount = 0;
public DrawingPanelKeyListener() {
panel.addKeyListener(this);
panel.requestFocus();
}
public void keyPressed(KeyEvent event) {
// System.out.println("key pressed");
repeatCount++;
if ((repeatCount == 1) || (getKeysSize() < 2))
insertKeyData(event.getKeyChar(),event.getKeyCode());
}
public void keyTyped(KeyEvent event) {
}
public void keyReleased(KeyEvent event) {
repeatCount = 0;
}
}
}
感谢所有帮助的朋友。你们都非常宝贵。
答案 0 :(得分:0)
最初设置
public static int patrolMissileY = 0;
但在
public static void movePatrolMissileAndDraw(DrawingPanel panel, Graphics g)
你做了
if(UP_ARROW==i && patrolMissileY>0){
永远不会进入这里。