Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. var start_map = [
  2. [0, 0, 0, 0, 0, 0, 0, 0],
  3. [0, 0, 0, 0, 0, 0, 0, 0],
  4. [0, 0, 0, 0, 0, 0, 0, 0],
  5. [0, 0, 0, 0, 0, 0, 0, 0],
  6. [0, 0, 0, 0, 0, 0, 0, 0],
  7. [0, 0, 0, 0, 0, 0, 0, 0],
  8. [0, 9, 0, 0, 0, 0, 0, 0],
  9. [1, 1, 1, 1, 1, 1, 1, 1]
  10. ];
  11. var playerX;
  12. var playerY;
  13.  
  14. function drawPlayer() {
  15. fill(0);
  16. rect(playerX, playerY, 50, 50);
  17. }
  18.  
  19. function drawMap(map) {
  20. // The x and y do not represent the x and y axis
  21. // Keep in mind a 2d array is an array of an array
  22. noStroke();
  23. var playerIsSpawned = false;
  24.  
  25. for (var x = 0; x < map.length; x++) {
  26. for (var y = 0; y < map.length; y++) {
  27. // Background
  28. if (map[y][x] == 0) {
  29. fill(184, 236, 255);
  30. rect((10 + 50*x), (10 + 50*y), 50, 50);
  31. }
  32. // Ground
  33. else if (map[y][x] == 1) {
  34. fill(51, 153, 51);
  35. rect((10 + 50*x), (10 + 50*y), 50, 50);
  36. }
  37. // Player
  38. else if (map[y][x] == 9) {
  39. if (playerIsSpawned == false) {
  40. playerX = (10 + 50*x);
  41. playerY = (10 + 50*y);
  42. playerIsSpawned = true;
  43. }
  44. fill(184, 236, 255);
  45. rect((10 + 50*x), (10 + 50*y), 50, 50);
  46. }
  47. }
  48. }
  49. drawPlayer();
  50. function keyPressed() {
  51. if (key == "d") {
  52. playerX += 5;
  53. }
  54. else if (key == "a") {
  55. playerX -= 5;
  56. }
  57. }
  58. keyPressed();
  59. }
  60.  
  61. function setup() {
  62. background(0);
  63. createCanvas(800, 800);
  64. }
  65.  
  66. function draw() {
  67. drawMap(start_map);
  68. }
Add Comment
Please, Sign In to add comment