Here is my code:
main ino file:
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
#include "mario_bitmaps.h"
#include "player.h"
#define MAX_LEVEL_WIDTH 30
#define MAX_LEVEL_HEIGHT 6
extern const byte PROGMEM levels[];
const byte *block = NULL;
int cameraPos = 0;
byte lvldata[MAX_LEVEL_WIDTH * MAX_LEVEL_HEIGHT];
byte lvl = 1;
Gamebuino gb;
Player player(0, LCDHEIGHT - 24);
byte i = 0, j = 0; // for loop variables
void loadLevel(int curLev) {
for(i = 0; i<MAX_LEVEL_WIDTH * MAX_LEVEL_HEIGHT; i++){//Loops through the level
lvldata[i] = pgm_read_byte(levels + (MAX_LEVEL_WIDTH * MAX_LEVEL_HEIGHT) * (curLev - 1) + i);
}
}
void renderLevel() {
for(i = 0;i < MAX_LEVEL_HEIGHT;i++) {
for(j = 0;j < MAX_LEVEL_WIDTH;j++) {
switch(lvldata[j + i * MAX_LEVEL_WIDTH]) {
case 0:
block = empty;
break;
case 1:
block = sand_ground;
break;
case 2:
block = diagonal_right;
break;
case 3:
block = diagonal_left;
break;
default:
break;
}
if(block != NULL) {
if((j << 3) - cameraPos >= -8 && (j << 3) - cameraPos < LCDWIDTH ||
(i << 3) > 0 && (i << 3) < LCDHEIGHT) {
gb.display.drawBitmap((j << 3) - cameraPos, i << 3, block);
}
} else {
gb.display.drawBitmap((j << 3) - cameraPos, i << 3, empty);
}
}
}
cameraPos = player.x * 2;
}
void setup() {
gb.begin();
gb.titleScreen(F("Super Mario Bros"));
loadLevel(lvl);
}
void loop() {
if(gb.update()) {
gb.display.clear();
renderLevel();
player.render(gb);
if(gb.buttons.pressed(BTN_C)) {
gb.titleScreen(F("Super Mario Bros"));
loadLevel(1);
}
}
}
Levels.ino
- Code: Select all
const byte levels[] PROGMEM = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,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diagonal Right 2
// Diagonal Left 3
marioBitmaps.h
- Code: Select all
#ifndef MARIO_BITMAPS_H
#define MARIO_BITMAPS_H
byte mario_still[] PROGMEM = {16,16,
0x1F,0x0,
0x20,0xC0,
0x4F,0x20,
0x72,0xC0,
0xB2,0x20,
0xB1,0x20,
0x63,0xC0,
0x30,0x80,
0x4F,0x0,
0x86,0x80,
0xBF,0x80,
0x4F,0x80,
0x53,0x80,
0x21,0x0,
0x3E,0x0,
0x3E,0x0,
};
byte mario_walking_1[] PROGMEM = {16,16,
0x1F,0x0,
0x20,0xC0,
0x4F,0x20,
0x72,0xC0,
0xB2,0x20,
0xB1,0x20,
0x63,0xC0,
0x30,0x80,
0x4F,0x0,
0x86,0x80,
0xBF,0x80,
0x4F,0x80,
0x5B,0x80,
0xBE,0x40,
0x96,0x40,
0x61,0x80,
};
byte sand_ground[] PROGMEM = {8,8,
0xFF,
0x0,
0xFF,
0xFF,
0x10,
0x0,
0x80,
0x21,
};
const byte diagonal_right[] PROGMEM = {8,8,
0x80,
0x40,
0x20,
0x10,
0x8,
0x4,
0x2,
0x1,
};
const byte diagonal_left[] PROGMEM = {8,8,
0x1,
0x2,
0x4,
0x8,
0x10,
0x20,
0x40,
0x80,
};
const byte empty[] PROGMEM = {8,8,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
};
#endif /* MARIO_BITMAPS_H */
Player.h
- Code: Select all
#ifndef PLAYER_H
#define PLAYER_H
#include "mario_bitmaps.h"
#define MAX_COLLISION_LEVEL_WIDTH 30
#define MAX_COLLISION_LEVEL_HEIGHT 6
extern const byte PROGMEM collision_levels[];
extern byte lvl;
extern int cameraPos;
byte collvldata[MAX_COLLISION_LEVEL_WIDTH * MAX_COLLISION_LEVEL_HEIGHT];
void loadColLevel(int curLev) {
for(byte i = 0; i<MAX_COLLISION_LEVEL_WIDTH * MAX_COLLISION_LEVEL_HEIGHT; i++){//Loops through the level
collvldata[i] = pgm_read_byte(collision_levels + (MAX_COLLISION_LEVEL_WIDTH * MAX_COLLISION_LEVEL_HEIGHT) * (curLev - 1) + i);
}
}
class Player {
public:
Player(float x, float y) {
this->x = x;
this->y = y;
this->velX = 0.0f;
this->velY = 0.0f;
this->bitMap = mario_still;
this->flip = false;
this->walking = false;
this->lives = 3;
loadColLevel(lvl);
}
float x, y;
float velX, velY;
byte *bitMap;
boolean flip;
boolean walking;
byte lives;
void restart() {
this->x = 0;
this->y = 0;
this->velX = 0.0f;
this->velY = 0.0f;
this->bitMap = mario_still;
this->flip = false;
this->walking = false;
loadColLevel(lvl);
}
void render(Gamebuino gb) {
if(lives <= 0) {
if(!gb.buttons.pressed(BTN_A)) {
gb.display.clear();
gb.display.fillScreen(BLACK);
gb.display.setColor(WHITE, BLACK);
gb.display.cursorX = (LCDWIDTH / 2) - 15;
gb.display.cursorY = (LCDHEIGHT / 2) - 5;
gb.display.println("Game over!");
gb.display.cursorX = (LCDWIDTH / 2) - 9;
gb.display.cursorY = (LCDHEIGHT / 2) + 1;
gb.display.println("Press A");
} else {
lives = 3;
restart();
}
return;
}
gb.display.print("x");
gb.display.println(lives);
gb.display.cursorX = LCDWIDTH / 2 - 3;
gb.display.cursorY = 0;
gb.display.print("LEVEL ");
gb.display.println(lvl);
if(this->x < 0) this->x = 0;
if(this->x + 8 > LCDWIDTH) this->x = LCDWIDTH - 8;
if(this->y < -8) this->y = -8;
if(this->y + 16 > LCDHEIGHT) {
restart();
lives--;
}
if(gb.buttons.repeat(BTN_RIGHT, 1)){
velX = 1.0f;
flip = false;
walking = true;
}
else if(gb.buttons.repeat(BTN_LEFT, 1)) {
velX = -1.0f;
flip = true;
walking = true;
}
else {
velX = 0;
walking = false;
}
if((collvldata[(int)(((y / 8) * MAX_COLLISION_LEVEL_WIDTH) + ((x / 8 + velX) + cameraPos / 8))]) == 0 &&
(collvldata[(int)(((y / 8 + 1) * MAX_COLLISION_LEVEL_WIDTH) + ((x / 8 + velX) + cameraPos / 8))]) == 0) {
x += velX;
}
if((gb.buttons.timeHeld(BTN_UP) > 0 && gb.buttons.timeHeld(BTN_UP) < 5)){
if((collvldata[(int)(((y / 8 - 1) * MAX_COLLISION_LEVEL_WIDTH) + (x / 8 + cameraPos / 8))]) == 0) {
velY = -8.0f;
}
}
y += velY;
if((collvldata[(int)(((y / 8 + 2) * MAX_COLLISION_LEVEL_WIDTH) + (x / 8 + cameraPos / 8))]) == 0) {
velY = 8.0f;
} else {
velY = 0;
}
if(walking) {
if(gb.frameCount % 6 >= 0 && gb.frameCount % 6 < 3) {
this->bitMap = mario_still;
}
else {
this->bitMap = mario_walking_1;
}
}
else {
this->bitMap = mario_still;
}
if(flip) {
gb.display.drawBitmap(x, y, this->bitMap, NOROT, FLIPH);
}
else {
gb.display.drawBitmap(x, y, this->bitMap, NOROT, NOFLIP);
}
}
};
#endif
Thanks in advance
awesome 101