- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名文凭学生,正在制作一个基于处理的游戏,这是一款塔防游戏,女主角将向冲向城堡的僵尸射箭。我对此有多个问题。
据说我希望僵尸在一群被击落后反复地一波又一波地产生,然后我希望充电器僵尸单独死亡,具体取决于我点击的僵尸。
这是我的源代码:
import processing.opengl.*; // transparency
import gifAnimation.*; // after add gifAnimation library
import ddf.minim.*; // importing music
PImage backgroundImg;
PImage animation;
int counter;
//settings
int score = 50, totalScore = 0;
int titleX = 200, titleY = 200;
boolean shotsFired = false;
PFont scorePoints, castleHealthPoints, font; //font for startingScreen
int chargerKilled;
//Charger class
int maxChargerOnScreen;
Charger[] chargerArr; //chargerArray undefined
int chargerIndex;
final int numCharger = 300;
ArrayList<Charger> onScreen = new ArrayList<Charger>();
boolean chargerReachedTarget = false, chargerShot = false, chargerDive = true;
Gif chargerCharge, chargerAttack, chargerDied, shootingStance;
int chargerMoveX = 800, chargerMoveY = 450;
// CastleWallHealth
int castleHealth = 1000;
// audio
Minim minim;
AudioPlayer song;
AudioSnippet arrowShoot;
//-------------------------------Codes start here --------------------------------------------------//
void setup() {
//ipad mini size
// frameRate(10);
frameRate(60);
size(800, 600);
smooth();
minim = new Minim(this);
// song = minim.loadFile("InGameSoundtrack.mp3");
// song.play();
arrowShoot = minim.loadSnippet("arrowSound1.wav");
backgroundImg = loadImage("GameBackground.jpg");
backgroundImg.resize(800, 600);
chargerCharge = new Gif(this, "ChargerCharge.gif");
chargerAttack = new Gif(this, "ChargerAttack.gif");
chargerDied = new Gif(this, "ChargerDie.gif");
gargoyleFly = new Gif(this, "GargoyleFly.gif");
gargoyleAttack = new Gif(this, "GargoyleAttack.gif");
shootingStance = new Gif(this, "ShootingGif.gif");
scorePoints = createFont("Arial", 16, true);
castleHealthPoints = createFont("Arial", 16, true);
// loopingGif.play() plays the animation without loop
// must make charger
chargerCharge.play();
chargerAttack.play();
chargerDied.play();
resetGames(); // starts with respawning 5 charges
}
void draw() {
// backgroud
background(backgroundImg);
image(shootingStance, 120,200);
// scores and health
textFont(scorePoints, 15);
fill(0);
text("Score: " + totalScore, 100, 100);
displayCastleHealth();
//*********************************************charger settings code********************************************************************
// If there's no more zombies. prompt reset (screen no zombies) , index from 0 add till 300 then game won
if (onScreen.size() == 0 && chargerIndex == numCharger) {
println("Game won! :)");
// gameWon = true;
}
// while onScreen charger less than 5 only and chargerIndex havent reach 300 it will only respawn
while (onScreen.size () < maxChargerOnScreen && chargerIndex <= numCharger - 1) {
if (chargerIndex < chargerArr.length) { // <--- this is the problem chargerArrayLength
Charger chargerMobs = chargerArr[chargerIndex];
// starts from chargerArray[0] to chargerArray[300]
onScreen.add(chargerMobs);
chargerIndex++;
// chargerIndex keeps looping why?
// println("ADDED ZOMBIE TO ONSCREEN Zombs on screen: " + onScreen.size());
}
}
// Adjusts maxZombOnScreen based off the current index Difficulties?
if (chargerIndex == 10)
maxChargerOnScreen++;
else if (chargerIndex == 50)
maxChargerOnScreen++;
else if (chargerIndex == 100)
maxChargerOnScreen += 2;
else if (chargerIndex == 150)
maxChargerOnScreen += 2;
else if (chargerIndex == 190)
maxChargerOnScreen += 3;
else if (chargerIndex == 250)
maxChargerOnScreen += 3;
else if (chargerIndex == 260)
maxChargerOnScreen += 3;
else if (chargerIndex == 270)
maxChargerOnScreen += 5;
// removes dead zombies, living zombies act normally
for(int i = 0; i < onScreen.size(); i++) {
Charger c = onScreen.get(i);
// add money when a zombie is dead
// if (c.getHealth() <= 0)
if (chargerShot==true) {
c.chargerDying();
onScreen.remove(i);
chargerKilled++;
i--;
} else {
c.act(); ///it should just act
// c.displayHealth(); // if gt time only do
// }
}
}
// creating list of predetermined zombies and their characteristics
println("chargerIndex: " + chargerIndex); // debug
println("onScreenSize: " + onScreen.size());
// println("ChargerArray: " + chargerArray.length); chargerArray point null?
// cursor
if (mouseY > height - 500) {
noFill();
stroke(255, 0, 0);
ellipse(mouseX, mouseY, 35, 35);
line(mouseX - 25, mouseY, mouseX + 25, mouseY);
line(mouseX, mouseY - 25, mouseX, mouseY + 25);
}
}
void resetGames() {
// resetting variables
chargerIndex = 0;
maxChargerOnScreen = 5;
chargerKilled = 0;
createNewCharger();
}
void createNewCharger() {
Charger[] temp = new Charger[numCharger];
for (int i = 0; i < numCharger; i++)
temp[i] = new Charger((int) random(800, 1200), chargerMoveY);
chargerArr = temp;
}
//____________________________________________________________________________________________________________-
class Charger {
private int chargerMoveX, chargerMoveY;
Gif chargerGif; // by default
// chargerGif will change based on input (either it reaches the castle or its shot)
// no constructor
Charger() {
chargerMoveX = 0;
chargerMoveY = 450;
}
// overloaded constructor
Charger(int cMoveX,int cMoveY) {
chargerMoveX = cMoveX;
chargerMoveY = cMoveY;
}
void act() { // use
// if reached castle it wont move
if (chargerMoveX <= 180) {
// remove gif images
chargerDive = false;
chargerReachedTarget = true;
}
// if not shot or reached castle it will move
if (chargerDive == true) {
chargerGif = chargerCharge;
image(chargerGif, chargerMoveX, chargerMoveY);
chargerMoveX -= 2;
}
// if reached castle perform attack animation
if (chargerReachedTarget == true) {
chargerGif = chargerAttack;
image(chargerGif, chargerMoveX - 80, chargerMoveY - 120);
}
if (mousePressed &&
mouseX < chargerMoveX + 180
&& mouseX > chargerMoveX
&& mouseY < chargerMoveY + 120
&& mouseY > chargerMoveY) {
// score board
totalScore = totalScore + score ;
float r = random(50);
println(r);
chargerShot = true;
chargerDive = false;
}
}
void chargerDying() {
chargerReachedTarget = false;
chargerGif = chargerDied;
image(chargerGif, chargerMoveX, chargerMoveY);
}
}
void displayCastleHealth() {
if (castleHealth <= 0) {
fill(250, 70, 0);
textFont(castleHealthPoints, 20);
text("HP: 0", 100, 50);
} else {
fill(250, 70, 0);
textFont(castleHealthPoints, 20);
text("HP: " + castleHealth, 100, 50);
}
}
void mousePressed() {
strokeWeight(2);
arrowShoot.rewind();
arrowShoot.play();
for (int i = 0 ; i < 3; i++)
shootingStance.play();
shotsFired = true;
}
void mouseReleased() {
strokeWeight(1);
if (shotsFired == true)
shootingStance.pause();
}
public void stop() {
arrowShoot.close();
}
充电器的大部分问题都来自这里:
if (onScreen.size() == 0 && chargerIndex == numCharger) {
println("Game won! :)");
// gameWon = true;
}
// while onScreen charger less than 5 only and chargerIndex havent reach 300 it will only respawn
while (onScreen.size () < maxChargerOnScreen && chargerIndex <= numCharger - 1) {
if (chargerIndex < chargerArr.length) { // <--- this is the problem chargerArrayLength
Charger chargerMobs = chargerArr[chargerIndex];
// starts from chargerArray[0] to chargerArray[300]
onScreen.add(chargerMobs);
chargerIndex++;
// chargerIndex keeps looping why?
// println("ADDED ZOMBIE TO ONSCREEN Zombs on screen: " + onScreen.size());
}
}
// Adjusts maxZombOnScreen based off the current index Difficulties?
if (chargerIndex == 10)
maxChargerOnScreen++;
else if (chargerIndex == 50)
maxChargerOnScreen++;
else if (chargerIndex == 100)
maxChargerOnScreen += 2;
else if (chargerIndex == 150)
maxChargerOnScreen += 2;
else if (chargerIndex == 190)
maxChargerOnScreen += 3;
else if (chargerIndex == 250)
maxChargerOnScreen += 3;
else if (chargerIndex == 260)
maxChargerOnScreen += 3;
else if (chargerIndex == 270)
maxChargerOnScreen += 5;
// removes dead zombies, living zombies act normally
for(int i = 0; i < onScreen.size(); i++) {
Charger c = onScreen.get(i);
// add money when a zombie is dead
// if (c.getHealth() <= 0)
if (chargerShot==true) {
c.chargerDying();
onScreen.remove(i);
chargerKilled++;
i--;
} else {
c.act(); ///it should just act
// c.displayHealth(); // if gt time only do
// }
}
}
欢迎评价和批评。
最佳答案
代码存在几个问题,但现在,我将忽略有些“不干净”或“不优雅”的部分,并重点关注引起问题的主要问题:
所有 Charger
实例的状态都是相同的。字段
boolean chargerReachedTarget = false, chargerShot = false, chargerDive = true;
已被全局定义。只需将这一行(即这 3 个字段)移动到 Charger 类中,然后更改该行
if (chargerShot==true) {
至
if (c.chargerShot==true) {
这款游戏似乎已经更接近您可能一直在寻找的东西了。修改后,可以射击单个对手,老对手消失后新对手不断出现。
再说一遍:代码的结构并不是很好,并且有许多可能的小改进。例如,类似 if (c.chargerShot==true) { ... }
的内容最好是 if (charger.isShot()) { ... }
。但我不太熟悉处理及其最佳实践,所以我不确定“最佳”解决方案在全局意义上是什么样子......
关于java - 处理游戏: Gif images not respawning as intended,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338279/
当我调用 png.Decode(imageFile) 时,它返回类型 image.Image。但我找不到将其转换为 image.NRGBA 或 image.RGBA 的记录方式,我可以在其上调用 At
image/jpeg 和 image/png 包有 Decode 和 Encode 函数,可以读取和写入 jpeg 和 png 图像,但 image/gif 包没有 - 只有 Decode 和 Dec
我正在尝试从一系列任意的非调色板图像创建动画 GIF。为了创建调色板图像,我需要以某种方式想出一个调色板。 // RGBA, etc. images from somewhere else var f
我在今年夏天的空闲时间使用 Go 镜像包进行一些练习。 package main import ( "os" "image" "image/png" "image/co
关闭。这个问题需要debugging details .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 1年前关闭。 Improve this question 今天
我正在尝试在 TilePane 中列出图像。当我尝试创建图像 new ImageView("address"); 时出现错误,地址如下: "file:D:/Chrysanthemum.jpeg/" 以
我有一个用于为画廊选择图像的表单,我希望允许用户仅选择 jpg、gif 和 png 图像格式。 现在,为了测试,我将图像的扩展名更改为 .bmp,例如“image1.bmp”,当我在输入文件中单击以选
我有创建图像的代码:(m_img 是 javafx.scene.image.Image) Image m_img = new Image("file:" + p_Fil.getAbsoluteFile
假设我有一个这样的 8 位灰度图像: var pixels []byte = ... width := 100 height := 100 如何将其转换为实现 image.Image 的东西? 最佳答
这段代码是我在localhost:8088 URL上的索引/主页的一部分,如果我想将用户发送到url localhost:8088/image/1,我应该写href='image/{{$image->
我正在尝试对图像进行简单的裁剪。这是代码 from PIL.Image import Image def get_image_half(image, half="upper"): if hal
我在这个问题上花了一整天,但在堆栈溢出中没有看到答案! 我试过了但是没用: >> pil_image = Image.frombytes('RGBA', wand_image.size, wa
所以,我是那些以始终使用最新版本的浏览器而自豪的人之一(当然 Internet Explorer 除外 - 我说的不是那个浏览器)。 我遇到了 this awesome CSS3 website详细介
如果 image_tag 无法从 url 加载图像,我想呈现默认图像: 因此,如果 image_tag 无法从 url 加载图像: 然后呈现默认值: 这将生成结果 HTML: 关于image -
我正在创建一个类似横幅的组件,并将图像设置为组件的背景,但我无法让它工作。我尝试了网上发布的不同建议,但没有成功,目前我不确定我的错误是否在 react 代码中,或者是 webpack 没有正确加载文
如何解决 Dart 中的这种歧义错误。 import 'dart:io'; import 'package:flutter/material.dart'; import 'package:camera
Center( child: CachedNetworkImage( imageUrl: "http:/ sosme link he
设置 www.website.com/sds/(index.htm) 以便鼠标悬停在不同位置时显示图像。 出于某种原因,当您将鼠标悬停在蓝色气球上时,图像 2.jpg 和 3.jpg(在蓝色气球上来回
社交网络在共享 URL 时可以很好地从网站中提取标题和描述,但对于图像,仍然需要创建自定义元标记:property="og:image" name="twitter:image" itemprop="
我正在尝试写一个简短的,它将读取一个 PNG 文件,并将一个 channel 与另一个 channel (R,G,B) 交换作为可能的选择。 但是,我无法找到如何从 image.At(x,y) 返回的
我是一名优秀的程序员,十分优秀!