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

2021/12/14

Shooter game

Pie8756 Pie8756

2021/12/14

#
Hello I'm trying to create a game that allows you to shoot at each other. so kind of like a first-person shooter. I am using the controls from the asteroid game in the book but the code is not working in my favor. Any idea what is wrong? Here is the code for flaco one of the characters public class Flaco extends Mover { private int gunReloadTime; // The minimum delay between firing the gun. private int reloadDelayCount; // How long ago we fired the gun the last time. private int shotsFired; // Number of shots fired. private Vector acceleration; public Flaco() { gunReloadTime=5; reloadDelayCount=0; shotsFired=0; acceleration=new Vector (0,0.3); } /** * Act - do whatever the Flaco wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("Left")) { moveLeft(); } if (Greenfoot.isKeyDown("Right")) { moveRight(); } if (Greenfoot.isKeyDown("Down")) { moveDown(); } if (Greenfoot.isKeyDown("Up")) { moveUp(); } if(Greenfoot.isKeyDown("space")) { } } public int getShotsFired() { return shotsFired; } public void setGunReloadTime(int reloadTime) { gunReloadTime=reloadTime; } public Vector getMovement() { return movement; } private void fire() { if (reloadDelayCount >= gunReloadTime) { Bullet b = new Bullet(getMovement().copy(), getRotation()); getWorld().addObject(b, getX(), getY()); b.move(); shotsFired++; reloadDelayCount = 0; // time since last shot fired } } }
KIITAKII KIITAKII

2021/12/15

#
I'm not entirely sure if I'm correct but: - Unless it's declared else where, I don't think MoveUp, MoveDown/Left/Right are Greenfoot methods, instead you can try: • move(x) : if x is a positive number, it'll move forward, if x is negate, it'll move backward (according to its rotation) • setLocation( getX()+x, getY()+x) : similar to move(), but you can also go in the Y axis - Here's what that might look like: if(Greenfoot.isKeyDown("up")) setLocation(getX()+5, getY()-5); //make sure you have the () after getX and getY otherwise it won't work //if you're going up, a negative integer is needed to decrease the distance from the Bottom of the world if(Greenfoot.isKeyDown("right")) move(5); if(Greenfoot.isKeyDown("down")) setLocation(getX(), getY()+5); if(Greenfoot.isKeyDown("left")) move(-5); .... - Lastly, you might wanna check to see if you have the Vector Class with the correct information in your Scenario Hopefully this helps, but just know that I'm not a master programmer lol
danpost danpost

2021/12/15

#
What do you mean by "the code is not working in my favor"? That is vague. How is it not what you want? What is it doing that is should not do and what should it do that it is not doing?
Pie8756 Pie8756

2021/12/15

#
to answer the first question I have a subclass of actor that is called mover and that is where I called all of the move()'s. here is the code as well because I am having some issues with the moving and the Y axis and X axis are not liking the double that I put next to it although I did an int at the top. public abstract class Mover extends Actor { int x=0; int y; public Vector movement = new Vector(); /** * Act - do whatever the Mover wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { } //public Mover(Vector speed) { //movement = speed; } public void moveRight() { // setLocation(getX() + zoom, getY()); } public void moveUp() { //setLocation(getX(), getY() + zoom); } public void moveDown() { //setLocation(getX(), getY()-zoom); } public void moveLeft() { //setLocation(getX() - zoom, getY()); } public void moveAll() { double x=x+movement.getX(); double y=y+movement.getY(); if(x>= getWorld().getWidth()) x=0; if(x<0) x=getWorld().getWidth()-1; if(y>=getWorld().getHeight()) y=0; if(y<0) y=getWorld().getHeight()-1; } }
danpost danpost

2021/12/15

#
Pie8756 wrote...
the Y axis and X axis are not liking the double that I put next to it
Again, this is vague. Is it underlining something? If so, what is being underlined and what error message do you get when hovering the mouse over it? Also, please provide the following: (1) Vector class codes; (2) your world constructor codes;
Pie8756 Pie8756

2021/12/15

#
public class Vector { double dx = 0; double dy = 0; int direction = 0; double length; /** * Create a default vector initialised to zero. */ public Vector() { } /** * Create a vector with given direction and length. */ public Vector(int direction, double length) { this.length = length; this.direction = direction; dx = length * Math.cos(Math.toRadians(direction)); dy = length * Math.sin(Math.toRadians(direction)); } /** * Set the direction of this vector. */ public void setDirection(int direction) { this.direction = direction; dx = length * Math.cos(Math.toRadians(direction)); dy = length * Math.sin(Math.toRadians(direction)); } /** * Add another vector to this vector. */ public void add(Vector other) { dx += other.dx; dy += other.dy; this.direction = (int) Math.toDegrees(Math.atan2(dy, dx)); this.length = Math.sqrt(dx*dx+dy*dy); } /** * Return the x offset of this vector. */ public double getX() { return dx; } /** * Return the y offset of this vector. */ public double getY() { return dy; } /** * Return the current direction (in degrees). */ public int getDirection() { return direction; } /** * Return the current length of the vector. */ public double getLength() { return length; } /** * Create a copy of this vector. */ public Vector copy() { Vector copy = new Vector(); copy.dx = dx; copy.dy = dy; copy.direction = direction; copy.length = length; return copy; } } Here is my Vector class codes and here is the world code public World(int worldWidth, int worldHeight, int cellSize, boolean bounded) worldWidth - The width of the world (in cells). worldHeight - The height of the world (in cells). cellSize - Size of a cell in pixels. bounded - Should actors be restricted to the world boundary? I didn't really know exactly what you meant by that but I think that might be it. for the error messages that are showing up in Mover, it says variable y and Variable x might not have been initialized. So that is why I wrote the integers at the top. Also in flaco in private void fire() on .move() it says method move in class greenfoot.Actor cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length hopefully that is less vague
danpost danpost

2021/12/16

#
Pie8756 wrote...
here is the world code << World(int, int, boolean) API description Omitted >> I didn't really know exactly what you meant by that but I think that might be it.
No. Your code in your subclass of World where you create YOUR world.
for the error messages that are showing up in Mover, it says variable y and Variable x might not have been initialized. So that is why I wrote the integers at the top.
Remove first two lines in Mover class (int x=0; int y;). Use:
double x = movement.getX();
double y = movement.getY();
(you were using the declared variable inside the formula to initialize it)
Also in flaco in private void fire() on .move() it says method move in class greenfoot.Actor cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length
Need to see the Bullet class codes for this one.
Pie8756 Pie8756

2021/12/16

#
Alright the code at the top that you gave me fixed the initial error but now the new code is saying that it is an illegal forward reference.
danpost wrote...
Pie8756 wrote...
here is the world code << World(int, int, boolean) API description Omitted >> I didn't really know exactly what you meant by that but I think that might be it.
No. Your code in your subclass of World where you create YOUR world. here is the Myworld code I haven't really put much into it. public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } } And as well as the Bullet code public class Bullet extends Mover { /** A bullet looses one life each act, and will disappear when life = 0 */ private int life = 30; /** The damage this bullet will deal */ private int damage = 16; public Bullet() { } public Bullet(Vector speed, int rotation) { super(speed); setRotation(rotation); increaseSpeed(new Vector(rotation, 15)); Greenfoot.playSound("EnergyGun.wav"); } /** * The bullet will damage asteroids if it hits them. */ public void act() { if(life <= 0) { getWorld().removeObject(this); } else { move(); Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class); if (asteroid != null) { getWorld().removeObject(this); asteroid.hit(damage); } else { life--; } } } } the error that is showing up in Bullet is the Super(speed); it is saying the same error message as in Flaco and not that I look at it super is also in My world so it might have to do with something in there.
danpost danpost

2021/12/16

#
In the Mover class above, you have lines commented out that probably should not be. Make sure that you have the following in that class:
public Mover() {}

public Mover(Vector speed)
{
    movement = speed;
}
You need to login to post a reply.