Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


LeisureForce

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

Topics I've Started

Why won't it go to the method?

06 October 2012 - 12:54 AM

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

Leisure
Force
Point
Phile
Cloaked
Saint
Death
Zodiak
Fiasco
Undead
Menacing
Final
Autophagy
Dark
Matter

Thanks!

[Java] Draw something more than once?

03 October 2012 - 09:47 PM

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

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();
 
  }
}
}

And the images used

(ship)
http://i.imgur.com/5ZsbS.png

(bullet)
http://i.imgur.com/w5qIM.png

Thanks in advance!

PARTNERS