Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (528)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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 achieve stated movement?  (Read 771 times)
0 Members and 1 Guest are viewing this topic.
Offline Shazer2

Junior Devvie


Medals: 3


Aspiring developer.


« Posted 2011-05-24 06:36:53 »

Trying to achieve movement like in Legends of Yore (http://legendsofyore.com), not sure how to achieve it. Current code I have is:
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  
package Game;

import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Input;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class GameState extends BasicGameState {
   
   GameContainer container;
   StateBasedGame stateContainer;
   Image grassImage;
   Image playerImage;
   Image treeImage;
   Image flowerImage;
   Player player;
   
   public Input input;
   public final static int tile = 32;
   public final static int width = 480;
   public final static int height = 320;
   final int tileSize = 32;
   final int widthInTiles = width/tileSize;
   final int heightInTiles = height/tileSize;
   //int startTileX = player.x / tileSize - widthInTiles/2;
   //int startTileY = player.y / tileSize - heightInTiles/2;
   private int[][] level;
   
   public int getID() {
      return RPG.GAME_STATE;
   }
   
   public void init(GameContainer container, StateBasedGame stateContainer) throws SlickException {
      this.container = container;
      this.stateContainer = stateContainer;
     
      grassImage = new Image("Images/Backgrounds/grass.png");
      playerImage = new Image("Images/Players/defPlayer.png");
      treeImage = new Image("Images/Backgrounds/tree.png");
      flowerImage = new Image("Images/Backgrounds/flower.png");
     
      input = container.getInput();
     
      level = new int[width/tile][height/tile];
      player = new Player(playerImage, 0, 0);
     
      Random r = new Random(3424);
        for (int i = 0; i < 3; i++) {
            int x = r.nextInt(level.length);
            int y = r.nextInt(level[x].length);
           
            level[x][y] = 1;
        }
        for (int i = 0; i < 2; i++) {
            int x = r.nextInt(level.length);
            int y = r.nextInt(level[x].length);
       
           level[x][y] = 2;
        }
   }
   
   public void update(GameContainer container, StateBasedGame stateContainer, int delta) throws SlickException {
      if (input.isKeyPressed(Input.KEY_P)) {
         stateContainer.enterState(RPG.PAUSED_STATE);
      }
     
      if (input.isKeyPressed(Input.KEY_ESCAPE)) {
         stateContainer.enterState(RPG.HOME_STATE);
      }
     
      if (input.isKeyPressed(Input.KEY_LEFT)) {
         player.x -= 32;
         if (player.x < 0 || checkCollide()) {
            player.x += 32;
         }
      }
     
      if (input.isKeyPressed(Input.KEY_RIGHT)) {
         player.x += 32;
         if (player.x > 448 || checkCollide()) {
            player.x -= 32;
         }
      }
     
      if (input.isKeyPressed(Input.KEY_UP)) {
         player.y -= 32;
         if (player.y < 0 || checkCollide()) {
            player.y += 32;
         }
      }
     
      if (input.isKeyPressed(Input.KEY_DOWN)) {
         player.y += 32;
         if (player.y > 288 || checkCollide()) {
            player.y -= 32;
         }
      }
   }
   
   public boolean checkCollide() {
      return level[(int)player.x/tile][(int)player.y/tile] == 1;
   }
   
   public void render(GameContainer container, StateBasedGame stateContainer, Graphics g) throws SlickException {
      g.setColor(Color.black);
       
      for (int x = 0; x < 480; x += 32) {
         for (int y = 0; y < 320; y += 32) {
            g.drawImage(grassImage, x, y);
         }
      }
        //background
      for(int x = 0; x < level.length; x++)
      {
         for(int y = 0; y < level[x].length; y++)
         {      
            if(level[x][y] == 1) {
               g.drawImage(treeImage, x*tile, y*tile);
            }
           
            if (level[x][y] == 2) {
                 g.drawImage(flowerImage, x*tile, y*tile);
            }
         }
      }
       
      //grid
       for (int h = 0; h < height; h += 32) {
          g.drawLine(0, h, width, h);
       }
       
       for (int w = 0; w < width; w += 32) {
          g.drawLine(w, 0, w, height);
       }
       
        // draw player
        player.draw(g);
   }
}


I know I can implement it, but I'm not sure how. If anyone can give me some explanation it would be wonderful.
Shannon.

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline dishmoth
« Reply #1 - Posted 2011-05-24 08:49:11 »

You need to make it clearer what you're asking.

In Legends of Yore you can move using either the mouse or the keyboard.  Which are you aiming for?

What's wrong with your current code?  Is there a bug?  Describe the problem.

Simon

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.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (10 views)
2014-11-28 16:25:09

PocketCrafter7 (11 views)
2014-11-28 16:24:29

toopeicgaming1999 (77 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (17 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!