dimanche 31 janvier 2016

Small Basic How do I get my players square to "die" even if my enemies square can't be matched up exactly to it?

Here's the code:

'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

And heres the problem. To make it look nice and not be super slow, whenever the Player is moved, it's moved in multiples of 10. However the Enemy's X axis is random, and not always on a multiple of 10. What I'm trying to do is whenever my Player's square is inside my Enemy's square, it will display "NO!" on the Graphics Window. But I can't unless the random number that the Enemy's X axis is on is a multiple of 10. How can I work around this?

Aucun commentaire:

Enregistrer un commentaire