Setting a picture to random coordinates? - java

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;
}
}

Related

Firing bullet from gun's current position to mouse x & y position

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.

Trouble with sprite animation slick2d

We are having trouble making our character animate in a java slick2d game. The tutorial I seen shows doing it how I have done it. Right now it only shows the 1st image when going left or right. It never cycles between the 2 images set in walkLeft and walkRight.
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends BasicGameState {
Animation cowboy;
Animation movingLeft;
Animation movingRight;
Image background;
boolean quit = false;
int[] animationDuration = {200,200};
float cowboyPositionX = 0;
float cowboyPositionY = -370;
float shiftX = cowboyPositionX + 200;
float shiftY = cowboyPositionY + 830;
public Game(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException {
background = new Image("images/background.png");
Image[] walkLeft = { new Image("images/cowboyleft1.png"), new Image("images/cowboyleft2.png") };
Image[] walkRight = { new Image("images/cowboyright1.png"), new Image("images/cowboyright2.png") };
movingLeft = new Animation(walkLeft, animationDuration, false);
movingRight = new Animation(walkRight, animationDuration, false);
cowboy = movingRight;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
background.draw(cowboyPositionX, cowboyPositionY);
cowboy.draw(shiftX, shiftY);
g.drawString("Cowboy's X:" + cowboyPositionX + "\nCowboy's Y: "
+ cowboyPositionY, 400, 20);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_LEFT)) {
cowboy = movingLeft;
cowboyPositionX += horizontalSpeed;
if (cowboyPositionX > 0) {
cowboyPositionX -= delta * horizontalSpeed;
}
}
if (input.isKeyDown(Input.KEY_RIGHT)) {
cowboy = movingRight;
cowboyPositionX -= horizontalSpeed;
if (cowboyPositionX < -2975.0) {
cowboyPositionX += delta * horizontalSpeed;
}
}
public int getID() {
return 1;
}
}
In your update method try inserting cowboy.update(delta); that should work. It worked for the program that I have been working on.

Java, Slick, An object that shouldnt move is moving?

In my game I'm making, I need NPC's/mobs.
I made a class called peasant (my first mob).
In the main class from which the game is running, I have all the information, as well as calling an object called mob1 from Peasant
I need to make it so that if the player is within 300 pixels of the mob, it starts to move towards it. Ive tried doing this but so far, even if the player is 2000 pixels away, the mob starts moving???
Here is my Peasant class
package Entitys.NPCs;
public class Peasant {
public Peasant(float InitX, float InitY, int MobID){
MobX = InitX;
MobY = InitY;
}
//This class shall be used as an object creator. This will randomly move a graphic around, near to a player
private float MobX;
private float MobY;
private int AmountMoved = 0;
private boolean MoveRight = true;
private boolean MoveLeft;
public boolean PlayerDetected = false;
long timer;
//Used to find the mobs X
public float getX(){
return MobX;
}
//Used to find the mobs Y
public float getY(){
return MobY;
}
//Used to set the mobs X
public void setX(float X){
MobX = X;
}
//Used to set the mobs Y
public void setY(float Y){
MobY = Y;
}
//Used to simply move the mob on its X co-ords
public void moveX(int delta){
MobX += delta*0.1f;
}
//Used to simply move the mob on its Y co-ords
public void moveY(int delta){
MobY += delta*0.1f;
}
public void autoEveryThing(int delta, float playerX, float playerY) {
// If the player has not been spotted the NPC/Mob will move left and
// right by 100 Pixels.
if(MobX-300<playerX){
PlayerDetected=true;
}
if(PlayerDetected=true){
if(MobX>playerX){
MobX-=delta*0.12;
}
}
}
}
And here is the main class:
package Worlds.World1;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
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.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import Entitys.NPCs.*;
import Main.SimpleMob;
public class World1A extends BasicGameState{
String mousePosition;
Image world;
Animation player, playerLeft, playerRight;
int[] duration = {200,200};
float playerX;
float playerY;
float WorldX;
float WorldY;
float PlayerVisibleScreenX;
float PlayerVisibleScreenY;
String MovementDirection;
Peasant mob1;
public World1A(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
Image [] WalkingLeft = {new Image("res/Sprites/buckysLeft.png"),new Image("res/Sprites/buckysLeft.png")};
Image [] WalkingRight = {new Image("res/Sprites/buckysRight.png"),new Image("res/Sprites/buckysRight.png")};
playerLeft = new Animation(WalkingLeft, duration, false);
playerRight = new Animation(WalkingRight, duration, false);
player = playerRight;
WorldX = 0;
WorldY = 0;
world= new Image("res/WorldImages/WorldBackGround.png");
mousePosition="null";
MovementDirection = "Not Moved Yet";
mob1= new Peasant(2000, 200, 1);
if(WorldX<=0){
playerX = Math.abs(WorldX);
}else{
playerX=0-WorldX;
}
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
player.draw(450, 300);
if(WorldX>0){
world.draw(0, 0);
g.fillOval(0+mob1.getX(), 0+mob1.getY(), 50, 50);
g.fillRect(0, 0+340, 500, 10);
player.draw(-WorldX + 450, 300);
}else{
world.draw(WorldX, WorldY);
g.fillOval(WorldX+mob1.getX(), WorldY+mob1.getY(), 50, 50);
g.fillRect(WorldX, WorldY+340, 500, 10);
g.fillOval(WorldX+mob1.getX(), WorldY+mob1.getY(), 50, 50);
player.draw(450, 300);
}
g.setColor(Color.white);
//All this is co-ords ect, it is for developement help
g.drawString(String.valueOf(mob1.getX()), 50, 200);
g.drawString("World X: "+ String.valueOf(WorldX), 50, 225);
g.drawString("Player X: "+ String.valueOf(playerX), 50, 250);
g.drawString("Player Detetcted?: "+ String.valueOf(mob1.PlayerDetected), 50, 265);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
if(WorldX<=0){
playerX = Math.abs(WorldX);
}else{
playerX=0-WorldX;
}
mob1.autoEveryThing(delta, playerX, playerY);
int posX = Mouse.getX();
int posY = Mouse.getY();
mousePosition = "X: " + posX + "\nY: " + posY;
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_LEFT)){
WorldX += delta * 0.1f;
MovementDirection = "Left";
player = playerLeft;
}else if(input.isKeyDown(Input.KEY_RIGHT)){
WorldX -= delta * 0.1f;
MovementDirection = "Right";
player = playerRight;
}else{
MovementDirection = "Not Moving";
}
}
//DO NOT CHANGE
public int getID(){
return 2;
}
}
The autoEveryThing method in the Peasant class should make it so that if(mobX-300
But when I first run this it starts moving towards the player?
even though (2000-300<0) is false, it still sets the PlayerDetected boolean to true???
Why does this happen?
Thanks
EDIT:
After trying to go through thi and fix it I found somthing strange, even if I take out the whole bit which can change PlayerDetected to true, the mob still moves towards the player. This meens that PlayerDetected is becominbg true somwhere, but I cant figure out where?
if(PlayerDetected=true){
is wrong you should use ==
if(PlayerDetected==true){
or even better
boolean isPlayerDetected;
if (isPlayerDetected)
further consider
double dx = mobX - playerX;
double dy = mobY - playerY;
double distance = Math.sqrt(dx*dx + dy*dy);
if (distance < 300) {
// is within distacne threshold
}

Drawing multiple pixels/rectangles

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.

Stopping image movement

okay, so i have my image move straight down along the y axis whenever i click the mouse, my only problem is i don't know how to get it to stop when it hits the bottom of the screen, can someone please help?
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;
String Build = "1.1";
public void init(GameContainer container, StateBasedGame game) throws SlickException{
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (Point p : point) {
if (p != null) {
m.drawParticle(p.x, p.y += 1);
}
}
g.drawString("Particle Test", 680, 0);
g.drawString("Build: " + Build, 680, 15);
g.drawString("Pixels: " + num, 10, 25);
}
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] = new Point(pressedX, pressedY);
}
public int getID() {
return ID;
}
}
I'd imagine somewhere you will want to check the x/y pos of the particle before you renderer it and remove it from the array when it's out of bounds...
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (int index = 0; index < point.length; index++) {
Point p = point[index];
if (p != null) {
p.y++;
if (p.y > height) { // You'll need to define height...
point[index] = null; // Or do something else with it??
} else {
m.drawParticle(p.x, p.y);
}
}
}
g.drawString("Particle Test", 680, 0);
g.drawString("Build: " + Build, 680, 15);
g.drawString("Pixels: " + num, 10, 25);
}
You could also do a preemptive check, which would allow you to know what points are at the bottom of the screen...
if (p != null) {
if (p.y >= height) { // You'll need to define height...
// Do something here
} else {
p.y++;
m.drawParticle(p.x, p.y);
}
}

Categories