Increase the value of a variable WHILE conditions are met

 while (bulx > 0) {
bulx += 10;
ab.setCursor((bulx), (buly)); 
  ab.print("-");

so im trying to make “-” move across the screen while conditions are met, am i doing something wrong? ive done ALOT of googling, i hate to ask but im stuck

What is happening, what is not happening?

If bulx is unsigned, your while loop will never exit.

Also, I presume you want the thing to move over successive frames. What you have here will only run the while loop each frame, so you will only see the last result, i would guess, or mybe a line of -

you want the following.

 void setup() {
   // put your setup code here, to run once:
 
 }

 uint8_t bulx;

 void loop() {
   // put your other code here.

   bulx += 10;
   if (bulx > 128) { bulx = 0; }
   ab.setCursor((bulx), (buly)); 
   ab.print("-");
 }

it appears when i trigger it to than imediatly dissapears

#include "Arduboy.h"

Arduboy ab;
int gamestate = 0;
int locx = 0; //player location x
int locy = 0; //player location y
int bulx = 0; //player bullet location x
int buly = 0; // player bullet location y
int hp = 0; //current planes health
int life = 0; // extra lives (planes)
int elocx = 100; //enemy location x
int elocy = 30; // enemy location y
int enemyex = 1; //enemy exists (0 no 1 yes)
int ehp = 0; //enemy hit points
int ebulx = 0; // enemy bullet x coord
int ebuly = 0; //enemy bulley y coord
void setup() {
// put your setup code here, to run once:
ab.begin();
ab.setFrameRate(30);
ab.clear();
}

void loop() {
ab.clear();
if (!(ab.nextFrame()))
return;

// move the player back one space
if (ab.pressed(LEFT_BUTTON) && (gamestate == 0) && (locx > 1)){
locx -= 1;

}

// move player forward one space
if (ab.pressed(RIGHT_BUTTON) && (gamestate == 0) && (locx < 100)){
locx += 1;
}

// move player up one space
if (ab.pressed(UP_BUTTON) && (gamestate == 0) && (locy > 1)){
locy -= 1;
}

//move player down one space
if (ab.pressed(DOWN_BUTTON) && (gamestate == 0) && (locy < 44)){
locy += 1;
}

// print the players location
ab.setCursor((locx), (locy));
ab.print(“x”);
// if enemy exists, make it be
if (enemyex = 1) {

ab.setCursor((elocx), (elocy)); 
  ab.print("@");

}
// enemy shoot if within 80 px or player
if ((enemyex = 1 ) && (80 > (elocx-locx)) && (ebulx == 0)) {

ab.setCursor((elocx - 5), (elocy)); 
  ab.print("-");
  ebulx = (elocx - 10);
  ebuly = (elocy);

}
//if a bullet is not on the screen and player presses a shoot
if (ab.pressed(A_BUTTON) && (gamestate == 0) && (bulx == 0)){
bulx = (locx + 10);
buly = (locy);
ab.setCursor((locx + 10), (locy));
ab.print("-");

}
while (ebulx > 0) {
++ebulx;
ab.setCursor((ebulx) , (ebuly));
ab.print("-");
}
while (bulx > 0) {
bulx += 1;
}
while (bulx > 0) { //tried adding a second while
ab.setCursor((bulx), (buly));
ab.print("-");
}

ab.display();
}

let it be known this is my first attempt at making a game from scratch, it dosent look like much but i feel ive come a long way in a short time

Alright, If no one else does i’ll respond when i get home form work tonight :slight_smile:

good stuff so far, don’t be discouraged, and you’ve done the right thing asking for help

not discouraged , starting small.

i WANTED to make something much more epic, but i need to get the basics down first, i have alot of free time so expect many games from me in the future :wink:

for right now im just trying to get the player to shoot, from there i can figure out how to get the enemy to shoot, and ill figure out the rest from there

1 Like

soooo after much more playing around with it i came to realize it did technicly work however its moving across the screen much faster than the screen can show, like wayyyyyyy off the screen before the screen can refresh, im thinking that if i can slow it down it will work fine

slowing it down shouldnt be a problem something simalar to the “blink without delay” example in the arduino ide should do the trick

You should be using the library setFrameRate(), nextFrame() and possibly everyXFrames() functions to control the pace of your game.

https://mlxxxp.github.io/documents/Arduino/libraries/Arduboy2/Doxygen/html/index.html

2 Likes

well that will simplify alot thanks

i cant seem to find how to use everyXFrames()

everyXFrames(4) will return with a value of true every 4th frame.

‘everyXFrames’ was not declared in this scope

im using this library #include “Arduboy2.h” do i need another?

everyXFrames is a function of arduboy instance.

so you would call it like the following

if( arduboy.everyXFrames(10) ) {
     //this is a thing you do every x frames.
}

or

//increase x by 1 every 2 frames
x += arduboy.everyXFrames(2);

well i THOUGHT i tried that guess not, re writing everything, only need small tweeks to implement this bit, but the whole thing needs some cleaning up thanks for the help

Not quite. The loop is while(bulx > 0), which means 0 will break out of it because 0 is not greater than 0 and unsigned numbers do include 0.

that point is moot, there are plenty of things that take bulx back to zero, but im a little scared to play with this now, had issues geting a different game on my arduboy when i decided to take a break, (its probably this pc being jankey) in a couple days ill have a screen for my raspberry pi, and will run it all off that to see if i can fix some of the issues im having

When you’re able to, repost all your code with any changed you’ve made and I’ll have a quick look/run to see if I can see what’s happening/not happening. (And hopefully someone else will come along and help too.)

Although valid for C++, it’s not very good practice to assume a returned boolean value will equal 1. I don’t find it very readable either.

Your if( arduboy.everyXFrames(10) ) example is better, or at least:
x += arduboy.everyXFrames(2) ? 1 : 0;

1 Like

Yep, you are absolutely right. for some reason i read it as GEQ

1 Like

I don’t find it very readable either.

Oh man, it’s terribly ambiguous, i’m not even going to try and defend that.

TBH

x += arduboy.everyXFrames(2) ? 1 : 0;

Is also kind of hard to read, but at least it won’t break if arduboy.everyXFrames starts returning any nonzero.

still having problems with the enemy being as stupid as i programmed it to be but its getting better, the bullets now work (well mine do, i dont want the enemy killing the player while im trying to figure out what my code is doing)