[英]How do I pass a value throughout my project and use it anywhere in the program
[英]Is it possible to create a centralized saving point of all the variables I use all throughout my program? If so, how?
这是我第一次在Stackoverflow,所以原谅我的noob帖子。
我的朋友和我有一个创建游戏的课程项目。 现在,我们每个人都制作了游戏的一部分,比如Battle,MapMovement等。我们的问题是,为了将我们单独的代码“拼接”在一起,我们必须不断地为每个“部分”来回传递值游戏
例如,用户正在探索地图,然后遇到触发战斗的怪物,之后用户返回地图或进入死亡屏幕。 所以变量将不得不移动Map - > Battle - > Map。
我一直在考虑使用接口或抽象类作为所有变量的主要中心,但我想不出如何实际做到这一点。 有任何想法吗?
所以我的部分是地图,这里称为MapFrame.java
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class MapFrame implements KeyListener{
JFrame frame;
int heroLocation, level;
MapFrame(){}
MapFrame(int heroLocation, int level){
this.heroLocation = heroLocation;
this.level = level;
frame = new JFrame();
frame.setResizable(false);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
System.out.println(BattleFrame.variable);
JPanel panel = new JPanel(new GridLayout(12, 12, 0, 0));
JLabel l;
for(int tile = 0; tile < 144; tile++){
l = helper.tile_set(tile, heroLocation, level);
panel.add(l);
}
frame.addKeyListener(this);
frame.setContentPane(panel);
frame.setVisible(true);
}
public static void main(String[] args){
new MapFrame(132, 1);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_W) { //UP
if(heroLocation>=0 && heroLocation <= 11){
}else if(helper.tile_restriction(heroLocation, level, 1)){
}else if(helper.encounter()){
frame.dispose();
new BattleFrameUsage(heroLocation, level, 1);
}else{
heroLocation -= 12;
new MapFrame(heroLocation, level);
frame.dispose();
}
}
if(e.getKeyCode() == KeyEvent.VK_A) { //LEFT
if(heroLocation%12 == 0){
}else if(helper.tile_restriction(heroLocation, level, 2)){
}else if(helper.encounter()){
frame.dispose();
new BattleFrameUsage(heroLocation, level, 2);
}else{
heroLocation -= 1;
new MapFrame(heroLocation, level);
frame.dispose();
}
}
if(e.getKeyCode() == KeyEvent.VK_S) { //DOWN
if(heroLocation>=132 && heroLocation<=143){
}else if(helper.tile_restriction(heroLocation, level, 3)){
}else if(helper.encounter()){
frame.dispose();
new BattleFrameUsage(heroLocation, level, 3);
}else{
heroLocation += 12;
new MapFrame(heroLocation, level);
frame.dispose();
}
}
if(e.getKeyCode() == KeyEvent.VK_D) { //RIGHT
if(heroLocation%12 == 11){
}else if(helper.tile_restriction(heroLocation, level, 4)){
}else if(helper.encounter()){
frame.dispose();
new BattleFrameUsage(heroLocation, level, 4);
}else{
heroLocation += 1;
new MapFrame(heroLocation, level);
frame.dispose();
}
}
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}
我把一些方法放入一个帮助文件中,只是为了让MapFrame.java不那么混乱。 在这里:helper.java
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class helper{
helper(){}
public static void main(String[] args){}
//For the tiles
public static double[] level_1 =
{
1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.2,1.2,1.2,1.2,1.2, //1.1 is tree tile, 1.2 is fire tile
1.1,1.1,1.1,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,2.3, //1.3 is grass tile, 1.4 is river tile
1.1,2.2,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.4, //1.5 is bridge tile, 1.6 is bucket tile
1.1,1.3,1.3,1.3,1.3,1.3,1.4,1.4,1.5,1.5,1.4,1.4, //2.1 is merchant god, 2.2 is portal,
1.3,1.3,1.3,1.3,2.1,1.4,1.4,1.4,1.5,1.5,1.4,1.4, //2.3 is shrine, 2.4 is atk up
1.3,1.3,1.3,1.4,1.4,1.4,1.4,1.4,1.5,1.5,1.4,1.4, //2.5 is def up, 2.6 is agi up
1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.6,1.3,1.3,1.2,2.4,
1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.4,1.5,1.5,1.4,1.4,
1.4,1.4,1.4,1.3,1.3,1.3,1.4,1.4,1.5,1.5,1.4,1.4,
1.4,1.3,1.3,1.3,1.3,1.3,1.3,1.4,1.5,1.5,1.1,1.4,
1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3, //1 are general level-only tiles,
1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3 //2 are merchant god, portal, shrine
};
public static JLabel tile_set(int x, int heroLocation, int level){
JLabel l = null;
/*if (x == 0){
l = new JLabel(new ImageIcon("tile-grass.png"));
return l;
}*/
if (x == heroLocation){
if(level_1[x] == 1.3){
l = new JLabel(new ImageIcon("grass_char.png"));
}else if(level_1[x] == 1.5){
l = new JLabel(new ImageIcon("bridge_char.png"));
}else{
l = new JLabel(new ImageIcon("breakdancing cat.gif"));
}
return l;
}else if(level == 1){
if (level_1[x] == 1.1){
l = new JLabel(new ImageIcon("tree.png"));
}else if (level_1[x] == 1.2){
l = new JLabel(new ImageIcon("fire.png"));
}else if (level_1[x] == 1.3){
l = new JLabel(new ImageIcon("grass.png"));
}else if (level_1[x] == 1.4){
l = new JLabel(new ImageIcon("water.png"));
}else if (level_1[x] == 1.5){
l = new JLabel(new ImageIcon("bridge.png"));
}else if (level_1[x] == 1.6){
l = new JLabel(new ImageIcon("bucket.png"));
}else if (level_1[x] == 2.1){
l = new JLabel(new ImageIcon("mammon.png"));
}else if (level_1[x] == 2.2){
l = new JLabel(new ImageIcon("portal.png"));
}else if (level_1[x] == 2.3){
l = new JLabel(new ImageIcon("level up shrine.png"));
}else if (level_1[x] == 2.4){
l = new JLabel(new ImageIcon("sword.png"));
}
}else if(level == 2){
if (level_1[x] == 2.1){
l = new JLabel(new ImageIcon("buddypoke roundhouse jordan.gif"));
}else if (level_1[x] == 2.2){
l = new JLabel(new ImageIcon("buddypoke roundhouse jordan.gif"));
}else if (level_1[x] == 2.1){
l = new JLabel(new ImageIcon("mammon.png"));
}else if (level_1[x] == 2.2){
l = new JLabel(new ImageIcon("portal.png"));
}else if (level_1[x] == 2.3){
l = new JLabel(new ImageIcon("level up shrine.png"));
}else if (level_1[x] == 2.4){
l = new JLabel(new ImageIcon("sword.png"));
}
}
return l;
}
public static int heroLocation1, move;
public static boolean level_1_restriction(){
if(move == 1){
if(level_1[heroLocation1-12] == 1.1 || level_1[heroLocation1-12] == 1.2 || level_1[heroLocation1-12] == 1.4 || level_1[heroLocation1-12] == 2.1 || level_1[heroLocation1-12] == 2.3){
return true;
}else{
return false;
}
}else if (move == 2){
if(level_1[heroLocation1-1] == 1.1 || level_1[heroLocation1-1] == 1.2 || level_1[heroLocation1-1] == 1.4 || level_1[heroLocation1-1] == 2.1 || level_1[heroLocation1-1] == 2.3){
return true;
}else if(level_1[heroLocation1-1] == 1.6){
level_1[heroLocation1-1] = 1.4;
level_1[82] = 1.3;
return false;
}else if(heroLocation1 == 83 && level_1[heroLocation1-1] == 1.3){
level_1[heroLocation1-1] = 1.4;
return false;
}else{
return false;
}
}else if (move == 3){
if(level_1[heroLocation1+12] == 1.1 || level_1[heroLocation1+12] == 1.2 || level_1[heroLocation1+12] == 1.4 || level_1[heroLocation1+12] == 2.1 || level_1[heroLocation1+12] == 2.3){
return true;
}else{
return false;
}
}else if (move == 4){
if(level_1[heroLocation1+1] == 1.1 || level_1[heroLocation1+1] == 1.2 || level_1[heroLocation1+1] == 1.4 || level_1[heroLocation1+1] == 2.1 || level_1[heroLocation1+1] == 2.3){
return true;
}else if(level_1[heroLocation1+1] == 2.4){
level_1[83] = 1.4;
}else{
return false;
}
}else{
return false;
}
return false;
}
/*
Movement variable legend:
1 = north
2 = west
3 = south
4 = east
*/
public static boolean tile_restriction(int heroLocation, int level, int movement){
heroLocation1 = heroLocation;
move = movement;
if(level == 1){
return level_1_restriction();
}else if(level == 2){
}else if(level == 3){
}else if(level == 4){
}else if(level == 5){
}
return false;
}
public static boolean encounter(){
double chance = Math.random() * 100;
System.out.println(chance);
if (chance >= 0 && chance <= 25){
//return true;
return false;
}else{
return false;
}
}
}
这是我朋友的两段代码,第一段是BattleFrame:
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BattleFrame extends JFrame {
BattleFrame () {
//Frame
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Battle Frame");
getContentPane().setBackground(Color.lightGray);
//battlePanel buttons
JButton buttonSkills = new JButton("Skills");
JButton buttonItems = new JButton("Items");
JButton buttonParty = new JButton("Party");
JButton buttonRun = new JButton("Run");
buttonSkills.setForeground(Color.BLUE);
buttonItems.setForeground(Color.BLUE);
buttonParty.setForeground(Color.BLUE);
buttonRun.setForeground(Color.BLUE);
//
//battlePanel
JPanel battlePanel = new JPanel();
battlePanel.add(buttonSkills, BorderLayout.CENTER);
battlePanel.add(buttonItems, BorderLayout.CENTER);
battlePanel.add(buttonParty, BorderLayout.CENTER);
battlePanel.add(buttonRun, BorderLayout.CENTER);
battlePanel.setBackground(Color.cyan.darker());
//
add(battlePanel, BorderLayout.SOUTH);
setVisible(true);
//Skills
buttonSkills.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SkillsFrame sf = new SkillsFrame();
dispose();
}
}
);
//Items
buttonItems.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ItemsFrame sf = new ItemsFrame();
dispose();
}
}
);
buttonRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Run effect
dispose();
}
}
);
}
}
class SkillsFrame extends JFrame {
SkillsFrame () {
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Skills Frame");
getContentPane().setBackground(Color.lightGray);
//SkillsPanel Buttons
JButton buttonWeakAttack = new JButton("Weak Attack");
JButton buttonNormalAttack = new JButton("Normal Attack");
JButton buttonPowerAttack = new JButton("Power Attack");
JButton buttonBack = new JButton("Back");
buttonWeakAttack.setForeground(Color.BLUE);
buttonNormalAttack.setForeground(Color.BLUE);
buttonPowerAttack.setForeground(Color.BLUE);
buttonBack.setForeground(Color.BLUE);
//
//Skills Panel
JPanel skillsPanel = new JPanel();
skillsPanel.add(buttonWeakAttack, BorderLayout.CENTER);
skillsPanel.add(buttonNormalAttack, BorderLayout.CENTER);
skillsPanel.add(buttonPowerAttack, BorderLayout.CENTER);
skillsPanel.add(buttonBack, BorderLayout.CENTER);
skillsPanel.setBackground(Color.cyan.darker());
//
add(skillsPanel, BorderLayout.SOUTH);
setVisible(true);
buttonWeakAttack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Weak Attack effects
}
}
);
buttonNormalAttack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Normal Attack effects
}
}
);
buttonPowerAttack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Power Attack effects
}
}
);
buttonBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BattleFrame bf = new BattleFrame();
dispose();
}
}
);
}
}
class ItemsFrame extends JFrame {
ItemsFrame () {
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Skills Frame");
getContentPane().setBackground(Color.lightGray);
JButton buttonPotion = new JButton("Potion");
JButton buttonTeleportScroll = new JButton("Teleport Scroll");
JButton buttonFireballScroll = new JButton("Fireball Scroll");
//ADD MORE ITEMS IF LIKE
buttonPotion.setForeground(Color.BLUE);
buttonTeleportScroll.setForeground(Color.BLUE);
buttonFireballScroll.setForeground(Color.BLUE);
//ItemsPanel
JPanel itemsPanel = new JPanel();
itemsPanel.add(buttonPotion, BorderLayout.CENTER);
itemsPanel.add(buttonTeleportScroll, BorderLayout.CENTER);
itemsPanel.add(buttonFireballScroll, BorderLayout.CENTER);
add(itemsPanel, BorderLayout.SOUTH);
setVisible(true);
buttonPotion.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Potion effects
}
}
);
buttonTeleportScroll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Teleport Scroll effects
}
}
);
buttonFireballScroll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Fireball Scroll Effects
}
}
);
}
}
以及如何调用它在这里:
import java.awt.*;
public class BattleFrameUsage {
int herolocation, maplevel, direction;
BattleFrameUsage(){}
BattleFrameUsage(int hero, int level, int move) {
herolocation = hero;
maplevel = level;
direction = move;
new BattleFrame();
if (direction == 1){
herolocation -= 12;
new MapFrame(herolocation, maplevel);
}else if (direction == 2){
herolocation -= 1;
new MapFrame(herolocation, maplevel);
}else if (direction == 3){
herolocation += 12;
new MapFrame(herolocation, maplevel);
}else if (direction == 4){
herolocation += 1;
new MapFrame(herolocation, maplevel);
}
}
public static void main (String[] args) {}
}
我承认我们的效率很低,但是我们没有多少时间让它变得如此棒。 欢迎任何帮助。
如前所述,您可以使用Singleton将所有游戏数据保持在一起,然后逐帧切换,显示从该单例中提取的变量呈现的不同视图。
但。
您可能想要了解另一种模式。 它被称为模型 - 视图 - 控制器 。
这里的基本概念是从视图 (如何显示,游戏屏幕)和控制器 (如何与视图交互以及如何与视图交互)中抽象模型 (数据,在您的情况下:游戏信息)该模型)
在您的情况下,视图将是地图框架。 地图框访问模型(TheGame)以渲染英雄的位置(它通过调用getHeroPosition()
方法从TheGame中检索)
MapFrame的KeyListeners是控制器的一部分 - 它们在模型(TheGame)和视图之间进行接口。 如果控制器点击“向下”按钮,它将在模型上调用方法'goSouth()',然后告诉视图更新。 通过向模型询问Hero#s的位置,可以做到这一点。
如果通过向南移动,英雄会遇到一个恶棍,控制器会调出BattleView,然后向模型询问Hero和Villain的特征......
由于您已经作为一个团队工作(这很棒! ),您可以获取您拥有的代码并将这些角色中的一个分配给彼此。
法师将是观点。 您可以询问模型(让我们称她为Molly)获取信息,您可以告诉 Controller(Conny)您的视图中发生了什么。
莫莉将成为典范。 她掌握有关谁,什么,在哪里的所有信息。 这是你询问的那种“变量商店”。 她告诉Mage每个人都在哪里并接收来自Conny的更新
Conny接到了Mage的通知,将他们传递给了Molly并告诉Mage改变了一些东西。
如果以这种方式设置它,您的代码将更易读,更容易扩展,因为角色 - 哪个类必须执行整个部分更清晰。
祝你的游戏好运。 需要测试人员吗?
是的,有一种模式与您描述的模式相匹配。 它被称为Singleton模式 。 模式背后的基本要点是,只会创建一个Singleton类的实例,这意味着您附加到它的任何变量都可以从代码中的任何位置访问。
但是 ,重要的是要指出,当应该应用其他软件设计原则时,通常会使用Singleton模式。 在您的情况下,数据绑定或提升事件可能是正确的行动方案。 一般来说,你只想在绝对必要的时候创建一个Singleton - 如果你有很多Singletons(或者一个附带了很多变量的Singleton),那就是你的代码结构不正确的好兆头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.