LeisureForce

Member
  • Content count

    8
  • Joined

  • Last visited

Community Reputation

119 Neutral

About LeisureForce

  • Rank
    Newbie
  1. Why won't it go to the method?

    It worked! I'm coming from Python, so I guess it's a little different. Thanks for your help!
  2. Hey all, I have been working on a username generator as some practise in java. But I have come into some issues. When I take input from the user on which function they'd like to go to, the program just stops. And how would I go about picking 2 random lines from the NameList.txt so I can pair them up to make a name. Also, can you tell me if this is the right way to go about reading/writing to my code. [source lang="java"]import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; public class UsernameGen { public Random randomNumber = new Random(); public List<String> userNames = new ArrayList<String>(); public static Scanner scanner = new Scanner(System.in); public boolean isRunning = true; public String command; public UsernameGen(){ home(); } public void home(){ System.out.println("--(A)dd a name to the list. "); System.out.println("--(G)enerate a name. "); System.out.println("--(Q)uit. "); System.out.println("Enter a command: "); command = scanner.next(); if (command == "g"){ getNames(); } if (command == "a"){ addToList(); } if (command == "q"){ isRunning = false; System.exit(0); } } public void addToList(){ String nameToAdd; System.out.println("What name would you like to add? "); nameToAdd = scanner.nextLine(); FileWriter fstream; try { fstream = new FileWriter("NameList.txt", true); BufferedWriter out = new BufferedWriter(fstream); out.write(nameToAdd); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } home(); } public void getNames(){ try { BufferedReader in = new BufferedReader(new FileReader("NameList.txt")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (IOException e) { } home(); } public static void main(String[] argsv){ UsernameGen usng = new UsernameGen(); } }[/source] And the NameList.txt [code]Leisure Force Point Phile Cloaked Saint Death Zodiak Fiasco Undead Menacing Final Autophagy Dark Matter[/code] Thanks!
  3. [Java] Draw something more than once?

    [quote name='SimonForsman' timestamp='1349419263' post='4987029'] private float dx,dy; public Bullet(float dx, float dy, etc) { this.dx = dx; this.dy = dy; } [/quote] Okay, so instead of calling update when I hit space, I would do Bullet(dx, dy)?
  4. [Java] Draw something more than once?

    [quote name='rockstar8577' timestamp='1349404208' post='4986996'] Maybe a picture to see exactly what's wrong? [/quote] Sorry. Before: [url="http://i.imgur.com/tsy2M.png"]http://i.imgur.com/tsy2M.png[/url] After: http://i.imgur.com/DflSe.png
  5. [Java] Draw something more than once?

    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). [code]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(); } }[/code]
  6. [Java] Draw something more than once?

    Thanks a ton!
  7. [Java] Draw something more than once?

    [quote name='DARSC0D3' timestamp='1349343094' post='4986701'] and like SimonForsman said don't use static you have noneed for this at this point. [/quote] Thanks for your help! And I only used static because eclipse was yelling at me, and it wouldn't work unless I put a static behind everything
  8. Hey all, I'm a beginner to the LWJGL framework, and cannot for the life of me figure this out. I am recreating the space invader game. I have the ship on the screen and moving, but I can't draw the bullet so that you can have more than 1 on the screen. Right now it just draws and goes forward when you push space, but stops drawing when you let go of space. And then when you hit space again, it draws from where it left off. Here is the code [code] 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 { private static Texture texture; private static Texture bull; public static int mx = 200; public static int my = 400; public static int bvelx = SetShip.x; public static int bvely = SetShip.y; public static void main(String[] args){ setUpDisplay(640,480); SetShip.init(); ShootBullet.init(); while (true){ glClear(GL_COLOR_BUFFER_BIT); SetShip.draw(); logic(); Display.update(); Display.sync(60); if(Display.isCloseRequested()){ Display.destroy(); System.exit(0); } } } private static void logic() { // TODO Auto-generated method stub if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){ mx = -5; SetShip.update(mx); } if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){ mx = 5; SetShip.update(mx); } if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){ bvely = -15; ShootBullet.update(bvely); ShootBullet.draw(); } } private static void setUpDisplay(int width, int height) { // TODO Auto-generated method stub try{ Display.setDisplayMode(new DisplayMode(width, height)); 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); } private static class ShootBullet { private static int bulletx = SetShip.x, bullety = SetShip.y; static void init(){ try{ bull = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/bullet.png"))); } catch (IOException e){ e.printStackTrace(); } } static void update(int by){ bullety += by; } static void draw(){ bull.bind(); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(bulletx, bullety); glTexCoord2f(1,0); glVertex2f(bulletx +50, bullety); glTexCoord2f(1,1); glVertex2f(bulletx +50, bullety+50); glTexCoord2f(0,1); glVertex2f(bulletx, bullety + 50); glEnd(); } } private static class SetShip{ public static int x = 200, y = 400; static void init() { // TODO Auto-generated method stub try{ texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/spaceship.png"))); } catch (IOException e){ e.printStackTrace(); } } static void update(int mx){ x += mx; } static void draw() { // TODO Auto-generated method stub texture.bind(); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(x, y); glTexCoord2f(1,0); glVertex2f(x +200, y); glTexCoord2f(1,1); glVertex2f(x +200, y+100); glTexCoord2f(0,1); glVertex2f(x, y + 100); glEnd(); } } } [/code] And the images used (ship) http://i.imgur.com/5ZsbS.png (bullet) http://i.imgur.com/w5qIM.png Thanks in advance!