Okey 我对编程比较陌生(只完成了一个学期的编程),我正在尝试创建我的 Space Invaders 游戏版本 (http://www.aeonity.com/ab/games/arcade-classics/space-入侵者.php)。我不想将它编程为小程序,我使用 java 中的 StdDraw 库来绘制所有内容。
现在我已经完成了所有的画,但是我的“宇宙飞船/敌人”的运动有问题我已经想出了如何移动一个敌人但不能让他们一次移动。
我认为我创建敌人的功能不是那么好,因为我必须自己编写每 48 个敌人的位置……我将让我的代码跟随,以便您看一看。提前致谢。
/** Draw Enemies*/
public class Enemy{
//yr and xr positions for rectangle
private double x1, y1, r1, w, h, xr, yr;
private double x2, y2, r2, x3, y3, r3;
public Enemy(double x, double y, double r, double width, double height, double xRec, double yRec,
double xSec, double ySec, double rSec, double xThird, double yThird, double rThird)
{x1 = x; y1 = y; r1 = r; w = width; h = height; xr = xRec; yr = yRec;
x2 = xSec; y2 = ySec; r2 = rSec; x3 = xThird; y3 = yThird; r3 = rThird;}
public void DRAW_ENEMY(){
StdDraw.setPenColor(StdDraw.PINK);
StdDraw.filledCircle(x1, y1, r1);
StdDraw.filledCircle(x2, y2, r2);
StdDraw.filledCircle(x3, y3, r3);
StdDraw.filledRectangle(xr, yr, w, h);
}
public void MOVE_ENEMY(Enemy e11, Enemy e12) {
double vx = 0.005;
while(true){
if(e11.x2 > e12.x2 || e11.x1 < 0.004) {
vx = -vx;
}
//Update position
e11.x1 = e11.x1 + vx;
e11.x2 = e11.x2 + vx;
e11.x3 = e11.x3 + vx;
e11.xr = e11.xr + vx;
// clear the background
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledRectangle(0.5, 0.67, 0.6,0.18 );
// draw ball on the screen
StdDraw.setPenColor(StdDraw.PINK);
e11.DRAW_ENEMY();
// display and pause for 20 ms
StdDraw.show(80);
}
}
//Test client
public static void main(String[] args){
// create every object
Enemy e11 = new Enemy(0.10, 0.84, 0.01, 0.007, 0.022, 0.112, 0.82, 0.124, 0.84, 0.01, 0.112, 0.8, 0.009);
e11.DRAW_ENEMY();
Enemy e12 = new Enemy(0.17, 0.84, 0.01, 0.007, 0.022, 0.182, 0.82, 0.194, 0.84, 0.01, 0.182, 0.8, 0.009);
e12.DRAW_ENEMY();
Enemy e13 = new Enemy(0.24, 0.84, 0.01, 0.007, 0.022, 0.252, 0.82, 0.264, 0.84, 0.01, 0.252, 0.8, 0.009);
e13.DRAW_ENEMY();
Enemy e14 = new Enemy(0.31, 0.84, 0.01, 0.007, 0.022, 0.322, 0.82, 0.334, 0.84, 0.01, 0.322, 0.8, 0.009);
e14.DRAW_ENEMY();
Enemy e15 = new Enemy(0.38, 0.84, 0.01, 0.007, 0.022, 0.392, 0.82, 0.404, 0.84, 0.01, 0.392, 0.8, 0.009);
e15.DRAW_ENEMY();
Enemy e16 = new Enemy(0.45, 0.84, 0.01, 0.007, 0.022, 0.462, 0.82, 0.474, 0.84, 0.01, 0.462, 0.8, 0.009);
e16.DRAW_ENEMY();
Enemy e17 = new Enemy(0.52, 0.84, 0.01, 0.007, 0.022, 0.532, 0.82, 0.544, 0.84, 0.01, 0.532, 0.8, 0.009);
e17.DRAW_ENEMY();
Enemy e18 = new Enemy(0.59, 0.84, 0.01, 0.007, 0.022, 0.602, 0.82, 0.614, 0.84, 0.01, 0.602, 0.8, 0.009);
e18.DRAW_ENEMY();
Enemy e19 = new Enemy(0.66, 0.84, 0.01, 0.007, 0.022, 0.672, 0.82, 0.684, 0.84, 0.01, 0.672, 0.8, 0.009);
e19.DRAW_ENEMY();
Enemy e110 = new Enemy(0.73, 0.84, 0.01, 0.007, 0.022, 0.742, 0.82, 0.754, 0.84, 0.01, 0.742, 0.8, 0.009);
e110.DRAW_ENEMY();
Enemy e111 = new Enemy(0.80, 0.84, 0.01, 0.007, 0.022, 0.812, 0.82, 0.824, 0.84, 0.01, 0.812, 0.8, 0.009);
e111.DRAW_ENEMY();
Enemy e112 = new Enemy(0.87, 0.84, 0.01, 0.007, 0.022, 0.882, 0.82, 0.894, 0.84, 0.01, 0.882, 0.8, 0.009);
e112.DRAW_ENEMY();
Enemy e21 = new Enemy(0.10, 0.74, 0.01, 0.007, 0.022, 0.112, 0.72, 0.124, 0.74, 0.01, 0.112, 0.7, 0.009);
e21.DRAW_ENEMY();
Enemy e22 = new Enemy(0.17, 0.74, 0.01, 0.007, 0.022, 0.182, 0.72, 0.194, 0.74, 0.01, 0.182, 0.7, 0.009);
e22.DRAW_ENEMY();
Enemy e23 = new Enemy(0.24, 0.74, 0.01, 0.007, 0.022, 0.252, 0.72, 0.264, 0.74, 0.01, 0.252, 0.7, 0.009);
e23.DRAW_ENEMY();
Enemy e24 = new Enemy(0.31, 0.74, 0.01, 0.007, 0.022, 0.322, 0.72, 0.334, 0.74, 0.01, 0.322, 0.7, 0.009);
e24.DRAW_ENEMY();
Enemy e25 = new Enemy(0.38, 0.74, 0.01, 0.007, 0.022, 0.392, 0.72, 0.404, 0.74, 0.01, 0.392, 0.7, 0.009);
e25.DRAW_ENEMY();
Enemy e26 = new Enemy(0.45, 0.74, 0.01, 0.007, 0.022, 0.462, 0.72, 0.474, 0.74, 0.01, 0.462, 0.7, 0.009);
e26.DRAW_ENEMY();
Enemy e27 = new Enemy(0.52, 0.74, 0.01, 0.007, 0.022, 0.532, 0.72, 0.544, 0.74, 0.01, 0.532, 0.7, 0.009);
e27.DRAW_ENEMY();
Enemy e28 = new Enemy(0.59, 0.74, 0.01, 0.007, 0.022, 0.602, 0.72, 0.614, 0.74, 0.01, 0.602, 0.7, 0.009);
e28.DRAW_ENEMY();
Enemy e29 = new Enemy(0.66, 0.74, 0.01, 0.007, 0.022, 0.672, 0.72, 0.684, 0.74, 0.01, 0.672, 0.7, 0.009);
e29.DRAW_ENEMY();
Enemy e210 = new Enemy(0.73, 0.74, 0.01, 0.007, 0.022, 0.742, 0.72, 0.754, 0.74, 0.01, 0.742, 0.7, 0.009);
e210.DRAW_ENEMY();
Enemy e211 = new Enemy(0.80, 0.74, 0.01, 0.007, 0.022, 0.812, 0.72, 0.824, 0.74, 0.01, 0.812, 0.7, 0.009);
e211.DRAW_ENEMY();
Enemy e212 = new Enemy(0.87, 0.74, 0.01, 0.007, 0.022, 0.882, 0.72, 0.894, 0.74, 0.01, 0.882, 0.7, 0.009);
e212.DRAW_ENEMY();
Enemy e31 = new Enemy(0.10, 0.64, 0.01, 0.007, 0.022, 0.112, 0.62, 0.124, 0.64, 0.01, 0.112, 0.6, 0.009);
e31.DRAW_ENEMY();
Enemy e32 = new Enemy(0.17, 0.64, 0.01, 0.007, 0.022, 0.182, 0.62, 0.194, 0.64, 0.01, 0.182, 0.6, 0.009);
e32.DRAW_ENEMY();
Enemy e33 = new Enemy(0.24, 0.64, 0.01, 0.007, 0.022, 0.252, 0.62, 0.264, 0.64, 0.01, 0.252, 0.6, 0.009);
e33.DRAW_ENEMY();
Enemy e34 = new Enemy(0.31, 0.64, 0.01, 0.007, 0.022, 0.322, 0.62, 0.334, 0.64, 0.01, 0.322, 0.6, 0.009);
e34.DRAW_ENEMY();
Enemy e35 = new Enemy(0.38, 0.64, 0.01, 0.007, 0.022, 0.392, 0.62, 0.404, 0.64, 0.01, 0.392, 0.6, 0.009);
e35.DRAW_ENEMY();
Enemy e36 = new Enemy(0.45, 0.64, 0.01, 0.007, 0.022, 0.462, 0.62, 0.474, 0.64, 0.01, 0.462, 0.6, 0.009);
e36.DRAW_ENEMY();
Enemy e37 = new Enemy(0.52, 0.64, 0.01, 0.007, 0.022, 0.532, 0.62, 0.544, 0.64, 0.01, 0.532, 0.6, 0.009);
e37.DRAW_ENEMY();
Enemy e38 = new Enemy(0.59, 0.64, 0.01, 0.007, 0.022, 0.602, 0.62, 0.614, 0.64, 0.01, 0.602, 0.6, 0.009);
e38.DRAW_ENEMY();
Enemy e39 = new Enemy(0.66, 0.64, 0.01, 0.007, 0.022, 0.672, 0.62, 0.684, 0.64, 0.01, 0.672, 0.6, 0.009);
e39.DRAW_ENEMY();
Enemy e310 = new Enemy(0.73, 0.64, 0.01, 0.007, 0.022, 0.742, 0.62, 0.754, 0.64, 0.01, 0.742, 0.6, 0.009);
e310.DRAW_ENEMY();
Enemy e311 = new Enemy(0.80, 0.64, 0.01, 0.007, 0.022, 0.812, 0.62, 0.824, 0.64, 0.01, 0.812, 0.6, 0.009);
e311.DRAW_ENEMY();
Enemy e312 = new Enemy(0.87, 0.64, 0.01, 0.007, 0.022, 0.882, 0.62, 0.894, 0.64, 0.01, 0.882, 0.6, 0.009);
e312.DRAW_ENEMY();
Enemy e41 = new Enemy(0.10, 0.54, 0.01, 0.007, 0.022, 0.112, 0.52, 0.124, 0.54, 0.01, 0.112, 0.5, 0.009);
e41.DRAW_ENEMY();
Enemy e42 = new Enemy(0.17, 0.54, 0.01, 0.007, 0.022, 0.182, 0.52, 0.194, 0.54, 0.01, 0.182, 0.5, 0.009);
e42.DRAW_ENEMY();
Enemy e43 = new Enemy(0.24, 0.54, 0.01, 0.007, 0.022, 0.252, 0.52, 0.264, 0.54, 0.01, 0.252, 0.5, 0.009);
e43.DRAW_ENEMY();
Enemy e44 = new Enemy(0.31, 0.54, 0.01, 0.007, 0.022, 0.322, 0.52, 0.334, 0.54, 0.01, 0.322, 0.5, 0.009);
e44.DRAW_ENEMY();
Enemy e45 = new Enemy(0.38, 0.54, 0.01, 0.007, 0.022, 0.392, 0.52, 0.404, 0.54, 0.01, 0.392, 0.5, 0.009);
e45.DRAW_ENEMY();
Enemy e46 = new Enemy(0.45, 0.54, 0.01, 0.007, 0.022, 0.462, 0.52, 0.474, 0.54, 0.01, 0.462, 0.5, 0.009);
e46.DRAW_ENEMY();
Enemy e47 = new Enemy(0.52, 0.54, 0.01, 0.007, 0.022, 0.532, 0.52, 0.544, 0.54, 0.01, 0.532, 0.5, 0.009);
e47.DRAW_ENEMY();
Enemy e48 = new Enemy(0.59, 0.54, 0.01, 0.007, 0.022, 0.602, 0.52, 0.614, 0.54, 0.01, 0.602, 0.5, 0.009);
e48.DRAW_ENEMY();
Enemy e49 = new Enemy(0.66, 0.54, 0.01, 0.007, 0.022, 0.672, 0.52, 0.684, 0.54, 0.01, 0.672, 0.5, 0.009);
e49.DRAW_ENEMY();
Enemy e410 = new Enemy(0.73, 0.54, 0.01, 0.007, 0.022, 0.742, 0.52, 0.754, 0.54, 0.01, 0.742, 0.5, 0.009);
e410.DRAW_ENEMY();
Enemy e411 = new Enemy(0.80, 0.54, 0.01, 0.007, 0.022, 0.812, 0.52, 0.824, 0.54, 0.01, 0.812, 0.5, 0.009);
e411.DRAW_ENEMY();
Enemy e412 = new Enemy(0.87, 0.54, 0.01, 0.007, 0.022, 0.882, 0.52, 0.894, 0.54, 0.01, 0.882, 0.5, 0.009);
e412.DRAW_ENEMY();
e11.MOVE_ENEMY(e11,e12);
}
}