0

I'm trying to make a space invaders game and I've got an issue where I'm trying to move the spaceinvaders in a loop [though the code shows an iteration of 5 movements in a for] but they aren't updated in the window.

as you can see from the picture, the spaceinvaders have been moved to the right only once, after the iterations of run() have ended

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;

import java.util.Random;

public class SpaceInvadersPanel extends JPanel {

    boolean running = false;
    
    int indice = 0;                     //placeholder
    int fine = 10;                      //placeholder
    int speed = 10;                     //placeholder
    String correctAnswer = "prova";     //Placeholder
    
    Aliens[] alien = new Aliens[fine];
    int alienX = 35;
    int alienY = 35;

    SpaceInvadersPanel(int SCREEN_WIDTH, int SCREEN_HEIGHT) throws IOException, InterruptedException {
        
        this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
        this.setBackground(Color.white);
        this.setFocusable(true);
        
        startGame();
        
    }
    
    public void startGame() throws IOException, InterruptedException{
        
        newSpaceInvader();
            
    }
    
    
    public void newSpaceInvader() {
            
        for(int i = 0; i<alien.length; i++) {
                        
            alien[i] = new Aliens(alienX, alienY, speed);
            alienX += 50;
            if (i == 4) {alienY += 50; alienX = 35;}
            
            }
    }
    
    public void paintComponent(Graphics g) {
        
        super.paintComponent(g);
        try {
            try {
                draw(g);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {e.printStackTrace();
        }
    }

    public void draw(Graphics g) throws IOException, InterruptedException {
        
        BufferedImage image = ImageIO.read(new File("C:\\Users\\39340\\Desktop\\", "spaceinvadersart40bianco.png"));
        g.setColor(Color.red);
        
        for(int i = 0; i<alien.length; i++) {
            
        g.drawImage(image, alien[i].x, alien[i].y, null);

        }
        
        run();

    }
    
    
    public void run() throws IOException, InterruptedException {
        for (int i=0; i<5; i++) {System.out.println("sono al run");
            move();Thread.sleep(500);}

    }
    
    public void move() throws IOException {
            
            for(int i = 0; i<alien.length; i++) {
            
            System.out.println("sto aumentando roba nel move");
            System.out.println("alien x = " +alien[i].x);
            alien[i].x += 50;
            System.out.println("ora alien x = " +alien[i].x);
            //alien[i].y += speed;

        }
        
    }
        
    public void checkCollision() {}

    public void gameOver(Graphics g) {}
    

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Guido
  • 1
  • 1
  • 2
    1) Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in .. – Andrew Thompson Aug 09 '21 at 10:30
  • 1
    .. [this question](https://stackoverflow.com/q/10861852/418556). 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Aug 09 '21 at 10:32
  • 1
    For animation in _Swing_ applications, use a [timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Abra Aug 09 '21 at 12:16
  • 1
    The problem with searching the Internet is that you have no idea how old some of the Web pages are nor how up to date they are. Originally, i.e. over 20 years ago, most _Swing_ example applications on the Internet extended class `javax.swing.JFrame`. Note that a _Swing_ application does **not** need to extend class `JFrame`. – Abra Aug 09 '21 at 12:20
  • Go through some examples of swing animations using timer, like @Abra suggested: [1](https://stackoverflow.com/questions/43706438/moving-image-randomly-multiple-times/43706965#43706965) , [2](https://stackoverflow.com/a/53701618/3992939) , [3](https://stackoverflow.com/a/52685403/3992939). – c0der Aug 10 '21 at 04:26

0 Answers0