Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Painting an object to another object before the JFrame? on: 2012-04-04 20:46:36
Is this possible? For I am trying it, and currently it is not working..

Won't do as last time and post all the code Smiley

Here's the object with the paintComponent():
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public Platform() {
    }
   
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        g.setColor(Color.gray);
        g.fillRect(0, 0, getBounds().width, getBounds().height);


The Platform-object is created first here:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
    private class Level extends JPanel implements KeyListener, ActionListener {
        Platform ground = new Platform();
        Platform p1 = new Platform();
        Platform p2 = new Platform();
        Platform p3 = new Platform();
       
        public Level() {
            setPreferredSize(new Dimension(400, 400));
           
            ground.setBounds(0, 380, 400, 20);
            p1.setBounds(150, 200, 100, 20);
            p2.setBounds(100, 300, 200, 20);
            p3.setBounds(100, 360, 40, 20);
           
            add(ground);
            add(p1);
            add(p2);
            add(p3);
           
        }


(This private class does also have a paintComponent(), is that the problem..?)

Then the Level object is added to the JFrame in the superclass.

The mechanics of the platformer works just as I want it now, but having invisible platforms bothers me a bit : D
2  Games Center / Archived Projects / Re: ShitBricks on: 2012-03-25 16:41:03
: D
Really cool and addicting!

Would like some music and SFX though, but I know that stuff can be hard to find if you can't make it yourself..

Great work!
3  Game Development / Newbie & Debugging Questions / Re: Umm.. Hi? on: 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
4  Game Development / Newbie & Debugging Questions / Re: Umm.. Hi? on: 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
5  Game Development / Newbie & Debugging Questions / Re: Umm.. Hi? on: 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?
6  Game Development / Newbie & Debugging Questions / Umm.. Hi? on: 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
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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!