Player hit one wall but not the other
Posted: Mon Jul 28, 2014 11:13 am
Firstly wasn't sure whether to put this here or in the games development. This is the first time I am coding a game using this. I have learnt another programming language in the past and looked at C++ briefly however I don't know much. Everything that I have learnt about coding in the gamebuino so far has come from the beginner and intermediate examples that were provided. I had decided that I wanted to make a simple game to make sure I understood all what I had looked through so far. The game I am making will eventually consist of a wall (2 walls is how i set it up) that moves towards the player with a gap in it, if the player goes through the gap they get a point and the wall resets to the other side of the screen with a different position, after getting a certain amount of points the wall would begin to speed up. The code currently is only set to send the wall at the player and doesn't remove the wall or reset it or count a point. The problem I am having is that if the player touches wall2 (the wall set to move along the bottom of the screen) they die and the game over screen shows up, however if the player touches wall1 they pass right through it and don't die at all.
Code:
TL;DR: The player dies if they touch wall2 but not if they touch wall1, and i cannot find why this happens.
Code:
- Code:
#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);
}
}
}
TL;DR: The player dies if they touch wall2 but not if they touch wall1, and i cannot find why this happens.