Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to move shapes with keyboard in slick 2d  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
Offline streek405

Senior Newbie





« Posted 2014-01-26 00:03:09 »

Im trying to move a shape, an octagon, in my game but I dont know how. Here is what I have so far
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  
import java.awt.Container;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
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.Circle;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;


public class ShapesAndCollisionTest extends BasicGame{
   
   private Shape circle;
   private boolean collides = false;
   private float x = 300;
   private float y = 100;
   float speed = .25f;
   private Shape octagon;
   
   public ShapesAndCollisionTest(String title) {
     
      super(title);
   }
   
   public void init(GameContainer gc) throws SlickException {
     
      circle =  new Circle(100, 100, 50);// x, y, radius
     octagon = new Circle(x, y, 30, 8);//x, y, radius, number of segments
     
   }
   
   public void render(GameContainer gc, Graphics g) throws SlickException {
     
      //set the color
     g.setColor(new Color(0, 255, 255));//inside color
     g.fill(circle);
      g.setColor(new Color(255, 0, 0));//red, green, blue OUTLINE of circle
     g.draw(circle);
           
      g.setColor(Color.magenta);
      g.draw(octagon);
     
      //just show whether the collision is changing or not
     g.drawString("Collides " + collides, 350, 30);
   }
   
   public void update(GameContainer gc, int delta) throws SlickException {
     
      Input input = gc.getInput();
      //control octogon
     //up
     if(input.isKeyDown(Input.KEY_UP)){
         y = y - speed * delta;//delta makes the speed run the same on ANY computer
     }
      //down
     if(input.isKeyDown(Input.KEY_DOWN)){
         y = y + speed * delta;
      }
      //left
     if(input.isKeyDown(Input.KEY_LEFT)){
         x = x - speed * delta;
      }
      //right
     if(input.isKeyDown(Input.KEY_RIGHT)){
         x = x + speed * delta;
      }
     
      //check if it collides
     collides = circle.intersects(octagon);
     
   }
Offline Axeman

Senior Member


Medals: 7



« Reply #1 - Posted 2014-01-26 00:27:34 »

Well... First of all you need to call update() for anything to happen. Smiley Second, since the delta is time between frames per second it shouldn´t be an int. Let´s say your game is running at 60 fps, every frame is 1/60 seconds or 0,016 seconds so that int is most likely going to be zero. The only way that delta value can be correct if is your game is running at 1 fps and nobody want´s that. Smiley Change it to float and then I suggest this post about game loop: http://www.java-gaming.org/index.php?topic=24220.0

Do you have any specific problem you need help with?
Offline streek405

Senior Newbie





« Reply #2 - Posted 2014-01-26 02:36:56 »

But doesnt everything automatically work when i call start()?  I mean the game already runs and updates itself right now...or is there more to the update method than just moving stuff around?
And the question I have is how can I make a shape object move around? I know how to make pictures and "built in" shapes move around like this:

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  
   public void render(GameContainer gc, Graphics g) throws SlickException {
     
   
      g.drawOval(x, y, 40, 60, 50);//move the octagon around
     
      //just show whether the collision is changing or not
     g.drawString("Collides " + collides, 350, 30);
   }
   
   public void update(GameContainer gc, int delta) throws SlickException {
     
      Input input = gc.getInput();
      //control octogon
     //up
     if(input.isKeyDown(Input.KEY_UP)){
         y = y - speed * delta;//delta makes the speed run the same on ANY computer
     }
      //down
     if(input.isKeyDown(Input.KEY_DOWN)){
         y = y + speed * delta;
      }
      //left
     if(input.isKeyDown(Input.KEY_LEFT)){
         x = x - speed * delta;
      }
      //right
     if(input.isKeyDown(Input.KEY_RIGHT)){
         x = x + speed * delta;
      }
     

   }


But, if I want to do something like the following in my update method

1  
2  
      //check if it collides
     collides = circle.intersects(oval);

for a non object shape, like in line 4, then I have no idea how to pass in the value(oval) into the parameter. Right now Im just getting my feet wet with the whole collision thing because I have made another project that is essentially a kids maze game, but its only fault as of now is that the player can go through the lines(the walls in the game). Soooooo if I figure this out I will be able to figure that out too, I think. BUT, I'm a noob and need help still.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2014-01-26 02:40:24 »

You don't need to pass in anything else. Slick handles the collision detection internally, and gets the coordinates of the two objects without you manually telling you where they are.

Like someone mentioned above, delta should not be an integer. Slick also handles updating internally, contrary to what Axeman said, so don't worry.

Offline streek405

Senior Newbie





« Reply #4 - Posted 2014-01-26 03:23:40 »

But when I change delta to a float I get an error stating that it needs to be an int. Also, is there a way that I can make slick detect a certain color in the background and not allow the user to pass it, like a maze? Or would I just have to outline the areas with shapes and make it go through the collision detection thing?

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2014-01-26 03:26:46 »

Well it seems Slick uses an integer for delta time, which is interesting. As for your collision detection question, no I don't believe Slick2D will handle color picking for you. If you really want to, you can always do it yourself, but I wouldn't. Use AABBs (Axis Aligned Bounding Boxes). Basically, yes you should outline the area with shapes and detect if you collide with them.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #6 - Posted 2014-01-26 03:40:33 »

I always hesitated when I made my own delta system in float value. I still wonder to this day why slick is using an integer, and if it is the 'correct' way. You would go pretty speedy at just 2px/sec.

Offline Axeman

Senior Member


Medals: 7



« Reply #7 - Posted 2014-01-26 12:36:31 »

Whoops! Sorry for doing nothing but adding confusion. Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (37 views)
2014-09-12 09:08:26

Norakomi (65 views)
2014-09-10 13:57:51

TehJavaDev (91 views)
2014-09-10 06:39:09

Tekkerue (45 views)
2014-09-09 02:24:56

mitcheeb (66 views)
2014-09-08 06:06:29

BurntPizza (49 views)
2014-09-07 01:13:42

Longarmx (36 views)
2014-09-07 01:12:14

Longarmx (42 views)
2014-09-07 01:11:22

Longarmx (38 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!