Problem with code

I have a game I am working on. But when I go from the “menu”, it draws 1 frame and then freezes (the game logic stil works though :clue:. The source code is here.
Thanks

EDIT: The new source code is in the link and on pastebin!!

I have to download the code?
Just pastebin it…

AAAAAAAAAAAAAAAAAAARGH!

goes and hides in box and tries to recover sanity

…bad… …variable names…
…bad… …formatting…*

(Yes, there is bad formatting in parts, I just wanted to emphasize the unreadability of the code)

I can’t see that image too well but s1by, s2by, s3by are pretty obscure…

Copy the image URL and paste it in the title bar.

At least he won’t have to use an obfuscator.

@OP

You are using deprecated GL functions (Like immediate mode, GL_QUADS etc.)
Use your IDE to format your code properly. AND PLEASE: Make the variable names more understandable.

Also, I recommend having an ArrayList of objects instead of having s1by, s2by, s3by etc.

Also, although I am not 100% sure, I think the reason its getting jammed up is because of your

public void start()

more specifically your

if/else statement revolving around the boolean started

You have imported import static org.lwjgl.opengl.GL11.*; statically, and then, in the initGL() method, you have done:



GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);


which should be:



glMatrixMode(GL_PROJECTION);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glLoadIdentity();
glOrtho(0, 800, 0, 600, 1, -1);
glMatrixMode(GL_MODELVIEW);


:slight_smile:

I’ve done both of those, and it still won’t work! But the animation works fine for the square that you can move. ???

Rename your variables and format your code before you ask anyone to try and fix it.

Holy shiza. Learn what an object is and start writing your own classes.

Seriously, you should delete everything you have written so far and start from ground zero. Try learning programming techniques with simple text based programs before moving into graphics.

Here’s a tip: never try to duplicate code. So if you have this:

if (a) {
    glBegin(..)
    glVertex(..)
    glVertex(..)
    glEnd(..);
} else {
    glBegin(..);  <- DUPLICATE CODE!!! 
    glVertex(..); <-
    glVertex(..); <-
    glEnd(..);    <-
}

You can use a method “drawQuad” instead of duplicating code:

if (a) {
    drawQuad(...);
} else {
    drawQuad(...);
}

You can further minimize code duplication by using classes and collections:

//render each entity
for (Sprite entity : enemies) {
    entity.render():
}

...... now ... inside the entity class

class Entity {
    ...

    public void render() {
        GLUtils.drawQuad(this.x, this.y, this.width, this.height);
    } 
}

Your entire app structure might look more like this:

List<Sprite> enemies = new ArrayList<Sprite>();
PlayerSprite player;

public void init() {
    ... add sprites to "enemies" list ...
        
    player = new PlayerSprite();
}

public void render() {
    for (Sprite s : enemies) {
        s.render();
    }

    player.render();
}

Holy mother of the gods of atheism that code burns my eyes :o
Never have I facepalmed so hard

Completely agree with davedes, go back to the basics with normal java (not lwjgl) and learn to write with object oriented design paradigm. Use loops instead of copying+pasting, follow naming conventions, be more descriptive with class/variable/method names, and don’t use so many if statements.

I’m sure I can write a program that does the same as those 700 lines of code in less than 200 lines. To give you a check list:

  1. Create a class that contains the position, size and state of a square.
  2. Give all variables and classes sensible names. Someone else (= your future self) should be able to instantly understand what everything is used for.
  3. Create an ArrayList and just add instances to it to create new objects. You won’t be limited to only 12 squares this way.
  4. Use loops for manipulating. If you want to draw all squares, just loop over the objects in your list and draw each of them.

See this

And dont be discouraged, we all started with no idea

the lack of loops and arrays and bad names and such does suggest that you have not read a book or went through tutorials for the basics
and it is pretty fundamental - so do it.

Its like trying to drift when you cant even drive