Help with game


(porter) #1

Im having a problem with the hangman game I am making where the letters are constantly scrolling to the bottom and off screen then appearing at the top of the screen here is my code:

#include <Arduboy.h>

Arduboy arduboy;

int playerx = 20;
int playery = 20;
const unsigned char player[] PROGMEM = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
};

void setup() {
  // put your setup code here, to run once:
  arduboy.begin();
  arduboy.clear();
  arduboy.setTextWrap(true);
  arduboy.setTextSize(.1);
  arduboy.display();
}

void loop() {
  arduboy.clear();
  arduboy.print("A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z");
  arduboy.drawBitmap(playerx, playery, player, 16,16, WHITE);
  arduboy.drawLine(20,10,20,30,WHITE);
  arduboy.drawLine(20,10,30,10,WHITE);
  if( arduboy.pressed(RIGHT_BUTTON)){
  playerx = playerx + 10;
  }
  delay(100);
  if( arduboy.pressed(LEFT_BUTTON)){
  playerx = playerx - 10;
  }
  delay(100);
  arduboy.display();
 
}

(Scott) #2

I’ve edited your post to enclose your code in triple backticks to format it. Please do the same in the future.

```cpp

// your code here
#include <Arduboy.h>

Arduboy arduboy;

int playerx = 20;
int playery = 20;
// ...

```


(Scott) #3

It might be because you’ve set text size to a non-integer value. It must be 1, 2, 3, etc.


(Scott) #4

No, sorry, that’s not the problem but you should fix it anyway.

The problem is that the text cursor is left at the end of the text string printed by the print() function. Each time loop() runs, the text starts at the end of the previous text’s location.

When I first looked at the code, I didn’t notice that you’re using the original Arduboy library. The Arduboy2 library’s clear() function sets the text cursor to 0, 0 but the Arduboy library’s clear() doesn’t change the cursor.

If you switch to the Arduboy2 library (which you should because the Arduboy library is no longer being maintained), your code will work as you expect.

At the beginning of your code change:

#include <Arduboy.h>

Arduboy arduboy;

to:

#include <Arduboy2.h>

Arduboy2 arduboy;

Also note that your player[] array is too small for the 16 x 16 bitmap that you’re trying to display, which is why you get a strange looking “player”. drawBitmap() is using “garbage” past the end of the array.


(porter) #5

thank you it worked!


(porter) #6

one more thing i have it set so when i press the A button in a certain position it changes a variable but instead when i press A it moves my sprite to the last coordinate i entered please help:confounded::

#include <Arduboy2.h>


Arduboy2 arduboy;

int playerx = 20;
int playery = 20;
int lives = 6;
int guess = 0;
const unsigned char player[] PROGMEM = {
0xff, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xff, 
};
void setup() {
 // put your setup code here, to run once:
 arduboy.begin();
 arduboy.clear();
 arduboy.setTextWrap(true);
 arduboy.setTextSize(.1);
 arduboy.display();
 playerx = -1;
 playery = 30;
}

void loop() {
 arduboy.clear();
 arduboy.setCursor(0, 30);
 arduboy.print( "A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z");
 arduboy.drawBitmap(playerx, playery, player, 8,8, WHITE);
 arduboy.drawLine(20,10,20,30,WHITE);
 arduboy.drawLine(20,10,30,10,WHITE);
 if( arduboy.pressed(RIGHT_BUTTON)){
 playerx = playerx + 18;
 }
 if( arduboy.pressed(LEFT_BUTTON)){
 playerx = playerx - 18;
 }
 if( arduboy.pressed(UP_BUTTON) and (playery = 30) == false){
 playery = playery - 8;
 }
 if( arduboy.pressed(DOWN_BUTTON) and (playery < 60)){
 playery = playery + 8;
 }
 if( arduboy.pressed(A_BUTTON) and (lives > 0)){
   if(playery = 30){
     if(playerx = -1){
       guess = 1;
     }
     if(playerx = 17){
       guess = 2;
     }
     if(playerx = 35){
       guess = 3;
     }
     if(playerx = 53){
       guess = 4;
     }
     if(playerx = 71){
       guess = 5;
     }
     if(playerx = 89){
       guess = 6;
     }
     if(playerx = 108){
       guess = 7;
     }
   }
   if(playery = 38){
     if(playerx = -1)
     guess = 8;
   }
 }
 if( arduboy.pressed(B_BUTTON)){
   
 }
 
 arduboy.display();
 delay(200);
}

(Simon) #7

The code if(playerx = -1){ does not perform a test - it actually assigns a value to playerx.

You need to use the equality operator instead. Like so if(playerx == -1){. There a numerous spots whjere this needs to be fixed.

Also, the line …

if( arduboy.pressed(UP_BUTTON) and (playery = 30) == false){

… suffers from a number of problems. Firstly, you are assigning a value to playery which I am sure is not what you wanted and secondly the overall test logic is not clear.

Did you want to test to see if the player pressed the up button and the playery is not 30? If so, use:

if ( (arduboy.pressed(UP_BUTTON) && (playery != 30) ) {

Or, are you testing to see if the player has not (pressed the up button and the playery equals 30). If so, use:

if ( ( arduboy.pressed(UP_BUTTON) && (playery == 30) ) == false) {

Use additional brackets to make sure you intent is clear to you and the compiler :slight_smile: