3 Replies - 4617 Views - Last Post: 19 May 2009 - 11:00 AM Rate Topic: -----

#1 sladde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 09

Highscore function in Applet: Read + Write to a text file

Post icon  Posted 18 May 2009 - 10:25 AM

Hey All,

First of all, I would like to introduce myself as a first timer on this forum. I am currently a high school student in South Florida and am enrolled in my school's advanced computer science track. As of now I am taking the class called "Introduction to C++/Java Honors", and am working on my final project, for which the prompt was to create an object-oriented program in Java or C++ that is useful, intuitive, and representative of one's abilities in the chosen language. The sole requirement functionality wise is that it must be able to read from or/and write to a text file.

For my project I have chosen to create a variation of the classic game 'Pong' in which there is only one player and the ball is bounced against a wall and sped up each time the player's paddle hits the ball.

Already, I have created said program but have not been able to create a function or call that is able to read from a text file. My idea for this is to create a high score mechanism which is displayed after the player loses.

In accordance with the site's rules, I have genuinely used both Google and this forum to search for a solution. Though I have found some suggestions none have worked once implemented into the applet. The most common results being the program stalling or the virtual machine crashing.

Any and all help would be appreciated.

Thanks,

Alejandro Perez

This is the latest working code I have as of now:

//********IMPORTANT********//
//******PLEASE READ*******//
//******THE INCLUDED*****//
//****DOCUMENTATION*****//

//ALEJANDRO Perez//
//Mr. Morell//
//Period 1//
//5.18.2009//

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*; //you need this preprocessor directive for user input
import java.util.Scanner; //you need this preprocessor directive for user input
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File;
import java.io.BufferedReader;

public class Game_pong extends Applet
   implements MouseMotionListener {	 //copied from a program and modified to work with game all other code is original//

   int width, height;
   int my;  // the mouse y coordinate
   int ball_x, ball_y, move_x, move_y;
   long t0,t1;
   int lives;
   int score;
   
   public void init() {
	  setBackground( Color.black);

	  my = height/2;

	  addMouseMotionListener( this );			//COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	  
	  ball_x= 300;
	  ball_y= 100;
	  move_x= -1;
	  move_y= 1;
	  t0=0;
	  t1=1;
	  lives=5;
	  score=0;
   }
   
   public void mouseMoved( MouseEvent e ) {	 //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	  my = e.getY();							//COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	  repaint();								//COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	  e.consume();							  //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
   }
   public void mouseDragged( MouseEvent e ) {   //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	  my = e.getY();							//COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	  repaint();								//COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	  e.consume();							  //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
   }
   
   public int ball(int my){

	 if(ball_x>480){	
		 move_x= move_x*(-1);
		}
	  if(ball_x<1){	
		 lives = lives-1;
		 ball_x= 250;
		 ball_y= 300;
		 
			t0=System.currentTimeMillis();   
		do{								 
			t1=System.currentTimeMillis();  
		}								   
		while (t1-t0<1500);  
		
		move_y= 1;
		move_x= 1;
		
		}
		
		
	 if((ball_y<25)||(ball_y>485)){
		 move_y= move_y*(-1);
		}
												   //********FLAWLESS*********//
	 if(ball_x<20){								//***COLLISION DETECTION***//
		 if((ball_y>(my-50))&&(ball_y<(my+50))){   //***COLLISION DETECTION***//
		 move_x= move_x*(-1);					  //***COLLISION DETECTION***//
		 score = score + 1;						//Score add//
		 move_y = move_y + 1;					  //Increment speed in the Y direction//
		 move_x = move_x + 1;					  //Increment speed in the X direction//
		}										  //***COLLISION DETECTION***//
	}											  //***COLLISION DETECTION***//
												   //********FLAWLESS*********//
												   
												   
	 ball_x= ball_x + move_x;
	 ball_y= ball_y + move_y;
	 repaint();							  //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
	 return ball_x & ball_y;
	}
	
   public void paint( Graphics g ) {
	   
	   g.setColor( Color.white);
	   g.fillRect( 0 , 0, 500, 25);
	   g.setColor( Color.black);
	   g.drawString( "Lives: " + lives +  "										 " + "TENNIS" + "										 " +  "Score: " + score, 10, 15);
	   
		 if(lives<1){
			   g.setColor( Color.white);
			g.drawString( "GAME OVER", 250, 250);
			g.drawString( "Score: " + score, 250, 260);
			repaint();
			System.exit(0);
		  }
	   
											// Start Wait//
		t0=System.currentTimeMillis();	  //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
		do{								 //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
			t1=System.currentTimeMillis();  //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
		}								   //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
		while (t1-t0<10);				   //COPIED FROM A PROGRAM AND MODIFIED TO WORK WITH GAME, ALL OTHER CODE IS ORIGINAL//
											//End Wait
										  
	  g.setColor( Color.blue );			 //Change color to blue//
	  
	  if((my<475)&&(my>75)){				
	  g.fillRect( 10 , (my-50), 15, 100);
	}
	  else if(my>475){
	  g.fillRect(10, 425, 15, 100);
	}
	else if(my<75){
		g.fillRect(10,25, 15, 100);
	}
	

	
	g.setColor( Color.red );
	ball(my);
	g.fillOval( ball_x, ball_y, 20, 20);
	}
	  
}




Is This A Good Question/Topic? 0
  • +

Replies To: Highscore function in Applet: Read + Write to a text file

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,652
  • Joined: 20-September 08

Re: Highscore function in Applet: Read + Write to a text file

Posted 18 May 2009 - 10:34 AM

To read from a text file do


Scanner s = new Scanner(getClass().getResourceAsStream("x.txt"));




and put x.txt in the same directory as your applet class file

This post has been edited by g00se: 18 May 2009 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 sladde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 09

Re: Highscore function in Applet: Read + Write to a text file

Posted 19 May 2009 - 10:55 AM

I am sorry, but after trying to use this code as resolution to my problem I found that it causes the program to stall out when called. Also, the text it returns is some random mess of code like: "java{whitespace:get.Class()...."
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,652
  • Joined: 20-September 08

Re: Highscore function in Applet: Read + Write to a text file

Posted 19 May 2009 - 11:00 AM

You need to call it in init() to get your data. Is that what you're doing?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1