这是代码:
'Player and Enemy Variables
PlayerX = 0
PlayerY = 339
EnemyX = Math.GetRandomNumber(590)
EnemyY = 339
'Just setting up
GraphicsWindow.Show()
GraphicsWindow.Width = "600"
GraphicsWindow.Height = "400"
GraphicsWindow.KeyDown = OnKeyDown
'Ground, Enemy, and Player Drawn
GraphicsWindow.FillRectangle(0,350,600,50)
GraphicsWindow.DrawRectangle(PlayerX,PlayerY,10,10)
GraphicsWindow.DrawRectangle(EnemyX,EnemyY,10,10)
GraphicsWindow.DrawBoundText(50,50,100,"WASD")
Sub OnKeyDown
'What button was pressed?
If (GraphicsWindow.LastKey = "W") Then
GraphicsWindow.Clear()
PlayerY = PlayerY - 10
GraphicsWindow.FillRectangle(0,350,600,50)
GraphicsWindow.DrawRectangle(PlayerX,PlayerY,10,10)
GraphicsWindow.DrawRectangle(EnemyX,EnemyY,10,10)
GraphicsWindow.DrawBoundText(50,50,100,"WASD")
EndIf
If (GraphicsWindow.LastKey = "S") Then
GraphicsWindow.Clear()
PlayerY = PlayerY + 10
GraphicsWindow.FillRectangle(0,350,600,50)
GraphicsWindow.DrawRectangle(PlayerX,PlayerY,10,10)
GraphicsWindow.DrawRectangle(EnemyX,EnemyY,10,10)
GraphicsWindow.DrawBoundText(50,50,100,"WASD")
EndIf
If (GraphicsWindow.LastKey = "A") Then
GraphicsWindow.Clear()
PlayerX = PlayerX - 10
GraphicsWindow.FillRectangle(0,350,600,50)
GraphicsWindow.DrawRectangle(PlayerX,PlayerY,10,10)
GraphicsWindow.DrawRectangle(EnemyX,EnemyY,10,10)
GraphicsWindow.DrawBoundText(50,50,100,"WASD")
EndIf
If (GraphicsWindow.LastKey = "D") Then
GraphicsWindow.Clear()
PlayerX = PlayerX + 10
GraphicsWindow.FillRectangle(0,350,600,50)
GraphicsWindow.DrawRectangle(PlayerX,PlayerY,10,10)
GraphicsWindow.DrawRectangle(EnemyX,EnemyY,10,10)
GraphicsWindow.DrawBoundText(50,50,100,"WASD")
EndIf
'Keep in the Graphics Window!
If (PlayerX < 10) Then
PlayerX = 10
EndIf
If (PlayerX > 590) Then
PlayerX = 590
EndIf
If (PlayerY < 10) Then
PlayerY = 10
EndIf
If (PlayerY > 339) Then
PlayerY = 339
EndIf
'Player and Enemy Collide.
If (PlayerX = EnemyX And PlayerY = EnemyY) Then
GraphicsWindow.DrawBoundText(100,50,100,"NO!")
EndIf
EndSub
这就是问题所在。为了使它看起来不错并且不会太慢,每当移动播放器时,它都会以 10 的倍数移动。然而,敌人的X轴是随机的,并不总是10的倍数。我想做的是,每当我的玩家方块在我的敌人的方块内时,它就会在图形窗口中显示"NO!"。但我不能,除非敌人的 X 轴所在的随机数是 10 的倍数。我该如何解决此问题?
(PlayerX >= EnemyX and PlayerX<= Enemyx +10 And PlayerY >= EnemyY And PlayerY <= EnemyY +10)
change the code where it says plyaerX= enemy and change it to the above V full code
'Player and Enemy Variables
PlayerX = 0
PlayerY = 339
EnemyX = Math.GetRandomNumber(590)
EnemyY = 339
player = Shapes.AddRectangle(10, 10)
Enemy = Shapes.AddRectangle(10, 10)
Shapes.Move(player, PlayerX, PlayerY)
Shapes.Move(enemy, EnemyX, EnemyY)
'Just setting up
GraphicsWindow.Show()
GraphicsWindow.Width = "600"
GraphicsWindow.Height = "400"
GraphicsWindow.KeyDown = OnKeyDown
'Ground, Enemy, and Player Drawn
GraphicsWindow.FillRectangle(0,350,600,50)
GraphicsWindow.DrawBoundText(50,50,100,"WASD")
Sub OnKeyDown
'What button was pressed?
If (GraphicsWindow.LastKey = "W") Then
PlayerY = PlayerY - 10
Shapes.Move(player, PlayerX, PlayerY)
Shapes.Move(enemy, EnemyX, EnemyY)
EndIf
If (GraphicsWindow.LastKey = "S") Then
PlayerY = PlayerY + 10
Shapes.Move(player, PlayerX, PlayerY)
Shapes.Move(enemy, EnemyX, EnemyY)
EndIf
If (GraphicsWindow.LastKey = "A") Then
PlayerX = PlayerX - 10
Shapes.Move(player, PlayerX, PlayerY)
Shapes.Move(enemy, EnemyX, EnemyY)
EndIf
If (GraphicsWindow.LastKey = "D") Then
PlayerX = PlayerX + 10
Shapes.Move(player, PlayerX, PlayerY)
Shapes.Move(enemy, EnemyX, EnemyY)
GraphicsWindow.DrawBoundText(50,50,100,"WASD")
EndIf
'Keep in the Graphics Window!
If (PlayerX < 10) Then
PlayerX = 10
EndIf
If (PlayerX > 590) Then
PlayerX = 590
EndIf
If (PlayerY < 10) Then
PlayerY = 10
EndIf
If (PlayerY > 339) Then
PlayerY = 339
EndIf
'Player and Enemy Collide.
If (PlayerX >= EnemyX and PlayerX<= Enemyx +10 And PlayerY >= EnemyY And PlayerY <= EnemyY +10) Then
GraphicsWindow.DrawBoundText(100,50,100,"NO!")
EndIf
EndSub'
你可以让随机数始终是 10 的倍数:
EnemyX = Math.GetRandomNumber(59)*10
很容易解决这个问题(完全重写了代码)
'Show Graphics Window and some settings
GraphicsWindow.Show()
GraphicsWindow.BackgroundColor = "DarkBlue"
GraphicsWindow.Width = "600"
GraphicsWindow.Height = "400"
GraphicsWindow.CanResize = "False"
GraphicsWindow.KeyDown = OnKeyDown
'Ground
GraphicsWindow.FillRectangle(0,350,630,75)
'Set Player and Coordinates
Player = Shapes.AddRectangle(10, 10)
PX = 0
PY = 340
Shapes.Move(Player,PX,PY)
'Set Enemy and Coordinates
Enemy = Shapes.AddEllipse(10, 10)
EX = Math.GetRandomNumber(300)
EY = 340
Shapes.Move(Enemy,EX,EY)
RunLoop:
GraphicsWindow.KeyDown = OnKeyDown
'Did Player and Enemy Collide?
If (EX <= PX + 10) Then
Shapes.HideShape(Enemy)
EndIf
'Stay in Graphics Window Please!
If PX < 10 Then
PX = 10
EndIf
If PX > 590 Then
PX = 590
EndIf
Goto RunLoop
'Move Player
Sub OnKeyDown
If(GraphicsWindow.LastKey = "A") Then
PX = PX - 10
Shapes.Move(Player,PX,PY)
EndIf
If(GraphicsWindow.LastKey = "D") Then
PX = PX + 10
Shapes.Move(Player,PX,PY)
EndIf
EndSub