Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
120 views

Tutorial Java Game

1. The document outlines the framework for a game application built in Java using Applets. It includes sections on the core applet class, handling input, drawing images, and implementing game states. 2. Key aspects covered include initializing the applet, handling input from keyboard keys, creating a double buffer to draw images, loading image files, and using an enum to track the current game state and draw the appropriate screen. 3. Game state is tracked with an enum, input is handled with key listeners, and images are loaded and drawn to the screen based on the current game state.

Uploaded by

Adilson Junior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

Tutorial Java Game

1. The document outlines the framework for a game application built in Java using Applets. It includes sections on the core applet class, handling input, drawing images, and implementing game states. 2. Key aspects covered include initializing the applet, handling input from keyboard keys, creating a double buffer to draw images, loading image files, and using an enum to track the current game state and draw the appropriate screen. 3. Game state is tracked with an enum, input is handled with key listeners, and images are loaded and drawn to the screen based on the current game state.

Uploaded by

Adilson Junior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1 - Esqueleto do Framework

package nome_do_package;

import
import
import
import
import

java.applet.Applet;
java.awt.Color;
java.awt.Frame;
java.awt.event.KeyEvent;
java.awt.event.KeyListener;

public class MainClass extends Applet implements Runnable, KeyListener {


@Override
public void init() {
setSize(640, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Q-Bot Alpha");
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:

System.out.println("Move up");
break;
case KeyEvent.VK_DOWN:
System.out.println("Move down");
break;
case KeyEvent.VK_LEFT:
System.out.println("Move left");
break;
case KeyEvent.VK_RIGHT:
System.out.println("Move right");
break;
case KeyEvent.VK_SPACE:
System.out.println("Jump");
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Stop moving up");
break;
case KeyEvent.VK_DOWN:
System.out.println("Stop moving down");
break;
case KeyEvent.VK_LEFT:
System.out.println("Stop moving left");
break;
case KeyEvent.VK_RIGHT:
System.out.println("Stop moving right");
break;
case KeyEvent.VK_SPACE:
System.out.println("Stop jumping");
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}

2 - Inserindo imagens
2.1 Update e Paint
@Override
public void update(Graphics g) {
}
@Override
public void paint(Graphics g) {
g.drawImage(Image, x, y, Image Observer);
}

2 .2 Double BackBuffer
No mtodo Update:
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);

2.3 Inserindo uma Imagem

a) Variavel referente a imagem:


private Image nome_da_imagem;

b)Apontamos o caminho da imagem no disco:


nome_da_imagem = getImage(base, "data/TitleScreen.png");

c)Desenhamos a imagem no mtodo paint:


g.drawImage(nome_da_imagem, 0, 0, this);

3 GameStates

a) Inserindo os estados do jogo


enum GameStates {
TitleScreen, Playing, PlayerDead, GameOver
};

b)Uma varivel precisa armazenar o estado do jogo.


GameStates gameState = GameStates.TitleScreen;

c)No mtodo Update() feito o teste que verifica qual o estado do jogo.
public void Update() {
switch (gameState) {
case TitleScreen:
break;
case Playing:
break;
case PlayerDead:
break;
case GameOver:
break;
}
}

d)Uma tela vai ser desenhada, dependendo do estado da varivel.


public void Draw(Graphics g){
if(gameState == gameState.TitleScreen)
{
g.drawImage(titleScreen, 0, 0, this);
}
}

You might also like