1: 2: rem 3: rem BUTTONS!!!! 4: rem 5: rem This program creates and utilizes buttons in BasicBasic. While the 6: rem buttons it creates are used for an unusual purpose, the programming 7: rem methods shown will be useful to any button program. This program 8: rem works the same under DOS or Windows. 9: rem 10: 11: 12: rem 13: rem make windows window my size and give it a name 14: rem 15: 16: rem windows size 29,9,45,15 17: rem windows name "Button Move" 18: 19: cls 20: 21: rem 22: rem if no mouse this doesn't run 23: rem 24: a=mouseon 25: if a<>-1 then 26: beep 27: print "Mouse required to run this program." 28: stop 29: end if 30: 31: 32: rem 33: rem some general inits 34: rem 35: buttonfore=7 36: buttonback=4 37: borderfore=7 38: borderback=1 39: boardtop=10 40: boardleft=30 41: topy=3 42: topx=3 43: maxbuttons=8 44: totalx= (boardleft+(topx+2)*3)-(boardleft-1)+1 45: 46: dim board(topx+1,topy+1) : rem keeps pointer to text for button 47: dim boardc(topx+1,topy+1) : rem keeps button foreground color 48: dim text$(maxbuttons) : rem keeps text 49: 50: rem 51: rem define text 52: rem 53: text$(0)="Bas" 54: text$(1)="ic " 55: text$(2)="is " 56: text$(3)="you" 57: text$(4)="r b" 58: text$(5)="est" 59: text$(6)="val" 60: text$(7)="ue " 61: 62: 63: 64: 65: rem 66: rem flag border as occupied 67: rem 68: 69: for x=0 to topx+1 70: board(x,0)=1 71: board(x,topy+1)=1 72: next x 73: for y=0 to topy+1 74: board(0,y)=1 75: board(topx+1,y)=1 76: next y 77: 78: 79: rem 80: rem place my buttons on board 81: rem 82: 83: rem 84: rem first skip randomly into random number generator using 85: rem seconds of clock 86: rem 87: t$=time$ 88: t=val(right$(t$,2)) 89: for i=1 to t 90: x=rnd 91: next i 92: 93: for i=59 to 66 94: place10: 95: x=int(rnd*3)+1 96: y=int(rnd*3)+1 97: if board(x,y)<>0 then goto place10 98: board(x,y)=i 99: next i 100: 101: 102: rem 103: rem place button colors 104: rem 105: boardc(1,1)=0 106: boardc(1,2)=4 107: boardc(1,3)=5 108: boardc(2,1)=6 109: boardc(2,2)=0 110: boardc(2,3)=4 111: boardc(3,1)=5 112: boardc(3,2)=6 113: boardc(3,3)=0 114: 115: 116: rem 117: rem 118: rem color background 119: rem 120: color borderfore,borderback 121: for y=boardtop-1 to boardtop+topy+2 122: locate y,boardleft-1,0 123: print space$(totalx); 124: next y 125: 126: 127: rem 128: rem display exit and help buttons 129: rem 130: CBUTTON "Exit",1068,0,"Push",0,boardleft+(topy*3)+1,boardtop-1,6,1,buttonfore,4 131: CBUTTON "Help",1067,0,"Push",0,boardleft-1,boardtop-1,6,1,buttonfore,4 132: 133: 134: rem 135: rem display buttons of puzzle. 136: rem 137: 138: for x=1 to topx 139: for y=1 to topy 140: num=board(x,y) 141: if num<>0 then 142: CBUTTON text$(num-59),num+1000,0,"Push",0,(x*3)+boardleft,y+boardtop,3,1,buttonfore,boardc(x,y) 143: end if 144: next y 145: next x 146: 147: 148: rem 149: rem start of main program input loop 150: rem 151: 152: 100 153: a$=inkey$ 154: if a$="" then goto 100 155: if len(a$)=1 then goto 100 156: 157: rem 158: rem get here if extended keycode returned 159: rem 160: num=asc(right$(a$,1)) 161: 162: if num=68 then 163: rem 164: rem 1068 is what we defined our exit button to be 165: rem 166: stop 167: 168: elseif num=67 then 169: rem 170: rem help button pressed if here 171: rem 172: a$="Basic is your best value" 173: ll=len(a$) 174: a$=space$(totalx-1)+a$ 175: ll=len(a$) 176: color borderfore,borderback 177: for i=1 to ll 178: locate boardtop+5,boardleft-1,0 179: b$=left$(a$,totalx) 180: print b$; 181: l=len(a$) 182: a$=right$(a$,l-1)+" " 183: t=timer 184: t100: 185: if timer-t<.05 then goto t100 186: next i 187: locate boardtop+5,boardleft-1,0 188: print space$(11); 189: goto 100 190: 191: end if 192: 193: if num<59 or num>66 then goto 100 194: 195: rem 196: rem If we get here then one of our puzzle buttons pressed 197: rem 198: rem button keycode selected is in num 199: rem find which square contains this button 200: rem 201: bx=0 202: by=0 203: for x=1 to topx 204: for y=1 to topy 205: if board(x,y)=num then bx=x:by=y 206: next y 207: next x 208: if bx=0 then beep:goto 100: rem oops, something wrong if can't find key 209: 210: rem 211: rem see if adjacent square is empty 212: rem 213: for x=-1 to 1 step 1 214: for y=-1 to 1 step 1 215: if abs(x)<>abs(y) then 216: if board(bx+x,by+y)=0 then goto gotempty 217: end if 218: next y 219: next x 220: 221: beep : rem no empty square around this one 222: goto 100 223: 224: 225: rem 226: rem have empty square move to it 227: rem 228: 229: gotempty: 230: 231: dbutton num+1000 232: board(bx,by)=0 233: board(bx+x,by+y)=num 234: newx=(bx+x)*3 235: newy=(by+y) 236: CBUTTON text$(num-59),num+1000,0,"Push",0,newx+boardleft,newy+boardtop,3,1,buttonfore,boardc(bx+x,by+y) 237: 238: goto 100 239: |