Jump to content

  • Log In with Google      Sign In   
  • Create Account

LeisureForce

Member Since 03 Oct 2012
Offline Last Active Jan 20 2013 03:52 PM

Posts I've Made

In Topic: Why won't it go to the method?

06 October 2012 - 11:42 AM

It worked! I'm coming from Python, so I guess it's a little different. Thanks for your help!

In Topic: [Java] Draw something more than once?

05 October 2012 - 11:58 AM

private float dx,dy;
public Bullet(float dx, float dy, etc) {
this.dx = dx;
this.dy = dy;
}


Okay, so instead of calling update when I hit space, I would do Bullet(dx, dy)?

In Topic: [Java] Draw something more than once?

04 October 2012 - 09:35 PM

Maybe a picture to see exactly what's wrong?


Sorry.

Before: http://i.imgur.com/tsy2M.png

After: http://i.imgur.com/DflSe.png

In Topic: [Java] Draw something more than once?

04 October 2012 - 05:50 PM

Okay, now i've taken all of that into consideration, but I still have a few problems. Now, when I go to draw the bullet, it just turns the ship purple instead of drawing a rectangle to the screen. Here is my code. (I know that i am still drawing the rectangle only when i hit space, I'm just doing it for now).

import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Move {
//make the screen width and height values public
public int SCREENWIDTH = 640, SCREENHEIGHT = 480;
private boolean isRunning = true;
private Texture spaceShip;
public int inx = 100;
public SetShip myShip = new SetShip();
public Bullet myBullet = new Bullet();
public int x = myShip.ShipXStart;
public int y = myShip.ShipYStart;

public Move(){
  //Set up the display
  try{
   Display.setDisplayMode(new DisplayMode(SCREENWIDTH, SCREENHEIGHT));
   Display.setTitle("Space Invaders");
   Display.create();
  } catch (LWJGLException e){
   e.printStackTrace();
  }
 
 
 
 
 
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, 640, 480, 0, 1, -1);
  glMatrixMode(GL_MODELVIEW);
  glEnable(GL_TEXTURE_2D);
 
  myShip.init();
 
  //main game loop
  while (isRunning){
   glClear(GL_COLOR_BUFFER_BIT);
   myShip.draw();
   input();
  
  
   Display.update();
   Display.sync(60);
  
   if(Display.isCloseRequested()){
    isRunning = false;
    Display.destroy();
    System.exit(0);
   }
  }
  Display.destroy();

 
}

private void input() {
  // TODO Auto-generated method stub
  if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
   inx = -5;
   myShip.update(inx);
  }
  if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
   inx = 5;
   myShip.update(inx);
  }
  if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
   myBullet.update(10, 20);
   myBullet.draw();
  }
}
private Texture loadTexture(String key) {
  // TODO Auto-generated method stub
  try{
   return TextureLoader.getTexture("PNG", new FileInputStream(new File("img/" + key + ".png")));
  } catch (FileNotFoundException e){
   e.printStackTrace();
  } catch (IOException e){
   e.printStackTrace();
  }
  return null;
}

public class SetShip{
  public int ShipXStart = 100, ShipYStart = 400;
  //initialize the ship
  void init(){
   //store the image in a texture
   //load the image
   try {
   
    spaceShip = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/spaceship.png")));
   } catch (FileNotFoundException e){
    e.printStackTrace();
   } catch (IOException e){
    e.printStackTrace();
   }
  
  
  
  }
 
  
  void update(int inx){
   ShipXStart += inx;
  }
 
  void draw(){
  
  
   spaceShip.bind();
  
   glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(ShipXStart, ShipYStart);
    glTexCoord2f(1, 0);
    glVertex2f(ShipXStart + 200, ShipYStart);
    glTexCoord2f(1, 1);
    glVertex2f(ShipXStart + 200, ShipYStart + 100);
    glTexCoord2f(0, 1);
    glVertex2f(ShipXStart, ShipYStart + 100);
   glEnd();
  }
 
 
}
private static class Bullet{
  //set up the variables
  public int x, y; //set the starting x and y for the bullets
 
  void update(int dx, int dy){
   x += dx;
   y += dy;
  }
 
  void draw(){
   glColor3f(1.0f,0f,1.0f);
   glBegin(GL_QUADS);
    glVertex2i(50, 50);
    glVertex2i(100, 100);
    glVertex2i(50, 100);
    glVertex2i(100, 100);
   glEnd();
  }
 
}

public static void main(String[] args) {
  // TODO Auto-generated method stub
  new Move();
}
 
}

In Topic: [Java] Draw something more than once?

04 October 2012 - 03:31 PM

Thanks a ton!

PARTNERS