Показать сообщение отдельно
Старый 27.05.2012, 16:38
wvxvw вне форума Посмотреть профиль Отправить личное сообщение для wvxvw Найти все сообщения от wvxvw
  № 9  
Ответить с цитированием
wvxvw
Modus ponens
 
Аватар для wvxvw

модератор форума
Регистрация: Jul 2006
Адрес: #1=(list #1#)
Сообщений: 8,049
Записей в блоге: 38
Вот, собственно, накидал приме, но на Хексе, в качестве стимула к изучению :P
Код AS3:
package ;
 
import flash.display.Sprite;
import flash.Lib;
import flash.display.Graphics;
 
class Target extends Sprite
{
	private var _color:UInt;
 
	public var angle:Float;
 
	public var speed:Float;
 
	public var radius:Int;
 
	public function new(radius:Int)
	{
		super();
		this.radius = radius;
		_color = Std.int(Math.random() * 4294967295.0);
		speed = Math.random() + 1;
		draw();
	}
 
	public function draw()
	{
		var canvas:Graphics = graphics;
 
		canvas.clear();
		canvas.beginFill(_color);
		canvas.drawCircle(0, 0, radius);
		canvas.endFill();
 
		if (!Math.isNaN(angle))
		{
			var cos:Float = Math.cos(angle);
			var sin:Float = Math.sin(angle);
			canvas.lineStyle(1, 0);
			canvas.moveTo(-cos * radius, sin * radius);
			canvas.lineTo(cos * radius, -sin * radius);
		}
	}
 
	public function setAngle(angle:Float)
	{
		this.angle = angle;
		draw();
	}
}
 
class Hunter extends Sprite
{
	public function new()
	{
		super();
		draw();
	}
 
	public function draw()
	{
		var canvas:Graphics = graphics;
		canvas.clear();
		canvas.beginFill(0xFF0000);
		canvas.drawRect(-10, -10, 20, 20);
		canvas.endFill();
	}
}
 
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
 
class ShootingGame extends Sprite
{
	private var _targets:Array<Target>;
 
	private var _hunter:Hunter;
 
	public function new()
	{
		super();
		addEventListener(Event.ADDED_TO_STAGE, init);
	}
 
	private function init(_)
	{
		super.removeEventListener(Event.ADDED_TO_STAGE, init);
		createTargets();
		startGame();
		createHunter();
	}
 
	private function createHunter()
	{
		_hunter = new Hunter();
		super.addChild(_hunter);
		stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
		stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
	}
 
	private function keyHandler(event)
	{
		if (event.keyCode == Keyboard.UP && _hunter.y > 0)
		{
			_hunter.y -= 5;
		}
		else if (event.keyCode == Keyboard.DOWN && _hunter.y < stage.stageHeight)
		{
			_hunter.y += 5;
		}
	}
 
	private function mouseDownHandler(_)
	{
		var canvas:Graphics = Lib.current.graphics;
		canvas.clear();
		canvas.lineStyle(1, 0);
		canvas.moveTo(0, _hunter.y);
		canvas.lineTo(mouseX, mouseY);
		for (t in _targets)
		{
			if (lineIntersects(t, mouseX, mouseY))
			{
				t.x = Math.random() * 1000 + stage.stageWidth;
			}
		}
	}
 
	private inline function lineIntersects(target:Target,
	clickX:Float, clickY:Float):Bool
	{
		var clickAngle:Float = Math.atan2(clickX, clickY - _hunter.y);
		var cos:Float = Math.cos(target.angle);
		var sin:Float = Math.sin(target.angle);
		var radius:Float = target.radius;
		var low:Float = Math.atan2(target.x - cos * radius,
		target.y + sin * radius - _hunter.y);
		var high:Float = Math.atan2(target.x + cos * radius,
		target.y - sin * radius - _hunter.y);
		return clickAngle >= low && clickAngle <= high;
	}
 
	private function createTargets()
	{
		if (_targets == null)
			_targets = new Array<Target>();
		for (i in 0...20)
			_targets[i] = new Target(Std.random(25) + 25);
	}
 
	private function startGame()
	{
		super.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		for (t in _targets)
		{
			t.y = Math.random() * stage.stageHeight;
			t.x = Math.random() * 1000.0 + stage.stageWidth;
			super.addChild(t);
		}
	}
 
	private function enterFrameHandler(_)
	{
		for (t in _targets)
		{
			t.x -= t.speed;
			if (t.x <= 0)
				t.x = stage.stageWidth + Math.random() * 1000;
			t.setAngle(calculateAngle(t.x, t.y));
		}
	}
 
	private inline function calculateAngle(tx:Float, ty:Float):Float
	{
		return Math.atan2(tx, ty - _hunter.y);
	}
 
	private function stopGame()
	{
		for (t in _targets) super.removeChild(t);
		_targets = null;
		super.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
	}
 
	public static function main()
	{
		Lib.current.addChild(new ShootingGame());
	}
}
Код:
-main ShootingGame
-debug
-swf-version 10
-swf-header 800:600:31
-swf ./bin/shooting-game.swf
Вложения
Тип файла: zip shooting-game.swf.zip (6.2 Кб, 41 просмотров)
__________________
Hell is the possibility of sanity


Последний раз редактировалось wvxvw; 27.05.2012 в 16:42.