Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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  
  Umm.. Hi?  (Read 2196 times)
0 Members and 1 Guest are viewing this topic.
Offline Istarnion

Junior Newbie





« Posted 2012-03-24 00:10:47 »

I started learning Java quite recently, after my elder brother who studies computer engineering gave me his Java textbook.

I made 'HelloWorld' in notepad, and then moved on to TextPad to make the compiling easier, but when I started using several classes, I moved on yet again, now to NetBeans. Probably a bit too early, but oh well...

Right now, I'm trying to make small games to practice, but somehow, there is always some sort of problem, and could really need help from experienced programmers, in addition to the book.

Right now, I'm trying to write a basic platformer with move and jump functions. I have created a grid-system using an array of rectangles, and then trying to wrap the program around this. It works fairly well, but somethings is a bit ...odd.

I just thought I'd ask first, can I post the entire game code here for feedback? Or will I get killed and banned and cursed?
It's just one .java-file yet, on aprox. 250 lines..

I have read around on the forum a bit now, and everyone here seems very friendly and nice (and so many skilled people!). I hope I will become a part of your comunity, and one day, an asset, that can help those who at that time is at the level I am at now Smiley

Huh?
~Istarnion
Offline Mike

JGO Wizard


Medals: 84
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #1 - Posted 2012-03-24 00:30:30 »

The more code you post the less is the chance that you get a lot of feedback. It's usually better if you try to figure out what part of your program is going wrong and then ask more specific questions.

And no, you won't get bad for posting 250 lines Smiley

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Istarnion

Junior Newbie





« Reply #2 - Posted 2012-03-24 00:43:52 »

Yay! Thank you for a quick reply! Smiley

Here is the entire code, so you can compile and run if you so wish.
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  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**Testing how a gridsystem for a platformer would work.
 *
 * TODO:
 * -Fix movement and sideways collision.
 * -Avoid the ArrayIndex out of bounds error when it should be respawn.
 * .Fix the jumpfunction.
 *
 * @author hallgeir
 */

public class PlatformTest extends JFrame implements ActionListener{
    Level level1 = new Level();
   
    /*Arrayrelated variables*/
    Rectangle[][] levelGrid = new Rectangle[20][20];
    Boolean[][] physicGrid = new Boolean[20][20];
    int gridX, gridY, i, j;
    final int size = 20;
   
    /*Variables of the player component*/
    int x = 200, y = 0, xSpd, ySpd, dir, degrees, rot, yB;
    boolean moving, jumping, onFloor;
   
    public PlatformTest() {
        setTitle("PlatformTest");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400,400);
        setResizable(false);
       
        /*Creates the rectangles of the grid. 400 rectangles total.*/
        for (i = 0; i < 20; i++) {
            for (j = 0; j < 20; j++) {
                levelGrid[i][j] = new Rectangle(gridX, gridY, size, size);
                physicGrid[i][j] = false; //sets all fields to not physical.
//                System.out.print(levelGrid[i][j]);
//                System.out.print(" " + i);
//                System.out.println(", " + j);
                gridX += 20;
                if (gridX == 400) {
                    gridX = 0;
                    gridY += 20;
                }
            }
        }
       
        addKeyListener(level1);
        add(level1);
        new Timer(10, this).start();
       
        setVisible(true);
    }
   
    public static void main(String args[]) {
        new PlatformTest();
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        repaint();
    }
   
    private class Level extends JPanel implements ActionListener, KeyListener {
        //URL url = getClass().getResource("Resources/Images/1.png");
        //Image img = Toolkit.getDefaultToolkit().getImage(url);
       
        public Level() {
            setPreferredSize(new Dimension(400,400));
            new Timer(10, this).start();
        }
        @Override
        public void paintComponent(Graphics g) {
            /*Background:*/
            g.setColor(Color.gray);
            g.fillRect(0, 0, 400, 400);
           
            /*Platforms:*/
            g.setColor(Color.black);
            for (int a = 15; a < 20; a++) {
                int yA = 9;
                int aX = (int)levelGrid[yA][a].getX();
                int aY = (int)levelGrid[yA][a].getY();
                physicGrid[yA][a] = true;
                g.fillRect(aX, aY, 20, 20);
            }
            for (int a = 5; a < 15; a++) {
                int yA = 11;
                int aX = (int)levelGrid[yA][a].getX();
                int aY = (int)levelGrid[yA][a].getY();
                physicGrid[yA][a + 1] = true;
                g.fillRect(aX, aY, 20, 20);
            }
            for (int a = 1; a < 11; a++) {
                    int yA = 15;
                    int aX = (int)levelGrid[yA][a].getX();
                    int aY = (int)levelGrid[yA][a].getY();
                    physicGrid[yA][a + 1] = true;
                    g.fillRect(aX, aY, 20, 20);
            }
            for (int a = 12; a < 15; a++) {
                    int yA = 17;
                    int aX = (int)levelGrid[yA][a].getX();
                    int aY = (int)levelGrid[yA][a].getY();
                    physicGrid[yA][a + 1] = true;
                    g.fillRect(aX, aY, 20, 20);
            }
            for (int a = 1; a < 19; a++) {
                    int yA = 18;
                    int aX = (int)levelGrid[yA][a].getX();
                    int aY = (int)levelGrid[yA][a].getY();
                    physicGrid[yA][a] = true;
                    g.fillRect(aX, aY, 20, 20);
            }
           
            /*Player:*/
//            Graphics2D g2 = (Graphics2D)g;
//            g2.rotate(Math.toRadians(degrees), x + 10, y + 10);
//            g2.drawImage(img, x, y, this);
            g.setColor(Color.black);
            g.fillOval(x, y, 20, 20);
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            int posX = 0, posY = 0;
            Position pos = new Position(posX, posY);
           
            /*Cretes the player rectangle:*/
            Rectangle pRect = new Rectangle(x, y, 20, 20);
           
            /*To recognize the position of the player:*/
            int aX = 0, aY = 0;
            while (aY < 20) {
                if (pRect.intersects(levelGrid[aX][aY])) {
                    pos.setPos(aX, aY);
                }
                aX++;
                if (aX == 20) {
                    aX = 0;
                    aY++;
                }
            }
           
            /*General movement:*/
            if (x < 400 && x > 0) {
                x += xSpd;
            }
            y += ySpd;
           
               
            /*Gravity:*/
            if (physicGrid[pos.getPosY()][pos.getPosX()]) {
                ySpd = 0;
                onFloor = true;
            }
            else {
                ySpd = 2;
                onFloor = false;
            }
           
            /*Moving:*/
            if (moving) {
                if (dir == 1 && x > 0) {
                    if (!physicGrid[pos.getPosY() - 1][pos.getPosX() - 1]) {
                        xSpd = -2;
                        System.out.println(physicGrid[pos.getPosY() - 1][pos.getPosX() - 1]);
                    }
                }
                if (dir == 2 && x < 380) {
                    if (!physicGrid[pos.getPosY() - 1][pos.getPosX() + 1]) {
                        xSpd = 2;
                        System.out.print(pos.getPosX() + " ");
                        System.out.println(pos.getPosY());
                    }
                }
            }
            else {
                xSpd = 0;
            }
           
            /*Jumping:*/
            if (jumping) {
                if (!physicGrid[pos.getPosY() - 1][pos.getPosX()]) {
                    if (yB - 50 < y && y > 0) {
                        ySpd = -3;
                    }
                    else {
                        ySpd = 0;
                    }
                }
                else {
                    ySpd = 0;
                }
            }
           
            /*Respawn:*/
            if (y > 420) {
                x = (int)levelGrid[2][10].getX();;
                y = (int)levelGrid[2][10].getY();
            }
        }
       
        @Override
        public void keyTyped(KeyEvent ke) {}
        @Override
        public void keyPressed(KeyEvent ke) {
            switch (ke.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    moving = true;
                    dir = 1;
                    break;
                case KeyEvent.VK_RIGHT:
                    moving = true;
                    dir = 2;
                    break;
                case KeyEvent.VK_SPACE:
                    if (onFloor) {
                        jumping = true;
                        yB = y;
                    }
                default:
                    break;
            }
        }
        @Override
        public void keyReleased(KeyEvent ke) {
            switch (ke.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    moving = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    moving = false;
                    break;
                case KeyEvent.VK_SPACE:
                    jumping = false;
                    break;
                default:
                    break;
            }
        }
    }
    private class Position {
        int posX, posY;
       
        public Position(int posX, int posY) {
            this.posX = posX;
            this.posY = posY;
        }
       
        public void setPos(int posX, int posY) {
            this.posX = posX;
            this.posY = posY;
        }
       
        public int getPosX() {
            return posY;
        }
        public int getPosY() {
            return posX;
        }
    }
}


As you can see, I have made a mess, trying to fix things..
And I messed up somehow on handling the axises properly...
The problems is in the TODO in the head comment, but what is most urgent to fix for me, is to avoid being able to roll into a platform from the side.

So, should I scrap this code, and start anew in a different way, or can this work?

Huh?
~Istarnion
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-03-24 06:59:33 »

JGO has no bad rep button as far as I seen, but it's better to paste long code to forum's pastebin Smiley

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-03-24 07:22:47 »

Hey Istarnion and welcome to JGO!

Why have 2 different ActionListeners? It's best to combine them in 1 and just move the repaint() at the end of the second one.
I tried following your code but then I TL;DR'ed XD

Instead of going through a loop to figure out what grid the player is in, you could simply divide the player's X and Y by the grid's width and height and cast to int to figure what grid the player is in. As for gravity, just add a constant to the Y every time, then check for collision.

I would follow your last idea and start anew, maybe with some tutorials from Google. Smiley

Offline Istarnion

Junior Newbie





« Reply #5 - Posted 2012-03-24 12:28:30 »

: D
Thanks for the help! I'll take the advice and start anew. Smiley

Sorry for posting the entire code... When I tried to isolate the problem, I just thought 'Hm. Everything is the problem!' Smiley

Huh?
~Istarnion
Offline Fokusas
« Reply #6 - Posted 2012-03-24 15:09:51 »

I just thought 'Hm. Everything is the problem!' Smiley
Nice point of view  Grin
Offline Istarnion

Junior Newbie





« Reply #7 - Posted 2012-03-24 16:20:06 »

Instead of going through a loop to figure out what grid the player is in, you could simply divide the player's X and Y by the grid's width and height and cast to int to figure what grid the player is in.

Um, I'm doing my second attempt now, but not sure if I understood everything here.. By grid, do you mean one tile (20 x 20 px), or the entire thing (400, 400px)?

I'm norwegian, so I don't know if 'grid' refers to the entire eh, system, or just one tile..

EDIT: Fixed it. My brain just takes some time to get started in the weekends Smiley

Huh?
~Istarnion
Offline ReBirth
« Reply #8 - Posted 2012-03-25 03:33:29 »

Everything in the world is problem.

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.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
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!