Skip to content

Instantly share code, notes, and snippets.

@varesa
Created November 27, 2012 08:43
Show Gist options
  • Save varesa/4153185 to your computer and use it in GitHub Desktop.
Save varesa/4153185 to your computer and use it in GitHub Desktop.
flash peli
package fi.dy.esav.loppupeli {
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;
public class Main extends movieclip {
private static const playerSpeed:number = 5;
private static const bulletSpeed:number = 9;
private static const enemyInitialSpeed:number = 4;
private static const enemySpeedIncreaseFactor:number = 0.01;
private static const loopPeriod:number = 0.1;
private static const gunInterval:number = 500;
private static const firstEnemyDelay:number = 1500;
private static const enemySpawnIncreaseFactor:number = 0.95;
public var scoreBox:textfield;
public var gameOver:textfield;
private var enemyInterval:number = 900;
var score:number;
var player:Player;
var bullets:array;
var enemies:array;
var mainLoop:Timer;
var gunTimer:Timer;
var enemyTimer:Timer;
var keys:object;
public function Main(){
this.keys = {};
super();
this.player = new Player(this);
this.player.setPos(new Point(((this.stage.stageWidth / 2) - (this.player.getDims().width / 2)), ((this.stage.stageHeight / 2) - (this.player.getDims().height / 2))));
addChild(this.player);
this.bullets = new array();
this.enemies = new array();
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, this.keyUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, this.startShooting);
stage.addEventListener(MouseEvent.MOUSE_UP, this.stopShooting);
this.mainLoop = new Timer(loopPeriod);
this.mainLoop.addEventListener(TimerEvent.TIMER, this.gameLoop);
this.mainLoop.start();
this.gunTimer = new Timer(gunInterval);
this.gunTimer.addEventListener(TimerEvent.TIMER, this.shoot);
this.enemyTimer = new Timer(firstEnemyDelay);
this.enemyTimer.addEventListener(TimerEvent.TIMER, this.spawnEnemy);
this.enemyTimer.start();
this.score = 0;
this.scoreBox.text = this.score.tostring();
this.gameOver.visible = false;
}
public function getsize():Rectangle{
return (new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight));
}
private function keyDown(_arg1:KeyboardEvent):void{
if (this.keys[_arg1.keyCode]){
return;
};
this.keys[_arg1.keyCode] = true;
switch (_arg1.keyCode){
case Keyboard.A:
this.player.modVelocity(-(playerSpeed), 0);
break;
case Keyboard.D:
this.player.modVelocity(playerSpeed, 0);
break;
case Keyboard.W:
this.player.modVelocity(0, playerSpeed);
break;
case Keyboard.S:
this.player.modVelocity(0, -(playerSpeed));
break;
case Keyboard.space:
this.gunTimer.delay = 1;
this.gunTimer.start();
break;
};
}
private function keyUp(_arg1:KeyboardEvent):void{
switch (_arg1.keyCode){
case Keyboard.A:
this.player.modVelocity(playerSpeed, 0);
break;
case Keyboard.D:
this.player.modVelocity(-(playerSpeed), 0);
break;
case Keyboard.W:
this.player.modVelocity(0, -(playerSpeed));
break;
case Keyboard.S:
this.player.modVelocity(0, playerSpeed);
break;
case Keyboard.space:
this.gunTimer.stop();
break;
};
delete this.keys[_arg1.keyCode];
}
private function startShooting(_arg1:MouseEvent){
this.gunTimer.delay = 1;
this.gunTimer.start();
}
private function stopShooting(_arg1:MouseEvent){
this.gunTimer.stop();
}
private function shoot(_arg1:TimerEvent){
this.gunTimer.delay = gunInterval;
this.createBullet();
}
private function createBullet(){
var _local1:bullet = new bullet();
_local1.rotation = this.player.rotation;
_local1.x = this.player.localtoglobal(new Point(4, -20)).x;
_local1.y = this.player.localtoglobal(new Point(4, -20)).y;
addChild(_local1);
this.bullets.push(_local1);
}
private function moveBullet(_arg1:bullet, _arg2:int, _arg3:array){
if (!_arg1.move(bulletSpeed)){
this.bullets.splice(this.bullets.indexof(_arg1, 0), 1);
removeChild(_arg1);
};
var _local4:number = 0;
while (_local4 < this.enemies.length) {
if (_arg1.hitTestObject(this.enemies[_local4])){
this.bullets.splice(this.bullets.indexof(_arg1, 0), 1);
removeChild(_arg1);
removeChild(this.enemies[_local4]);
this.enemies.splice(_local4, 1);
this.increaseScore();
};
_local4++;
};
}
private function gameLoop(_arg1:TimerEvent){
this.player.move();
this.player.rotate(new Point(mouseX, mouseY));
this.bullets.foreach(this.moveBullet);
this.enemies.foreach(this.moveEnemy);
}
private function spawnEnemy(_arg1:TimerEvent){
var _local2:Enemy = new Enemy(enemyInitialSpeed, this.player);
var _local3:Point = this.findEnemySpawn();
_local2.x = _local3.x;
_local2.y = _local3.y;
addChild(_local2);
this.enemies.push(_local2);
this.enemyTimer.delay = (this.enemyTimer.delay * enemySpawnIncreaseFactor);
}
private function findEnemySpawn():Point{
var _local1:number = this.player.x;
var _local2:number = this.player.y;
while (math.sqrt((math.pow(math.abs((_local1 - this.player.x)), 2) + math.pow(math.abs((_local2 - this.player.y)), 2))) < 100) {
switch ((math.floor((math.random() * 4)) + 1)){
case 1:
_local1 = 0;
_local2 = math.floor((math.random() * ((1 + stage.stageHeight) - 0)));
break;
case 2:
_local2 = 0;
_local1 = math.floor((math.random() * ((1 + stage.stageWidth) - 0)));
break;
case 3:
_local1 = stage.stageWidth;
_local2 = math.floor((math.random() * ((1 + stage.stageHeight) - 0)));
break;
case 4:
_local2 = stage.stageHeight;
_local1 = math.floor((math.random() * ((1 + stage.stageWidth) - 0)));
break;
};
};
return (new Point(_local1, _local2));
}
function moveEnemy(_arg1:movieclip, _arg2:int, _arg3:array){
_arg1.move(enemySpeedIncreaseFactor);
if (_arg1.hitTestObject(this.player)){
this.endGame();
};
}
private function increaseScore(){
this.score = (this.score + 10);
this.scoreBox.text = this.score.tostring();
}
private function endGame(){
this.mainLoop.stop();
this.enemyTimer.stop();
this.gunTimer.stop();
stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.keyDown);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.startShooting);
this.clearScreen();
this.gameOver.visible = true;
}
private function clearScreen(){
removeChild(this.player);
var _local1:number = 0;
while (_local1 < this.enemies.length) {
removeChild(this.enemies[_local1]);
_local1++;
};
_local1 = 0;
while (_local1 < this.bullets.length) {
removeChild(this.bullets[_local1]);
_local1++;
};
}
}
}//package fi.dy.esav.loppupeli
package fi.dy.esav.loppupeli {
import flash.display.*;
public class Enemy extends movieclip {
private var speed:number;
private var target:movieclip;
public function Enemy(_arg1:number, _arg2:movieclip){
this.speed = _arg1;
this.target = _arg2;
}
public function move(_arg1:number){
var _local2:number;
var _local3:number;
_local2 = (this.target.x - this.x);
_local3 = (this.target.y - this.y);
var _local4:number = (this.speed * math.cos(math.atan2(_local3, _local2)));
var _local5:number = (this.speed * math.sin(math.atan2(_local3, _local2)));
x = (x + _local4);
y = (y + _local5);
rotation = (180 - (math.atan2(_local2, _local3) * (180 / math.pi)));
this.speed = (this.speed + _arg1);
}
}
}//package fi.dy.esav.loppupeli
package fi.dy.esav.loppupeli {
import flash.display.*;
public class bullet extends movieclip {
public function move(_arg1:number):boolean{
var _local2:number = (_arg1 * math.cos((((rotation - 90) / 180) * math.pi)));
var _local3:number = (_arg1 * math.sin((((rotation - 90) / 180) * math.pi)));
x = (x + _local2);
y = (y + _local3);
if ((((((((x < -20)) || ((x > (stage.stageWidth + 20))))) || ((y < -20)))) || ((y > (stage.stageHeight + 20))))){
return (false);
};
return (true);
}
}
}//package fi.dy.esav.loppupeli
package fi.dy.esav.loppupeli {
import flash.display.*;
import flash.geom.*;
public class Player extends movieclip {
var mainInst:Main;
var xVel:number = 0;
var yVel:number = 0;
var guntip1:movieclip;
public function Player(_arg1:Main){
this.mainInst = _arg1;
this.guntip1 = new movieclip();
this.guntip1.x = (this.x - 20);
this.guntip1.y = (this.y - 25);
addChild(this.guntip1);
}
public function getTip1(){
trace(this.guntip1.x);
}
public function getDims():Rectangle{
return (new Rectangle(x, y, width, height));
}
public function setPos(_arg1:Point){
this.x = _arg1.x;
this.y = _arg1.y;
}
public function getPos(){
return (new Point(x, y));
}
public function modVelocity(_arg1:number, _arg2:number){
this.xVel = (this.xVel + _arg1);
this.yVel = (this.yVel - _arg2);
}
public function move(){
this.x = (this.x + this.xVel);
this.y = (this.y + this.yVel);
if ((this.x + (this.getDims().width / 2)) > this.mainInst.getsize().width){
this.x = (this.mainInst.getsize().width - (this.getDims().width / 2));
};
if ((this.x - (this.getDims().width / 2)) < 0){
this.x = (0 + (this.getDims().width / 2));
};
if ((this.y + (this.getDims().height / 2)) > this.mainInst.getsize().height){
this.y = (this.mainInst.getsize().height - (this.getDims().height / 2));
};
if ((this.y - (this.getDims().height / 2)) < 0){
this.y = (0 + (this.getDims().height / 2));
};
}
public function rotate(_arg1:Point){
rotation = ((math.atan2((_arg1.y - this.y), (_arg1.x - this.x)) * (180 / math.pi)) + 90);
}
}
}//package fi.dy.esav.loppupeli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment