estou aprendendo a programar agora, então me perdoem se estiver muito mal feito. O meu restart não esta funcionando, não sei qual é o problema
package com.Cauã.main;
<
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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 com.Cauã.Entity.BulletShoot;
import com.Cauã.Entity.Enemy;
import com.Cauã.Entity.Entity;
import com.Cauã.Entity.Player;
import com.Cauã.World.Camera;
import com.Cauã.World.World;
import com.Cauã.graficos.Spritesheet;
import com.Cauã.graficos.UI;
public class Game extends Canvas implements Runnable,KeyListener,MouseListener{
private static final long serialVersionUID = 1L;
public static JFrame frame;
private Thread thread;
private boolean isRunning = true;
public static final int WIDTH = 240;
public static final int HEIGHT = 160;
private final int SCALE = 3;
private int CUR_LEVEL = 1,MAX_LEVEL = 2;
private BufferedImage image;
public static List<Entity> entities;
public static List<Enemy> enemies;
public static List<BulletShoot> bullets;
public static Spritesheet spritesheet;
public static World world;
public static Player player;
public static Random rand;
public UI ui;
public static String gameState = "NORMAL";
private boolean showMessageGameOver = true;
private int framesGameOver = 0;
private boolean restartGame = false;
public Game() {
rand = new Random();
addKeyListener(this);
addMouseListener(this);
setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
initFlame();
ui = new UI();
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
entities = new ArrayList<Entity>();
enemies = new ArrayList<Enemy>();
bullets = new ArrayList<BulletShoot>();
spritesheet = new Spritesheet("/spritesheet.png");
player = new Player(0,0,19,21,spritesheet.getSprite(39, 0, 19, 21));
entities.add(player);
world = new World ("/level1.png");
}
public void initFlame() {
frame = new JFrame();
frame.add(this);
frame.setResizable(false);
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 () {
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void tick() {
if(gameState == "Normal") {
this.restartGame = false;
for(int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
e.tick();
}
for(int i = 0; i < bullets.size(); i++) {
bullets.get(i).tick();
}
if(enemies.size() == 0) {
//avançar para o proximo level
CUR_LEVEL++;
if(CUR_LEVEL > MAX_LEVEL) {
CUR_LEVEL = 1;
}
String newWorld = "Level"+CUR_LEVEL+".png";
World.restartGame(newWorld);
}
}else if(gameState == "Game_Over") {
this.framesGameOver++;
if(this.framesGameOver == 30) {
this.framesGameOver = 0;
if(this.showMessageGameOver)
this.showMessageGameOver = false;
else
this.showMessageGameOver = true;
}
if(restartGame) {
this.restartGame = false;
this.gameState = "NORMAL";
CUR_LEVEL = 1;
String newWorld = "Level"+CUR_LEVEL+".png";
World.restartGame(newWorld);
}
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(0,0,0));
g.fillRect(0, 0, WIDTH, HEIGHT);
world.render(g);
for(int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
e.render(g);
}
for(int i = 0; i < bullets.size(); i++) {
bullets.get(i).render(g);
}
ui.render(g);
g.dispose();
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0,WIDTH*SCALE,HEIGHT*SCALE,null );
g.setColor(Color.WHITE);
g.setFont(new Font("Arial",Font.BOLD,20));
g.drawString("Munição:"+ Player.munição, 600, 46);
if(gameState == "Game_Over") {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(0,0,0,100));
g2.fillRect(0, 0, WIDTH*SCALE, HEIGHT*SCALE);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial",Font.BOLD,28));
g.drawString("GAME OVER!", WIDTH*SCALE/2-80, HEIGHT*SCALE/2+15);
g.setFont(new Font("Arial",Font.BOLD,20));
if(showMessageGameOver)
g.drawString(">Precione ''Enter'' Para Continuar<", WIDTH*SCALE/2-155, HEIGHT*SCALE/2+50);
}
bs.show();
}
public void run() {
long lastTime = System.nanoTime();
double amontOfTicks = 60.0;
double ns = 1000000000 / amontOfTicks;
double delta = 0;
int frames = 0;
double timer = System.currentTimeMillis();
requestFocus();
while(isRunning) {
long now = System.nanoTime();
delta+= (now - lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
render();
frames++;
delta--;
}
if(System.currentTimeMillis() - timer >= 1000) {
System.out.println("FPS: " + frames );
frames = 0;
timer+=1000;
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
e.getKeyCode() == KeyEvent.VK_D) {
System.out.println("andando");
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.shoot = true;
}
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
this.restartGame = true;
}
}
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) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
player.mouseshoot = true;
player.mx = (e.getX()/3);
player.my = (e.getY()/3);
System.out.println("atirando");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
<
package com.Cauã.World;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import com.Cauã.Entity.*;
import com.Cauã.World.Tile;
import com.Cauã.graficos.Spritesheet;
import com.Cauã.main.Game;
import com.Cauã.Entity.Enemy;
import com.Cauã.Entity.Entity;
import com.Cauã.Entity.Player;
import com.Cauã.World.World;
public class World {
public static Tile[] tiles;
public static int WIDTH,HEIGHT;
public static final int TILE_SIZE = 18;
public World(String path) {
try {
BufferedImage mapa = ImageIO.read(getClass().getResource(path));
int[] pixels = new int[mapa.getWidth() * mapa.getHeight()];
WIDTH = mapa.getWidth();
HEIGHT = mapa.getHeight();
tiles = new Tile[mapa.getWidth() * mapa.getHeight()];
mapa.getRGB(0, 0, mapa.getWidth(), mapa.getHeight(), pixels, 0, mapa.getWidth());
for( int xx = 0; xx < mapa.getWidth();xx++) {
for(int yy = 0;yy < mapa.getHeight();yy++) {
int pixelAtual = pixels[xx + (yy*mapa.getHeight())];
tiles[xx + (yy*WIDTH)] = new FloorTile(xx*18,yy*19,Tile.TILE_FLOOR);
if (pixelAtual == 0xFF000000) {
//chão
tiles[xx + (yy*WIDTH)] = new FloorTile(xx*18,yy*19,Tile.TILE_FLOOR);
}
else if(pixelAtual == 0xFFFFFFFF) {
//parede
tiles[xx + (yy*WIDTH)] = new WallTile(xx*18,yy*19,Tile.TILE_WALL);
}
else if (pixelAtual == 0xFF00FF21) {
//player
Game.player.setX(xx*19);
Game.player.setY(yy*21);
}
else if(pixelAtual == 0xFFFF0000) {
//enemy
Enemy en = new Enemy (xx*16,yy*16,17,21,Entity.ENEMY_EN);
Game.entities.add(en);
Game.enemies.add(en);
}
else if(pixelAtual == 0xFFFF006E) {
//cura
Game.entities.add(new LifePack(xx*16,yy*16,17,21,Entity.LIFEPACK_EN));
}
else if(pixelAtual == 0xFFFFD800) {
//arma
Game.entities.add(new Weapon(xx*16,yy*16,17,21,Entity.WEAPON_EN));
}
else if(pixelAtual == 0xFF0026FF) {
//munição
Game.entities.add(new Bullet(xx*16,yy*16,17,21,Entity.BULLET_EN));
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void restartGame(String level){
Game.entities.clear();
Game.enemies.clear();
Game.entities = new ArrayList<Entity>();
Game.enemies = new ArrayList<Enemy>();
Game.spritesheet = new Spritesheet("/spritesheet.png");
Game.player = new Player(0,0,19,21,Game.spritesheet.getSprite(39, 0,19,21));
Game.entities.add(Game.player);
Game.world = new World("/"+level);
return;
}
public static boolean isFree(int xnext,int ynext) {
int x1 = xnext / TILE_SIZE;
int y1 = ynext / 19;
int x2 = (xnext+TILE_SIZE-1) / TILE_SIZE;
int y2 = ynext / 19;
int x3 = ynext / TILE_SIZE;
int y3 = (ynext+19-1) / 19;
int x4 = (xnext+TILE_SIZE-1) / TILE_SIZE;
int y4 = (ynext+19-1)/ 19;
return !((tiles[x1 + (y1*World.WIDTH)] instanceof WallTile)||
(tiles[x2 + (y2*World.WIDTH)] instanceof WallTile) ||
(tiles[x3 + (y3*World.WIDTH)] instanceof WallTile) ||
(tiles[x4 + (y4*World.WIDTH)] instanceof WallTile));
}
public void render(Graphics g) {
int xstart = Camera.x/19;
int ystart = Camera.y/21;
int xfinal = xstart + (Game.WIDTH / 16);
int yfinal = ystart + (Game.HEIGHT / 16);
for(int xx = xstart; xx <= xfinal; xx++){
for(int yy = ystart; yy <= yfinal; yy++) {
if(xx < 0 | yy< 0 | xx>= WIDTH | yy>= HEIGHT)
continue;
Tile tile = tiles[xx + (yy*WIDTH)];
tile.render(g);
}
}
}
}
><
package com.Cauã.Entity;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import com.Cauã.World.Camera;
import com.Cauã.World.World;
import com.Cauã.graficos.Spritesheet;
import com.Cauã.main.Game;
public class Player extends Entity{
public boolean right,up,left,down;
public int right_dir = 0, left_dir = 1;
public int dir = right_dir;
public double speed = 1.4;
private int frames = 0,maxFrames = 5,index = 0, maxIndex = 3;
private boolean moved = false;
private BufferedImage[] rightPlayer;
private BufferedImage[] leftPlayer;
private BufferedImage playerDamage;
public double life = 100,maxlife = 100;
public int mx,my;
private boolean arma = false;
public static int munição = 0;
public boolean isDamaged = false;
private int damageFlames = 0;
public boolean shoot = false,mouseshoot = false;
public Player(int x, int y, int width, int height, BufferedImage sprite) {
super(x, y, width, height, sprite);
rightPlayer = new BufferedImage[4];
leftPlayer = new BufferedImage[4];
playerDamage = Game.spritesheet.getSprite(0, 20, 18, 19);
for(int i = 0;i < 4;i++) {
rightPlayer[i] = Game.spritesheet.getSprite(40 + (i*20), 0, 20, 20);
}
for(int i = 0;i < 4;i++) {
leftPlayer[i] = Game.spritesheet.getSprite(40 + (i*20), 20, 20, 20);
}
}
public void tick() {
moved = false;
if(right && World.isFree((int)(x+speed),this.getY())) {
moved = true;
dir = right_dir;
x+=speed;
}
else if(left && World.isFree((int)(x-speed),this.getY())) {
moved = true;
dir = left_dir;
x-=speed;
}
if(up && World.isFree(this.getX(),(int)(y-speed))) {
moved = true;
y-=speed;
}
else if(down && World.isFree(this.getX(),(int)(y+speed))) {
moved = true;
y+=speed;
}
if(moved) {
frames++;
if(frames == maxFrames) {
frames = 0;
index++;
if(index > maxIndex) {
index = 0;
}
}
this.checkCollisionLifePack();
this.checkCollisionAmmo();
this.checkCollisionGun();
if(isDamaged) {
this.damageFlames++;
if(this.damageFlames == 5) {
this.damageFlames = 0;
isDamaged=false;
}
}
if(shoot && arma && munição > 0) {
munição--;
//criar bala e atirar
shoot = false;
int dx = 0;
int px = 0;
int py = 9;
if(dir == right_dir) {
px = 20;
dx = 1;
}
else{
px = -5;
dx = -1;
}
BulletShoot bullet = new BulletShoot(this.getX()+px,this.getY()+py,3,3,null,dx,0);
Game.bullets.add(bullet);
}
if(mouseshoot) {
mouseshoot = false;
if(arma && munição > 0) {
munição--;
//criar bala e atirar
int px = 0,py = 9;
double angle = 0;
if(dir == right_dir) {
px = 20;
angle = Math.atan2(my - (this.getY()+py - Camera.y), mx - (this.getX()+px - Camera.x));
}
else{
px = -5;
angle = Math.atan2(my - (this.getY()+py - Camera.y), mx - (this.getX()+px - Camera.x));
}
double dx = Math.cos(angle);
double dy = Math.sin(angle);
BulletShoot bullet = new BulletShoot(this.getX()+px,this.getY()+py,3,3,null,dx,dy);
Game.bullets.add(bullet);
}
}
// if(Game.player.life <= 0) {
//Game over!
//System.exit(1);
if(life<=0) {
life = 0;
Game.gameState = "Game_Over";
}
Camera.x = Camera.clamp(this.getX() - (Game.WIDTH/2),0, World.WIDTH*18 - Game.WIDTH);
Camera.y = Camera.clamp(this.getY() - (Game.HEIGHT/2),0,World.HEIGHT*19 - Game.HEIGHT);
}}
public void checkCollisionGun(){
for(int i = 0; i < Game.entities.size(); i ++ ) {
Entity Atual = Game.entities.get(i);
if(Atual instanceof Weapon) {
if(Entity.isColidding(this, Atual)) {
arma=true;
Game.entities.remove(Atual);
}
}
}
}
public void checkCollisionAmmo(){
for(int i = 0; i < Game.entities.size(); i ++ ) {
Entity Atual = Game.entities.get(i);
if(Atual instanceof Bullet) {
if(Entity.isColidding(this, Atual)) {
munição+=20;
Game.entities.remove(Atual);
}
}
}
}
public void checkCollisionLifePack(){
for(int i = 0; i < Game.entities.size(); i ++ ) {
Entity Atual = Game.entities.get(i);
if(Atual instanceof LifePack) {
if(Entity.isColidding(this, Atual)) {
life+=10;
if(life > 100) {
life = 100;
}
Game.entities.remove(Atual);
}
}
}
}
public void render(Graphics g) {
if(!isDamaged){
if(dir == right_dir) {
g.drawImage(rightPlayer[index],this.getX() - Camera.x,this.getY() - Camera.y,null);
if(arma) {
//arma pra direita
g.drawImage(Entity.GUN_RIGHT, this.getX()+11 - Camera.x, this.getY()-1 - Camera.y, null);
}
}
else if(dir == left_dir) {
g.drawImage(leftPlayer[index], this.getX() - Camera.x,this.getY() - Camera.y,null);
if(arma) {
//arma pra esquerda
g.drawImage(Entity.GUN_LEFT, this.getX()-11 - Camera.x, this.getY()-1 - Camera.y, null);
}
}
}else {
g.drawImage(playerDamage, this.getX() - Camera.x, this.getY() - Camera.y,null);
}
}
}>