Skip to content

Instantly share code, notes, and snippets.

@ftuyama
Last active August 29, 2015 14:20
Show Gist options
  • Save ftuyama/6a7fa3a15db4d8407788 to your computer and use it in GitHub Desktop.
Save ftuyama/6a7fa3a15db4d8407788 to your computer and use it in GitHub Desktop.
Versão Final
package cs;
import javax.swing.*;
public class Bizuca extends JFrame {
public Bizuca ()
{
add(new Game());
setTitle("Bizuca V 2.0");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1280,720);
setVisible(true);
setResizable(true);
}
public static void main(String[] args) {
new Bizuca();
}
}
package cs;
import java.awt.image.*;
import javax.swing.ImageIcon;
public class Fase extends Figura
{
boolean[][] fase = new boolean[imagem.getHeight(null)][imagem.getWidth(null)];
public Fase()
{
super(new ImageIcon("src\\cs\\imagens\\fase.png").getImage(), 0, 0);
BufferedImage I = toBufferedImage(imagem);
int[] pixels = ((DataBufferInt) I.getRaster().getDataBuffer()).getData();
final int width = I.getWidth(), height = I.getHeight();
final boolean hasAlphaChannel = I.getAlphaRaster() != null;
if (hasAlphaChannel)
{
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength)
{
int argb = 0;
argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
argb += ((int) pixels[pixel + 1] & 0xff); // blue
argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
fase[row][col] = (argb!=0);
col++;
if (col == width) {
col = 0;
row++;
}
}
} else
{
final int pixelLength = 3;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength)
{
int argb = 0;
argb += -16777216; // 255 alpha
argb += ((int) pixels[pixel] & 0xff); // blue
argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
fase[row][col] = (argb!=0);
col++;
if (col == width) {
col = 0;
row++;
}
}
}
}
}
package cs;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
public class Figura
{
final int HTela = 1280, LTela = 720;
final int PlayerSpeed = 7, BotSpeed = 1, BulletSpeed = 3;
final int NBullets = 10, Rcontain = 3, Trecharge = 100;
final float Vparede = (float) 1.5, Vbot = 10, Vplayer = 7, Stress = 100;
public int dparede, Rvisao, Rparede;
public int ID, HP, Bullets, change, recharge;
public boolean parede, atirou, olhou, move;
public boolean moved, toAdd, friendly;
public Image imagem;
private int x, y;
public int ax, ay, AbsX, AbsY, xVet, yVet;
public float Sin, Cos, Speed;
public int Width, Height, Radius;
int [][] vertices = new int[5][2];
ArrayList<Point> olhar;
Figura vBullet;
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXX Posicionamento XXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public int getX () { return x; }
public int getY () { return y; }
public int getXc (){ return x - Width/2;}
public int getYc (){ return y - Height/2;}
public void setXY (int X, int Y) { x = X; y = Y; }
public void move (float Cos, float Sin){
x = (int) (x + Speed*Cos); y = (int) (y + Speed*Sin); }
public float distance(float Xa, float Ya, float Xb, float Yb){
return (float)Math.sqrt((Xa-Xb)*(Xa-Xb)+(Ya-Yb)*(Ya-Yb));
}
public boolean contains(int Xa, int Ya){
return (distance(x,y,Xa,Ya)<Rcontain*Radius);
}
public void setVertices(int Xs, int Ys)
{
vertices[0][0] = Xs; vertices[0][1] = Ys;
vertices[1][0] = Xs; vertices[1][1] = Ys+Height/4;
vertices[2][0] = Xs+Width/4; vertices[2][1] = Ys+Height/4;
vertices[3][0] = Xs+Width/4; vertices[3][1] = Ys;
vertices[4][0] = Xs+Width/8; vertices[4][1] = Ys+Height/8;
}
public void setUp (){
Width = imagem.getHeight(null); Height = imagem.getHeight(null);
Radius = (int) distance(0,0,Width,Height);
if(friendly) Rvisao = (int) (Vplayer*Radius);
else Rvisao = (int) (Vbot*Radius);
Rparede = (int) (Vparede*Radius);
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXX Construtores XXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Figura (){this.olhar = new ArrayList<>();
}
Figura (Image Im, int X, int Y){
this.olhar = new ArrayList<>();
setImagem(Im); x = X; y = Y; setUp();
}
Figura (Image Im, int X, int Y, int ID, int HP, float Sin, float Cos, boolean F){
this.olhar = new ArrayList<>();
setImagem(Im); x = X; y = Y; setUp();
this.ID = ID; this.HP = HP; Bullets = NBullets;
this.Sin = Sin; this.Cos = Cos;
vBullet = new Figura();
toAdd = moved = atirou = parede = move = false;
recharge = change = 0;
friendly = F;
if (F) Speed = PlayerSpeed;
else Speed = BotSpeed;
}
Figura (Image Im, int X, int Y, float Sin, float Cos, boolean F){
this.olhar = new ArrayList<>();
setImagem(Im); x = X; y = Y; setUp();
friendly = F; Speed = BulletSpeed;
this.Sin = Sin; this.Cos = Cos;
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXX Jogabilidade XXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public boolean alive() { return HP>0;}
public boolean atirar (){
if (Bullets>0){
toAdd = true;
Bullets--;
}
return (Bullets>=0);
}
public void dano (int damage){ HP-=damage; }
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXX Imagem Soft XXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public Image getImagemPlayer () {
float angulo = (float) Math.atan2(ay-LTela/2, ax-HTela/2);
return (Image)rotateImage(toBufferedImage(imagem), angulo*180/Math.PI);
}
public Image getImagemBot () {
float angulo = (float) Math.atan2(Sin,Cos);
return (Image)rotateImage(toBufferedImage(imagem), angulo*180/Math.PI);
}
public Image getImagem () {
float angulo = (float) Math.atan2(ay-y, ax-x);
return (Image)rotateImage(toBufferedImage(imagem), angulo*180/Math.PI);
}
public void setImagem (Image imagem) { this.imagem = imagem; }
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXX Imagem Hard XXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public static BufferedImage rotateImage(BufferedImage rotateImage, double angle)
{
angle %= 360;
if (angle < 0) angle += 360;
AffineTransform tx = new AffineTransform();
tx.rotate(Math.toRadians(angle), rotateImage.getWidth() / 2.0, rotateImage.getHeight() / 2.0);
double ytrans = 0, xtrans = 0;
if (angle <= 90) {
xtrans = tx.transform(new Point2D.Double(0, rotateImage.getHeight()), null).getX();
ytrans = tx.transform(new Point2D.Double(0.0, 0.0), null).getY();
} else if (angle <= 180) {
xtrans = tx.transform(new Point2D.Double(rotateImage.getWidth(), rotateImage.getHeight()), null).getX();
ytrans = tx.transform(new Point2D.Double(0, rotateImage.getHeight()), null).getY();
} else if (angle <= 270) {
xtrans = tx.transform(new Point2D.Double(rotateImage.getWidth(), 0), null).getX();
ytrans = tx.transform(new Point2D.Double(rotateImage.getWidth(), rotateImage.getHeight()), null).getY();
} else {
xtrans = tx.transform(new Point2D.Double(0, 0), null).getX();
ytrans = tx.transform(new Point2D.Double(rotateImage.getWidth(), 0), null).getY();
}
AffineTransform translationTransform = new AffineTransform();
translationTransform.translate(-xtrans, -ytrans);
tx.preConcatenate(translationTransform);
return new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR).filter(rotateImage, null);
}
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage) return (BufferedImage) img;
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
}
package cs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Game extends JPanel
{
String bot_ = "src\\cs\\imagens\\bot.png";
String bala_ = "src\\cs\\imagens\\bala.png";
String tiro_ = "src\\cs\\imagens\\tiro.png";
String intro_ = "src\\cs\\imagens\\intro.png";
String player_ = "src\\cs\\imagens\\player.png";
String vitoria_ = "src\\cs\\imagens\\vitoria.png";
String gameover_ = "src\\cs\\imagens\\gameover.png";
String fasedesign_ = "src\\cs\\imagens\\fasedesign.png";
private JButton jplay, jexit, jteste;
private JTextField jtempo, jmusica, jbots, jsaude;
private JLabel jtexto1, jtexto2, jtexto3, jtexto4;
private JLabel jex1, jex2, jTitle, jCredit;
final static int HFase = 1600 , LFase = 2000;
final static int HTela = 1280, VTela = 720;
Random gerador = new Random();
boolean Start = false, AllAI = false, VersaoTeste = false;
private Thread Jogo, AI;
private ArrayList<Thread> AIvector = new ArrayList<>();
float Sin, Cos, Theta;
int AbsX, AbsY, Xp, Yp, xVet, yVet;
int Nbots, recover, distance, Tmax, tempo = 0;
Fase fase = new Fase();
Musica Music = new Musica();
Image intro = new ImageIcon(intro_).getImage();
Image vitoria = new ImageIcon(vitoria_).getImage();
Image gameover = new ImageIcon(gameover_).getImage();
Image fasedesign = new ImageIcon(fasedesign_).getImage();
ArrayList<Figura> Balas = new ArrayList<>();
ArrayList<Figura> RemBalas = new ArrayList<>();
ArrayList<Figura> Tiros = new ArrayList<>();
ArrayList<Figura> RemTiros = new ArrayList<>();
ArrayList<Figura> Bots = new ArrayList<>();
private Figura P1;
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXX Inicialização XXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public Game()
{
setFocusable(true);
setBounds(0, 0, HTela, VTela);
setLayout(null);
init();
eventos();
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXX Menu XXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public void Inicializar()
{
Start = true;
removeAll(); validate();
P1 = new Figura(new ImageIcon(player_).getImage(),
350,350,-1,100, 0, 0, true);
for (int n = 0; n<Nbots; n++) {
Xp = gerador.nextInt(HFase); Yp = gerador.nextInt(LFase);
float Sin = 2*gerador.nextFloat()-1;
Figura novoBot = new Figura(new ImageIcon(bot_).getImage(),
Xp,Yp, n, 100, Sin, (float)Math.sqrt(1-Sin*Sin), false);
while (colisao2(novoBot, Xp/4, Yp/4)){
Xp = gerador.nextInt(HFase); Yp = gerador.nextInt(LFase);
novoBot.setXY(Xp, Yp);
}
Bots.add(novoBot);
}
}
public void init()
{
int H = 350, D = 150, V = 150;
jtexto1 = new JLabel("Música?"); jtexto1.setBounds(H, V, 200, 30); add(jtexto1);
jtexto2 = new JLabel("Nº Bots:"); jtexto2.setBounds(H, V+50, 200, 30); add(jtexto2);
jtexto3 = new JLabel("Regenaração?"); jtexto3.setBounds(H, V+100, 200, 30); add(jtexto3);
jtexto4 = new JLabel("Tempo máximo:"); jtexto4.setBounds(H, V+150, 200, 30); add(jtexto4);
jex1 = new JLabel("(1=sim) (0=não)"); jex1.setBounds(H, V+15, 200, 30); add(jex1);
jex2 = new JLabel("(1=sim) 0=não) (-1=negativa)"); jex1.setBounds(H, V+115, 200, 30); add(jex2);
jTitle = new JLabel("Bizuca V 2.0");
jTitle.setFont(new java.awt.Font("Old English Text MT", 0, 36));
jTitle.setBounds(HTela/2, 0, 300, 100); add(jTitle);
jCredit = new JLabel("By Felipe Tuyama & Uriel");
jCredit.setFont(new java.awt.Font("Euclid Fraktur", 1, 18));
jCredit.setBounds(HTela/2, 400, 300, 100); add(jCredit);
jmusica= new JTextField("1"); jmusica.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jmusica.setBounds(H+D,V,200,30); add(jmusica);
jbots = new JTextField("5"); jbots.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jbots.setBounds(H+D,V+50,200,30); add(jbots);
jsaude = new JTextField("0"); jsaude.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jsaude.setBounds(H+D,V+100,200,30); add(jsaude);
jtempo = new JTextField(""+HTela); jtempo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jtempo.setBounds(H+D,V+150,200,30); add(jtempo);
jplay = new JButton ("Play"); jplay.setBounds(HTela/2, 350, 100, 40); add(jplay);
jteste = new JButton ("Versão Teste"); jteste.setBounds(0, 0, 200, 40); add(jteste);
jexit = new JButton ("X"); jexit.setBounds(HTela-75, 0, 50, 25); add(jexit);
}
public void eventos(){
jplay.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e) {
Music.MusicOn = (Integer.parseInt(jmusica.getText().toString())==1);
Nbots = (Integer.parseInt(jbots.getText().toString()));
Tmax = (Integer.parseInt(jtempo.getText().toString()));
recover = (Integer.parseInt(jsaude.getText().toString()));
startGame();
}
});
jteste.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e) {
Nbots = 5; recover = 1; Tmax = HTela;
VersaoTeste = true;
startGame();
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e) {
System.exit(1);
}
});
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXX Loop Principal XXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public void startGame(){
Inicializar();
Jogo = new Thread(){
public void run()
{
Music.play();
controle();
setUpAI();
while (GameOn())
{
timeEvents();
physics();
repaint();
waitAI();
try {
Thread.sleep(5);
} catch (InterruptedException ex) {}
}
Music.GO();
repaint();
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {}
}
};
Jogo.start();
}
// Verifica o término do jogo
public boolean GameOn(){
return P1.alive() && tempo<Tmax && Nbots>0;
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXX Thread da AI XXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public synchronized void waitAI()
{
while (!Allmoved())
try {
wait();
} catch (InterruptedException e) { }
for (Figura bot : Bots)
bot.moved = false;
notifyAll();
}
public boolean Allmoved(){
AllAI = true;
for (Figura bot : Bots){
if (AllAI) AllAI = bot.moved && AllAI;
else break;
}
return AllAI;
}
public synchronized void botWait(Figura bot) {
bot.moved = true;
while (bot.moved) {
try {
notifyAll();
wait();
} catch (InterruptedException e) {}
}
}
public void setUpAI(){
for(Figura bot: Bots)
AIvector.add(AI = new Thread(){
public void run(){
while (bot.alive()) {
botLook(bot);
botDecide(bot);
botMove(bot);
botWait(bot);
}
Bots.remove(bot);
tempo-=100;
Nbots--;
}
});
for (Thread AIbot: AIvector)
AIbot.start();
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXX Loop do Thread XXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public boolean insideBounds(int X, int Y){
return (X>0&&Y>0&&X<LFase&&Y<HFase);
}
public void botLook(Figura bot)
{
bot.olhar.clear();
bot.atirou = bot.parede = false; distance = 0;
int xOrig = bot.getXc(), yOrig = bot.getYc();
int StepX = (int) (5*bot.Cos), StepY = (int) (5*bot.Sin);
if (Math.abs(bot.Cos)<0.1) { StepX = 0; StepY = 5*(int) Math.signum(bot.Sin);}
else if (Math.abs(bot.Sin)<0.1) { StepX = 5*(int) Math.signum(bot.Cos); StepY = 0;}
for (int i = 5; i<10; i++)
if (Math.abs(bot.Cos)*i>1 && Math.abs(bot.Sin)*i>1){
StepX = (int) (i*bot.Cos); StepY = (int) (i*bot.Sin);
break;
}
for (xVet = xOrig, yVet = yOrig; insideBounds(xVet,yVet)&&
distance<bot.Rvisao/4; xVet+=StepX, yVet+=StepY)
{
distance = distance(xOrig,yOrig,xVet,yVet);
//bot.olhar.add(new Point(xVet,yVet));
if (distance>1 && distance<bot.Rparede)
if (!fase.fase[xVet/4][yVet/4] ) {
bot.parede = true; bot.dparede = distance; }
if (P1.contains(xVet, yVet)) bot.atirou = true;
if(bot.atirou||bot.parede) break;
}
bot.xVet = xVet; bot.yVet = yVet;
}
public void botDecide(Figura bot){
if (bot.atirou){
if (bot.recharge==0)
{
if (bot.atirar())
bot.vBullet = new Figura(new ImageIcon(tiro_).getImage(),
bot.getX(), bot.getY(), bot.Sin, bot.Cos, false);
botTurnAround(bot);
bot.recharge = bot.Trecharge;
}
else bot.recharge--;
}
}
public void botMove(Figura bot)
{
if (bot.parede)
botTurnAround(bot);
mover(bot, bot.Cos, bot.Sin);
if(bot.change>bot.Stress)
{
botTurnAround(bot);
bot.change = 0;
}
bot.change++;
}
public void botTurnAround(Figura bot){
Theta = (float) (2*Math.PI*gerador.nextFloat());
bot.Sin = (float) Math.sin(Theta);
bot.Cos = (float) Math.cos(Theta);
bot.change = 0;
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXX Métodos Periódicos XXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private void timeEvents(){
if (tempo%50==0)
{
for(Figura bot: Bots) bot.Bullets++;
Xp = gerador.nextInt(HFase); Yp = gerador.nextInt(LFase);
Figura novaBala = new Figura(new ImageIcon(bala_).getImage(),
Xp,Yp, 1, 0, false);
while (colisao(novaBala, Xp/4, Yp/4)){
Xp = gerador.nextInt(HFase); Yp = gerador.nextInt(LFase);
novaBala.setXY(Xp, Yp);
}
Balas.add(novaBala);
P1.HP+=recover*(100-P1.HP)/20;
tempo++;
}
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXX Métodos de Colisão XXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private void physics ()
{
for (Figura bot: Bots)
if (bot.toAdd)
{
Figura novoTiro = new Figura();
novoTiro = bot.vBullet;
Tiros.add(novoTiro);
bot.toAdd = false;
}
if (P1.toAdd)
{
Figura novoTiro = new Figura();
novoTiro = P1.vBullet;
Tiros.add(novoTiro);
P1.toAdd = false;
}
if (!Tiros.isEmpty())
{
for (Figura T : Tiros)
{
if (!T.friendly)
if (colisaoFig(T, P1, T.Cos, T.Sin)){
P1.dano(20+gerador.nextInt(20));
RemTiros.add(T);
}
if (T.friendly && !RemTiros.contains(T))
for (Figura Bot : Bots)
if (!RemTiros.contains(T))
if (colisaoFig(T, Bot, T.Cos, T.Sin)) {
Bot.dano(20+gerador.nextInt(20));
RemTiros.add(T);
}
if (!RemTiros.contains(T))
if(!mover(T, T.Cos, T.Sin))
RemTiros.add(T);
}
if (!RemTiros.isEmpty()) {
for (Figura T : RemTiros)
Tiros.remove(T);
RemTiros.clear();
}
}
if(!Balas.isEmpty())
{
for (Figura candy: Balas)
{
if (colisaoFig(P1, candy, 0, 0)) {
tempo-=10; P1.Bullets+=5; RemBalas.add(candy);
}
for (Figura Bot : Bots)
if (!RemBalas.contains(candy))
if (colisaoFig(Bot, candy, 0, 0)) {
P1.Bullets+=5; RemBalas.add(candy);
}
}
if (!RemBalas.isEmpty()) {
for (Figura candy : RemBalas)
Balas.remove(candy);
RemBalas.clear();
}
}
}
public boolean mover (Figura aux, float X, float Y)
{
int Xs = (int)(aux.getX()/4+aux.Speed*X);
int Ys = (int)(aux.getY()/4+aux.Speed*Y);
aux.move = !colisao2(aux,Xs,Ys);
if (aux.move) aux.move(X, Y);
return aux.move;
}
public boolean colisao2 (Figura aux, int Xs, int Ys){
boolean colisao = colisao(aux,Xs,Ys);
for (Figura bot: Bots)
if (aux.ID!=bot.ID)
if (colisaoFig(aux,bot,Xs,Ys))
colisao = true;
return colisao;
}
public boolean colisao (Figura aux, int Xs, int Ys)
{
boolean colisao = false;
aux.setVertices(Xs, Ys);
for (int i = 0; i<5 && !colisao; i++)
if (aux.vertices[i][0]>0 && aux.vertices[i][1]>0)
if (aux.vertices[i][0]<LFase/4 && aux.vertices[i][1]<HFase/4)
colisao = !fase.fase[aux.vertices[i][1]][aux.vertices[i][0]];
else colisao = true;
else colisao = true;
return colisao;
}
public boolean colisaoFig (Figura aux1, Figura aux2, float X, float Y)
{
int Xs = (int)(aux1.getX()+aux1.Speed*X);
int Ys = (int)(aux1.getY()+aux1.Speed*Y);
int X2 = aux2.getX(), Y2 = aux2.getY();
return (distance(Xs,Ys,X2,Y2) < (aux1.Radius + aux2.Radius));
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXX Métodos de Desenho XXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public void paintComponent(Graphics g)
{
if(Start)
{
if(GameOn())
{
super.paintComponent(g);
AbsX = HTela/2-P1.getX(); AbsY = VTela/2-P1.getY();
g.drawImage(fasedesign,AbsX, AbsY,this);
g.setColor(Color.black); g.fillRect(0, 0, HTela, 40); g.fillRect(0, 0, 70, 60);
g.setColor(Color.red); g.fillRect(5, 5, (tempo++)*HTela/Tmax, 30);
g.setColor(Color.black); g.drawString(" "+tempo+"/"+HTela, 10, 30);
g.setColor(Color.red); g.drawString("Faltam: "+Nbots, 10, 50);
if (P1.alive())
{
Xp = HTela/2-P1.Width/2; Yp = VTela/2-P1.Height/2;
if(VersaoTeste)
g.drawOval(Xp-P1.Rvisao/2, Yp-P1.Rvisao/2,P1.Rvisao,P1.Rvisao);
g.setColor(Color.red); g.drawString("HP:"+P1.HP, Xp-25, Yp-30);
g.setColor(Color.blue); g.drawString("B:"+P1.Bullets, Xp+25, Yp-30);
g.drawImage(P1.getImagemPlayer(),Xp, Yp, this);
g.setColor(Color.black); g.fillRect(Xp-4, Yp-24, P1.HP/2+8, 18);
g.setColor(Color.red); g.fillRect(Xp, Yp-20, P1.HP/2, 10);
}
if(!Balas.isEmpty())
for (int i = 0; i<Balas.size(); i++)
g.drawImage(Balas.get(i).getImagemBot(), AbsX+Balas.get(i).getX(),
AbsY+Balas.get(i).getY(), this);
if(!Tiros.isEmpty())
for (int i = 0; i<Tiros.size(); i++)
g.drawImage(Tiros.get(i).getImagemBot(), AbsX+Tiros.get(i).getX(),
AbsY+Tiros.get(i).getY(), this);
for (int i = 0; i<Bots.size(); i++)
if (Bots.get(i).alive())
{
Figura B = Bots.get(i);
Xp = AbsX+B.getX(); Yp = AbsY+B.getY();
if(VersaoTeste)
{
/*
try{
if(!B.olhar.isEmpty())
for(int i=0; i<B.olhar.size(); i++)
g.drawRect(AbsX+B.olhar.get(i).x, AbsY+B.olhar.get(i).y, 10, 10);
} catch (Exception NullPointerException){ }
*/
g.setColor(Color.MAGENTA); g.drawLine(Xp, Yp, B.xVet, B.yVet);
g.setColor(Color.blue);
g.drawOval(Xp-B.Rvisao/2,Yp-B.Rvisao/2,B.Rvisao,B.Rvisao);
if(B.atirou) g.drawString("atirou",Xp+30,Yp-30);
g.setColor(Color.red);
if(B.parede) g.drawString("parede",Xp-30,Yp-30);
if(B.parede) g.drawString(""+B.dparede,Xp-30,Yp+30);
g.drawString(""+B.change,Xp-30,Yp+60);
g.drawOval(Xp-B.Rparede/2,Yp-B.Rparede/2,B.Rparede,B.Rparede);
}
g.setColor(Color.red); g.drawString("HP:"+B.HP, Xp-25, Yp-60);
g.setColor(Color.blue); g.drawString("B:"+B.Bullets, Xp+25, Yp-60);
g.setColor(Color.black); g.fillRect(Xp-20, Yp-60, B.HP/2+8, 18);
g.setColor(Color.green); g.fillRect(Xp-16, Yp-55, B.HP/2, 10);
g.drawImage(B.getImagemBot(),AbsX+B.getXc(), AbsY+B.getYc(), this);
}
}
else if (Nbots>0) g.drawImage(gameover,0,0,this);
else g.drawImage(vitoria,0,0,this);
}
else {
super.paintComponent(g);
g.drawImage(intro,0,0,this);
}
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXX Métodos de Controle XXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
public void CalculaSinCos()
{
float ax = P1.ax, ay = P1.ay;
float Hip = distance(ax,ay,HTela/2, VTela/2);
Sin = (ay-VTela/2)/Hip; Cos = (ax-HTela/2)/Hip;
}
public void controle()
{
// XXXXXXXXXXXXXXXXXXX Controle do Mouse XXXXXXXXXXXXXXXXXXXXX
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if (P1.atirar()) {
CalculaSinCos();
P1.vBullet = new Figura(new ImageIcon(tiro_).getImage(),
P1.getX(), P1.getY(), Sin, Cos, true);
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
});
addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) { }
public void mouseMoved(MouseEvent e) {
P1.ax = e.getX(); P1.ay = e.getY();
}
});
// XXXXXXXXXXXXXXXXXXX Controle das Teclas XXXXXXXXXXXXXXXXXXXXX
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e) {
int Key = e.getKeyCode();
CalculaSinCos();
if (Key == 38 || Key == 87) mover(P1, Cos, Sin);
if (Key == 40 || Key == 83) mover(P1,-Cos, -Sin);
if (Key == 37 || Key == 68) mover(P1, -Cos, Sin);
if (Key == 39 || Key == 65) mover(P1, Cos, -Sin);
if (Key == 27) System.exit(0);
}
public void keyReleased(KeyEvent e) {
//System.out.println(e.getKeyCode());
}
});
}
public int distance(int Xa, int Ya, int Xb, int Yb){
return (int)Math.sqrt((Xa-Xb)*(Xa-Xb)+(Ya-Yb)*(Ya-Yb));
}
public float distance(float Xa, float Ya, float Xb, float Yb){
return (float)Math.sqrt((Xa-Xb)*(Xa-Xb)+(Ya-Yb)*(Ya-Yb));
}
}
package cs;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.*;
public class Musica
{
Sequencer seq, seqGO;
boolean MusicOn = false;
String musica_ = "src\\cs\\imagens\\musica.mid";
String GO_ = "src\\cs\\imagens\\GO.mid";
File midiFile = new File(musica_);
File midiFileGO = new File(GO_);
public Musica(){
try {
seq = MidiSystem.getSequencer();
seq.setSequence(MidiSystem.getSequence(midiFile));
seq.open();
seqGO = MidiSystem.getSequencer();
seqGO.setSequence(MidiSystem.getSequence(midiFileGO));
seqGO.open();
} catch (MidiUnavailableException
| InvalidMidiDataException| IOException mue) { }
}
public void play(){
if (MusicOn) seq.start();
}
public void GO(){
seq.stop();
seqGO.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment