Показать сообщение отдельно
Старый 28.11.2011, 12:25   #120
Greymem
Нуждающийся
 
Регистрация: 31.05.2010
Сообщений: 63
Написано 3 полезных сообщений
(для 3 пользователей)
Ответ: Вопрос-Ответ (для новичков BlitzMax)

Вот что получилось, на случай, если кому-нибудь понадобится

SuperStrict

Framework BRL.Blitz
Import brl.Math
Import brl.basic
Import BRL.LinkedList
Import BRL.KeyCodes
Import brl.GLMax2D

Function GetRange:Int(x1:Int, x2:Int, y1:Int, y2:Int)
Return Sqr(((x1 - x2) ^ 2) + ((y1 - y2) ^ 2)) ;
End Function

Function Strike_Angle:Double(x0:Double, y0:Double, x1:Double, y1:Double)
	Return ATan2(y1 - y0, x1 - x0) ;
End Function


Type TBullet
	
	Field Link:TLink
	
	Field PositionX:Float
	Field PositionY:Float
	
	'= = ОРИГИНАЛЬНЫЙ КОД = =
	'Field DirectionX:Float
	'Field DirectionY:Float
	' = = = = = = = = = = = =
	
	Field Direction:Float;
	
End Type

Global BulletSpeed:Float = 1

Local Bullets:TList = CreateList()

Local TargetPositionX:Float = 100
Local TargetPositionY:Float = 100

Local TargetDirectionX:Float = Sin(45)
Local TargetDirectionY:Float = Cos(45)

Global TargetSpeed:Float = 1

Local BotPositionX:Float = 400
Local BotPositionY:Float = 300

Local TicksCount:Int = 0

Local ShootTick:Int = 0

Local ShotsCount:Int = 0
Local HitsCount:Int = 0

Graphics(800, 600)

Repeat
	
	TicksCount :+ 1
	If KeyDown(KEY_UP) Then
		BulletSpeed:+0.1
	ElseIf KeyDown(KEY_DOWN) And BulletSpeed > 0 Then
		BulletSpeed:-0.1
	ElseIf KeyDown(KEY_LEFT) And TargetSpeed > 0 Then
		TargetSpeed:-0.1
	ElseIf KeyDown(KEY_RIGHT)
		TargetSpeed:+0.1
	End If
	
	
	' движение цели
	
	TargetPositionX :+ TargetDirectionX * TargetSpeed
	TargetPositionY :+ TargetDirectionY * TargetSpeed
	
	' отскок от стен
	
	If TargetPositionX < 0 Then
		TargetPositionX = 0
		TargetDirectionX = -TargetDirectionX
	End If
	
	If TargetPositionX >= 800 Then
		TargetPositionX = 800 - 1
		TargetDirectionX = -TargetDirectionX
	End If
	
	If TargetPositionY < 0 Then
		TargetPositionY = 0
		TargetDirectionY = -TargetDirectionY
	End If
	
	If TargetPositionY >= 600 Then
		TargetPositionY = 600 - 1
		TargetDirectionY = -TargetDirectionY
	End If
	
	' стрельба
	
	If Bullets.Count() = 0 And TicksCount >= ShootTick Then
		
		ShootTick = TicksCount + 10
		
		ShotsCount :+ 1
		
		' расчет упреждения
		
		Local DeflexionX:Float = TargetPositionX
		Local DeflexionY:Float = TargetPositionY
		
		Local TargetVelocityX:Float = TargetDirectionX * TargetSpeed
		Local TargetVelocityY:Float = TargetDirectionY * TargetSpeed
		
		For Local ComputingPass:Int = 1 To 5
			' = = = = = = = = = = ОРИГИНАЛЬНЫЙ КОД = = = = = =
			'Local DeltaX:Float = (DeflexionX - BotPositionX)
			'Local DeltaY:Float = (DeflexionY - BotPositionY)
			' = = = = =  = = = = = = = = = = = = = = = = = = =
			
			Local Distance:Float = GetRange(BotPositionX, DeflexionX, BotPositionY, DeflexionY)
			Local Time:Float = Distance / BulletSpeed
			
			DeflexionX = TargetPositionX + TargetVelocityX * Time
			DeflexionY = TargetPositionY + TargetVelocityY * Time
			
		Next
		
		' выстрел
		
		Local Bullet:TBullet = New TBullet
		
		Bullet.Link = Bullets.AddLast(Bullet)
		
		' = = = = = = = = = = ОРИГИНАЛЬНЫЙ КОД = = = = =  = = = = = = =
		'Local DeltaX:Float = DeflexionX - BotPositionX
		'Local DeltaY:Float = DeflexionY - BotPositionY
		'Local Distance:Float = Sqr(DeltaX * DeltaX + DeltaY * DeltaY)
		'Bullet.DirectionX = DeltaX / Distance
		'Bullet.DirectionY = DeltaY / Distance
		' = = = = =  = = = = = = = = = = = = = = = = = =  = = = = = = =
		
		Bullet.Direction = Strike_Angle(BotPositionX, BotPositionY, DeflexionX, DeflexionY)
		Bullet.PositionX = BotPositionX
		Bullet.PositionY = BotPositionY
		
	End If
	
	' обновление пуль
	
	For Local Bullet:TBullet = EachIn Bullets
		
		
		'If Bullet.Force <= 0 Or Bullet.PositionX + 8 > GraphicsWidth() Or Bullet.PositionX + 8 < 0 Or Bullet.PositionY + 8 > GraphicsHeight() Or Bullet.PositionY + 8 < 0
		If Bullet.PositionX + 8 > GraphicsWidth() Or Bullet.PositionX + 8 < 0 Or Bullet.PositionY + 8 > GraphicsHeight() Or Bullet.PositionY + 8 < 0
	
			Bullet.Link.Remove()
			Continue
			
		End If
		
		' полет
		
		' = = = = = = = = = = ОРИГИНАЛЬНЫЙ КОД = = = = =  = = = = = = =
		'Bullet.PositionX :+ Bullet.DirectionX * BulletSpeed
		'Bullet.PositionY :+ Bullet.DirectionY * BulletSpeed
		' = = = = =  = = = = = = = = = = = = = = = = = =  = = = = = = =
		
		Bullet.PositionX:+BulletSpeed * Cos(Bullet.Direction) ;
		Bullet.PositionY:+BulletSpeed * Sin(Bullet.Direction) ;
		
		' проверка на попадание в цель
		
		Local DeltaX:Float = TargetPositionX - Bullet.PositionX
		Local DeltaY:Float = TargetPositionY - Bullet.PositionY
		
		Local Distance:Float = Sqr(DeltaX * DeltaX + DeltaY * DeltaY)
		
		If Distance <= 8 Then
			
			HitsCount :+ 1
			
			Bullet.Link.Remove()
			
		End If
		
	Next
	
	' отрисовка
	
	Cls()
	
	SetColor(111, 111, 111) ;
	DrawText("[UP] - Increase bullet speed, [DOWN] - Decrease bullet speed", 40, 5) ;
	DrawText("Bullet speed: " + BulletSpeed, 600, 5) ;
	DrawText("[left] - Increase target speed, [right] - Decrease target speed", 40, 20) ;
	DrawText("Target speed: " + TargetSpeed, 600, 20) ;
	SetColor(0, 255, 0)
	
	DrawOval(BotPositionX - 8, BotPositionY - 8, 16, 16)
	
	SetColor(255, 0, 0)
	
	DrawOval(TargetPositionX - 8, TargetPositionY - 8, 16, 16)
	
	SetColor(255, 255, 0)
	
	For Local Bullet:TBullet = EachIn Bullets
		
		DrawOval(Bullet.PositionX - 3, Bullet.PositionY - 3, 6, 6)
		
	Next
	
	SetColor(255, 255, 255)
	
	DrawText(String.FromInt(HitsCount * 100 / ShotsCount) + "%", 10, 10)
	
	Flip()
	
Until KeyDown(KEY_ESCAPE)

End
__________________
Мозги... у них есть метод "Storm"
(Offline)
 
Ответить с цитированием