Advertisement
Snopel

GameV1

Apr 11th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.44 KB | None | 0 0
  1. import java.awt.Rectangle;
  2. import java.io.*;
  3. import org.newdawn.slick.*;
  4.  
  5. public class GameV1 extends BasicGame
  6. {
  7.  
  8.     static File playerStats = new File("PlayerStats.txt");
  9.    
  10.     Image player = null;
  11.     Image player_Waypoint = null;
  12.    
  13.     Rectangle rect_mouse_Test = new Rectangle(2,2);
  14.     Rectangle rect_player_Test = new Rectangle(2,2);
  15.     double player_waypoint_X = 0;
  16.     double player_waypoint_Y = 0;
  17.    
  18.     float velocityX = 0;
  19.     float velocityY = 0;
  20.     double player_X = 400; //the player's X position
  21.     double player_Y = 300; //the player's Y position
  22.     float length;
  23.  
  24.     public GameV1() throws IOException
  25.     {
  26.         super("Slick2D Path2Glory - SlickBasicGame");
  27.        
  28.         if (playerStats .exists())
  29.         {
  30.             fileLoad();
  31.         } else
  32.         {
  33.             fileCreate();
  34.         }
  35.     }
  36.  
  37.     public static void fileCreate() throws IOException
  38.     {
  39.         // Set up the FileWriter with our file name.
  40.         FileWriter saveFile = new FileWriter("PlayerStats.txt");
  41.  
  42.         // Write the data to the file.
  43.         saveFile.write("\n");
  44.         saveFile.write(PlayerStats.player.str_player_Name + "\n");
  45.         saveFile.write(PlayerStats.player.int_player_Level + "\n");
  46.         saveFile.write(PlayerStats.player.int_player_Exp + "\n");
  47.         saveFile.write(PlayerStats.player.int_player_maxExp + "\n");
  48.         saveFile.write(PlayerStats.player.int_player_skillPoints + "\n");
  49.         saveFile.write(PlayerStats.player.int_player_Skill + "\n");
  50.         saveFile.write(PlayerStats.player.db_player_Speed + "\n");
  51.         saveFile.write(PlayerStats.player.int_player_maxEnergy + "\n");
  52.         saveFile.write(PlayerStats.player.int_player_Regen + "\n");
  53.         saveFile.write(PlayerStats.player.str_player_Ability + "\n");
  54.  
  55.         saveFile.write("\n");
  56.  
  57.         // All done, close the FileWriter.
  58.         saveFile.close();
  59.     }
  60.    
  61.     public static void fileLoad() throws FileNotFoundException, IOException
  62.     {
  63.         BufferedReader saveFile= new BufferedReader(new FileReader("PlayerStats.txt"));
  64.  
  65.         // Throw away the blank line at the top.
  66.         saveFile.readLine();
  67.         // Get the integer value from the String.
  68.         PlayerStats.player.str_player_Name = saveFile.readLine();
  69.         PlayerStats.player.int_player_Level = Integer.parseInt(saveFile.readLine());
  70.         PlayerStats.player.int_player_Exp = Integer.parseInt(saveFile.readLine());
  71.         PlayerStats.player.int_player_maxExp = Integer.parseInt(saveFile.readLine());
  72.         PlayerStats.player.int_player_skillPoints  = Integer.parseInt(saveFile.readLine());
  73.         PlayerStats.player.int_player_Skill = Integer.parseInt(saveFile.readLine());
  74.         PlayerStats.player.db_player_Speed = Double.parseDouble(saveFile.readLine());
  75.         PlayerStats.player.int_player_maxEnergy = Integer.parseInt(saveFile.readLine());
  76.         PlayerStats.player.int_player_Regen = Integer.parseInt(saveFile.readLine());
  77.         PlayerStats.player.str_player_Ability = saveFile.readLine();
  78.    
  79.         // Not needed, but read blank line at the bottom.
  80.         saveFile.readLine();
  81.  
  82.         saveFile.close();
  83.     }
  84.    
  85.     public void init(GameContainer gc)
  86.             throws SlickException
  87.     {
  88.         player = new Image("src/Game/v1/res/Player Blue.png");
  89.         player_Waypoint = new Image("src/Game/v1/res/Waypoint.png");
  90.        
  91.     }
  92.  
  93.     public void update(GameContainer gc, int delta)
  94.             throws SlickException
  95.     {
  96.         Input input = gc.getInput();
  97.        
  98.         //Player Movement
  99.         if(input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
  100.         {
  101.             length = (float) Math.sqrt((player_waypoint_X - player_X)*(player_waypoint_X - player_X) + (player_waypoint_Y - player_Y)*(player_waypoint_Y - player_Y));
  102.             velocityX = (float) (player_waypoint_X - player_X) /length * (float) PlayerStats.player.db_player_Speed;
  103.             velocityY = (float) (player_waypoint_Y - player_Y) /length * (float) PlayerStats.player.db_player_Speed;
  104.            
  105.             player_waypoint_X = input.getMouseX() - 2;
  106.             player_waypoint_Y = input.getMouseY() - 2;
  107.            
  108.             rect_mouse_Test.x = (int) player_waypoint_X;
  109.             rect_mouse_Test.y = (int) player_waypoint_Y;
  110.         }
  111.        
  112.        
  113.         if (rect_player_Test.intersects(rect_mouse_Test))
  114.         {
  115.             velocityX = 0;
  116.             velocityY = 0;
  117.         }
  118.  
  119.         rect_player_Test.x = (int) player_X;
  120.         rect_player_Test.y = (int) player_Y;
  121.        
  122.         player_X=player_X+velocityX;
  123.         player_Y=player_Y+velocityY;
  124.     }
  125.  
  126.     public void render(GameContainer gc, Graphics g)
  127.             throws SlickException
  128.     {
  129.  
  130.         player.draw((float) player_X-15, (float) player_Y-15);
  131.         player_Waypoint.draw((float) player_waypoint_X-6, (float) player_waypoint_Y-6);
  132.        
  133.         //g.drawRect(rect_player_Test.x, rect_player_Test.y, rect_player_Test.width, rect_player_Test.height);
  134.         //g.drawRect(rect_mouse_Test.x, rect_mouse_Test.y, rect_mouse_Test.width, rect_mouse_Test.height);
  135.  
  136.     }
  137.  
  138.     public static void main(String[] args)
  139.             throws SlickException, IOException
  140.     {
  141.          AppGameContainer app =
  142.             new AppGameContainer( new GameV1() );
  143.  
  144.          //app.setMaximumLogicUpdateInterval(60);
  145.          //app.setMinimumLogicUpdateInterval(1);
  146.          
  147.          app.setDisplayMode(800, 600, false);
  148.          app.start();
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement