コードは次のとおりです。
'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 の倍数でない限り、できません。これを回避するにはどうすればよいですか?