0

コードは次のとおりです。

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

4

3 に答える 3

0

(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'
于 2016-02-01T16:38:46.313 に答える