Source Code. Projects. Nerd Stuff. Art Stuff.

Super Mario


Posted on September 6th, by amoeboar in classes, icm. No Comments

Using mouseX and mouseY I made a simple interaction between Mario and Boo. Hovering the mouse either to the left or right of Mario causes him to face different directions. When Mario faces left, Boo stops in place and becomes transparent. The way to do this and to isolate each image is to use pushMatrix() and popMatrix(). Then, to animate Boo and give him the proper conditions, boolean variables need to be created so that he travels under certain conditions and stops under others. For this project, I learned how to load and manipulate images.

 

[gn_spoiler title="Here is the source code:" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/********************************
/ John Capogna
/ marioAndBoo.pde
/ ITP, 2012
********************************/

PImage mario;
PImage boo;
PImage marioDead;
PImage bg;
float booX = 80;
float booY = 115;
float marioX = 655;
float marioY = 390;
boolean arrived = false;
boolean rising = true;

void setup() {
  size(800, 488);
  frameRate(90);
  // Make new instances of a PImage by loading an image file
  mario = loadImage("mario.png");
  boo = loadImage("boo.png");
  bg = loadImage("bg.png");
  marioDead = loadImage("marioDead.png");
}

void draw() {
  imageMode(CORNER);
  image(bg, 0, 0);

    if(mouseX > marioX) {   // if mouse is to the right of Mario
      if(booX == 580) {     // boo has reached Mario when booX is 580
      arrived = true;
    }
    if (arrived == false) { // while boo has not arrived
      pushMatrix();         // and while Mario faces right
      imageMode(CENTER);
      image(mario, marioX, marioY);
      popMatrix();
    }
      pushMatrix();         // Boo advances while (x < 655)
      imageMode(CENTER);
      if (arrived == false) {  // and boo hasn't arrived yet
        booX += 1;
        booY += 0.5;
      }
      image(boo, booX, booY);
      popMatrix();    
                            // else: mouse is to the left of Mario
  } else {
    if(arrived == false) {  // Mario faces left
      pushMatrix();         // and only while condition arrived is false
    scale(-1, 1);           // reverse his image
    imageMode(CENTER);
    image(mario, -marioX, marioY);
    popMatrix();
    }
                            // Boo goes invisible whie cursor is to left
    pushMatrix();
    imageMode(CENTER);
    tint(255, 35);          // make transparent
    image(boo, booX, booY);
    noTint();               // noTint so other images are not transparent
    popMatrix();
  }

                            // when boo reaches Mario
  if (arrived == true) {
    imageMode(CENTER);
    if (rising == true) {   // Mario rises
      marioY += -6;
    } else {                // Mario falls
      marioY += 6;
    }

    if (marioY < 315) {     // condition: when marioY < 315
      rising = false;
    }

    image(marioDead, marioX, marioY);   // new Mario image
  }
}
[/gn_spoiler]

 

In order to run the sketch yourself, you’ll need the image files. You can download them here.





Leave a Reply