对于这个问题,我事先表示歉意,可能需要付出一些努力才能解决。这是一个我已经从事一段时间的小组项目,因此脚本有点复杂,我和我正在使用的另外两个脚本都是Java的初学者。不幸的是,我的问题是我不知道问题是什么,所以我的问题有点含糊。我会尽力向您展示代码的相关部分,任何表示问题的帮助将不胜感激:)
SpriteAlien此类创建外星人,并具有定义外星人运动的方法。
import javax.swing.JPanel;
public class SpriteAlien extends JPanel{
public int hPosAlien; // alien's horizontal coordinate
public int vPosAlien; // alien's vertical coordinate
public int hPosBullet, vPosBullet;
public int life; // aliens life, some may have more than one
int dx = 1; // number of pixels the alien moves horizontally every timer cycle
int dy = 10; // number of pixels the alien moves down when it hits the boundary
boolean direction = true; // false - left, true - right
boolean shoot, defeated = false;
// constructor
public SpriteAlien (int horizontalPos, int verticalPos){
hPosAlien = horizontalPos;
vPosAlien = verticalPos;
}
public void moveAlien()
{
//This creates the alien's movement
// if the alien has not reached the right wall and is moving right
if ((hPosAlien <= 740) && (direction == true)) {
moveRight();
}
// if the alien has reached the right wall and is moving right
else if ((hPosAlien >= 740) && (direction == true)) {
moveDown();
moveLeft();
direction = false;
}
// if the alien has not reached the right wall and is moving left
else if ((hPosAlien <= 740) && (hPosAlien >= 0) && (direction == false)) {
moveLeft();
}
// if the alien has reached the left wall and is moving left
else if ((hPosAlien < 0) && (direction == false)) {
moveDown();
moveRight();
direction = true;
}
}
// methods used for alien movement
void moveLeft() {
hPosAlien -= dx;
}
void moveRight() {
hPosAlien += dx;
}
void moveDown() {
vPosAlien += dy;
}
}
此类SpriteArray创建了一系列外星人,它们像队形一样一起移动
//创建一系列外星人以组成我们的外星人军队
公共类SpriteArray扩展了Random {
SpriteAlien[] alienArmy = new SpriteAlien[10]; // an array of alien objects
int index; // index of the array
int x, y; // coordinates of the alien
// constructor
SpriteArray(){
// nested loop to position aliens like 2 dimensional arrays on the screen
for(int i = 0; i < 2; i++){
for(int j = 0; j < 5; j++)
{
index = i*5 + j; // (0*1 + 0) (0*1 + 1) etc
x = j*120 + 100; // each column is 120 pixels apart
y = i*80 + 100; // each row is 80 pixels apart
alienArmy[index] = new SpriteAlien(x, y);
}
}
}
这是涉及到的主要类Sprite,也是最有可能发生错误的类,因为这是我编写了发射子弹并应该使外星人被摧毁的代码的地方。我知道这段代码很烦人,所以为了使其更容易,我在最相关的位置:P之前加了“ LOOK HERE LOOK HERE”作为注释。
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.util.TimerTask;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Sprite extends JPanel implements ActionListener{
// GAME ANIMATION
public int hPosShooter; // shooter's horizontal coordinate
public int vPosShooter; // shooter's vertical coordinate
public int hPosBullet; // bullet's horizontal coordinate
public int vPosBullet = 490; // bullet's vertical coordinate
public int movementSpeed; // shooter's speed(describe shooter movement)
public Timer loopTime = new Timer(9, this); // used to loop through movements and repaint
public Timer generateBullet;
public boolean started = false; // whether or not the game has started
public boolean fired = false; // whether or not a shot should exist
public int numberEnemies = 1; // with more enemies this will need to be edited
public boolean[] destroyed = new boolean[10]; //Array of boolean variables that tell whether or not the alien with the matching index has been destroyed
public int shootingIndex;
// display text information
PlayerInfo pInfo = new PlayerInfo();
JLabel scoreLabel = new JLabel("score: " + Integer.toString(pInfo.getPlayerScore()));
JLabel lifeLabel = new JLabel("lives: " + Integer.toString(pInfo.getPlayerLives()));
// LOAD IMAGES
String imageNameShooter; //The shooter
String imageNameAlien; //The alien
String imageNameBarrier; //The barrier
protected BufferedImage imageShooter;
protected BufferedImage imageAlien;
protected BufferedImage imageBarrier;
SpriteArray enemies = new SpriteArray();
// SpriteBarrier barrier1 = new SpriteBarrier(400, 400);
SpriteBarrierArray barrier1 = new SpriteBarrierArray(160, 360);
SpriteBarrierArray barrier2 = new SpriteBarrierArray(360, 360);
SpriteBarrierArray barrier3 = new SpriteBarrierArray(560, 360);
// int shootingIndex = enemies.AlienShoot();
public Sprite (String imageNameShooter, String imageNameAlien, String imageNameBarrier){
try {
imageShooter = ImageIO.read(new File(imageNameShooter));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
imageAlien = ImageIO.read(new File(imageNameAlien));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
imageBarrier = ImageIO.read(new File(imageNameBarrier));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
scoreLabel.setHorizontalTextPosition(JLabel.RIGHT);
lifeLabel.setHorizontalTextPosition(JLabel.LEFT);
add(scoreLabel);
add(lifeLabel);
}
public static void SpriteImage(JFrame frame, JPanel panel) throws IOException
{
frame.add(panel);
}
// PAINT SPRITE
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(imageShooter, hPosShooter, vPosShooter, null); //Draws the shooter in the correct position
//Draws bullet
if (fired)
{
g.fillRect (hPosBullet, vPosBullet, 8, 12);
}
//Draws the aliens
for(int i = 0; i < 10 ; i++ ){
g.drawImage(imageAlien, enemies.alienArmy[i].hPosAlien, enemies.alienArmy[i].vPosAlien, null);
if(destroyed[i] == false)
{
g.fillRect (enemies.alienArmy[i].hPosBullet, enemies.alienArmy[i].vPosBullet, 8, 12);
}
}
//Draw the Barriers
for(int i = 0; i < 12 ; i++ ){
g.drawImage(imageBarrier, barrier1.barrier[i].hPosBarrier, barrier1.barrier[i].vPosBarrier, null);
g.drawImage(imageBarrier, barrier2.barrier[i].hPosBarrier, barrier2.barrier[i].vPosBarrier, null);
g.drawImage(imageBarrier, barrier3.barrier[i].hPosBarrier, barrier3.barrier[i].vPosBarrier, null);
}
loopTime.start(); // refreshes the image to make animation work
}
//Makes the enemies drop missiles
void enemyAttack() {
generateBullet = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e){
shootingIndex = enemies.AlienShoot();
}
});
generateBullet.start();
}
//SHOOTER MOVEMENT MECHANICS
public void actionPerformed(ActionEvent e)
{
//Conditional only allows movement once game has started
if (started)
{
//This only allows movement to the right if the right border hasn't been reached
if (hPosShooter < 733 && movementSpeed > 0)
{
hPosShooter = hPosShooter + movementSpeed;
}
//This only allows movement to the left if the left border hasn't been reached
if (hPosShooter > 0 && movementSpeed < 0)
{
hPosShooter = hPosShooter + movementSpeed;
}
}
//FIRE!! LOOK HERE LOOK HERE LOOK HERE!!!!!
if (fired)
{
vPosBullet = vPosBullet - 6; //Bullet moves upwards
//If alien is hit... it (SHOULD) be destroyed!
for(int i = 0; i < 10 ; i++ ){
if (!(destroyed[i]))
{
if (vPosBullet == (enemies.alienArmy[i].vPosAlien + 20) && hPosBullet > enemies.alienArmy[i].hPosAlien && hPosBullet < (enemies.alienArmy[i].hPosAlien + 50))
{
destroyed[i] = true;
repaint();
fired = false;
vPosBullet = 490;
}
}
}
for(int i=0; i<12; i++)
{
if (vPosBullet == barrier3.barrier[i].vPosBarrier && hPosBullet > barrier3.barrier[i].hPosBarrier && hPosBullet < (barrier3.barrier[i].hPosBarrier + 20))
{
fired = false;
vPosBullet = 490;
}
if (vPosBullet == barrier2.barrier[i].vPosBarrier && hPosBullet > barrier2.barrier[i].hPosBarrier && hPosBullet < (barrier2.barrier[i].hPosBarrier + 20))
{
fired = false;
vPosBullet = 490;
}
if (vPosBullet == barrier1.barrier[i].vPosBarrier && hPosBullet > barrier1.barrier[i].hPosBarrier && hPosBullet < (barrier1.barrier[i].hPosBarrier + 20))
{
fired = false;
vPosBullet = 490;
}
}
}
//OKAY THE IMPORTANT BIT'S OVER NOW!!!
//When bullet reaches top of screen 'fired' = false
if (vPosBullet < 0)
{
fired = false;
vPosBullet = 490;
}
//This bit of makes the aliens shoot missiles at the user's 'shooter'
for(int j = 0; j < 10 ; j++ ){
enemies.alienArmy[j].moveAlien();
if(enemies.alienArmy[j].shoot == true)
{
if(enemies.alienArmy[j].vPosBullet > 600)
{
enemies.alienArmy[j].shoot = false;
}
else
{
enemies.alienArmy[j].vPosBullet += 2;
}
}
}
//The position is then reset
repaint();
}
}
非常感谢您提供的任何帮助!
最佳答案
我不确定
if (vPosBullet == (enemies.alienArmy[i].vPosAlien + 20) && hPosBullet > enemies.alienArmy[i].hPosAlien && hPosBullet < (enemies.alienArmy[i].hPosAlien + 50))
在第一次测试中
vPosBullet == (enemies.alienArmy[i].vPosAlien + 20)
我不确定这个坐标是否准确,应该小于或大于。
因为这:
vPosBullet = vPosBullet - 6; //Bullet moves upwards
也许不是解决方案,但是您的代码太长了; P
编辑:
正如@ user3659404在评论中所说,将if语句更新为:
if (vPosBullet < (enemies.alienArmy[i].vPosAlien + 20) && vPosBullet > enemies.alienArmy[i].vPosAlien && hPosBullet > enemies.alienArmy[i].hPosAlien && hPosBullet < (enemies.alienArmy[i].hPosAlien + 50))
关于java - 编写太空入侵者代码,无法获得子弹来摧毁外星人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29935159/