This site requires JavaScript, please enable it in your browser!
Greenfoot back
Izzmemitch
Izzmemitch wrote ...

2017/1/5

Object out of balance

1
2
3
4
Izzmemitch Izzmemitch

2017/1/5

#
hi im working on this game with my group at school and i need a code to get a ship out of balance, can someone help me out?
danpost danpost

2017/1/5

#
Izzmemitch wrote...
hi im working on this game with my group at school and i need a code to get a ship out of balance, can someone help me out?
What view is the game (top-down, side-view)? If side-view, do you see side, front or back of ship? What is the current code for Ship? What behavior is the ship to have when out of balance?
Izzmemitch Izzmemitch

2017/1/6

#
its sideview...
Izzmemitch Izzmemitch

2017/1/6

#
https://instagram.com/p/BO6rYdPjwNg/ ..tbh im just starting to code and just starting with greenfoot. its a schoolproject that im doing with classmates and everyone has to do a bit of coding.
Izzmemitch Izzmemitch

2017/1/6

#
this is what we currently have import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.*; /** * Write a description of class MiniGame2World here. * * @author (your name) * @version (a version number or a date) */ public class MiniGame2World extends World { GameState globalGameState; MG2Ship ship; /** * Constructor for objects of class MiniGame2World. * */ public MiniGame2World() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1280, 720, 1, false); prepare(); } public void setGameState(GameState gamestate) { this.globalGameState = gamestate; } public GameState getGameState() { return this.globalGameState; } private void prepare() { MG2Quay quay = new MG2Quay(); addObject(quay, 640,680); MG2Train train = new MG2Train(); addObject(train,1100,620); this.ship = new MG2Ship(this); addObject(ship,400,615); MG2Hook hook = new MG2Hook(); addObject(hook,450,-250); } public void act() { buttonPressed(); } public void buttonPressed() { if(Greenfoot.isKeyDown("escape")) { int n = JOptionPane.showConfirmDialog(null, "Do you really want to exit to the main menu?", "Exit", JOptionPane.YES_NO_OPTION); if (n == 0) { MainMenuWorld newWorld = new MainMenuWorld(); newWorld.setGameState(this.getGameState()); Greenfoot.setWorld(newWorld); } } } public MG2Ship getShip() { return this.ship; } public void win() { int n = JOptionPane.showConfirmDialog(null, "You won! Do you want to go to minigame 3?", "Winner!", JOptionPane.YES_NO_OPTION); this.getGameState().setFinished(2, 0); if (n == 0) { MiniGame3World newWorld = new MiniGame3World(); newWorld.setGameState(this.getGameState()); Greenfoot.setWorld(newWorld); } else { MainMenuWorld newWorld = new MainMenuWorld(); newWorld.setGameState(this.getGameState()); Greenfoot.setWorld(newWorld); } } }
danpost danpost

2017/1/6

#
danpost wrote...
What is the current code for Ship? What behavior is the ship to have when out of balance?
These questions have not been answered. Plus, one more thing, now: I presume that adding the crates in some incorrect manner will be the cause of the unbalance. So, you need to describe how the ship is to react in different arrangements of crates.
Izzmemitch Izzmemitch

2017/1/6

#
the current code for the ship i dont have yet... you mean the off balance coding for the ship right? the ships behaviour is to capsize either left or right, depending on where the containers are taken off of the ship. ok so i am to write a code which will get the ship out of balance if: for example the containers on the left are stacked higher than on the right, then the ship is to capsize a bit. if capsized any further the player will lose. also one of the other criteria is that the player is to be warned if the ship is capsizing too much. its a game which requires the player to get the containers from the ship to load on to the cargo train as fast as possible with the crane. this because the player will be competing the computer. hope this makes sense
danpost danpost

2017/1/6

#
Izzmemitch wrote...
the ships behaviour is to capsize either left or right, depending on where the containers are taken off of the ship.
The first thing that is needed is for the ship to know what containers are on it. You will need a method that can be called when a container is picked-up by the hook, call it 'removingContainer'. Another method you might need is 'addingContainer' for when a container is replaced to correct an unbalanced state. Both these method should calculate the new balance and tilt the ship appropriately. After setting the new balance state, it should be checked to provide the warning when needed. I do not know how in-depth you need to go with this -- or, what level of experience you have in programming (I am guessing: not too much). I say this because tilting the ship is not going to move the containers with it and programming the movement of the containers with it is not beginner stuff (not when there is rotational factors involved). Maybe it would be better to just show a gauge that shows the tilt of the ship; but, somehow, I do not think that is what you wanted. I mean, if you were to go all out, you would have different weights for the crates and different forces on the ship depending on the location even if they were all the same weight. This could get quite involved. To keep things simple, the inner two stacks of crates can be counted as +/- 1 and the outer stacks as +/- 3; and the sum for all crates can be considered the tilt of the ship which capsizes at some +/- limit. These values (1 and 3) are for where the center of the ship is right in the center of the pile of crates (helping to keep things simple).
Izzmemitch Izzmemitch

2017/1/6

#
thanks for the reply and no my programming skills are equal to a padawans skills this is the ships code btw.. sorry for not grasping the question earlier public class MG2Ship extends Actor { private MG2Container containers; private int startX = 310; private int startY = 625; public MG2Ship(World world) { this.containers = new MG2Container; for(int i= 0; i < 4; i++){ for(int i2=0; i2 < 5; i2++){ if (i2 >= 3) { this.containers = null; } else { MG2Container container = new MG2Container(false, null, (MiniGame2World)world); this.containers = container; world.addObject(container, this.startX + (i*100), this.startY - (i2*66)); } } } }
danpost danpost

2017/1/6

#
Now, add the methods for removing and adding crates. We can add the balancing code to them later. Show what you wrote. The method signatures should be 'removeContainer(MG2Container)' and 'addContainer(MG2Container)'.
Izzmemitch Izzmemitch

2017/1/6

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MG2Ship here. * * @author (your name) * @version (a version number or a date) */ public class MG2Ship extends Actor { private MG2Container containers; private int startX = 310; private int startY = 625; public MG2Ship(World world) { this.containers = new MG2Container; for(int i= 0; i < 4; i++){ for(int i2=0; i2 < 5; i2++){ if (i2 >= 3) { this.containers = null; } else { MG2Container container = new MG2Container(false, null, (MiniGame2World)world); this.containers = container; world.addObject(container, this.startX + (i*100), this.startY - (i2*66)); } } } } /** * Act - do whatever the MG2Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } public int getStartX() { return this.startX; } public int getStartY() { return this.startY; } public MG2Container getRandomContainer() { MG2Container allContainers = new MG2Container; int currentIndex = 0; for(int i=0; i < this.containers.length; i++){ for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { allContainers = this.containers; currentIndex++; } } } if (currentIndex > 0) { int randomIndex = Greenfoot.getRandomNumber(currentIndex); return allContainers; } return null; } public void removeContainer(MG2Container container) { for(int i=0; i < this.containers.length; i++){ for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null && container == this.containers) { this.containers = null; break; } } } } public boolean spaceAtRow(int i) { int containersInRow = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { containersInRow++; } } if (containersInRow == 5) { return false; } return true; } public void addContainer(int i, MG2Container container) { int containersInRow = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { containersInRow++; } } this.containers = container; container.setLocation(this.startX + (i*100), this.startY - (containersInRow*66)); } public boolean isLastContainerInColumn(MG2Container container) { for(int i=0; i < this.containers.length; i++){ boolean foundContainer = false; int myIndex = 0; int lastIndex = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers == container) { foundContainer = true; myIndex = i2; } if (this.containers != null) { lastIndex = i2; } } if(foundContainer && myIndex == lastIndex) { return true; } } return false; } }
danpost danpost

2017/1/6

#
Now, you will need a field to track the weight distribution of the containers. Its value can be kept updated if you only use the methods addContainer and removeContainer to add and remove the containers (including the initial ones being added) by adjusting the value of the field within those methods. You can use the formula 'n = (i*2)-3', where 'n' is the adjustment whose value will be in the set { -3, -1, 1, 3 } depending on where the container is placed; or '-n' if removed.
Izzmemitch Izzmemitch

2017/1/8

#
hey @danpost, me and my teammembers agreed on using a level bar instead of making the ship tilt. That will be too difficult. Making this level bar is way easier but i still havent got a clue on how to start. i was thinking that i would have to make a level bar first in photoshop, then add that to the world and right a code for this level bar. am i going in the right direction with this?
danpost danpost

2017/1/8

#
Izzmemitch wrote...
i would have to make a level bar first in photoshop, then add that to the world and right a code for this level bar. am i going in the right direction with this?
You could do that; or, you could programmatically create the images needed. Yes, there will be two images needed; one for the background of the gauge and the other for the level part (the moving part). That does not mean two actors are needed; just that the gauge actor will require both each time it updates, rotating the image of the level and drawing it on background.
Izzmemitch Izzmemitch

2017/1/8

#
https://www.dropbox.com/s/us1p76dhpw6zy0v/Level%20Sensor.jpeg?dl=0 this is what i have so far these are indeed two images and i'd like to resize them i saw on https://www.greenfoot.org/files/javadoc/that this is the scale code public void scale(int width, int height) how do i get it to work
There are more replies on the next page.
1
2
3
4