|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Junior Member
Iscritto dal: Mar 2017
Messaggi: 1
|
[HELP]Collisioni Java Grafica 2D [HELP]
Ciao ragazzi sto preparando questo codice per un progetto. Mi servirebbe una grossa mano in quanto sono riuscito diciamo ad impostare quello che è il giochino in 2d soltanto che non riesco a fare in modo che i due oggetti in questione (ovvero la sfera che lancia il lupo) ed appunto il nemico collidono. Mi servirebbe davvero una mano su come gestire le collisioni in modo che non appena diciamo parte la sfera e collide con il nemico, esso appunto scompare. Per favore aiutatemi vi lascio il codice qui sotto.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JComponent.*; import java.io.File; import java.lang.Thread.*; import java.io.IOException; import javax.imageio.ImageIO; class Proiettile extends JLabel implements Runnable { public Proiettile(String immagine){ ImageIcon image= new ImageIcon(immagine); this.setIcon(image); } public void run() { int p=200; for(int i=0;i<900;i+=12) { try{ Thread.sleep(p); setLocation(getX()+i,getY()); } catch (InterruptedException e) { System.out.println("Errore"); } } } } class EventiTastiera extends KeyAdapter{ private Pg casella; private Grafica gra; public Pg oggetto; public EventiTastiera(Pg pg,Grafica gr){ casella=pg; gra=gr; } //pressione del tasto public void keyPressed(KeyEvent e){ int x=casella.getX(); int y=casella.getY(); //freccia sinistra if (e.getKeyCode()==37){ casella.setLocation(x-10,y); } //freccia su salto if (e.getKeyCode()==KeyEvent.VK_UP){ Thread t1 = new Thread(casella); t1.start(); } //Sparo if (e.getKeyCode()==32){ Proiettile p= new Proiettile("fuoco.gif"); gra.add(p); p.setBounds(casella.getX()+150,casella.getY()+30,150,150); Thread t[]=new Thread[200]; t[1]=new Thread(p); t[1].start(); } //freccia destra if (e.getKeyCode()==39){ casella.setLocation(x+10,y); } } } class Nemici extends JLabel implements Runnable{ public Nemici(String immagine){ super(new ImageIcon(immagine)); } public void run(){ int y; try{ int x=(int)(Math.random()*1500)+1000; int s=(int)(Math.random()*150)+30; y=600; for(int i=0;i<2000;i+=10){ //ritardo Thread.sleep(s); setLocation(x-i,y); } } catch (InterruptedException e){ System.out.println("errore"); } } } class Pg extends JLabel implements Runnable { public Pg(String immagine){ super(new ImageIcon(immagine)); } public void run(){ int y=getY(); int x=getX(); try{ if(y==500){ for(int i=0;i<20;i++){ y=y-i; Thread.sleep(22); setLocation(x,y); } for(int i=0;i<20;i++){ y=y+i; Thread.sleep(40); setLocation(x,y); } } } catch (InterruptedException e){ System.out.println("errore"); } } } class Grafica extends JPanel{ //Attributi private Image image, pers; private JFrame frame; public Grafica(String back){ setLayout(null); try { image = ImageIO.read(new File (back)); } catch (IOException e) { e.printStackTrace(); } } public void paintComponent(Graphics g){ super.paintComponent(g); if (image==null) return; g.drawImage(image,0,0,null); } } class Game{ public static void main(String[] args) { int x,y; x=0; y=0; JFrame frame=new JFrame(); Grafica c = new Grafica("sfondo2.jpg"); Pg e=new Pg("Okami.gif"); frame.add(c); c.add(e); Thread t[]=new Thread[100]; for(int i=0;i<5;i++) { Nemici f=new Nemici("vivi.gif"); f.setBounds(x,y,40,47); c.add(f); t[i]=new Thread(f); t[i].start(); } e.setBounds(200,500,300,208); frame.setSize(1063,752); //Dimensioni in pixel della finestra frame.setVisible(true); frame.setLocationRelativeTo(null); //Imposta finestra al centro del desktop frame.setTitle("Amaterasu"); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addKeyListener(new EventiTastiera(e,c)); //Thread t; } } |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:58.