2016-01-31 1 views
0

Voici le code:Small Basic Comment puis-je faire "mourir" mes joueurs même si mon carré d'ennemis ne peut pas correspondre exactement?

'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 

Et voici le problème. Pour le rendre joli et ne pas être super lent, chaque fois que le joueur est déplacé, il est déplacé par multiples de 10. Cependant, l'axe X de l'ennemi est aléatoire, et pas toujours sur un multiple de 10. Ce que j'essaie de faire est Chaque fois que la case de mon joueur est dans la case de mon ennemi, elle affiche "NON!" dans la fenêtre graphique. Mais je ne peux pas, sauf si le nombre aléatoire que l'axe X de l'ennemi est sur un multiple de 10. Comment puis-je contourner cela?

Répondre

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' 
+0

enemyy et x [ennemi] est +10 pour accomider la surface du carré. Donc, s'il tombe n'importe où dans l'ennemi, il déclenchera le NON! programme. bon travail que je vais voir si je peux raccourcir votre code ou le rendre plus efficace – Matthew

+0

vous pouvez utiliser des formes à la place. laissez-moi porivde le code. – Matthew

+0

Je suis nouveau dans Small Basic, donc je ne me suis pas rendu compte que vous pouviez faire des formes et les déplacer sans avoir à effacer et redessiner l'écran graphique. Mais le nouveau code est plus propre et plus efficace – Auxhil

0

Vous pouvez faire le nombre aléatoire toujours un multiple de 10:

EnemyX = Math.GetRandomNumber(59)*10 
+0

à droite, mais alors vous vous limitez à des multiples de 10 et l'espace est divisé par 10. – Matthew

0

fixe ce assez facilement (complètement réécrite Code)

'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