Boa tarde a todos,
Então bros eu utilizo a linguagem JAVA a uns 3 anos, só que em nível acadêmico mesmo e eu estou tentando desenvolver um jogo baseado no livro Programação de games com JAVA da Cengage Learning. Após verificar muitos tópicos deste fórum vi que ninguém havia postado esse tipo de dúvida e por essa ser a primeira dúvida que eu encaminho para vocês me desculpem se eu procedi errado :oops: .
O seguinte código exibe um Applet que exibe uma imagem de fundo e um spritesheet que se move aleatoriamente, as atualizações dos parâmetros é determinado por uma thread.
[code]import java.awt.;
import java.applet.;
import java.util.;
import java.awt.image.;
import java.net.*;
public class AnimationTest extends Applet implements Runnable {
static int SCREENWIDTH = 640;
static int SCREENHEIGHT = 480;
Thread gameloop;
Random rand = new Random();
//double buffer objects
BufferedImage backbuffer;
Graphics2D g2d;
Image background;
//sprite variables
Image ball;
int ballX = 300, ballY = 200;
int speedX, speedY;
//animation variables
int currentFrame = 0;
int totalFrames = 64;
int animationDirection = 1;
int frameCount = 0;
int frameDelay = 5;
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) {}
return url;
}
public void init() {
Toolkit tk = Toolkit.getDefaultToolkit();
//create the back buffer for smooth graphics
backbuffer = new BufferedImage(SCREENWIDTH, SCREENHEIGHT,
BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
//load the background image
background = tk.getImage(getURL("woodgrain.png"));
//load the ball animation strip
ball = tk.getImage(getURL("xball.png"));
speedX = rand.nextInt(6)+1;
speedY = rand.nextInt(6)+1;
}
public void start() {
gameloop = new Thread(this);
gameloop.start();
}
public void stop() {
gameloop = null;
}
public void run() {
Thread t = Thread.currentThread();
while (t == gameloop) {
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
gameUpdate();
repaint();
}
}
public void gameUpdate() {
//see if it's time to animate
frameCount++;
if (frameCount > frameDelay) {
frameCount=0;
//update the animation frame
currentFrame += animationDirection;
if (currentFrame > totalFrames - 1) {
currentFrame = 0;
}
else if (currentFrame < 0) {
currentFrame = totalFrames - 1;
}
}
//update the ball position
ballX += speedX;
if ((ballX < 0) || (ballX > SCREENWIDTH - 64)) {
speedX *= -1;
ballX += speedX;
}
ballY += speedY;
if ((ballY < 0) || (ballY > SCREENHEIGHT - 64)) {
speedY *= -1;
ballY += speedY;
}
}
public void update(Graphics g) {
//draw the background
g2d.drawImage(background, 0, 0, SCREENWIDTH-1, SCREENHEIGHT-1, this);
//draw the current frame of animation
drawFrame(ball, g2d, ballX, ballY, 8, currentFrame, 64, 64);
g2d.setColor(Color.BLACK);
g2d.drawString("Position: " + ballX + "," + ballY, 5, 10);
g2d.drawString("Velocity: " + speedX + "," + speedY, 5, 25);
g2d.drawString("Animation: " + currentFrame, 5, 40);
paint(g);
}
public void paint(Graphics g) {
//draw the back buffer to the screen
g.drawImage(backbuffer, 0, 0, this);
}
//draw a single frame of animation
public void drawFrame(Image source, Graphics2D dest,
int x, int y, int cols, int frame,
int width, int height)
{
int fx = (frame % cols) * width;
int fy = (frame / cols) * height;
dest.drawImage(source, x, y, x+width, y+height,
fx, fy, fx+width, fy+height, this);
}
}
[/code]
Como este fórum e outros sites afirmam, os Applets estão caindo em desuso e infelizmente o livro todo se baseia em construir um jogo em Applet (já finalizei este livro anteriormente e os conceitos apresentados por ele foram de grande valia) e eu estou tentando converter de Applet para Swing. Pesquisei um pouco para tentar esta conversão, entendi o comportamento dos Applets, fiz as adaptações mas tô apanhando pra caramba e não consegui rodar essa classe.
Se vocês puderem me ensinar como esta conversão pode ser feita eu agradeço.
PS: A código que converti já está tão “rascunhado” que se eu postar o código aqui vai causar mais dúvidas rsrsrsrs