int gameMode=0;//0 - menu, 1 - play game, 2 - instructions, 3 - character selection
int selChar=1;//default character is "character 1"
int level=1;
color red=color(255, 0, 0);
color green=color(0, 255, 0);
color blue=color(0, 0, 255);
boolean moveRight=false;
boolean moveLeft=false;
boolean moveDown=false;
boolean moveUp=false;
int x=50;
boolean[] keys=new boolean[128];
int[] v={4, 0};//horizontal and vertical velocity
int[] g1={0, 0, -968};
int[] g2={968, 0, -968};
PImage pic1;
PImage pic2;
int speed=-5;
int[] g3={0, 0, -108};
int[] g4={108, 0, -108};
PImage pic3;
PImage pic4;
PImage a;
PImage b;
PImage c;
void setup() {
size(800, 600);
a = loadImage("mario.png");
b = loadImage("luigi.png");
c = loadImage("Yoshi.png");
pic1=loadImage("back.png");
pic2=loadImage("back1.png");
pic3=loadImage("ground1.png");
pic4=loadImage("ground2.png");
}
void draw() {//runs forever
background(red);
if (gameMode==0){
mainMenu();
}
else if (gameMode==1){
playL1();
}
else if (gameMode==2)
controls();
else if (gameMode==3)
select();
}//end draw
void playL1(){
image(pic1, g1[0], g1[1]);
image(pic2, g2[0], g2[1]);
g1[0] = g1[0] + speed;//moving left
if (g1[0] + speed < g1[2]) {
g1[0] = -g2[2];
}
g2[0] = g2[0] + speed;//moving left
if (g2[0] + speed < g2[2]) {
g2[0] = -g1[2];
}
image(pic3, g3[0], g3[1]);
image(pic4, g4[0], g4[1]);
g3[0] = g3[0] + speed;//moving left
if (g3[0] + speed < g3[2]) {
g3[0] = -g4[2];
}
g4[0] = g4[0] + speed;//moving left
if (g4[0] + speed < g4[2]) {
g4[0] = -g3[2];
}
} //end playL1
void controls() {
textSize(35);
fill(255);
text("press spacebar to jump",150,150);
text("move you characters", 150, 250);
text("collect coins",150,350);
text("avoid the enemies",150,450);
backToMain();
}
void select() {//character select
textAlign(LEFT);
textSize(20);
fill(0);//black
//displaying all characters
rect(200, 50, 100, 100);
rect(350, 50, 100, 100);
rect(500, 50, 100, 100);
if (mouseIn(200, 50, 100, 100)) {
fill(blue);
rect(200, 50, 100, 100);
if (mousePressed) {
selChar=1;//1 could mean Mario
gameMode=0;//main menu
}
}
if (mouseIn(350, 50, 100, 100)) {
fill(blue);
rect(350, 50, 100, 100);
if (mousePressed) {
selChar=2;//2 could mean Luigi
gameMode=0;
}
}
if (mouseIn(500, 50, 100, 100)) {
fill(blue);
rect(500, 50, 100, 100);
if (mousePressed) {
selChar=3;//3 could be Yoshi
gameMode=0;
}
}
fill(255);
text("Char 1", 210, 100);
text("Char 2", 360, 100);
text("Char 3", 510, 100);
backToMain();
}//end select
void mainMenu() {
textAlign(LEFT);
textSize(35);
fill(blue);
text("START", 350, 430);
text("INSTRUCTIONS", 290, 490);
text("CHARACTERS", 295, 555);
image(a, 200, 0, 150, 200);
image(b, 80, 250, 200, 250);
image(c,600,200,150,200);
if (mouseIn(330, 400, 150, 35)) {//"play"
fill(0);
text("START", 350, 430);
if (mousePressed) {
gameMode=1;
}
}
if (mouseIn(280, 460, 270, 35)) {//"instructions"
fill(0);
text("INSTRUCTIONS", 290, 490);
if (mousePressed) {
gameMode=2;
}
}
if (mouseIn(285, 525, 240, 35)) {//"characters"
fill(0);
text("CHARACTERS", 295, 555);
if (mousePressed) {
gameMode=3;
}
}
fill(255);
textSize(60);
text("Super Mario ", 230, 300);
}//end mainMenu
void backToMain() {
textAlign(LEFT);
textSize(20);
fill(green);
rect(20, 550, 120, 40);
fill(blue);
text("Menu", 50, 580);
if (mouseIn(20, 550, 120, 40)) {//"back to menu"
fill(0);
text("Menu", 50, 580);
if (mousePressed) {
gameMode=0;
}
}
}//end backToMain
void movePlayer() {
if (keys[39]) {//39 is the code for the RIGHT arrow key
v[0]=4;
moveRight=true;
} else {
moveRight=false;
}
if (keys[37]) {//37 is LEFT
v[0]=-4;
moveLeft=true;
} else {
moveLeft=false;
}
if (keys[40]) { //40 is DOWN
v[1]=4;
moveDown=true;
} else {
moveDown=false;
}
if (keys[38]) { //38 is UP
v[1]=-4;
moveUp=true;
} else {
moveUp=false;
}
}//end movePlayer
void keyPressed() {
//println(keyCode+" was pressed");
//keys[keyCode]=true;
}
void keyReleased() {
//println(keyCode+" was released");
//keys[keyCode]=false;
}
//The function returns true if the mouse pointer is INSIDE the rectangle, otherwise returns false
boolean mouseIn(int left, int top, int w, int h) {
return (mouseX > left && mouseX < left+w && mouseY > top && mouseY < top+h);
}//end mouseIn
I have this game code but I need help adding the ground and moving the screen the way the player moves. If you could help that would be great!
1 Like
Okay, this is a 2D Sketch
We can’t run it since we don’t have the images
Consider uploading to github or so
Anyway. I assume Mario moves left and right
The idea is that Mario stays in the center and the ground moves underneath him (ground left when Mario goes right)
Search for the word Viewport here on the forum please
4 Likes
When you have a ground image
Display it at 0,0
When he runs 5 right, move the image 5 left, that’s -5 (minus 5)
Don’t move Mario he is at width/2