gpt4 book ai didi

java - 如何在代码中为火球创建一个单独的类?

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:01 25 4
gpt4 key购买 nike

我的代码目前可以工作,也就是说我想为火球创建一个单独的类。我不知道如何做到这一点,而且找不到任何示例,我只是在寻求有关派生单独的类的帮助,以便我的主要代码不会太大。这是代码:

import java.awt.image.BufferedImage;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Image;
import java.io.*;
import javax.swing.JOptionPane;
import javax.imageio.*;
import java.awt.Rectangle;
public class Main extends JPanel implements ActionListener, KeyListener{
ImageIcon images[], imagesL[],standingImage[],punchImg[],
fireImg[],kickImg[],jumpImg[],slamImg[];
int x = 100, y = 530;
int count;
int velX,velY;
int velF;
int firey,firex;
int gravity = 20;
int totalImages =3, currentImage = 0, animationDelay = 80,
numSlam = 3,currentSlam = 0, numberImages = 1,
currentStand = 0,
numPunch = 3, currentPunch = 0;

int currentFire = 0, numFire =1,totalImagesL =3,
currentImageL = 0,currentKick = 0, numKick=2,currentJump = 0,
numJump = 1;
int height, totalHeight = -120;
int velEnemy = 5;
int counter;
int enemyX = 310;
Timer animationTimer;
boolean isMovingLeft;
boolean isKick;
boolean isFire = false, fireIs = false,isMovingRight = false,
isNotMoving = true, isPunch = false, startFire = false, isJump = false,
isSlam = false;
boolean enemyIsDead = false;
Image fireball;
Image back;
Image title;
Image alien;
long lastJumptime = 0;
long lastrJumptime = 0;
long currentTime;
long lastFiretime = 0;
private final int FRAME_DELAY = 50;
public Main() {
setFocusable(true); // make your panel focusable
addKeyListener(this); // register the key listener
setFocusTraversalKeysEnabled(false);
//WALKING/RUNNING RIGHT//
images = new ImageIcon[totalImages];
images[2] = new ImageIcon("standing.png");
images[1] = new ImageIcon("midWay.png");
images[0] = new ImageIcon("running.png");
//WALKING/RUNNING LEFT//
imagesL = new ImageIcon[totalImagesL];
imagesL[2] = new ImageIcon("standingL.png");
imagesL[1] = new ImageIcon("midWayL.png");
imagesL[0] = new ImageIcon("runningL.png");
///STANDING STILL
standingImage = new ImageIcon[numberImages];
standingImage[0] = new ImageIcon("standing.png");
//PUNCH/////
punchImg = new ImageIcon[numPunch];
punchImg[0] = new ImageIcon("midPunch.png");
punchImg[1] = new ImageIcon("punchFull.png");
punchImg[2] = new ImageIcon("midPunch.png");
//COMMENCE FIREBALL//
fireImg = new ImageIcon[numFire];
fireImg[0] = new ImageIcon("startFire.png");
//COMMENCE KICK//
kickImg = new ImageIcon[numKick];
kickImg[0]= new ImageIcon("startKick.png");
kickImg[1]= new ImageIcon("finishKick.png");
//JUMPING//
jumpImg = new ImageIcon[numJump];
jumpImg[0] = new ImageIcon("ready.png");
//TITLE THINGY///
ImageIcon title = new ImageIcon("gokuTitle.png");
//SLAM THINGY//
slamImg = new ImageIcon[numSlam];
slamImg[0] = new ImageIcon("startSlam.png");
slamImg[1] = new ImageIcon("midSlam.png");
slamImg[2] = new ImageIcon("endSlam.png");
startAnimation();
}

public void paintComponent(Graphics g) {
//SOUND FOR THIS STUFF//
Sound[] soundSelection = new Sound[4];
soundSelection[0]= new Sound("Fire Spell.wav");
soundSelection[1] = new Sound("kick effect.wav");
soundSelection[2] = new Sound("fire ball troll.wav");
soundSelection[3] = new Sound("punch sound effect.wav");
//END STUFF FOR SOUND//
super.paintComponent(g);
Graphics g2 = (Graphics) g;
try{
back = ImageIO.read(new File("backGround.png"));
title = ImageIO.read(new File("gokuTitle.png"));
}catch (IOException e) {
}
g2.drawImage(back, 0, 0, null);
g2.drawImage(title, 0, 0, null);
if (isMovingRight == true){//if moving right
if (images[currentImage].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image imgRun = images[currentImage].getImage();
g2.drawImage(imgRun, x, y, null);
currentImage = (currentImage + 1) % totalImages;
}
}
if (isMovingLeft == true){//if moving left
if (imagesL[currentImageL].getImageLoadStatus() == MediaTracker.COMPLETE)
{
Image imgRunL = imagesL[currentImageL].getImage();
g2.drawImage(imgRunL, x, y, null);
currentImageL = (currentImageL + 1) % totalImagesL;
}
}
if (isNotMoving == true){//IF not moving
if (standingImage[currentStand].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image imgStand = standingImage[currentStand].getImage();
g2.drawImage(imgStand, x, y, null);
currentStand = (currentStand + 1) % numberImages;
}
}
if (fireIs== true){
//Do a seperate class for fireball
isFire = true;
if (fireImg[currentFire].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image fireMe = fireImg[currentFire].getImage();
g2.drawImage(fireMe, x, y, null);
currentFire = (currentFire+ 1) % numFire;
soundSelection[0].play();
soundSelection[2].play();
}
try{
fireball = ImageIO.read(new File("fireball.png"));
}catch (IOException e) {
}
}
if(isFire ==true){
fireIs = false;
g2.drawImage(fireball, firex + 80, firey+30, null);
}
if(isPunch == true){
if (punchImg[currentPunch].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image punchMe = punchImg[currentPunch].getImage();
g2.drawImage(punchMe, x, y, null);
currentPunch = (currentPunch+ 1) % numPunch;
soundSelection[3].play();
}
}
if(isKick == true){
if (kickImg[currentKick].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image kickMe = kickImg[currentKick].getImage();
g2.drawImage(kickMe, x, y, null);
currentKick = (currentKick+ 1) % numKick;
soundSelection[1].play();
}
}
if (isJump == true){
if (jumpImg[currentJump].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image jumpUp = jumpImg[currentJump].getImage();
g2.drawImage(jumpUp,x, y+10, null);
currentJump = (currentJump+ 1) % numJump;
}
}
if (isSlam == true){
if (slamImg[currentSlam].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image slamUp = slamImg[currentSlam].getImage();
g2.drawImage(slamUp,x, y+10, null);
currentSlam = (currentSlam+ 1) % numSlam;
}
}
//ENEMY THAT DOES NOT WANT TO WORK//

//draw rect here//
Rectangle player = new Rectangle(x,y, 60,100);
Rectangle enemies = new Rectangle(enemyX,530, 60,100);
/////
if (player.intersects(enemies)){
enemyIsDead = true;
}
if (enemyIsDead == false){
try{
alien = ImageIO.read(new File("Alien1.png"));
}catch (IOException e) {
}
g2.drawImage(alien, enemyX, 530, null);
}


}

public void actionPerformed(ActionEvent e) {
repaint();
try {
Thread.sleep(FRAME_DELAY);
} catch (InterruptedException exception) {
throw new RuntimeException(exception);
}
x+=velX;
height += velY;
enemyX+=velEnemy;
if (height >= totalHeight){
y+=velY;
}else{
y+=gravity;
}
firex += velF;
if (y < 2){
y = 3;
}
if (y >530){
y = 531;
}
}
public void right(){
velX = 20;
}
public void left(){
velX = -20;
}
public void fire(){
velF = 30;
}
public void jump(){
velY = -30;
}
public void notMovingMethod(){
isMovingRight = false;
isNotMoving = true;
isPunch = false;
isMovingLeft = false;
isKick = false;
isJump = false;
isSlam = false;
}
public void everythingFalse(){
isPunch = false;
isNotMoving = false;
isMovingRight = false;
isMovingLeft = false;
isKick = false;
isJump = false;
isSlam = false;
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
//FIND OUT ABOUT COUNTER FOR ENEMIES NEARBY,LIKE IF AN ENEMY IS NEAR YOU FOR
2 SEC YOU LOSE HEALTH
if(code == KeyEvent.VK_Z){
currentTime=System.currentTimeMillis();
if (currentTime >= lastFiretime + 5000){
firex = x;
firey = y;
fire();
fireIs = true;
everythingFalse();
lastFiretime=System.currentTimeMillis();
}
}
if (code == KeyEvent.VK_P){
System.out.println("Paused");
stopAnimation();
}
if (code == KeyEvent.VK_A){
left();
everythingFalse();
isMovingLeft = true;

}

if (code == KeyEvent.VK_F){
everythingFalse();
isPunch = true;
}

if(code == KeyEvent.VK_D){
right();
everythingFalse();
isMovingRight = true;
}
if(code == KeyEvent.VK_Q){
everythingFalse();
isKick = true;
}
if(code == KeyEvent.VK_W){
currentTime=System.currentTimeMillis();
if (currentTime >= lastJumptime + 1000){
jump();
everythingFalse();
isJump = true;
lastJumptime=System.currentTimeMillis();
}
}
if (code == KeyEvent.VK_E){
everythingFalse();
isSlam = true;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
int code = e.getKeyCode();

if(code == KeyEvent.VK_D){
notMovingMethod();
}
if (code == KeyEvent.VK_P){
startAnimation();
}
if (code == KeyEvent.VK_A){
notMovingMethod();
}
if(code == KeyEvent.VK_Q){
notMovingMethod();
}
if (code == KeyEvent.VK_F){
notMovingMethod();
}
if (code == KeyEvent.VK_E){
notMovingMethod();
}
if(code == KeyEvent.VK_Z){
notMovingMethod();
}
if(code == KeyEvent.VK_W){
currentTime=System.currentTimeMillis();
if (currentTime >= lastrJumptime + 1000){
notMovingMethod();
height = 0;
}
lastrJumptime = System.currentTimeMillis();
}
velX = 0;
}
public void startAnimation() {
if (animationTimer == null) {
currentImage = 0;
currentImageL = 0;
currentStand = 0;
currentPunch=0;
currentFire = 0;
currentKick = 0;
currentJump = 0;
currentSlam = 0;
animationTimer = new Timer(animationDelay, this);
animationTimer.start();
} else if (!animationTimer.isRunning())
animationTimer.restart();
}

public void stopAnimation() {
animationTimer.stop();
}
}

最佳答案

嗯,我很高兴您询问如何使用单独的类,因为根据您发布的代码,您非常需要它们。不只是为了火球。对于声音,对于动画,对于玩家,对于敌人。每个类应该只有一个职责,这是您的代码不会做的事情,但它会使其更加清晰和可维护。

要回答您的问题,请使用新类创建一个新的 .java 文件,如下所示:

public class Fireball {
public Fireball() {
//do whatever you need to do in the constructor
}

//you can declare other methods of fireballs here
}

然后使用 import 语句将其包含在您的主类中。

有关单一职责原则的更多信息:https://en.wikipedia.org/wiki/Single_responsibility_principle

类的基本概述 - 如果您刚刚决定第一次使用不同的类,这是最好的起点:https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

关于java - 如何在代码中为火球创建一个单独的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598258/

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