Mon Jul 28, 2014 11:13 am
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb = Gamebuino();
static unsigned char PROGMEM explode0[] = {
8, 8,
B00010000,
B01000001,
B00000100,
B10010000,
B00000100,
B01000001,
B00001000,
B10100100,
};
static unsigned char PROGMEM explode1[] = {
8, 8,
B00000000,
B01000001,
B00000100,
B00010000,
B00000100,
B01000000,
B00001000,
B00000000,
};
static unsigned char PROGMEM explode2[] = {
8, 8,
B00000000,
B00000000,
B00000000,
B00010000,
B00000100,
B01000000,
B00001000,
B00000000,
};
//Record
typedef struct
{
int x;
int y;
int vx;
int vy;
byte w;
byte h;
byte size;
boolean exist;
int score;
} PlayerStruct;
PlayerStruct Player = (PlayerStruct) {
0, 0, 0, 0, true, 0
};
//Record
typedef struct
{
byte x;
byte y;
byte w;
byte h;
int vx;
} Wall1Struct;
Wall1Struct Wall1 = (Wall1Struct){
0
};
//Record
typedef struct
{
byte x;
byte y;
byte w;
byte h;
int vx;
} Wall2Struct;
Wall2Struct Wall2 = (Wall2Struct){
0
};
//Variables
boolean GameReset = true;
boolean GameFirstRun = true;
boolean paused = false;
//Function
boolean wallHit (int px, int py, int pw, int ph, int wx, int wy, int ww, int wh)
{
if (gb.collideRectRect(px, py, pw, ph, wx, wy, ww, wh)){
byte AnimationDelay=40;
gb.sound.playCancel();
gb.display.drawBitmap(Player.x - Player.size, Player.y - Player.size, explode2);
gb.display.update();
delay(AnimationDelay);
gb.display.drawBitmap(Player.x - Player.size, Player.y - Player.size, explode1);
gb.display.update();
delay(AnimationDelay);
gb.display.drawBitmap(Player.x - Player.size, Player.y - Player.size, explode0);
gb.display.update();
delay(AnimationDelay);
gb.display.drawBitmap(Player.x - Player.size, Player.y - Player.size, explode1);
gb.display.update();
delay(AnimationDelay);
gb.display.drawBitmap(Player.x - Player.size, Player.y - Player.size, explode2);
gb.display.update();
delay(AnimationDelay);
gb.sound.playTick();
return true;
}
else return false;
}
void setup()
//Code runs once
{
randomSeed(gb.battery.voltage + gb.backlight.ambientLight);
gb.begin();
gb.titleScreen(F("Impossible Game - Terramet"));
gb.battery.show = false;
}
void loop()
//code runs continuously
{
if (GameReset){
if (!GameFirstRun)
{
gb.display.clear();
gb.display.fontSize = 1;
gb.display.print("Game Over!\n");
gb.display.print("Score:\n");
gb.display.print(Player.score);
gb.sound.playCancel();
gb.display.update();
Player.score = 0;
delay(5000);
}
//Initialising Variables
GameReset = false;
GameFirstRun = false;
Wall1.w = 4;
Wall1.h = 48;
Wall1.x = LCDWIDTH - Wall1.w;
Wall1.y = 20 - 54;
Wall1.vx = 1;
Wall2.w = 4;
Wall2.h = 48;
Wall2.x = LCDWIDTH - Wall2.w;
Wall2.y = Wall1.y + 54;
Wall2.vx = 1;
Player.w = 4;
Player.h = 4;
Player.x = (LCDWIDTH - Player.w) / 2;
Player.y = (LCDHEIGHT - Player.h) / 2;
Player.size = 4;
Player.vx = 2;
Player.vy = 2;
}
if (gb.update()) {
if (gb.buttons.pressed(BTN_C)) {
paused = !paused;
}
if (!paused) {
//move the player using the buttons
if(gb.buttons.repeat(BTN_RIGHT, 1)){
Player.x = min(LCDWIDTH - Player.w, Player.x + Player.vx);
}
if(gb.buttons.repeat(BTN_LEFT, 1)){
Player.x = max(0, Player.x - Player.vx);
}
if(gb.buttons.repeat(BTN_UP, 1)){
Player.y = max(0, Player.y - Player.vy);
}
if(gb.buttons.repeat(BTN_DOWN, 1)){
Player.y = min(LCDHEIGHT - Player.h, Player.y + Player.vy);
}
//Move walls towards player
Wall1.x = Wall1.x - Wall1.vx;
Wall2.x = Wall2.x - Wall2.vx;
}
}
//Draw the game objects
gb.display.drawRect(Player.x, Player.y, Player.size, Player.size);
gb.display.fillRect(Wall1.x, Wall1.y, Wall1.w, Wall1.h);
gb.display.fillRect(Wall2.x, Wall2.y, Wall2.w, Wall2.h);
//Check for wall collision
GameReset = wallHit (Player.x, Player.y, Player.w, Player.h, Wall1.x, Wall1.y, Wall1.w, Wall1.h);
if (GameReset != true) {
GameReset = wallHit (Player.x, Player.y, Player.w, Player.h, Wall2.x, Wall2.y, Wall2.w, Wall2.h);
}
}
}
Mon Jul 28, 2014 1:45 pm
typedef struct
{
byte x;
byte y;
byte w;
byte h;
int vx;
} Wall_Struct;
Wall_Struct Wall1 = {0};
Wall_Struct Wall2 = {0};
Wall1.y = 20 - 54;
Wall1.w = 4;
Wall1.h = 14;
Wall1.x = LCDWIDTH - Wall1.w;
Wall1.y = 0;
Wall1.vx = 1;
Wall2.w = 4;
Wall2.h = 48;
Wall2.x = LCDWIDTH - Wall2.w;
Wall2.y = Wall1.h + 6;
Wall2.vx = 1;
Mon Jul 28, 2014 2:12 pm
Mon Jul 28, 2014 3:05 pm
Mon Jul 28, 2014 11:28 pm
Mon Jul 28, 2014 11:35 pm
Limited wrote:Bytes won't actually go negative, they will wrap around to 255.
Tue Jul 29, 2014 7:20 am
Limited wrote:Bytes won't actually go negative, they will wrap around to 255. Therefore if you have 5 in a variable and minus 6, you'll get 255. That will be why that function failed.
Tue Jul 29, 2014 8:01 pm
Tiash wrote:Limited wrote:Bytes won't actually go negative, they will wrap around to 255. Therefore if you have 5 in a variable and minus 6, you'll get 255. That will be why that function failed.
it's right, I had not considered that it was of type byte
it is interesting to note that when the byte is passed to gb.display.fillRect, being converted to int8_t, you do not notice the difference on the screen (which can be negative)
Wed Jul 30, 2014 4:35 pm