SO basically i have been trying to get my bullet to work, using some helpful code i found on the internet i was able to make my player turn and add my own touches but when i do the bullet i can not figure out how to make it work with the smooth mover and vector classes. if someone could instruct me on how to make the bullet fly properly that would be appreciated :D
Code snippets:
Player
Bullet
SmoothMover
Vector
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author Carlan * @version Beta NO F****** WORKING!@#@! */ public class Player extends SmoothMover { private GreenfootImage Up; private GreenfootImage Down; private GreenfootImage Left; private GreenfootImage Right; private static final int gunReloadTime = 5 ; private int reloadDelayCount; public boolean Facing_up; public boolean Facing_left; public boolean Facing_down; public boolean Facing_right; /** * Initilise */ public Player() { reloadDelayCount = 5 ; } /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeys(); test(); } /** * Make all of the boolean variables for directions false */ public void makeFalse() { Facing_up = false ; Facing_down = false ; Facing_left = false ; Facing_right = false ; } /** * Check the which keys are imputed to control the player */ public void checkKeys() { Up = new GreenfootImage( "Player up.png" ); Down = new GreenfootImage( "Player down.png" ); Left = new GreenfootImage( "Player left.png" ); Right = new GreenfootImage( "Player right.png" ); int x = getX(); int y = getY(); if (Greenfoot.isKeyDown( "w" )) { //setImage(Up); //this shows which way the player is facing y = y + - 5 ; //this increases the coordinates to prepare for movement setLocation(x, y); //this resets the players location to the new coordinates makeFalse(); //may be unimplimented (used for bulllet rotation) Facing_up = true ; //may be unimplimented (used for bulllet rotation) } else if (Greenfoot.isKeyDown( "a" )) { //setImage(Left); x = x + - 5 ; setLocation(x, y); makeFalse(); Facing_left = true ; } else if (Greenfoot.isKeyDown( "s" )) { //setImage(Down); y = y + 5 ; setLocation(x, y); makeFalse(); Facing_down = true ; } else if (Greenfoot.isKeyDown( "d" )) { //setImage(Right); x = x + 5 ; setLocation(x, y); makeFalse(); Facing_right = true ; } else if (Greenfoot.isKeyDown( "space" )) { fire(); } } /** * Fire a bullet if the gun is ready. */ private void fire() { if (reloadDelayCount >= gunReloadTime) { Bullet bullet = new Bullet (getMovement().copy(), getRotation()); getWorld().addObject (bullet, getX(), getY()); bullet.move(); reloadDelayCount = 0 ; } } /** * Test Method */ public void test() { // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse != null ) setRotation(( int )( 180 *Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI)); //move(speed); if (Greenfoot.mouseClicked( null )) { getWorld().addObject( new bullet(getRotation()),getX(),getY()); turnTowards(mouse.getX(), mouse.getY()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import greenfoot.*; /** * Carlans Bullet crap * It * doesnt like to work nicely, BEWARE */ public class Bullet extends SmoothMover { private int direction; private int life = 30 ; public Bullet(Vector speed, int rotation) { super (speed); setRotation(rotation); addForce( new Vector(rotation, 15 )); } /** * The bullet will do stuff */ public void act() { if (life <= 0 ) { getWorld().removeObject( this ); } else { life--; //move(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A variation of an actor that maintains a precise location (using doubles for the co-ordinates * instead of ints). This allows small precise movements (e.g. movements of 1 pixel or less) * that do not lose precision. * * @author Poul Henriksen * @author Michael Kolling * @author Neil Brown * * @version 3.0 */ public abstract class SmoothMover extends Actor { private Vector movement; private double exactX; private double exactY; /** * Create new thing initialised with given speed. */ public SmoothMover(Vector movement) { this .movement = movement; } /** * Increase the speed with the given vector. */ public void addForce(Vector force) { movement.add(force); } /** * Move forward by the specified distance. * (Overrides the method in Actor). */ @Override public void move( int distance) { move(( double )distance); } /** * Move forward by the specified exact distance. */ public void move( double distance) { double radians = Math.toRadians(getRotation()); double dx = Math.cos(radians) * distance; double dy = Math.sin(radians) * distance; setLocation(exactX + dx, exactY + dy); } /** * Set the location using exact coordinates. */ public void setLocation( double x, double y) { exactX = x; exactY = y; super .setLocation(( int ) (x + 0.5 ), ( int ) (y + 0.5 )); } /** * Set the location using integer coordinates. * (Overrides the method in Actor.) */ @Override public void setLocation( int x, int y) { exactX = x; exactY = y; super .setLocation(x, y); } /** * Return the exact x-coordinate (as a double). */ public double getExactX() { return exactX; } /** * Return the exact y-coordinate (as a double). */ public double getExactY() { return exactY; } /** * Return the current speed. */ public Vector getMovement() { return movement; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import greenfoot.Greenfoot; /** * A 2D vector. * * @author found on internet :o * @author * * @version 6.9 */ public final class Vector { double dx; double dy; int direction; double length; /** * Create a new, neutral vector. */ public Vector() { } /** * Create a vector with given direction and length. The direction should be in * the range [0..359], where 0 is EAST, and degrees increase clockwise. */ public Vector( int direction, double length) { this .length = length; this .direction = direction; updateCartesian(); } /** * Create a vector by specifying the x and y offsets from start to end points. */ public Vector( double dx, double dy) { this .dx = dx; this .dy = dy; updatePolar(); } /** * Set the direction of this vector, leaving the length intact. */ public void setDirection( int direction) { this .direction = direction; updateCartesian(); } /** * Add another vector to this vector. */ public void add(Vector other) { dx += other.dx; dy += other.dy; updatePolar(); } /** * Set the length of this vector, leaving the direction intact. */ public void setLength( double length) { this .length = length; updateCartesian(); } /** * Scale this vector up (factor > 1) or down (factor < 1). The direction * remains unchanged. */ public void scale( double factor) { length = length * factor; updateCartesian(); } /** * Set this vector to the neutral vector (length 0). */ public void setNeutral() { dx = 0.0 ; dy = 0.0 ; length = 0.0 ; direction = 0 ; } /** * Revert to horizontal component of this movement vector. */ public void revertHorizontal() { dx = -dx; updatePolar(); } /** * Revert to vertical component of this movement vector. */ public void revertVertical() { dy = -dy; updatePolar(); } /** * Return the x offset of this vector (start to end point). */ public double getX() { return dx; } /** * Return the y offset of this vector (start to end point). */ public double getY() { return dy; } /** * Return the direction of this vector (in degrees). 0 is EAST. */ public int getDirection() { return direction; } /** * Return the length of this vector. */ public double getLength() { return length; } /** * Update the direction and length fom the current dx, dy. */ private void updatePolar() { this .direction = ( int ) Math.toDegrees(Math.atan2(dy, dx)); this .length = Math.sqrt(dx*dx+dy*dy); } /** * Update dx and dy from the current direction and length. */ private void updateCartesian() { dx = length * Math.cos(Math.toRadians(direction)); dy = length * Math.sin(Math.toRadians(direction)); } /** * Return 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; } } |