Java 继承、静态和混乱
我有3本Java书籍,并且使用在线资源,包括这个网站上的帖子,但是有些东西没有点击。新的太一般编程,而不是最亮的灯泡,所以请尝试和哑下来的反应,如果有可能的话。我将尽我所能详细解释,如果这是一篇很长的帖子,很抱歉,谢谢你抽出时间来。另外,我正在使用IDE 目标: 我正在尝试创建一个实现GUI的简单“游戏”,单击3个JButtons中的1个执行操作,JButtons上方是一个JTextArea,用于将所有信息传递给用户 问题:我没有正确引用其他类中的对象,也没有创建静态方法和变量来补偿(我想),但请看一看,让我知道我还做了哪些不正确的事情。我需要知道问题所在的人的评论和批评 旁注: 它的bug和很多已经变成了“静态”方法或变量,以便在类之间使用。当需要创建一个新的“关卡”时,由于它的静态特性,我无法回收我的敌人ArrayList并在其中加载新的敌人。我将发布工作版本,但我已经开始重建它,以便“实践使完美”,并可能升级我的代码Java 继承、静态和混乱,java,Java,我有3本Java书籍,并且使用在线资源,包括这个网站上的帖子,但是有些东西没有点击。新的太一般编程,而不是最亮的灯泡,所以请尝试和哑下来的反应,如果有可能的话。我将尽我所能详细解释,如果这是一篇很长的帖子,很抱歉,谢谢你抽出时间来。另外,我正在使用IDE 目标: 我正在尝试创建一个实现GUI的简单“游戏”,单击3个JButtons中的1个执行操作,JButtons上方是一个JTextArea,用于将所有信息传递给用户 问题:我没有正确引用其他类中的对象,也没有创建静态方法和变量来补偿(我想),但
- 就结构而言,每个“关卡”是否应该是自己的类,所有关卡是否应该像敌人一样位于阵列列表中,我是否应该有一个类作为游戏循环
- 有没有一个通用的最佳方式来创建一些东西,或者只要它能工作就无所谓了?(请发表意见)
打包游戏;
输入字符。基本字符;
导入字符.UserCharacter;
导入GUI.bottom按钮;
导入GUI.TopButtons;
导入级别。级别为1;
导入级别。教程;
导入java.util.ArrayList;
公共类循环{
//类变量
公共静态用户角色播放器;
公共静态数组列表;
公共静态int currLevel=0;
公共静态void newGame(){
战斗障碍();
开关(电流电平){
案例0:
教程=新教程();
tutorial.runTutorial();
可战斗();
打破
案例1:
级别1=新级别1();
lvl1.runLevelOne();
可战斗();
打破
}
}
公共静态void combatEnabled(){
TopButtons.attackButton.setEnabled(真);
TopButtons.kickButton.setEnabled(真);
}
公共静态无效combatDisabled(){
TopButtons.attackButton.setEnabled(假);
TopButtons.kickButton.setEnabled(假);
BottomButtons.powAttButton.setEnabled(假);
}
}
第1级:
包级别;
输入字符。基本字符;
导入字符.UserCharacter;
进口作战系统;
导入GUI.OutputPanel;
导入游戏。循环;
导入java.util.ArrayList;
公开课辅导{
public void runTutorial(){
//实例变量
Loop.player=新用户字符(100,10);
BasicEnemy 0=新BasicEnemy(100,5);
BasicEnemy 1=新BasicEnemy(100,5);
BasicEnemy 2=新BasicEnemy(100,5);
Loop.enemyList=新的ArrayList();
Loop.enemyList.add(enemy0);
Loop.enemyList.add(enemy1);
Loop.enemyList.add(enemy2);
OutputPanel.output.append(“问候玩家!使用屏幕底部的按钮与对手作战。”);
batch.statusUpdate();
Loop.combatEnabled();
}
}
处理战斗的类:
组合作战系统;
导入GUI.bottom按钮;
导入GUI.OutputPanel;
导入GUI.StatsPanel;
导入游戏。循环;
导入java.util.Random;
公开阶级斗争{
//球员统计
私有静态int-playercurrheath=Loop.player.getHealth();
私有静态int-playerCurrage=Loop.player.getRageMeter();
私有静态int-playerAttack=Loop.player.getAttackPower();
私有静态int playerKick=Loop.player.getKickPower();
私有静态int-playerPowAttack=Loop.player.getRagePower();
//敌人统计数据
私有静态int-enemycurrheath=Loop.enemyList.get(0.getHealth();
私有静态int-enemycurrage=Loop.enemyList.get(0.getRageMeter();
私有静态int-enemyAttack=Loop.enemyList.get(0.getAttackPower();
//战斗
私有静态布尔值=true;
公共战斗(){
}
//更新状态字段
公共静态无效状态更新(){
StatsPanel.playerStats.setText(“[”+PlayerCurrage+“]);
StatsPanel.enemyStats.setText(“[”+enemycurrage+“]);
}
//基本攻击
公共静态无效BasicTack(){
如果(战斗){
playerbasic攻击();
如果(enemyCurrHealth=100){
PlayerCurrage=100;
BottomButtons.powAttButton.setEnabled(真);
}否则{
BottomButtons.powAttButton.setEnabled(假);
}
}
}
//敌军作战系统
私有静态void enemyBasicTack(){
OutputPanel.output.append(“\n敌人的攻击造成”+敌人攻击+“对你造成伤害!”);
playerCurrHealth-=敌人攻击;
gainEnemyRage();
增益层数();
statusUpdate();
}
私有静态无效gainEnemyRage(){
如果(灌肠电流<100){
灌肠电流+=5;
}否则{
灌肠电流=100;
}
}
//终局
私有静态无效死亡(){
如果(playerCurrHealth通常,正确的方法是创建一个名为控制器的类的实例。控制器是一个对象,它处理对其他对象的引用,并处理要完成的任务。因此,在主要情况下,您可以执行以下操作
GameController cont = new GameController();
那么,如果你需要
package GUI;
import javax.swing.*;
import java.awt.*;
/**
* Graphical User Interface requires:
* 1 JFrame (PrimaryFrame)
* 4 JPanel (outputPanel, characterStatsPanel, topButtonPanel, bottomButtonPanel)
* 3 JTextArea (output, playerStats, enemyStats)
* 1 JScrollPane (output)
* 3 JButton (attackButton, kickButton, powAttButton)
*/
public class PrimaryFrame extends JFrame {
private OutputPanel outPanel = new OutputPanel();
private StatsPanel statPanel = new StatsPanel();
private TopButtons topPanel = new TopButtons();
private BottomButtons botPanel = new BottomButtons();
//Constructor
public PrimaryFrame() {
//frame
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
Dimension FRAME_MIN = new Dimension(400, 600);
setMinimumSize(FRAME_MIN);
Dimension FRAME_MAX = new Dimension(600, 800);
setMaximumSize(FRAME_MAX);
setTitle("Generic Fighting Game");
getContentPane().setBackground(Color.BLACK);
}
public void buildGUI() {
//Add components and spacing
add(Box.createRigidArea(new Dimension(8, 8)));
add(outPanel);
add(Box.createRigidArea(new Dimension(8, 8)));
add(statPanel);
add(Box.createRigidArea(new Dimension(8, 8)));
add(topPanel);
add(Box.createRigidArea(new Dimension(8, 8)));
add(botPanel);
add(Box.createRigidArea(new Dimension(8, 8)));
//Set attributes
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
package GUI;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
/**
* Created by Thunderfoot on 8/4/2016. Keep Growing!
*/
public class OutputPanel extends JPanel {
//Class variables
public static JTextArea output;
OutputPanel() {
Font myFont = new Font("Serif", Font.BOLD, 16);
BoxLayout outFlow = new BoxLayout(this, BoxLayout.X_AXIS);
setLayout(outFlow);
setBackground(Color.GRAY);
setMinimumSize(new Dimension(400, 100));
setMaximumSize(new Dimension(600, 300));
//TextArea
output = new JTextArea(1,50);
output.setLineWrap(true);
output.setWrapStyleWord(true);
output.setEditable(false);
output.setFont(myFont);
JScrollPane outputScroll = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outputScroll.isWheelScrollingEnabled();
//TextArea always scroll to bottom after update
DefaultCaret caret = (DefaultCaret)output.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
//Add components and spacing
add(Box.createRigidArea(new Dimension(8, 8)));
add(outputScroll, CENTER_ALIGNMENT);
add(Box.createRigidArea(new Dimension(8, 8)));
}
}
package Game;
import Characters.BasicEnemy;
import Characters.UserCharacter;
import GUI.BottomButtons;
import GUI.TopButtons;
import Levels.Level_One;
import Levels.Tutorial;
import java.util.ArrayList;
public class Loop {
//Class variables
public static UserCharacter player;
public static ArrayList<BasicEnemy> enemyList;
public static int currLevel = 0;
public static void newGame() {
combatDisabled();
switch (currLevel) {
case 0:
Tutorial tutorial = new Tutorial();
tutorial.runTutorial();
combatEnabled();
break;
case 1:
Level_One lvl1 = new Level_One();
lvl1.runLevelOne();
combatEnabled();
break;
}
}
public static void combatEnabled() {
TopButtons.attackButton.setEnabled(true);
TopButtons.kickButton.setEnabled(true);
}
public static void combatDisabled() {
TopButtons.attackButton.setEnabled(false);
TopButtons.kickButton.setEnabled(false);
BottomButtons.powAttButton.setEnabled(false);
}
}
package Levels;
import Characters.BasicEnemy;
import Characters.UserCharacter;
import CombatSystem.Combat;
import GUI.OutputPanel;
import Game.Loop;
import java.util.ArrayList;
public class Tutorial {
public void runTutorial() {
//Instance variables
Loop.player = new UserCharacter(100, 10);
BasicEnemy enemy0 = new BasicEnemy(100, 5);
BasicEnemy enemy1 = new BasicEnemy(100, 5);
BasicEnemy enemy2 = new BasicEnemy(100, 5);
Loop.enemyList = new ArrayList<>();
Loop.enemyList.add(enemy0);
Loop.enemyList.add(enemy1);
Loop.enemyList.add(enemy2);
OutputPanel.output.append("Greetings player! Use the buttons at the bottom of the screen to fight your opponent.");
Combat.statusUpdate();
Loop.combatEnabled();
}
}
package CombatSystem;
import GUI.BottomButtons;
import GUI.OutputPanel;
import GUI.StatsPanel;
import Game.Loop;
import java.util.Random;
public class Combat {
//Player stats
private static int playerCurrHealth = Loop.player.getHealth();
private static int playerCurrRage = Loop.player.getRageMeter();
private static int playerAttack = Loop.player.getAttackPower();
private static int playerKick = Loop.player.getKickPower();
private static int playerPowAttack = Loop.player.getRagePower();
//Enemy stats
private static int enemyCurrHealth = Loop.enemyList.get(0).getHealth();
private static int enemyCurrRage = Loop.enemyList.get(0).getRageMeter();
private static int enemyAttack = Loop.enemyList.get(0).getAttackPower();
//Fighting
private static boolean fighting = true;
public Combat() {
}
//Update status fields
public static void statusUpdate() {
StatsPanel.playerStats.setText(" <" + playerCurrHealth + "> [" + playerCurrRage + "] ");
StatsPanel.enemyStats.setText(" <" + enemyCurrHealth + "> [" + enemyCurrRage + "] ");
}
//Basic attack
public static void basicAttack() {
if (fighting) {
playerBasicAttack();
if (enemyCurrHealth <= 0) {
death();
} else {
enemyBasicAttack();
if (playerCurrHealth <= 0) {
death();
}
}
}
}
//Player combat systems
private static void playerBasicAttack() {
OutputPanel.output.append("\n Your attack does " + playerAttack + " damage to the enemy!");
enemyCurrHealth -= playerAttack;
gainPlayerRage();
gainEnemyRage();
statusUpdate();
}
//Player kick attack
public static void playerKickAttack() {
OutputPanel.output.append("\n Your kick does " + playerKick + " damage to the enemy!");
enemyCurrHealth -= playerKick;
gainPlayerRage();
gainEnemyRage();
statusUpdate();
Random random = new Random();
int kickChance = random.nextInt(100) + 1;
if (kickChance >= 60) {
OutputPanel.output.append("\n Your kick has stunned the enemy!");
} else {
enemyBasicAttack();
gainEnemyRage();
gainPlayerRage();
statusUpdate();
}
}
//Player power attack
public static void playerPowAttack() {
OutputPanel.output.append("\n Your POWER ATTACK does " + playerPowAttack + " damage to the enemy!");
enemyCurrHealth -= playerPowAttack;
playerCurrRage = 0;
gainEnemyRage();
statusUpdate();
BottomButtons.powAttButton.setEnabled(false);
}
private static void gainPlayerRage() {
if (playerCurrRage < 100) {
playerCurrRage += 5;
if (playerCurrRage >= 100) {
playerCurrRage = 100;
BottomButtons.powAttButton.setEnabled(true);
} else {
BottomButtons.powAttButton.setEnabled(false);
}
}
}
//Enemy combat systems
private static void enemyBasicAttack() {
OutputPanel.output.append("\n Enemy's attack does " + enemyAttack + " damage to you!");
playerCurrHealth -= enemyAttack;
gainEnemyRage();
gainPlayerRage();
statusUpdate();
}
private static void gainEnemyRage() {
if (enemyCurrRage < 100) {
enemyCurrRage += 5;
} else {
enemyCurrRage = 100;
}
}
//END GAME
private static void death() {
if (playerCurrHealth <= 0) {
Loop.combatDisabled();
fighting = false;
loseGame();
} else if (enemyCurrHealth <= 0) {
if (!Loop.enemyList.isEmpty()) {
Loop.enemyList.remove(0);
OutputPanel.output.append("\nYou have killed an enemy!");
statusUpdate();
fighting = true;
} else {
fighting = false;
Loop.combatDisabled();
winGame();
}
}
}
private static void winGame() {
OutputPanel.output.setText("You won!");
Loop.combatDisabled();
Loop.currLevel++;
Loop.enemyList.clear();
Loop.newGame();
}
private static void loseGame() {
OutputPanel.output.setText("You lost!");
Loop.combatDisabled();
}
}
GameController cont = new GameController();
GUI gui = new GUI();
cont.setGUI(gui);
gui.setController(controller);