Please help I'm kinda lost in my coding.
So I've created a bullet class:
package javagame.states;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;
public class Bullet {
private Vector2f pos;
private Vector2f speed;
private int lived = 0;
private boolean aktiv = true;
private static int MAX_LIFETIME = 2000;
public Bullet (Vector2f pos, Vector2f speed){
this.pos = pos;
this.speed = speed;
}
public Bullet(){
aktiv = false;
}
public void update(int t){
rotation++;
if(aktiv){
Vector2f realSpeed = speed.copy();
realSpeed.scale((t/1000.0f));
pos.add(realSpeed);
lived += t;
if(lived > MAX_LIFETIME) aktiv = false;
}
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer gc, Graphics g) throws SlickException {
if(aktiv){
g.setColor(Color.red);
g.fillOval(pos.getX(), pos.getY(), 10, 10);
}
}
public boolean isAktiv(){
return aktiv;
}
}
then called the bullet class here in my robot class:
package javagame.states;
import java.util.Iterator;
import java.util.LinkedList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Robot2 extends BasicGameState{
private LinkedList<Bullet> bullets;
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
bullets = new LinkedList<Bullet>();
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
for(Bullet b : bullets){
b.render(gc, g);
}
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Iterator<Bullet> i = bullets.iterator();
while(i.hasNext()){
Bullet b = i.next();
if(b.isAktiv()){
b.update(delta);
}else{
i.remove();
}
}
Input input = gc.getInput();
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
int xDistance = (int) (RobotX() + Robotwidth() / 2 - mouseX); //RobotX is the robot image x position same to RobotY
int yDistance = (int) (RobotY() + Robotheight() / 2 - mouseY);
double angleToTurn = Math.toDegrees(Math.atan2(yDistance, xDistance));
if(mouseX != 0){
angleToTurn += 270;
}else{
if(mouseY != 0)
angleToTurn += 180;
}
robot.setRotation((float) angleToTurn); //robot is an image. didn't included the code here to shorten my codes
if(gc.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)){
bullets.add(new Bullet(new Vector2f(Sprites.getRobotX(), Sprites.getRobotY()), new Vector2f(mouseX, mouseY)));
}
}
#Override
public int getID() {
return States.ROBOT;// has an integer value
}
}
the robot is rotating perfectly according to mouse's cursor movement but when I hit the mouse's left button to fire a bullet, it doesn't follow the mouse's cursor location. This is what I've got when i run these codes and pressing the left button of my mouse. Please check my sample image.
http://s12.postimg.org/kwsr41p71/Untitled_1.jpg
What I want to achieve is to correct the direction of the bullet according to mouse cursor location.
Any help would be greately much appreciated. Thank you very much.
Related
I want to make the code so that when I click somewhere in the container, I create a new image with the coordinates of mouseX and Y of where I clicked.
I tried to do this, but I'm not sure how I will draw a new image, without the old image getting the same coordinates as the new one, and I'm not sure if I'm even creating different images right now, or if it's just the same image I'm changing the coordinates of whenever I click.
I was thinking about creating an array list of images, and then whenever I click somewhere, it adds a new image to the list, and then the render just keeps rendering the entire list, but here I'm not sure how to tell the render where each image was clicked (the coordinates).
Here's what I have so far, I'd really appreciate if someone could help me. Let me know if there's something you need me to clarify :)
package example;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class SimpleSlickGame extends BasicGame
{
public String mouseCoords;
public String testClick ="Nothing clicked";
public int mouseX;
public int mouseY;
public SimpleSlickGame(String gamename)
{
super(gamename);
}
#Override
public void init(GameContainer gc) throws SlickException {
mouseCoords = "";
}
#Override
public void update(GameContainer gc, int i) throws SlickException {
int mouseX = Mouse.getX();
int mouseY = Mouse.getY();
mouseCoords = "Mouse X: "+ mouseX + " Mouse Y: "+ mouseY;
}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException
{
g.drawString(mouseCoords, 250, 200);
g.drawRect(100, 100, 100, 100);
g.drawString(testClick, 200, 400);
g.drawImage(new Image("images/house.png"), mouseX, mouseY);
}
public void mousePressed(int button, int x, int y){
mouseX = x;
mouseY = y;
if (button == 0){
if((x> 100 && x<200) && (y > 100 && y < 200)){
testClick = "inside box";
}
else{
testClick = "outside box";
}
}
}
public static void main(String[] args)
{
try
{
AppGameContainer appgc;
appgc = new AppGameContainer(new SimpleSlickGame("Simple Slick Game"));
appgc.setDisplayMode(600, 600, false);
appgc.start();
}
catch (SlickException ex)
{
Logger.getLogger(SimpleSlickGame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Ok so I did some reading of documents and googling and found a solution :P, although I'm not sure if it's the most optimal way of doing this (would really like some critique!!)
Here's what I did:
The main class:
package example;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class SimpleSlickGame extends BasicGame
{
public String mouseCoords;
public String testClick ="Nothing clicked";
public int mouseX;
public int mouseY;
private House[] house = new House[10];
public List<House> houses = new ArrayList<House>();
public House houseTest, houseTest2, houseTest3;
public SimpleSlickGame(String gamename)
{
super(gamename);
}
#Override
public void init(GameContainer gc) throws SlickException {
mouseCoords = "";
}
#Override
public void update(GameContainer gc, int i) throws SlickException {
int mouseX = Mouse.getX();
int mouseY = Mouse.getY();
mouseCoords = "Mouse X: "+ mouseX + " Mouse Y: "+ mouseY;
}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException
{
g.drawString(mouseCoords, 250, 200);
g.drawRect(100, 100, 100, 100);
g.drawString(testClick, 200, 400);
for(int i = 0; i<houses.size();i++){
if(houses.get(i) != null){
houses.get(i).render(gc, g);
}
}
}
public void mousePressed(int button, int x, int y){
mouseX = x;
mouseY = y;
if (button == 0){
if((x> 100 && x<200) && (y > 100 && y < 200)){
testClick = "inside box";
houses.add(new House(mouseX,mouseY));
}
else{
testClick = "outside box";
}
}
}
public static void main(String[] args)
{
try
{
AppGameContainer appgc;
appgc = new AppGameContainer(new SimpleSlickGame("Simple Slick Game"));
appgc.setDisplayMode(600, 600, false);
appgc.start();
}
catch (SlickException ex)
{
Logger.getLogger(SimpleSlickGame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
House class:
package example;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
public class House {
int x;
int y;
int width = 20;
int height = 20;
House(int x, int y){
this.x = x;
this.y = y;
}
public void render(GameContainer gc, Graphics g) throws SlickException
{
g.setColor(Color.white);
g.drawRect(x, y, width, height);
}
}
so I'm trying to write a flappy bird like coin collector game, and when I try to set the coin images coordinates to a random number the screen freezes. Any help?
Heres my player class:
package gamepackage;
import java.util.Random;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;
public class Playing extends BasicGameState{
Image coin;
Image guy;
int coincounter = 0;
float x = 30, y = 90;
int coiny = 20, coinx= 1000;
public Playing(int state){
}
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
guy = new Image("res/PlaneMovie1.gif");
coin = new Image("res/coin.gif");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.drawString("Coins:" + coincounter, 70, 70);
g.drawImage(coin, coinx, coiny);
g.drawImage(guy, x, y);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
coinx --;
if(input.isKeyDown(Input.KEY_SPACE)){ y -= 1 ; }
if(input.isKeyDown(Input.KEY_SPACE) == false){ y += .5;}
if(y == coiny){ coincounter += 1;coinx = 1000; coiny -= 20;}
if(coinx == -175){coinx = 1000; coiny += 20;}
}
public int getID(){
return 1;
}
}
I cannot find an answer in Google, but is there a Mouse Over Area for Slick2D? Google just gives me results of Java's MouseOverArea. I just want to know if there is a MouseOverArea for Slick2D, and how it looks like.
Here is my code:
Game class
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static final String gamename = "Life - Alpha";
public static int splash = 0;
public static int menu = 1;
public static int loading = 2;
public static int play= 3;
public Game(String gamename) {
super(gamename);
}
public void initStatesList(GameContainer gc) throws SlickException {
this.addState(new SplashScreen (splash));
this.addState(new Menu (menu));
this.addState(new Exit (exit));
this.addState(new Loading (loading));
this.addState(new Play(play));
this.enterState(0);
}
public static void main(String[] args) {
AppGameContainer app;
try {
app = new AppGameContainer(new Game(gamename));
app.setDisplayMode(800, 600, false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
SplashScreen class:
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class SplashScreen extends BasicGameState {
Image splash;
private int elapsedTime;
private final int DELAY = 3000;
public SplashScreen(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
splash = new Image("res/SplashScreen.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawImage(splash, 0, 0);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(1);
}
}
public int getID() {
return 0;
}
}
Menu class:
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
public Menu(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Background.png");
g.drawImage(background, 0, 0);
Image logo = new Image("res/Logo.png");
g.drawImage(logo, 275, 50);
Image playButton = new Image("res/Play button.png");
g.drawImage(playButton, 210, 250);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int xpos = Mouse.getX();
int ypos = Mouse.getY();
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
//I want to put the Slick2D MouseOverArea code here...
//So then when I put the mouse over the playButton, something will display.
}
}
public int getID() {
return 1;
}
}
Loading class:
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Loading extends BasicGameState {
private int elapsedTime;
private final int DELAY = 5000;
public Loading(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Back.png");
g.drawImage(background, 0, 0);
Image loading = new Image("res/Loading.png");
g.drawImage(loading, 210, 150);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(3);
}
}
public int getID() {
return 2;
}
}
Play class:
I'm working on it still... but this class doesn't need the MouseOverArea, the Menu class does.
So that was my code above. I just need a MouseOverArea for Slick2D. Google doesn't help. Hope you can.
Also, can you have a TextField in Slick2D? I don't know if I can. I know in normal Java you can, but can you in Slick2D?
If there are any mistakes, don't worry, I can fix them.
Thanks
Isn't the code put in here?
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
//mouseover
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
}
}
so I've been working on getting my boxes to save their position in an array all day and finally thought i came up with something (with a lot of help from you guys) and it just isn't working... can someone please tell me why?
Control class:
import java.awt.Point;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Control extends BasicGameState {
public static final int ID = 1;
public Methods m = new Methods();
public Point[] point = new Point[(800 * 600)];
int pressedX;
int pressedY;
int num = 0;
public void init(GameContainer container, StateBasedGame game) throws SlickException{
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
m.drawParticle(pressedX, pressedY);
}
public void update(GameContainer container, StateBasedGame game, int delta) {
}
public void mousePressed(int button, int x, int y) {
pressedX = x;
pressedY = y;
num = num + 1;
point[num].x = pressedX;
point[num].y = pressedY;
}
public int getID() {
return ID;
}
}
Methods class:
import org.newdawn.slick.Graphics;
public class Methods {
public Graphics g = new Graphics();
public int sizeX = 1;
public int sizeY = 1;
public void drawParticle(float x, float y){
g.drawRect(x, y, sizeX, sizeY);
}
}
While you've initialised the size of the point array, you've not initialised the contents.
public void mousePressed(int button, int x, int y) {
pressedX = x;
pressedY = y;
num++;
point[num] = new Point(pressedX, pressedY);
}
Also think in your render method, you need to re-render the graphics (I could be mistaken, I've not used Slick2D before)...
Withing something like...
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (Point p : point) {
if (p != null) {
m.drawParticle(p.x, p.y);
}
}
m.drawParticle(pressedX, pressedY);
}
I'm also curious about you creating you're own Graphics, especially when the render method passes you one, you may want to check into that further and make sure that this is acceptable...
I'm trying to make a java sand game and can't get past one bit. i've made my method that draws a rectangle at mouseX and mouseY, and i have set it up so it updates every frame so it constantly follows the mouse.
what i assume is that i would use an array to create each rectangle, and from there would use a pre-defined algorithm to float to the ground, I'm all good with that, i just don't understand how to 'hook my method' up to an array.
This is the method i am using to draw the rectangle (in it's own class called Methods)
import org.newdawn.slick.Graphics;
public class Methods {
public Graphics g = new Graphics();
public int sizeX = 4;
public int sizeY = 4;
public void drawParticle(float x, float y){
g.drawRect(x, y, sizeX, sizeY);
}
}
And this is my main class
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Control extends BasicGameState {
public static final int ID = 1;
public Methods m = new Methods();
int mouseX;
int mouseY;
public void init(GameContainer container, StateBasedGame game) throws SlickException{
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
m.drawParticle(mouseX, mouseY);
}
public void update(GameContainer container, StateBasedGame game, int delta) {
}
public void mouseReleased(int button, int x, int y){
mouseX = 0;
mouseY = 0;
}
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
mouseX = newx;
mouseY = newy;
}
public int getID() {
return ID;
}
}
but when i click, just one rectangle follows the mouse, instead of many being created AT the mouse :L
Public Variables:
Rectangle boxes[] = new Rectangle[maxnum];
int boxnum = 0;
On mouse move:
boxes[boxnum] = new Rectangle[e.getX(), e.getY(), sizeX, sizeY);
boxnum = boxnum + 1;
When drawing your particles:
counter = 0;
do
{
g.drawRect(boxes[counter].x, boxes[counter].y, sizeX, sizeY);
counter = counter + 1;
} while (counter < maxnum);
Where maxnum is the maximum number of boxes you will have. This way you can store multiple rectangles in your array and go through the array and draw them when you update the screen. Hope this helps.