这是代码:
'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 的倍数。我该如何解决这个问题?