LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

I cant figure out this simple code

Root / Programming Questions / [.]

🔒
JustBrianDCreated:
All I want it to do is make the number displayed go up by one digit everytime I hit the A button. I'm still learning but was really confident I could do this on my own, but obviously I suck a fat *tortilla* at BASIC. Here's what I have so far...
CLS
@TITLE
LOCATE 13,10
COLOR 7
PRINT "16 SHOT! Shooting Game"
IF BUTTON ()==16 THEN GOTO @GAME
GOTO @TITLE
@GAME
CLS
@GAMESTART
LOCATE 10,10
DIGIT = 0
PRINT DIGIT
IF BUTTON ()==16 THEN PRINT DIGIT + 1
GOTO @GAMESTART

Use VSYNC between IF BUTTON and GOTO. Also use the
code
function for your post, makes the code nicer maybe? :)* *not really necessary VSYNC 'syncs' commands to not execute too many times. Like a VSYNC 1 makes commands execute once per frame (or less if lots of lag and such) My explanation kinda sucks but there you go. Besides that, if you want you could get rid PRINT DIGIT + 1 and say INC DIGIT (increment DIGIT by 1) or DIGIT = DIGIT + 1. I use INC (or at least try to lately) EDIT: Oh, and your @GAMESTART loop could be
WHILE 1 
LOCATE 10,10
DIGIT = 0
PRINT DIGIT
IF BUTTON()==16 THEN PRINT DIGIT + 1
VSYNC 1
WEND
that. While just repeats until the condition (1) is false (0). Since it's 1 in my code, it would repeat forever, but if you said maybe WHILE DIGIT<100 it would only loop if DIGIT was less than 100. Don't worry about being bad at BASIC. My first program was worse than your program here ;)

Thanks buddy, which IF, BUTTON,and GOTO do I put VSYNC between? I tried putting it in a few spots lines (lines 5, 6, and 7) but either nothing happened or I hit a syntax error. I'm going to read up on VSYNC

IF BUTTON ()==16 THEN PRINT DIGIT + 1
Here's your problem. You tell the console to print DIGIT+1, but you never tell it to actually INCREMENT DIGIT. A better code would be:
IF BUTTON(1)==16 THEN INC DIGIT
PRINT DIGIT
I use the INC command to increment DIGIT, and then I output it. I also put a 1 in the parenthesis because that makes it to where only the button pressed will trigger the increment statement. Otherwise, it would spam increment if you held the button. Here's the full code that I personally would write (I'll use a few commands and techniques you might not have learned yet, but I highly suggest you read up on them).
CLS
LOCATE 13,10
COLOR 7
PRINT "16 SHOT! Shooting Game"
WHILE !(BUTTON() AND 16)
WEND
CLS
WHILE TRUE
LOCATE 10,10
PRINT DIGIT
IF BUTTON(1) AND 16 THEN INC DIGIT
VSYNC
WEND
(Notice: I have not tested this code. It may require tweaking.)

Here's the full code that I personally would write (I'll use a few commands and techniques you might not have learned yet, but I highly suggest you read up on them).
CLS
LOCATE 13,10
COLOR 7
PRINT "16 SHOT! Shooting Game"
WHILE !(BUTTON() AND 16)
WEND
CLS
WHILE TRUE
LOCATE 10,10
PRINT DIGIT
IF BUTTON(1) AND 16 THEN INC DIGIT
VSYNC
WEND
(Notice: I have not tested this code. It may require tweaking.)
This does exactly what I was going for, I need to read up on what these terms are and why/how this code works

The main problems with your original code were printing the the digit rather than incrementing it, as already pointed out, and that you were resetting digit to 0 at the start of your loop. Try to separate your code into blocks. Games usually do three main things in the main loop: process input, update the game state, and display the results. Separating your game like that makes it hard for the two main issues you had to happen. Initialise your game variables before you enter the main game loop. Eg. VAR DIGIT=0 Process user input with the BUTTON() function. Change variable values as appropriate. Eg. INC DIGIT Draw to the screen. Eg. PRINT DIGIT Most, if not all, of your games will be structured this way.
DIM ALL
DIM THE
DIM THINGS
VAR KEEPGOING=1
WHILE KEEPGOING
HANDLEINPUT
UPDATESTATE
DRAWGAME
WEND
DEF HANDLEINPUT
'button, circle pad, and touch screen reading code.
END
DEF UPDATESTATE
'change game variables value here, based on player input or based on time (eg. your DIGIT could decrease by 1 every 60 frames
'if it's game over then set KEEPGOING = 0 to get out of the loop
END
DEF DRAWGAME
'clear the screen and draw the visual representation of your game
END