Umm.. Hi?

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 :slight_smile:

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 :slight_smile:

Mike

Yay! Thank you for a quick reply! :slight_smile:

Here is the entire code, so you can compile and run if you so wish.

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?

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

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. :slight_smile:

: D
Thanks for the help! I’ll take the advice and start anew. :slight_smile:

Sorry for posting the entire code… When I tried to isolate the problem, I just thought ‘Hm. Everything is the problem!’ :slight_smile:

Nice point of view ;D

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 :slight_smile:

Everything in the world is problem.