因此,对于我在Java课程中的最后一个,我们正在制作一个小行星游戏(除了官方版本的简单版本,因为时间不够)。
问题是当我们的船发射一个镜头(空格键)时我们添加(镜头,x,y),但是当我们再次点击空格键时它只需要拍摄并将其放回原始的x,y。所以现在我们只能在屏幕上一次开枪。我们希望能够发射多个镜头并让它们全部可见并且充实。
不知道如何做到这一点。欢迎任何帮助,谢谢。
P.S。如果需要,我将添加我们的代码。 P.S.S.很抱歉再次发布这个我以为我可以稍后编辑帖子,这就是为什么我没有包含代码但显然没有。
package week7Homework;
import acm.program.GraphicsProgram;
import java.util.*;
import java.awt.event.*;
import acm.graphics.*;
import java.awt.Color;
import java.awt.Image;
public class space8bit extends GraphicsProgram
{
/* Initialize everything that is needed */
final int WIN_HEIGHT = 800;
final int WIN_WIDTH = 1900;
GImage space = new GImage("/College/IT219/Week7/src/week7Homework/outerspace.png");
GImage ship = new GImage("/College/IT219/Week7/src/week7Homework/ship.png");
GImage explosion = new GImage("/College/IT219/Week7/src/week7Homework/explosion.png");
GImage [] ast = new GImage[6];
Random rand = new Random();
pewpew shots = new pewpew();
int shipx;
int shipy;
public void init()
{
setSize(WIN_WIDTH, WIN_HEIGHT);
add(space);
ship.setLocation(50,330);
ship.scale(.8);
add(ship);
addKeyListeners( );
//scale explosion
explosion.scale(.5);
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode( );
if (key == KeyEvent.VK_UP)
{ ship.move(0, -15); }
/*else if (key == KeyEvent.VK_SPACE)
{ xMove = MV_AMT; }*/
else if (key == KeyEvent.VK_DOWN)
{ ship.move(0, 15); }
else if (key == KeyEvent.VK_SPACE)
{shipx = (int)ship.getX()+195;
shipy = (int)ship.getY() + 80;
add(shots,shipx,shipy);
}
}
public void asteriods()
{
GImage ast1 = new GImage("/College/IT219/Week7/src/week7Homework/asteriod.png",1300,100);
GImage ast2 = new GImage("/College/IT219/Week7/src/week7Homework/asteriod.png",1750,250);
GImage ast3 = new GImage("/College/IT219/Week7/src/week7Homework/asteriod.png",1650,350);
GImage ast4 = new GImage("/College/IT219/Week7/src/week7Homework/asteriod.png",1550,650);
GImage ast5 = new GImage("/College/IT219/Week7/src/week7Homework/asteriod.png",1450,600);
GImage ast6 = new GImage("/College/IT219/Week7/src/week7Homework/asteriod.png",1350,750);
ast [0] = ast1;
ast [1] = ast2;
ast [2] = ast3;
ast [3] = ast4;
ast [4] = ast5;
ast [5] = ast6;
add(ast[0]);
add(ast[1]);
add(ast[2]);
add(ast[3]);
add(ast[4]);
add(ast[5]);
}
public void run()
{
asteriods();
while(true)
{
pause(120);
int random1x = rand.nextInt(-1+1+6)-9;
int random2x = rand.nextInt(-1+1+6)-15;
int random3x = rand.nextInt(-1+1+6)-25;
int random4x = rand.nextInt(-1+1+6)-16;
int random5x = rand.nextInt(-1+1+6)-8;
int random6x = rand.nextInt(-1+1+6)-13;
ast[0].move(random1x, 0);
ast[1].move(random2x, 0);
ast[2].move(random3x, 0);
ast[3].move(random4x, 0);
ast[4].move(random5x, 0);
ast[5].move(random6x, 0);
shots.move(50, 0);
for (int i = 0; i < ast.length; i++)
{
//integer that gets bounds of all rectangles and ovals in the arrays.
GRectangle asteroidBounds = ast[i].getBounds();
//check for collision with other objects
if (shots.getBounds().intersects(asteroidBounds))
{
int shotX = (int)shots.getX();
int shotY = (int)shots.getY();
pause(10);
remove(ast[i]);
add(explosion, shotX-100, shotY - 50);
remove(shots);
pause(25);
remove(explosion);
}
else if (ship.getY() <= 0)
{
ship.move(0, 15);
}
else if (ship.getY() >= WIN_HEIGHT - 168)
{
ship.move(0, -15);
}
}
}
}
}
这里也是一个示例代码,更短,但做同样的事情。只需按空格键,您就会看到我在说什么。
package week7Homework;
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
import java.awt.event.*;
public class collisionTry extends GraphicsProgram
{
final int WIN_WIDTH = 500;
final int WIN_HEIGHT = 500;
GOval ship = new GOval(100, 250, 50, 50);
GImage bullet = new GImage("/College/IT219/Week7/src/week7Homework/bullet.gif", 50, 50);
GImage bullet2;
boolean bulletFired;
public void init()
{
setSize(WIN_WIDTH, WIN_HEIGHT);
addKeyListeners();
}
public void run()
{
GRect rect = new GRect(400, 200, 50, 150);
add(rect);
rect.setColor(Color.BLACK);
rect.setFilled(true);
add(ship);
ship.setColor(Color.BLACK);
ship.setFilled(true);
while (true)
{
pause(50);
rect.move(-1, 0);
//bullet.move(5, 0);
if (bulletFired == true)
{
bullet.move(5, 0);
}
if (bullet.getBounds().intersects(rect.getBounds()))
{
remove(rect);
remove(bullet);
}
}
}//run
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE)
{
int shipX = (int)ship.getX();
int shipY = (int)ship.getY();
add(bullet, shipX+50, shipY+25);
bulletFired = true;
}
else if (key == KeyEvent.VK_UP){
ship.move(0, -5);
}
}
}
答案 0 :(得分:1)
这是因为你在整场比赛中只有1个子弹
if (bulletFired == true) {
bullet.move(5, 0);
}
你告诉一个子弹回到(5,0)
每次按空格键时,您需要找到一种制作新子弹对象的方法:
if (key == KeyEvent.VK_SPACE) {
int shipX = (int)ship.getX();
int shipY = (int)ship.getY();
Bullet firedBullet = new Bullet(/* params if any, x? y? */);
add(firedBullet, shipX+50, shipY+25);
bulletFired = true;
}
虽然这是我对自己如何做事的看法,但这可能不适用于你的游戏(我不知道你有什么课程,他们的角色等)。但希望你能看到我所得到的并适应它。
现在因为你有多个子弹,你还必须照顾他们的一生。你在游戏开始时发射了1000枚子弹,它们就会离开屏幕。你不想浪费时间在游戏的其他部分画出它们。因此,无论您使用什么列表/数组来跟踪要绘制的对象,您都必须定期查找并删除那些已经脱离屏幕的对象。