Thursday, May 5, 2011

Project 5: First Game

-Pong paddle does hit the ball depending on where it hits the paddle.
-The bears(targets) are at different speeds.
-If you play it in flash, it will output a hitcount, high score depending if you exit the game, and how many times you lost since last entered.

Tried to go score in game, but couldn't get it to work appropriately.
Will try to expand apawn this later.

Removed for repairs. haha A bit glitchy. But is added now. Removed because the text on the stage makes it glitchy. Maybe because it is called every frame.


Here is the code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.text.*;

public class FirstGame extends MovieClip
{
var myText:String;
var myText1:String;
var myText2:String;
var myTextBox:TextField;
var myTextBox1:TextField;
var myTextBox2:TextField;

var player:MovieClip;
var ball:MovieClip;
var vx:Number;
var vy:Number;
var losercount:Number;
var hitcount:Number;
var hs:Number;
var t1:MovieClip;
var t2:MovieClip;
var t3:MovieClip;
var maxVx:Number = 15;
var targetyx:Number = 5;
var block:MovieClip;

public function FirstGame()
{
player = new SpaceShipcopy;
ball = new Aircopy;
t1 = new Rupertcopy;
t2 = new Rupertcopy;
t3 = new Rupertcopy;
block = new AutoBotscopy;
block.scaleX = .2;
block.scaleY = .2;
ball.scaleX = .18;
ball.scaleY = .18;
t1.scaleX = .15;
t1.scaleY = .15;
t2.scaleX = .15;
t2.scaleY = .15;
t3.scaleX = .15;
t3.scaleY = .15;
player.scaleX = .3;
player.scaleY = .2;
vx = 5;
vy = 5;
losercount = 0;
hitcount = 0;
hs = 0;

Mouse.hide();

addChild(player);
addChild(ball);
addChild(t1);
addChild(t2);
addChild(t3);
addChild(block);

player.x = stage.stageWidth*.5;
player.y = stage.stageHeight*.85;
t1.x = stage.stageWidth*.15;
t1.y = stage.stageHeight*.2;
t2.x = stage.stageWidth*.75;
t2.y = stage.stageHeight*.25;
t3.x = stage.stageWidth*.35;
t3.y = stage.stageHeight*.3;
ball.x = stage.stageWidth*.3;
ball.y = stage.stageHeight*.1;
block.x = stage.stageWidth*.5;
block.y = stage.stageHeight*.1;
t1.vx = 6;
t1.vy = 5;
t2.vx = 15;
t2.vy = 5;
t3.vx = 9;
t3.vy = 5;

addEventListener(Event.ENTER_FRAME, onEnterFrame);
}


function onEnterFrame(event:Event):void
{

ballPaddleCollision();

checkStageCollision(player);
checkStageCollision(ball);
checkStageCollision(t1);
checkStageCollision(t2);
checkStageCollision(t3);

bounceWall(t1);
bounceWall(t2);
bounceWall(t3);


player.x = mouseX;

if(ball.x > stage.stageWidth || ball.x < 0)
{
vx = -vx;

}

if(ball.y > stage.stageHeight)
{
ball.x = stage.stageWidth*.3;
ball.y = stage.stageHeight*.1;
losercount += 1;
trace("You are a loser " + losercount + " times over...");
vx = 5;
vy = 5;

if(hs >= hitcount)
{
trace("You had " + hitcount + " hits!");
}

if(hs < hitcount)
{
hs = hitcount;
trace("NEW HIGH SCORE!");
trace("High score: " + hs);
}
hitcount = 0;

}

if( ball.y < 0)
{
vy = -vy;
}

ball.x += vx;
ball.y += vy;
ball.rotation += 5;

addText();

}

function addText():void
{
myText= "Hitcount = " + hitcount + " Highscore = " + hs + " Loser = " + losercount;
myTextBox = new TextField();
addChild(myTextBox);
myTextBox.text = myText;
myTextBox.x = stage.stageWidth*.1;
myTextBox.y = stage.stageHeight*.9;
myTextBox.width = 100;
myTextBox.height = 50;
myTextBox.multiline = true;
myTextBox.wordWrap = true;
myTextBox.background = true;



var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0xFF0000;
format.size = 10;

myTextBox.defaultTextFormat = format;


}
function ballPaddleCollision():void
{
if(ball.hitTestObject(player))
{
var relativeX:Number=ball.x-player.x;
var relativePercentage:Number=(relativeX/player.width)*2;
var newVx:Number=(relativePercentage*maxVx);
vx=newVx;
vy=-vy;
hitcount += 1;
}

// If ball hits block, bounce off.
if(ball.hitTestObject(block))
{
vx=-vx;
vy=-vy;
}

}


function moveThis(objectA:MovieClip):void
{
objectA.x += objectA.vx;
objectA.y += objectA.vy;
}

function bounceWall(objectA:MovieClip):void
{
objectA.x += objectA.vx;
}

function checkStageCollision(objectA:MovieClip):void
{
if (objectA.x <= 0 || objectA.x > stage.stageWidth)
{
objectA.vx = - objectA.vx;
}
if (objectA.y <= 0 || objectA.y > stage.stageHeight)
{
objectA.vy = - objectA.vy;
}


}

}
}

No comments: