Você está na página 1de 10

CRIANDO UM JOGO NO ESTILO PONG ACTIONSCRIPT 3

Para este exerccio, inicialmente vos criar dois arquivos: um arquivo ActionScript 3.0 e um
arquivo ActionScript file.

Salve o arquivo ActionScript 3.0 como Pong Game e o arquivo ActionScript file como Main.
Mantenha os dois arquivos numa mesma pasta.

Agora v nas Propriedades do arquivo Pong Game.fla. No campo de texto Document Class
insira o nome Main.

Desenhe um retngulo de 20x160 (largura e altura). Centralize este retngulo esquerda do


Palco (Ctrl+K). Duplique este retngo (Alt+clique e arraste do mouse) e posicione-o doa outro
lado do Palco. Centralize este retngulo esquerda (Ctrl+K).

Selecione o primeiro retngulo e converta-o em Smbolo (F8). Insira o nome playermc, em


Type >> Movie Clip e Registration >> Center. Na aba Advanced marque a opo Export for
ActionScript.

Em Instance name escreva . playe.

Converta o segundo retngulo em Movie Clip (F8). Use as opes mostradas na imagem
abaixo.

Em Instance name coloque o nome computer.

Usando a ferramenta Oval tool, desenhe uma bola e centralize-a (Ctrl+K) no Palco.

Converta a bola em Movie clip (F8) usando as opes exibidas na imagem abaixo.

Em Instance name coloque o nome ball.

Selecione a ferramenta Text tool. V em Propriedades e selecione as opes: Text engine:


Classic text, Text type: Dynamic text, Fonte: Arial, Tamanho: 40 e cor a sua escolha.

No palco, desenhe duas caixas de texto que serviro como placar dos jogadores.

Salve o seu arquivo.


Selecione o arquivo Main.as e digite o aqrquivo abaixo:

package {
//imports

import
import
import
import

flash.display.MovieClip;
flash.events.KeyboardEvent;
flash.ui.Keyboard;
flash.events.Event;

public class Main extends MovieClip {


//constants
const ballspeed:int = 10;
const playerspeed:int = 7;
const computerspeed:int = 10;
const computerIntelligence:int = 7;//intelligence is 7 out of 10
//global variables
var vx:int = -ballspeed; // x component of velocity of ball (velocity is speed with
direction)
var
var
var
var
var

vy:int = ballspeed; // y component of velocity of ball


v1:int = 0; // initial velocity of player
v2:int = 0; // initial velocity of computer
playerScore:int = 0;
computerScore:int = 0;

public function Main() {


init();
}
//this function will add all event listeners
function init():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
// this function resets the game
function reset():void {
player.y = stage.stageHeight/2;
computer.y = stage.stageHeight/2;
ball.x
= stage.stageWidth/2;
ball.y
= stage.stageHeight/2;
if(Math.abs(Math.random()*2) > 1){
vx = -ballspeed;
}else{
vx = ballspeed;
}
if(Math.abs(Math.random()*2) > 1){
vy = -ballspeed;
}else{
vy = ballspeed;
}
}
//this function sets the velocity of player when key is pressed
function onKeyDown(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.UP) {
v1 = -playerspeed;
}else if(event.keyCode == Keyboard.DOWN) {
v1 = playerspeed;
}
}
//this function sets the velocity of player to 0 if key is released
function onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
v1 = 0;

}
}
//This function is executed when a frame changes
function onEnterFrame(event:Event):void {
//variable decleration
var pHalfHeight = player.height/2; // half height of player(used for
collisions)
var pHalfWidth = player.width/2; // half width of player (used for collisions)
var bHalfHeight = ball.height/2; // half height of ball(used for collisions)
var bHalfWidth = ball.width/2; // half width of ball (used for collisions)
//moving the player
player.y += v1;
//limiting the motion of player (it should not move beyond the stageheight)
if(player.y + pHalfHeight > stage.stageHeight) {
player.y = stage.stageHeight - pHalfHeight;
}else if(player.y - pHalfHeight < 0) {
player.y = 0 + pHalfHeight;
}
//moving the ball
ball.x += vx;
ball.y += vy;
//moving the computer automatically
if(Math.abs(Math.random()*10) < computerIntelligence){
var d:int = computer.y - ball.y;
if(Math.abs(d) > pHalfHeight){
if(d>0) {
v2 = -computerspeed;
}else{
v2 = computerspeed;
}
}
}
computer.y += v2;
//limiting the motion of computer (it should not move beyond the
stageheight)
if(computer.y + pHalfHeight > stage.stageHeight) {
computer.y = stage.stageHeight - pHalfHeight;
}else if(computer.y - pHalfHeight < 0) {
computer.y = 0 + pHalfHeight;
}
//collision with horizontal walls
if(ball.y + bHalfHeight >= stage.stageHeight || ball.y - bHalfHeight <= 0) {
vy *= -1;
}
//collision with player and computer
if(ball.x - bHalfWidth <= player.x + pHalfWidth) {
if(Math.abs(ball.y - player.y) <= pHalfHeight) {
vx = ballspeed;
if(v1!=0){
vy = 2*v1;
}
}
}else if(ball.x + bHalfWidth >= computer.x - pHalfWidth) {
if(Math.abs(ball.y - computer.y) <= pHalfHeight) {

vx = -ballspeed;
if(v2!=0){
vy = v2;
}
}
}
//collision with vertical walls & updating scores
if(ball.x + bHalfWidth >= stage.stageWidth) {
playerScore += 1;
reset();
}else if(ball.x - bHalfWidth <= 0) {
computerScore += 1;
reset();
}
//display the score on the textfield
txtPlayer.text = String(playerScore);
txtComputer.text = String(computerScore);
}
}
}

Você também pode gostar