Zdravím,
dělám hru a potřebuji poradit, dělám tlačítka a chci až zmáčknu třeba R, tak se spustí metoda, která prohazuje dva šálky. Metodu už jsem otestoval přímo v kódu takže když jsem ji volal přímo v třída, tak to fungovalo.
Ale když jsem to dal na tlačítko a zavolal, provede se to, ale není vidět, jak se objekty pohybuji. Jde jen vidět, že na konci se objekty pohnuli.
Prosím o pomoc. Děkuji
package game;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import graphics.Canvas;
import javax.swing.JFrame;
import javax.swing.JLabel;
import objects.Background;
import objects.Ball;
import objects.Cup;
public class Game extends JFrame implements KeyListener {
private static final long serialVersionUID = 1L;
private static final String TITLE = "Skořápky";
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int countCups=3 ;
private Canvas canvas;
private Background background = new Background();
private Ball ball;
private Cup[] cups;
private boolean isRunning = false;
private boolean isRotate = false;
//vytvoření okna
public Game() {
this.setTitle(TITLE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WIDTH, HEIGHT);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.add(this.canvas=new Canvas(WIDTH,HEIGHT));
gameStart();
prepareGame();
//runningGame(); <-- když dám metodu tady, tak to funguje
this.addKeyListener(this);
}
//Uvodní stránka
public void gameStart() {
this.canvas.addObject(this.background);
}
//vytvoří objekty, nastaví pozadí hrací plochy
public void prepareGame() {
this.background.setImageString("res/table_texture.jpg");
createCupsOnTable();
this.canvas.repaint();
}
//vymaže z listu všechny objekty krom pozadí
public void removeAllObject() {
this.canvas.removeObject(ball);
for(int x=0; x<countCups; x++)
this.canvas.removeObject(this.cups[x]);
}
public void runningGame() {
int rotate=generatedCountRotate();
for(int r=0; r<rotate;r++) {
int numSecondCup=generatedCupForRotate(-1);
changeCups(cups[generatedCupForRotate(numSecondCup)], cups[numSecondCup]);
}
}
//nastaví velikost pole,vytvoří šálky na stůl a prostřednímu dá kuličku a všechny objekty přidá do listu
public void createCupsOnTable() {
int pom=(countCups/2)-1;
this.cups=new Cup[countCups+1];
if(countCups>=2 && countCups<=8) {
for(int x=0; x<countCups; x++) {
this.cups[x]=new Cup(((widthCreateCup()+(40/countCups))*x)+40,(HEIGHT/2)-heightCreateCup(), widthCreateCup(),heightCreateCup(), null);
if(x==pom) {
this.cups[x].setHasBall(true);
// this.ball=new Ball(widthCreateCup()/2, heightCreateCup()/4, 0, 0, null);
// this.ball.setPositionToCup(this.cups[x]);
// this.canvas.addObject(this.ball);
}
this.canvas.addObject(this.cups[x]);
}
}
}
private int heightCreateCup() {
int result=widthCreateCup()*2;
if(result>200)
result=200;
return result;
}
private int widthCreateCup() {
int result=(WIDTH-100)/countCups;
if(result>100)
result=100;
return result;
}
public void changeCups(Cup cup1, Cup cup2) {
int Cup1X =cup1.getPositionX();
int Cup2X =cup2.getPositionX();
int diff=0;
if(cup1.getPositionX()>cup2.getPositionX())
diff=cup1.getPositionX()-cup2.getPositionX();
else
diff=cup2.getPositionX()-cup1.getPositionX();
for(int y=0; y<diff; y++) {
if(Cup1X<Cup2X) {
cup1.moveRight();
cup2.moveLeft();
if(y<(diff/2)) {
cup1.moveUp();
cup2.moveDown();
} else {
cup1.moveDown();
cup2.moveUp();
}
}
else {
cup1.moveLeft();
cup2.moveRight();
if(y<(diff/2)) {
cup1.moveUp();
cup2.moveDown();
} else {
cup1.moveDown();
cup2.moveUp();
}
}
this.canvas.repaint();
System.out.println(y);
timeSleep(generatedTimeSleep());
}
}
private int generatedCountRotate() {
int x;
x=countCups+(int)(Math.random()*(5*countCups));
return x;
}
private int generatedCupForRotate(int firstIs) {
int cup;
cup=(int)(Math.random()*(countCups));
if(cup==firstIs) {
while(cup!=firstIs) {
cup=(int)(Math.random()*(countCups));
}
}
return cup;
}
private int generatedTimeSleep() {
int sleep;
sleep=5+(int)(Math.random()*(5));
return sleep;
}
private void showCup() {
for(int i=0;i<50; i++) {
cups[countCups/2-1].moveUp();
timeSleep(10);
this.canvas.repaint();
}
}
private void timeSleep(int time) {
try {
Thread.sleep(time); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("ENTER");
this.ball=new Ball(50, 50, 50, 50, null);
this.canvas.addObject(this.ball);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("RIGHT");
this.ball.setPositionX(this.ball.getPositionX()+5);
this.canvas.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_R) {
System.out.println("X");
runningGame();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}