#1 Josef
problém je s načtením proměnné ze souboru a pak následné uložení do souboru. Nevíte proč?
Příspěvky odeslané z IP adresy 37.48.35.–
nevíte někdo kde je chyba, byl bych moc rád, kdyby jste mi pomohli.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class App extends JPanel implements Runnable, Finals{
/**
*
*/
private static final long serialVersionUID = 1L;
private int kolo=0;
BufferedWriter wr;
BufferedReader re;
private Dimension d;
private ArrayList metheors;
private Player player;
private Shot shot;
private int deaths = 0;
private int score =0;
private boolean ingame = true;
private String message = "Game Over";
private Thread animator;
public App(){
addKeyListener(new TAdapter());
setFocusable(true);
d = new Dimension(APP_WIDTH, APP_HEIGTH);
setBackground(Color.black);
gameInit(kolo);
}
public void gameInit(int kolo) {
metheors = new ArrayList();
ImageIcon img = new ImageIcon("res\\meteor.png");
Random randomGenerator = new Random();
for (int j=0; j < kolo+3; j++) {
Metheor meteor = new Metheor( randomGenerator.nextInt(200)+50,randomGenerator.nextInt(200)+50,
randomGenerator.nextInt(10)-5,randomGenerator.nextInt(10)-5);
meteor.setImage(img.getImage());
metheors.add(meteor);
}
player = new Player(APP_WIDTH/2,GROUND-PLAYER_HEIGHT);
shot = new Shot();
if (animator == null || !ingame) {
animator = new Thread(this);
animator.start();
}
}
public void drawMeteors(Graphics g)
{
Iterator it = metheors.iterator();
while (it.hasNext()) {
Metheor meteor = (Metheor) it.next();
if (meteor.isVisible()) {
g.drawImage(meteor.getImage(), meteor.getX(), meteor.getY(), this);
}
if (meteor.isDying()) {
meteor.die();
}
}
}
public void drawPlayer(Graphics g) {
if (player.isVisible()) {
g.drawImage(player.getImage(), player.getX(), player.getY(), this);
}
if (player.isDying()) {
player.die();
ingame = false;
}
}
public void drawShot(Graphics g) {
if (shot.isVisible())
g.drawImage(shot.getImage(), shot.getX(), shot.getY(), this);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.white);
if (ingame) {
g.drawLine(0, GROUND, APP_WIDTH, GROUND);
drawMeteors(g);
drawPlayer(g);
drawShot(g);
}
g.dispose();
}
public void gameOver() throws IOException
{
Graphics g = this.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, APP_WIDTH, APP_HEIGTH);
g.setColor(new Color(0, 32, 48));
g.fillRect(50, APP_WIDTH/2-30, APP_WIDTH-100, 120);
g.setColor(Color.white);
g.drawRect(50, APP_WIDTH/2-30, APP_WIDTH-100, 120);
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(message, (APP_WIDTH - metr.stringWidth(message))/2,
APP_WIDTH/2);
g.drawString("Score: " + score*20, (APP_WIDTH - metr.stringWidth(message))/2,
(APP_HEIGTH/2)+30);
int stare_score=0;
String strLine;
re = new BufferedReader(new FileReader("bestScore.txt"));
while ((strLine = re.readLine()) != null) {
String[] s = strLine.split("\t");
for (int i = 0; i <= s.length; i++) {
stare_score = Integer.parseInt(s[i]);
}
}
re.close();
if (stare_score <score*20){
//zapis do souboru score * 20
try {
wr = new BufferedWriter(new FileWriter("bestScore.txt"));
wr.write("" + score*20);
wr.flush();
wr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawString("Best Score: " + score*20, (APP_WIDTH - metr.stringWidth(message))/2,
(APP_HEIGTH/2)+60);
}
else {
g.drawString("Best Score: " + stare_score, (APP_WIDTH - metr.stringWidth(message))/2,
(APP_HEIGTH/2)+60);
wr = new BufferedWriter(new FileWriter("bestScore.txt"));
wr.write("" + stare_score);
wr.flush();
wr.close();
}
//dopsat ulozeni skore .. do souboru a potom to skore nacist to nejlepsi ... :D
}
public void animationCycle() {
if (deaths == kolo+3) {
kolo++;
deaths=0;
ImageIcon img = new ImageIcon("res\\meteor.png");
Random randomGenerator = new Random();
for (int j=0; j < kolo+3; j++) {
Metheor meteor = new Metheor( randomGenerator.nextInt(200)+50,randomGenerator.nextInt(200)+50,
randomGenerator.nextInt(10)-5,randomGenerator.nextInt(10)-5);
meteor.setImage(img.getImage());
metheors.add(meteor);
}
}
// player
player.act();
// shot
if (shot.isVisible()) {
Iterator it = metheors.iterator();
int shotX = shot.getX();
int shotY = shot.getY();
while (it.hasNext()) {
Metheor alien = (Metheor) it.next();
int alienX = alien.getX();
int alienY = alien.getY();
if (alien.isVisible() && shot.isVisible()) {
if (shotX >= (alienX) &&
shotX <= (alienX + METEOR_WIDTH) &&
shotY >= (alienY) &&
shotY <= (alienY+METEOR_HEIGHT) ) {
ImageIcon img =
new ImageIcon("res//explode.png");
alien.setImage(img.getImage());
alien.setDying(true);
deaths++;
score++;
shot.die();
}
}
}
int y = shot.getY();
y -= 10;
if (y < 0)
shot.die();
else shot.setY(y);
}
// metheors
Iterator it1 = metheors.iterator();
while (it1.hasNext()) {
Metheor a1 = (Metheor) it1.next();
int x = a1.getX();
int y = a1.getY();
Random randomGenerator = new Random();
if (y >= GROUND-METEOR_HEIGHT) {
a1.setVelocityX(a1.getVelocityX()*(randomGenerator.nextInt(4)-2));
a1.setVelocityY(a1.getVelocityY()*(-1));
if(a1.getVelocityX()==0){
a1.setVelocityX(-11);
}
if(a1.getVelocityX()>10||a1.getVelocityX()<-10){
a1.setVelocityX(2);
}
a1.act();
}
if (y <= 5) {
a1.setVelocityX(a1.getVelocityX()*(randomGenerator.nextInt(4)-2));
a1.setVelocityY(a1.getVelocityY()*(-1));
if(a1.getVelocityX()==0){
a1.setVelocityX(-1);
}
if(a1.getVelocityX()>10||a1.getVelocityX()<-10){
a1.setVelocityX(2);
}
a1.act();
a1.act();
a1.act();
}
if (x >= APP_WIDTH - BORDER_RIGHT) {
a1.setVelocityX(a1.getVelocityX()*(-1));
a1.setVelocityY(a1.getVelocityY()*(randomGenerator.nextInt(4)-2));
if(a1.getVelocityY()==0){
a1.setVelocityY(-1);
}
if(a1.getVelocityY()>10||a1.getVelocityY()<-10){
a1.setVelocityY(2);
}
a1.act();
a1.act();
a1.act();
}
if (x <= BORDER_LEFT) {
a1.setVelocityX(a1.getVelocityX()*(-1));
a1.setVelocityY(a1.getVelocityY()*(randomGenerator.nextInt(4)-2));
if(a1.getVelocityY()==0){
a1.setVelocityY(-1);
}
if(a1.getVelocityY()>10||a1.getVelocityY()<-10){
a1.setVelocityY(2);
}
a1.act();
a1.act();
a1.act();
}
}
Iterator it = metheors.iterator();
while (it.hasNext()) {
Metheor meteor = (Metheor) it.next();
if (meteor.isVisible()) {
if (meteor.getX() < player.getX()+PLAYER_WIDTH &&
meteor.getX() + METEOR_WIDTH > player.getX() &&
meteor.getY() < player.getY() + PLAYER_HEIGHT &&
METEOR_HEIGHT + meteor.getY() > player.getY()) {
player.die();
ingame=false;
}
meteor.act();
}
}
}
@Override
public void run() {
long sleep;
while (ingame) {
repaint();
animationCycle();
sleep = DELAY ;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
}
try {
gameOver();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
player.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
int x = player.getX();
int y = player.getY();
if (ingame)
{
if (e.isControlDown()) {
if (!shot.isVisible())
shot = new Shot(x, y);
}
}
}
}
}