- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在为 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/
你好,我有一张 table : from | to | item | count ------- Jack | Danie| food | 10 Danie| Maria| food | 2 Ja
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在尝试解决以下面试问题 Given two arrays firstDay and lastDay representing the intervals in days of possible m
这个问题已经有答案了: Explanation of a output of a C program involving fork() (2 个回答) 已关闭 9 年前。 这是我从我的研究所去年的试卷
如何在 html 页面上重复一个 div X 次,可以说我想设置方差来声明重复次数。重复这个部分 5 次,我假设它是用 JS 的。 black BLUE WHITE strip 我
我目前使用类中的函数将数据插入数据库,如果每行成功插入(从 csv 文件),则会记录一条消息(logMessage 函数),以显示哪一行成功或失败。但是我想要已导入数据库的成功执行的计数。我遇到了一些
这个问题可能看起来非常基础,但我很难弄清楚如何做。我有一个整数,我需要使用 for 循环来循环整数次。 首先,我尝试了—— fn main() { let number = 10; // An
我正在准备 CS 125 期末考试,其中(简要地)介绍了 Big O Notation。 鉴于: Mergesort 的最佳运行时间为 O(N lg(N)),最坏运行时间为 O(N lg (N)) 有
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我正在构建一个简单的程序来计算骰子实验中数字的频率,但我尝试扩展它并将最大 throw 次数增加到巨大的数字,通过反复试验,我发现最大限制为519253。 使用这个最大值,我也无法创建任何新数组,它会
这是一道面试题 There is an airline company that wants to provide new updates to all of its flight attendant
我正在尝试以一种可以节省我无数小时的繁琐数据输入的方式实现 Excel 自动化。这是我的问题。 我们需要为所有库存打印条形码,其中包括 4,000 种型号,每种型号都有特定数量。 Shopify是我们
我想根据给定的预定义级别(从级别 1 到级别 6)分离代码中的所有内容,现在我的 JSON 读取 $scope.myJson=[{ id: 1, level: 1, name: "any
我创建了一个菜单,它使用一些 CSS 和 jquery 在悬停时显示其子菜单。事情是,如果用户在菜单项上多次悬停,它会有点滑稽。这是网址:http://91.202.168.37/~ibi/ ,这是
假设我对每小时的事件数进行了如下统计: np.random.seed(42) idx = pd.date_range('2017-01-01', '2017-01-14', freq='1H') df
我想确保我正确理解了这个概念: 在 Hadoop 权威指南中指出:“设计文件系统的目标始终是减少与要传输的数据量相比的查找次数。”在此声明中,作者指的是 Hadoop 逻辑 block 的“seeks
我有一个用 C++11 编写的程序,我想计算 std::vector 的 move 和复制(构造和赋值)次数。对象。有办法吗? 最好的问候 最佳答案 否。 std::vector<>的执行没有办法做到
我们组织的帐户空间不足,我们一直在尝试剔除一些较旧的存储库。问题在于一些较旧的存储库可能仍然是事件服务的依赖项(即使它们多年未更新)。 我知道我们可以跟踪克隆,但据我所知,我们看不到直接下载/pull
我是一名优秀的程序员,十分优秀!