[object Object]
Регистрация: 01.08.2008
Адрес: В России
Сообщений: 4,355
Написано 2,471 полезных сообщений (для 6,852 пользователей)
|
Ответ: Flight
Накатал пример, который демонстрирует взаимодействие игрока с врагами.
Можно стрелять, ходить, стрелять, умирать :D
Управление: WSAD + MOUSE
Graphics(800,600,0,2)
SetBuffer(BackBuffer())
SeedRnd(MilliSecs())
SetFont(LoadFont("Tahoma cyr", 16))
; Player position
Global player_x# = GraphicsWidth() * 0.5
Global player_y# = GraphicsHeight() * 0.5
Global player_shotdelay = 350 ; Delay per shot
Global player_shottime = 0
Global player_hp = 100
Global player_maxhp = 100
Global player_score = 0
Global spawner_level = 1
Global spawner_time = 0
Global spawner_delay = 6200
; Loop
While Not KeyHit(1)
;====== UPDATE PPLAYER =====
; Calc view angle
Local fang# = ATan2(MouseY() - player_y, MouseX() - player_x)
; Create bullet while mouse button pressed and shot_time
If MouseDown(1) And player_shottime < MilliSecs() Then
TBullet_Create(player_x, player_y, fang)
player_shottime = MilliSecs() + player_shotdelay
EndIf
; movement
Local vx = (KeyDown(32) - KeyDown(30))
Local vy = (KeyDown(31) - KeyDown(17))
Local mang# = ATan2(vy, vx)
If (Abs(vx) + Abs(vy)) Then
player_x = player_x + Cos(mang) * 3
player_y = player_y + Sin(mang) * 3
EndIf
;====== Enemy spawner ======
If spawner_time < MilliSecs() Then
Local i, sx%, sy%
For i = 0 To spawner_level
sx = (Rand(100)>50) * GraphicsWidth()
sy = (Rand(100)>50) * GraphicsHeight()
TEnemy_Create(sx, sy, 1 + Rand(spawner_level))
Next
spawner_time = MilliSecs() + spawner_delay
spawner_level = spawner_level + 1
EndIf
TBullet_Update() ; update all bullets
TEnemy_Update() ; update all enemies
TBullet_Draw() ; Draw all bullets
TEnemy_Draw(); Draw all enemies
;-- Draw player
If (player_hp > 0) Then
Line(player_x, player_y, player_x + Cos(fang) * 30, player_y + Sin(fang) * 30 )
Color(255,0,0)
Oval(player_x - 10,player_y - 10, 20, 20)
Color(255,255,255)
Else
Text(GraphicsWidth()*0.5, GraphicsHeight()*0.5, "YOU SUCK!", True, True)
EndIf
Text(5,0, "SCORE: " + player_score+" " + mang)
Text(5, 30, "HitPoints: " + player_hp * (player_hp > 0))
Flip() : Cls()
Wend
EndGraphics
End
Type TBullet
Field x#, y#, angle#
End Type
Function TBullet_Create(x#, y#, ang#)
Local this.TBullet = New TBullet
this\x=x
this\y=y
this\angle = ang
End Function
Function TBullet_Update()
For b.TBullet = Each TBullet
b\x = b\x + Cos( b\angle ) * 6
b\y = b\y + Sin( b\angle ) * 6
Local enemy.TEnemy
For enemy = Each TEnemy
Local dist# = Sqr((b\x - enemy\x) ^ 2 + (b\y - enemy\y) ^ 2)
If dist < 28 Then
enemy\hp = enemy\hp - 1
b\x = -1000
Exit
EndIf
Next
If b\x<0 Or b\x>GraphicsWidth() Or b\y<0 Or b\y>GraphicsHeight() Then
Delete(b)
EndIf
Next
End Function
Function TBullet_Draw()
For b.TBullet = Each TBullet
Oval(b\x - 3, b\y - 3, 6, 6)
Next
End Function
Type TEnemy
Field x#, y#, angle#
Field hp%
End Type
Function TEnemy_Create(x#, y#, hp)
Local this.TEnemy = New TEnemy
this\x = x
this\y = y
this\hp = hp
End Function
Function TEnemy_Update()
Local this.TEnemy
For this = Each TEnemy
this\angle = ATan2(player_y - this\y, player_x - this\x)
this\x = this\x + Cos(this\angle)*2
this\y = this\y + Sin(this\angle)*2
; if distance between player and enemy < 40
If Sqr((this\x - player_x) ^ 2 + (this\y - player_y) ^ 2)<40 Then
player_hp = player_hp - 1
EndIf
If this\hp < 1 Then
player_score = player_score + 1
Delete(this)
EndIf
Next
End Function
Function TEnemy_Draw()
Local this.TEnemy
For this = Each TEnemy
Color(60, 104, 24) ; Zombie ;D
Oval(this\x - 12, this\y - 12, 24, 24)
Next
Color(255,255,255)
End Function
Прочти внимательно код - там много приёмов которые тебе пригодятся.
__________________
Retry, Abort, Ignore? █
Intel Core i7-9700 4.70 Ghz; 64Gb; Nvidia RTX 3070
AMD Ryzen 7 3800X 4.3Ghz; 64Gb; Nvidia 1070Ti
AMD Ryzen 7 1700X 3.4Ghz; 8Gb; AMD RX 570
AMD Athlon II 2.6Ghz; 8Gb; Nvidia GTX 750 Ti
|