tentei meio mudo de coisa kkk ja to vendo as letrinhas verdes de Matrix, parei nessa gambiarra…
package Pack.game;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import Pack.Entities.Entity;
import Pack.Entities.Inimigos;
import Pack.Entities.Player;
import Pack.Entities.Tiros;
import Pack.Graphics.SpreteSheet;
import Pack.Graphics.UI;
import Pack.World.Camera;
import Pack.World.World;
public class Game extends Canvas implements Runnable, KeyListener, MouseListener {
private static final long serialVersionUID = 1L;
public static JFrame frame;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public final int scale = 1;
private int FPS;
private Thread thread;
private boolean isRunning;
private BufferedImage image;
public static Player player;
public static List<Entity>entities;
public static SpreteSheet spriteSheet;
public static World world;
public static Random aliatorio;
public static List<Inimigos>inimigosLista;
public static List<Tiros>tiros;
public UI ui;
double angleMouse;
public Game() {
aliatorio = new Random();
addKeyListener(this);
addMouseListener(this);
initFrame();
ui = new UI();
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
entities = new ArrayList<Entity>();
inimigosLista = new ArrayList<Inimigos>();
tiros = new ArrayList<Tiros>();
spriteSheet = new SpreteSheet("/sprites.png");
player = new Player(0,0,64,64,spriteSheet.getSprite(0, 0, 64, 64),angleMouse);
entities.add(player);
world = new World("/MiniMapa.png");
}
public void initFrame() {
frame = new JFrame("Meu Game");
frame.setPreferredSize(new Dimension(WIDTH * scale, HEIGHT * scale));
frame.add(this);
frame.setResizable(false);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icone.png")));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public synchronized void start() {
thread = new Thread(this);
isRunning = true;
thread.start();
}
public synchronized void stop() {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void tick() {
for (int i =0; i<entities.size();i++) {
Entity e =entities.get(i);
e.tick();
}
for(int i=0; i<tiros.size();i++) {
tiros.get(i).tick();
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(1,1,1));
g.fillRect(0,0,WIDTH, HEIGHT);
// objetos a serem renderizados
world.render(g);
for (int i =0; i<entities.size();i++) {
Entity e =entities.get(i);
e.render(g);
}
// Imagens a serem renderizadas
for(int i=0; i<tiros.size();i++) {
tiros.get(i).render(g);
}
ui.render(g);
g=bs.getDrawGraphics();
g.drawImage(image,0,0,WIDTH*scale, HEIGHT*scale,null);
g.setFont(new Font("Arial Black",Font.BOLD,30));
g.setColor(Color.BLACK);
g.drawString(""+player.municao, 717*scale, 105*scale);
g.setColor(new Color(0,255,255));
g.drawString(""+player.municao, 713*scale, 100*scale);
g.setFont(new Font("Arial Black",Font.BOLD,14));
g.setColor(Color.BLACK);
g.drawString("Pitombos",player.getX()-Camera.x+5, player.getY()-Camera.y-10);
g.setFont(new Font("Congenial UltraLight",Font.BOLD,14));
g.setColor(Color.WHITE);
g.drawString("FPS: "+FPS, 725*scale, 550*scale);
bs.show();
}
@Override
public void run() {
long lestTime = System.nanoTime();
double amountOfTicks = 60;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int frames = 0;
double time = System.currentTimeMillis();
requestFocus();
while (isRunning) {
long now = System.nanoTime();
delta += (now - lestTime) / ns;
lestTime = now;
if (delta >= 1) {
tick();
render();
frames++;
delta--;
}
if (System.currentTimeMillis() - time >= 1000) {
System.out.println("FPS: " + FPS);
FPS = frames;
frames = 0;
time += 1000;
}
}
stop();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
player.right= true;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
player.left= true;
}
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
player.up= true;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
player.down= true;
}
if(e.getKeyCode() == KeyEvent.VK_X) {
player.atirando=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
player.right= false;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
player.left= false;
}
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
player.up= false;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
player.down= false;
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
angleMouse = Math.atan2(e.getY() - player.getY(), e.getX() - player.getX());
player.atirandoComMouse = true;
player.posicaoMouseX = e.getX() / scale;
player.posicaoMouseY = e.getY() / scale;
// Crie um objeto de tiro com o ângulo calculado
Tiros tiros = new Tiros(player.getX(), player.getY(), 10, 10, null, 0, 0, angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros = tiros.right_dir_Tiros;
Game.tiros.add(tiros);
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
////////////////////////////////////////////////
package Pack.Entities;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import Pack.World.Camera;
import Pack.game.Game;
public class Tiros extends Entity {
private double angle;
private double direcaoX; // Mudei para double!
private double direcaoY; // Mudei para double!
private double speedT=4;
public boolean right_Tiros, up_Tiros, left_Tiros,down_Tiros;
public int right_dir_Tiros=0, up_dir_Tiros=1, left_dir_Tiros=3,down_dir_Tiros=4;
public int dir_Tiros = down_dir_Tiros;
private int frames_Tiros =0, maxFrame_Tiros=4, index_Tiros =1,maxIndex_Tiros=2;
public boolean muved_Tiros = false;
private int mascaraXTiros=16 ,mascaraYTiros=16,mascaraWTiros=30, mascaraHTiros=30;
private BufferedImage[] right_imTiros;
private BufferedImage[] left_imTiros;
private BufferedImage[] up_imTiros;
private BufferedImage[] down_imTiros;
public Tiros(int x, int y, int width, int height, BufferedImage sprite, double dx, double dy,double angle) {
super(x, y, width, height, null);
this.angle=angle;
this.direcaoX =dx;
this.direcaoY =dy;
this.right_imTiros = new BufferedImage[4];
this.left_imTiros = new BufferedImage[4];
this.up_imTiros = new BufferedImage[4];
this.down_imTiros= new BufferedImage[4];
for(int i=0; i<4;i++) {
this.right_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*6, 64, 64);
this.left_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*5, 64, 64);
this.up_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*7, 64, 64);
this.down_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*4, 64, 64);
}
}
public void tick(){
x+=this.direcaoX*speedT;
y+=this.direcaoY*speedT;
if (Game.player.atirando==true && muved_Tiros==true) {
frames_Tiros++;
if(frames_Tiros == maxFrame_Tiros) {
frames_Tiros=0;
index_Tiros ++;
if(index_Tiros >maxIndex_Tiros) {
index_Tiros=0;
}
}
}
if (muved_Tiros) {
frames_Tiros++;
if(frames_Tiros == maxFrame_Tiros) {
frames_Tiros=0;
index_Tiros ++;
if(index_Tiros >maxIndex_Tiros) {
index_Tiros=0;
}
}
}
}
public void render(Graphics g) {
AffineTransform transform = new AffineTransform();
transform.translate(this.getX() - Camera.x, this.getY() - Camera.y);
transform.rotate(angle); // Aplica a rotação
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(sprite, transform, null);
if(dir_Tiros ==right_dir_Tiros) {
g.drawImage(right_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
else if(dir_Tiros ==left_dir_Tiros) {
g.drawImage(left_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
else if(dir_Tiros ==up_dir_Tiros) {
g.drawImage(up_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
else if(dir_Tiros ==down_dir_Tiros) {
g.drawImage(down_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
// g.setColor(Color.BLACK);
// g.fillOval(this.getX()-Camera.x, this.getY()-Camera.y, width, height);
}
}
//////////////////////////////////
package Pack.Entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import Pack.Graphics.SpreteSheet;
import Pack.World.Camera;
import Pack.World.TileParede;
import Pack.World.World;
import Pack.game.Game;
public class Player extends Entity {
public boolean right, up, left, down;
public int right_dir = 0, up_dir = 1, left_dir = 3, down_dir = 4;
public int dir = down_dir;
public double speed = 3.0;
private int frames = 0, maxFrame = 7, index = 1, maxIndex = 2;
private boolean muved = false;
public double vidaPlayer = 100, vidaMaxima = 100;
public static int municao = 0;
public boolean tomandoDano = false;
private int danoFrames = 0;
public boolean temArma = false;
public boolean atirando = false,atirandoComMouse=false;
public int posicaoMouseX, posicaoMouseY;
private double angleMouse;
public int mascaraX = 25, mascaraY = 5, mascaraW = 16, mascaraH = 60;
private BufferedImage[] right_Player;
private BufferedImage[] left_Player;
private BufferedImage[] up_Player;
private BufferedImage[] down_Player;
private BufferedImage[] right_PlayerDano;
private BufferedImage[] left_PlayerDano;
private BufferedImage[] up_PlayerDano;
private BufferedImage[] down_PlayerDano;
public Player(int x, int y, int widtht, int height, BufferedImage sprite,double angleMouse) {
super(x, y, widtht, height, sprite);
this.angleMouse=angleMouse;
this.right_Player = new BufferedImage[3];
this.left_Player = new BufferedImage[3];
this.up_Player = new BufferedImage[3];
this.down_Player = new BufferedImage[3];
for (int i = 0; i < 3; i++) {
this.right_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 128, 64, 64);
this.left_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64, 64, 64);
this.up_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 192, 64, 64);
this.down_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 0, 64, 64);
}
// Player toma dano
this.right_PlayerDano = new BufferedImage[3];
this.left_PlayerDano = new BufferedImage[3];
this.up_PlayerDano = new BufferedImage[3];
this.down_PlayerDano = new BufferedImage[3];
for (int i = 0; i < 3; i++) {
this.right_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 6, 64, 64);
this.left_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 5, 64, 64);
this.up_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 7, 64, 64);
this.down_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 4, 64, 64);
}
}
public void tick() {
muved = false;
// se precisonasr right
if (right && World.caminhoLivre((int) (x + speed), this.getY())) {
muved = true;
dir = right_dir;
x += speed;
// System.out.println("valor"+x);
} if (left && World.caminhoLivre((int) (x - speed), this.getY())) {
muved = true;
dir = left_dir;
x -= speed;
} if (up && World.caminhoLivre(this.getX(), (int) (y - speed))) {
muved = true;
dir = up_dir;
y -= speed;
} if (down && World.caminhoLivre(this.getX(), (int) (y + speed))) {
muved = true;
dir = down_dir;
y += speed;
}
//
if (muved) {
frames++;
if (frames == maxFrame) {
frames = 0;
index++;
if (index > maxIndex) {
index = 0;
}
}
}
checarColisao_PVida();
checarColisao_Arma();
checarColisao_Municao();
if (tomandoDano) {
danoFrames++;
if (danoFrames == 8) {
danoFrames = 0;
tomandoDano = false;
}
}
//
if(atirando && temArma && municao>0) {
atirando = false;
int dx =0; // direçao de movimento em x !
int dy =0;
Tiros tiros = new Tiros(this.getX() + 70, this.getY() + 5, 10, 10, null, dx, dy,angleMouse);
if(dir==this.right_dir) {
dx =1;
Tiros tiros = new Tiros(this.getX()+70, this.getY()+5,10,10,null,dx,dy,angleMouse );
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.right_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
else if(dir==this.left_dir) {
dx =-1;
Tiros tiros = new Tiros(this.getX()-70, this.getY()+5,10,10,null,dx,dy,angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.left_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
else if(dir==this.up_dir) {
dy =-1;
Tiros tiros = new Tiros(this.getX()+15, this.getY()-30,10,10,null,dx,dy,angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.up_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
else if(dir==this.down_dir) {
dy =1;
Tiros tiros = new Tiros(this.getX()-15, this.getY()+45,10,10,null,dx,dy,angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.down_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
}
//
if(atirandoComMouse && temArma && municao>0) {
atirandoComMouse = false;
// observe que quando se calcula o angulo em Graus C° a posiçao de X e Y sao trocadas!
double anguloMouse =Math.atan2(posicaoMouseY-(this.getY()-Camera.y), posicaoMouseX-(this.getX()-Camera.x));
System.out.println(anguloMouse);
double dx =Math.cos(anguloMouse);
double dy =Math.sin(anguloMouse);
int px =0;
int py =0;
////
double da=Math.cos(anguloMouse);
double db=Math.sin(anguloMouse);
AffineTransform aff= AffineTransform.getTranslateInstance(da, db);
aff.rotate(Math.toRadians(da));
aff.rotate(Math.toRadians(db));
if(dir==this.right_dir && dx>=0 ) {
Tiros tiros = new Tiros(this.getX()+px, this.getY()+py,10,10,null,da,db,angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.right_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
else if(dir==this.left_dir && dx<=0) {
Tiros tiros = new Tiros(this.getX()+px, this.getY()+py,10,10,null,dx,dy,angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.left_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
else if(dir==this.up_dir && dy<=0) {
Tiros tiros = new Tiros(this.getX()+px, this.getY()+py,10,10,null,dx,dy,angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.up_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
else if(dir==this.down_dir && dy>=0) {
;
Tiros tiros = new Tiros(this.getX()+px, this.getY()+py,10,10,null,dx,dy,angleMouse);
tiros.muved_Tiros = true;
tiros.dir_Tiros=tiros.down_dir_Tiros;
Game.tiros.add(tiros);
municao-=1;
}
}
if (this.vidaPlayer <= 0) {
Game.entities = new ArrayList<Entity>();
Game.inimigosLista = new ArrayList<Inimigos>();
Game.spriteSheet = new SpreteSheet("/sprites.png");
Game.player = new Player(0, 0, 64, 64, Game.spriteSheet.getSprite(0, 0, 64, 64),angleMouse);
Game.entities.add(Game.player);
Game.world = new World("/MiniMapa.png");
return;
}
Camera.x = Camera.clamp(this.getX() - (Game.WIDTH / 2), 0, World.WIDTH * 64 - Game.WIDTH);
Camera.y = Camera.clamp(this.getY() - (Game.HEIGHT / 2), 0, World.HEIGHT * 64 - Game.HEIGHT);
}
private void checarColisao_Municao() {
for (int i = 0; i < Game.entities.size(); i++) {
Entity atual = Game.entities.get(i);
if (atual instanceof Municao) {
if (Entity.estaColidindo(this, atual)) {
municao+=100;
Game.entities.remove(atual);
}
}
}
}
public void checarColisao_Arma() {
for (int i = 0; i < Game.entities.size(); i++) {
Entity atual = Game.entities.get(i);
if (atual instanceof Arma) {
if (Entity.estaColidindo(this, atual)) {
temArma =true;
Game.entities.remove(atual);
}
}
}
}
public void checarColisao_Municao1() {
for (int i = 0; i < Game.entities.size(); i++) {
Entity atual = Game.entities.get(i);
if (atual instanceof Municao) {
if (Entity.estaColidindo(this, atual)) {
municao += 200;
// System.out.println("Municao " + municao);
Game.entities.remove(atual);
}
}
}
}
public void checarColisao_PVida() {
for (int i = 0; i < Game.entities.size(); i++) {
Entity atual = Game.entities.get(i);
if (atual instanceof PotionMana) {
if (Entity.estaColidindo(this, atual)) {
vidaPlayer += 20;
if (vidaPlayer > vidaMaxima)
vidaPlayer = vidaMaxima;
Game.entities.remove(atual);
}
}
}
}
public void render(Graphics g) {
if (!this.tomandoDano) {
if (dir == right_dir) {
g.drawImage(right_Player[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
if(this.temArma) {
// desenha arma para direita.
g.drawImage(Entity.Arma_V_direita,this.getX()+30- Camera.x, this.getY()+10 - Camera.y , null);
}
} else if (dir == left_dir) {
g.drawImage(left_Player[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
if(this.temArma) {
// desenha arma para esquerda.
g.drawImage(Entity.Arma_V_esquerda,this.getX()-30- Camera.x, this.getY()+10 - Camera.y , null);
}
} else if (dir == up_dir) {
g.drawImage(up_Player[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
if(this.temArma) {
// desenha arma para cima.
g.drawImage(Entity.Arma_V_cima,this.getX()-5- Camera.x, this.getY() - Camera.y , null);
}
} else if (dir == down_dir) {
g.drawImage(down_Player[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
if(this.temArma) {
// desenha arma para baixo.
g.drawImage(Entity.Arma_V_baixo,this.getX()-5- Camera.x, this.getY() - Camera.y , null);
}
}
} else {
if (dir == right_dir) {
g.drawImage(right_PlayerDano[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
} else if (dir == left_dir) {
g.drawImage(left_PlayerDano[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
} else if (dir == up_dir) {
g.drawImage(up_PlayerDano[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
} else if (dir == down_dir) {
g.drawImage(down_PlayerDano[index], this.getX() - Camera.x, this.getY() - Camera.y, null);
}
}
// g.setColor(Color.YELLOW);
// g.fillRect(this.getX()+mascaraX-Camera.x, this.getY()+mascaraY-Camera.y, mascaraW, mascaraH);
}
}