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

很容易地解决了这个问题(完全重写了代码)

'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
于 2016-02-02T00:59:44.130 回答
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 回答
0

您可以使随机数始终为 10 的倍数:

EnemyX = Math.GetRandomNumber(59)*10
于 2016-02-01T20:50:41.667 回答