SHARE
TWEET

Pixelkollision Test 2

a guest Jun 8th, 2019 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Graphics 800, 600, 16, 2
  2. SetBuffer BackBuffer()
  3.  
  4.  
  5. player_x = 590
  6. player_y = 440
  7. player_old_x = player_x
  8. player_old_y = player_y
  9. player = LoadImage("Bär.jpeg") ;füge hier ein bild ein, dass im selben verzeichnis liegt wie diese datei // Format "<Bildname>.<Bildtyp>" keine GIF
  10. player_height = 50
  11. player_width = 50
  12.  
  13. enemy_x = 604
  14. enemy_y = 503 ;enemy würde das _bla_old_x auch benötigen
  15. enemy = LoadImage("Ente.jpg") ;füge hier ein zweites ein
  16. enemy_height = 50
  17. enemy_width = 50
  18.  
  19. player_move_right = False
  20. player_move_left = False
  21. player_move_up = False
  22. player_move_down = False
  23.  
  24. hit_right = False
  25. hit_left = False
  26. hit_up = False
  27. hit_down = False
  28.  
  29. player_x_speed = 1
  30. player_y_speed = 1
  31.  
  32. ResizeImage player, player_width, player_height ;ändert bildgröße
  33. ResizeImage enemy, enemy_width, enemy_height
  34.  
  35.  
  36. Repeat ;Hauptschleife
  37. Cls
  38.  
  39. player_old_x = player_x
  40. player_old_y = player_y
  41.  
  42. DrawImage player, player_x, player_y ;zeichnet Bild
  43. DrawImage enemy, enemy_x, enemy_y
  44.  
  45.  
  46.  
  47. If Not ImagesOverlap(player, player_x+player_x_speed,player_y, enemy, enemy_x, enemy_y) Then player_move_right = True ;kann er nach rechts gehen?
  48. If Not ImagesOverlap(player, player_x-player_x_speed,player_y, enemy, enemy_x, enemy_y) Then player_move_left = True
  49. If Not ImagesOverlap(player, player_x,player_y+player_y_speed, enemy, enemy_x, enemy_y) Then player_move_down = True
  50. If Not ImagesOverlap(player, player_x,player_y-player_y_speed, enemy, enemy_x, enemy_y) Then player_move_up = True
  51.  
  52.  
  53.  
  54. If KeyDown(203) And player_move_left = True Then player_x = player_x - player_x_speed : player_x_speed = player_x_speed + 1 ;nach links bewegen wenn linke pfeiltaste gedrückt
  55. If KeyDown(205) And player_move_right = True Then player_x = player_x + player_x_speed : player_x_speed = player_x_speed + 1
  56. If KeyDown(200) And player_move_up = True Then player_y = player_y - player_y_speed : player_y_speed = player_y_speed + 1
  57. If KeyDown(208) And player_move_down = True Then player_y = player_y + player_y_speed : player_y_speed = player_y_speed + 1
  58.  
  59.  
  60. ;If KeyDown(205) And player_move_right = False Then player_x = player_x + Abs( enemy_x - player_x) - 1  ;Soll Player maximal herankommen lassen, vielleicht auch mit For-Schleife machbar //gelöst
  61.  
  62.  
  63. ;hier das diagonal gehen verhindern
  64.  
  65.     If ImagesOverlap(player, player_x, player_y, enemy, enemy_x, enemy_y) Then player_x = player_old_x : player_y = player_old_y
  66.  
  67. ;//end of diagonal stuff
  68.  
  69.  
  70. If ImagesOverlap(player, player_x+player_x_speed,player_y, enemy, enemy_x, enemy_y) Then hit_right = True ;gibt es einen kontakt beim nach rechts bewegen?
  71. If ImagesOverlap(player, player_x-player_x_speed,player_y, enemy, enemy_x, enemy_y) Then hit_left = True
  72. If ImagesOverlap(player, player_x,player_y+player_y_speed, enemy, enemy_x, enemy_y) Then hit_down = True
  73. If ImagesOverlap(player, player_x,player_y-player_y_speed, enemy, enemy_x, enemy_y) Then hit_up = True
  74.  
  75. hit_right_distance = Abs(enemy_x - (player_x+player_width)) ;abstand zwischen linkem rand des feindbildes und rechtem rand des playerbildes
  76. hit_left_distance = Abs(enemy_x+enemy_width - (player_x))
  77. hit_down_distance = Abs(enemy_y - (player_y+player_height))
  78. hit_up_distance = Abs(enemy_y+enemy_height - (player_y))
  79.  
  80.  
  81.  
  82. If hit_right = True Then
  83.     If hit_right_distance < player_x_speed Then player_x = player_x + hit_right_distance ;bewege player bis direkt an den rand des gegners, sodass keine lücken entstehen bei player_x_speed > 1 oder player_y_speed > 1
  84. EndIf
  85. If hit_down = True Then
  86.     If hit_down_distance < player_y_speed Then player_y = player_y + hit_down_distance
  87. EndIf
  88. If hit_left = True Then
  89.     If hit_left_distance < player_x_speed Then player_x = player_x - hit_left_distance
  90. EndIf
  91. If hit_up = True Then
  92.     If hit_up_distance < player_y_speed Then player_y = player_y - hit_up_distance
  93. EndIf
  94.  
  95. If hit_right = True Then Text 0, 0, "from se left" ;etwas verwirrend das hit_right bedeutet, dass der player von links kommend auf den feind trifft
  96. If hit_left = True Then Text 0, 0, "from se right" 
  97. If hit_up = True Then Text 0, 0, "from below"
  98. If hit_down = True Then Text 0, 0, "from se top"
  99.  
  100. ;setze alles zurück
  101.  
  102. player_move_right = False
  103. player_move_left = False
  104. player_move_up = False
  105. player_move_down = False
  106. hit_right = False
  107. hit_up = False
  108. hit_left = False
  109. hit_down = False
  110.  
  111. If player_x_speed > 2 Then player_x_speed = 2 ;setze maximale geschwindigkeiten, am besten statt einem festen wert eine variable verwenden, bspw. max_speed_x / max_speed_y
  112. If player_y_speed > 2 Then player_y_speed = 2
  113.  
  114.  
  115.  
  116. Flip
  117. Until KeyHit(1)
  118. End
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top