Dobrý večer,
podle návodu z internetu jsem se pokoušela napsat v javě hru had. Bohužel, když hru spustím, had reaguje na stisknuté klávesy, kterými se má ovládat směr jeho pohybu, velmi velmi pomalu. Nemohl by mi prosím někdo poradit, v čem je asi problém? Níže přikládám kód. Za jakékoliv podněty moc děkuji.
Frame.java:
package hadice;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JFrame;
/**
*
* @author fidelio
*/
public class Frame extends JFrame {
public Frame (){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Hadice");
setResizable(false);
init();
}
public void init (){
setLayout(new GridLayout(1,1,0,0));
Screen s = new Screen();
Component add = add(s);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Frame();
}
}
Screen.java:
package hadice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
/**
*
* @author fidelio
*/
public class Screen extends JPanel implements Runnable{
public static final int WIDTH=800, HEIGHT=800;
private Thread thread;
private boolean running = false;
private Body b;
private ArrayList<Body> had;
private Bonus bonus;
private ArrayList<Bonus> bonusy;
private int x = 10;
private int y = 10;
private int size = 5;
private boolean right=true, left=false, up=false, down=false;
private int ticks = 0;
private final Key key;
private Random r;
public Screen(){
setFocusable(true);
key = new Key();
addKeyListener(key);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
r = new Random();
had = new ArrayList<>();
bonusy = new ArrayList<Bonus>();
start();
}
public void tick(){
if(had.size()== 0){
b = new Body(x,y,10);
had.add(b);
}
if(x < 0 || x > 79 || y < 0 || y > 79){
stop();
}
ticks++;
if(bonusy.isEmpty()){
int x = r.nextInt(79);
int y = r.nextInt(79);
bonus = new Bonus(x,y,10);
bonusy.add(bonus);
}
for(int i = 0; i<bonusy.size(); i++){
if(x == bonusy.get(i).getX() && y == bonusy.get(i).getY()){
size++;
bonusy.remove(i);
i--;
}
}
for(int i = 0; i < had.size(); i++){
if(x==had.get(i).getX() && y==had.get(i).getY()){
if(i != had.size() - 1){
stop();
}
}
}
if(ticks > 250000){
if(right) x++;
if(left) x--;
if(up) y--;
if(down) y++;
ticks = 0;
b = new Body(x,y,10);
had.add(b);
if(had.size() > size){
had.remove(0);
}
}
}
@Override
// Zde se vykresluje mřížka herního pole
public void paint(Graphics g){
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLUE);
for(int i = 0; i < WIDTH/10;i++){
g.drawLine(i * 10, 0, i * 10, HEIGHT);
}
for(int i = 0; i < HEIGHT/10;i++){
g.drawLine(0, i * 10, WIDTH, i * 10);
}
for(int i = 0; i < had.size(); i++){
had.get(i).draw(g);
}
for(int i = 0; i<bonusy.size(); i++){
bonusy.get(i).draw(g);
}
}
public void start(){
running = true;
thread = new Thread(this, "Game Loop");
thread.start();
}
public void stop(){
running = false;
try {
thread.join();
} catch (InterruptedException ex) {
Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void run(){
while(running){
tick();
repaint();
}
}
private class Key implements KeyListener{
@Override
public void keyTyped(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT && !left){
up = false;
down = false;
right = true;
}
if(key == KeyEvent.VK_LEFT && !right){
up = false;
down = false;
left = true;
}
if(key == KeyEvent.VK_UP && !down){
left = false;
right = false;
up = true;
}
if(key == KeyEvent.VK_DOWN && !up){
left = false;
right = false;
down = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
}
Body.java:
package hadice;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author fidelio
*/
public class Body {
private int x;
private int y;
private final int width, height;
public Body(int x, int y, int tileSize){
this.x = x;
this.y = y;
width = tileSize;
height = tileSize;
}
public void tick(){
}
public void draw(Graphics g){
g.setColor(Color.RED);
g.fillOval(getX() * width, getY() * height, width, height);
g.setColor(Color.PINK);
g.fillOval(getX() * width + 2, getY() * height + 2, width - 4, height - 4);
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
}
Bonus.java:
package hadice;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author fidelio
*
*/
public class Bonus {
private int x, y, width, height;
public Bonus(int x,int y, int tileSize){
this.x = x;
this.y = y;
width = tileSize;
height = tileSize;
}
public void tick(){
}
public void draw(Graphics g){
g.setColor(Color.green);
g.fillRect(getX() * width, getY() * height, width, height);
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
}