Witam, chciałem napisać gierkę typu Space Invaders i problem pojawił się w ruchu "strzału" który oddaje, po naciśnięciu spacji, raz ruch zakończy się szybciej, raz wolniej. Timer za to odpowiedzialny, znajduje się w metodzie PaintComponent.
Przy okazji, chciałem się zapytać, dlaczego nawet po wpisaniu w miejscu opóźnienia (W tym przypadku jest 2000) Program, nadal płynnie wykonuje animacje, podczas gdy, strzał powinien się przesuwać, o jeden piksel co 2 sekundy.
Skopiowałem zawartość pliku, MainGame, mam jeszcze Main w którym uruchamiam okno, oraz MainFrame, w którym tworzę obiekt typu MainGame i dodaję go do kontenera.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.TimerTask;
import java.util.Timer;
public class MainGame extends JPanel implements KeyListener, MouseMotionListener
{
final public int WIDTH=1074;
final public int HEIGHT=691;
Color colorBlack = new Color(30, 30, 30);
Color colorGreen = new Color(125, 227, 80);
private BufferedImage imgPlayer;
Point player = new Point(WIDTH/2-40, HEIGHT-80);
Point bullet = new Point(-300, 0);
boolean shoot = false;
//---------------------------------------------------------------------------------------------------------------
public MainGame()
{
super();
File imageFile = new File("statek.png");
try{
imgPlayer = ImageIO.read(imageFile);
} catch(IOException e){
System.err.println("Blad odczytu obrazka");
e.printStackTrace();
}
setFocusable(true);
setBackground(colorBlack);
addMouseMotionListener(this);
addKeyListener(this);
}
//---------------------------------------------------------------------------------------------------------------
public boolean monsterCollision(Point bullet)
{
return false;
}
//---------------------------------------------------------------------------------------------------------------
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D player2D = (Graphics2D) g;
Graphics2D bullet2D = (Graphics2D) g;
bullet2D.setPaint(colorGreen);
bullet2D.fillRect(bullet.x, bullet.y, 10, 30);
player2D.drawImage(imgPlayer, player.x, player.y+10, 80, 80, this);
Timer shootTimer = new Timer();
shootTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run()
{
if(shoot)
{
bullet.y -= 1;
if(monsterCollision(bullet) || bullet.y <= 0)
{
shoot=false;
bullet.x=-300;
}
repaint();
}
}
}, 0, 2000);
}
//---------------------------------------------------------------------------------------------------------------
@Override
public void keyPressed(KeyEvent evt)
{
int k = evt.getKeyCode();
if(k == KeyEvent.VK_RIGHT)
{
if(player.x+80 + 10 <= WIDTH)
player.x += 10;
else
for(int i = 0; i<10; i++)
if(player.x+80 + 1 <= WIDTH)
player.x++;
}
if(k == KeyEvent.VK_LEFT)
{
if(player.x - 10 >= 0)
player.x -= 10;
else
for(int i = 0; i<10; i++)
if(player.x - 1 >= 0)
player.x--;
}
if(k == KeyEvent.VK_SPACE)
{
if(!shoot)
{
bullet.x = player.x + 35;
bullet.y = player.y + 45;
shoot=true;
}
}
repaint();
}
//---------------------------------------------------------------------------------------------------------------
@Override
public void keyReleased(KeyEvent evt) {
}
@Override
public void keyTyped(KeyEvent evt) {
}
//---------------------------------------------------------------------------------------------------------------
@Override
public void mouseMoved(MouseEvent evt) {
//System.out.println(evt.getX() + " " + evt.getY());
}
@Override
public void mouseDragged(MouseEvent evt) {
System.out.println(evt.getX() + " " + evt.getY());
}
}