gpt4 book ai didi

java - 减少 Android 游戏中的重复次数

转载 作者:行者123 更新时间:2023-12-01 18:45:26 25 4
gpt4 key购买 nike

我一直在为 Android 编写一款像《太空入侵者》这样的游戏,我有点担心我的代码中会有多少重复。游戏中将会有 45 个敌人,每个敌人都有命中框和射击功能。我从一开始就想我应该创造一系列使用几种方法的敌人,但从未创造过。我怎样才能减少这方面的重复呢?或者,我应该继续走这条路吗?

我不会询问所有的答案 - 那就没意思了。我只需要有关如何解决主要问题的想法。

通过下面的代码可以明确问题:

import android.graphics.Rect;
import java.util.ArrayList;

public class Enemy
{
private int maxHealth, currentHealth, power, speedX, speedY, centerX, centerY;
private Background bg = GameScreen.getBg1();
private Ship ship = GameScreen.getShip();
public static Rect xguy1Rect = new Rect(0,0,0,0);
public static Rect xguy2Rect = new Rect(0,0,0,0);
public static Rect xguy3Rect = new Rect(0,0,0,0);
public static Rect xguy4Rect = new Rect(0,0,0,0);
public static Rect xguy5Rect = new Rect(0,0,0,0);
public static Rect xguy6Rect = new Rect(0,0,0,0);
public static Rect xguy7Rect = new Rect(0,0,0,0);

private boolean isMovingRight = true;
private boolean isMovingLeft = false;
private boolean xguy1IsShooting = false;
private boolean xguy2IsShooting = false;
private boolean xguy3IsShooting = false;

private ArrayList<EnemyProjectile> eProjectiles = new ArrayList<EnemyProjectile>();

//Behavioral Methods
public void update() {
//centerX += speedY;
//moveRight();
//moveLeft();
//changeMovement();
autoFire();
speedY = bg.getSpeedY();
speedX = 1;
//setBounds???
xguy1Rect.set(GameScreen.xguy1.getCenterX() +22, GameScreen.xguy1.getCenterY()-1 , GameScreen.xguy1.getCenterX() + 4+22, GameScreen.xguy1.getCenterY() );
xguy2Rect.set(GameScreen.xguy2.getCenterX()+22, GameScreen.xguy2.getCenterY() -1, GameScreen.xguy2.getCenterX() +4+22, GameScreen.xguy2.getCenterY() );
xguy3Rect.set(GameScreen.xguy3.getCenterX() +22 , GameScreen.xguy3.getCenterY()-1, GameScreen.xguy3.getCenterX() +4+22, GameScreen.xguy3.getCenterY() );
xguy4Rect.set(GameScreen.xguy4.getCenterX() +22, GameScreen.xguy4.getCenterY()-1 , GameScreen.xguy4.getCenterX() + 4+22, GameScreen.xguy4.getCenterY() );
xguy5Rect.set(GameScreen.xguy5.getCenterX()+22, GameScreen.xguy5.getCenterY() -1, GameScreen.xguy5.getCenterX() +4+22, GameScreen.xguy5.getCenterY() );
xguy6Rect.set(GameScreen.xguy6.getCenterX() +22 , GameScreen.xguy6.getCenterY()-1, GameScreen.xguy6.getCenterX() +4+22, GameScreen.xguy6.getCenterY() );
xguy7Rect.set(GameScreen.xguy7.getCenterX() +22 , GameScreen.xguy7.getCenterY()-1, GameScreen.xguy7.getCenterX() +4+22, GameScreen.xguy7.getCenterY() );
}

public void autoFire()
{
int num = 1 + (int)(Math.random() * ((250 - 1) + 1));
//System.out.println(num);
if(num == 4 || num == 6 || num == 8 && xguy1IsShooting == false)
{
if(GameScreen.xguy1IsAlive == true)
{
xguy1Attack();
}
}
if(num == 1 || num == 3 || num == 5 && xguy2IsShooting == false)
{
if(GameScreen.xguy2IsAlive == true)
{
xguy2Attack();
}
}
if(num == 12 || num == 15 || num == 17 && xguy3IsShooting == false)
{
if(GameScreen.xguy3IsAlive == true)
{
xguy3Attack();
}
}
}

public void moveRight()
{
if(isMovingRight == true)
{
centerX += speedX;
if(centerX >= 630)
{
isMovingRight = false;
isMovingLeft = true;
}
}
}
public void moveLeft()
{
if(isMovingLeft == true)
{
centerX -= speedX;
if(centerX <= 10)
{
isMovingLeft = false;
isMovingRight = true;
}
}
}
public void changeMovement()
{
//causes delayed death - xguys only die after going right
if(centerX >= 630)
{
isMovingRight = false;
}
if(isMovingRight == false)
{
isMovingLeft = true;
}
}
public void die()
{

}
public void xguy1Attack()
{
EnemyProjectile e = new EnemyProjectile(GameScreen.xguy1.getCenterX()-6, GameScreen.xguy1.getCenterY());
eProjectiles.add(e);
xguy1IsShooting = false;
}
public void xguy2Attack()
{
EnemyProjectile e = new EnemyProjectile(GameScreen.xguy2.getCenterX()-6, GameScreen.xguy2.getCenterY());
eProjectiles.add(e);
xguy2IsShooting = false;
}

public void xguy3Attack()
{
EnemyProjectile e = new EnemyProjectile(GameScreen.xguy3.getCenterX()-6, GameScreen.xguy3.getCenterY() );
eProjectiles.add(e);
xguy3IsShooting = false;
}

public int getMaxHealth() {
return maxHealth;
}

public int getCurrentHealth() {
return currentHealth;
}

public int getPower() {
return power;
}

public int getSpeedY() {
return speedY;
}

public int getCenterX() {
return centerX;
}

public int getCenterY() {
return centerY;
}

public Background getBg() {
return bg;
}

public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}

public void setCurrentHealth(int currentHealth) {
this.currentHealth = currentHealth;
}

public void setPower(int power) {
this.power = power;
}

public void setSpeedX(int speedX) {
this.speedY = speedX;
}

public void setCenterX(int centerX) {
this.centerX = centerX;
}

public void setCenterY(int centerY) {
this.centerY = centerY;
}

public void setBg(Background bg) {
this.bg = bg;
}

public ArrayList getEProjectiles() {
return eProjectiles;
}
}

...................................................... ...................

import android.graphics.Rect;

public class Projectile {

private int x, y, speedY;
private boolean visible;

private Rect r;

public Projectile(int startX, int startY)
{
x = startX;
y = startY;
speedY = 7;
visible = true;

r = new Rect(0,0,0,0);
}

public void update()
{
y -= speedY;
r.set(x, y, x + 4, y + 10);
if (y < -10) {
visible = false;
r=null;
}
if (visible)
{
checkCollision();
}
}

private void checkCollision()
{
if(Rect.intersects(r, Enemy.xguy1Rect) && GameScreen.xguy1IsAlive == true)
{
visible = false;
GameScreen.score += 10;
GameScreen.xguy1IsAlive = false;
}
else if(Rect.intersects(r, Enemy.xguy2Rect) && GameScreen.xguy2IsAlive == true)
{
visible = false;
GameScreen.score += 10;
GameScreen.xguy2IsAlive = false;
}
else if(Rect.intersects(r, Enemy.xguy3Rect) && GameScreen.xguy3IsAlive == true)
{
visible = false;
GameScreen.score += 10;
GameScreen.xguy3IsAlive = false;
}
else if(Rect.intersects(r, Enemy.xguy4Rect) && GameScreen.xguy4IsAlive == true)
{
visible = false;
GameScreen.score += 10;
GameScreen.xguy4IsAlive = false;
}
else if(Rect.intersects(r, Enemy.xguy5Rect) && GameScreen.xguy5IsAlive == true)
{
visible = false;
GameScreen.score += 10;
GameScreen.xguy5IsAlive = false;
}
else if(Rect.intersects(r, Enemy.xguy6Rect) && GameScreen.xguy6IsAlive == true)
{
visible = false;
GameScreen.score += 10;
GameScreen.xguy6IsAlive = false;
}
else if(Rect.intersects(r, Enemy.xguy7Rect) && GameScreen.xguy7IsAlive == true)
{
visible = false;
GameScreen.score += 10;
GameScreen.xguy7IsAlive = false;
}

if(GameScreen.xguy1IsAlive == false && GameScreen.xguy2IsAlive == false
&& GameScreen.xguy3IsAlive == false)
{
GameScreen.allEnemiesAreDead = true;
}

// if(r.intersect(GameScreen.saucer.sRect))
// {
// visible = false;
// GameScreen.score += 100;
// GameScreen.saucerIsAlive = false;
// System.out.println("you hit the alien!");
// }

}


public int getX() {
return x;
}

public int getY() {
return y;
}

public int getSpeedY() {
return speedY;
}

public boolean isVisible() {
return visible;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public void setSpeedY(int speedY) {
this.speedY = speedY;
}

public void setVisible(boolean visible) {
this.visible = visible;
}

}

我认为如果我继续走这条路并分别初始化、设置和检查 28 个矩形上的碰撞,效率会非常低。另外,创建28个拍摄功能也会很麻烦。

游戏的主类中也存在重复。句点线分隔 block 。以下是 fragment :

public static BasicEnemy xguy1, xguy2, xguy3, xguy4, xguy5, xguy6, xguy7;
...........................................................................
static boolean xguy1IsAlive = true;
static boolean xguy2IsAlive = true;
static boolean xguy3IsAlive = true;
static boolean xguy4IsAlive = true;
static boolean xguy5IsAlive = true;
static boolean xguy6IsAlive = true;
static boolean xguy7IsAlive = true;
...........................................................................
xguy1 = new BasicEnemy(420, 100);
xguy2 = new BasicEnemy(480, 100);
xguy3 = new BasicEnemy(360, 100);
xguy4 = new BasicEnemy(300, 100);
xguy5 = new BasicEnemy(240, 100);
xguy6 = new BasicEnemy(540, 100);
xguy7 = new BasicEnemy(600, 100);
...........................................................................
xguy1.update();
xguy2.update();
xguy3.update();
xguy4.update();
xguy5.update();
xguy6.update();
xguy7.update();
..........................................................................
if(xguy1IsAlive == true)
{
g.drawImage(xanim.getImage(), xguy1.getCenterX()-16, xguy1.getCenterY()-12);
}
if(xguy2IsAlive == true)
{
g.drawImage(xanim.getImage(), xguy2.getCenterX()-16, xguy2.getCenterY()-12);
}
if(xguy3IsAlive == true)
{
g.drawImage(xanim.getImage(), xguy3.getCenterX()-16, xguy3.getCenterY()-12);
}
if(xguy4IsAlive == true)
{
g.drawImage(xanim.getImage(), xguy4.getCenterX()-16, xguy4.getCenterY()-12);
}
if(xguy5IsAlive == true)
{
g.drawImage(xanim.getImage(), xguy5.getCenterX()-16, xguy5.getCenterY()-12);
}
if(xguy6IsAlive == true)
{
g.drawImage(xanim.getImage(), xguy6.getCenterX()-16, xguy6.getCenterY()-12);
}
if(xguy7IsAlive == true)
{
g.drawImage(xanim.getImage(), xguy7.getCenterX()-16, xguy7.getCenterY()-12);
}
...............................................................................
xguy1 = null;
xguy2 = null;
xguy3 = null;
xguy4 = null;
xguy5 = null;
xguy6 = null;
xguy7 = null;

尝试使用数组来减少重复(所有代码都属于主类):(对于第三种敌人)

//make an array of 9 'piguys' that take act as basic enemies
public static BasicEnemy piguys[] = new BasicEnemy[8];

//create and set the positions of each piguy
for(int i = 0; i < 9; i ++)
{
piguys[num] = new BasicEnemy(180 + num*55, 200);
num +=1;
}
//short way of calling the update method in the Enemy class for each piguy
for(int x = 0; x < 9; x++)
{
piguys[num2].update();
num2 +=1;
}
//short way to draw each piguy to the screen
for(int f = 0; f < 9; f++)
{
g.drawImage(panim.getImage(), piguys[num3].getCenterX()-12, piguys[num3].getCenterY()-12);
num3 += 1;
}

//short way of nullifying each object so they can be created in the constructor
for(int s = 0; s < 9; s++)
{
piguys[num3] = null;
num +=1;
}

最佳答案

你想得对。您需要使用某种集合对象(如数组或列表)来减少代码中的重复,我对您寻求解决方案表示赞赏 code smell 。最终,当您继续开发游戏时,您可能会需要更好的方法来配置事情,就像您现在在代码中所做的那样。一种常见的解决方案是创建一种文件格式,其中包含每个敌人所需的所有配置(该文件通常也包含“ map ”数据)。您的代码将能够读取该文件并从配置中创建敌人(然后将它们放入之前的集合中)。在创建原型(prototype)时做您正在做的事情并不是一个坏主意,但是在决定将其制作为生产代码之前,您需要重构一些代码。

关于java - 减少 Android 游戏中的重复次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com