Author Topic: FlipIt v1.0 - Light Out puzzle game  (Read 4171 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
FlipIt v1.0 - Light Out puzzle game
« on: September 09, 2018, 10:37:05 pm »
This is a simple "Lights out" puzzle game.  I don't really like this kind of game, but just wanted to make something.  It's nothing special, but does show one way of breaking the screen into a clickable grid of buttons.

- Dav

Code: QB64: [Select]
  1. '==========
  2. 'FLIPIT.BAS v1.0
  3. '==========
  4. 'Like the lights out color flip puzzle.
  5. 'Try to make all squares the same by flipping them.
  6. 'When you select a square, its color will flip, and
  7. 'its neighboring squares will also flip colors.
  8. 'Coded by Dav for QB64, SEP/2018
  9.  
  10.  
  11. 'define button grid deminsions
  12. DIM SHARED grid: grid = 8 '           size of grid is 8x8
  13. DIM SHARED size: size = 65 '          size of buttons is 65x65
  14. DIM SHARED btns: btns = grid * grid ' number of buttons on grid
  15.  
  16. 'define button data
  17. DIM SHARED bv(btns) '              data for button
  18. DIM SHARED bx(btns), by(btns) '    top x/y cords of buttons
  19. DIM SHARED bx2(btns), by2(btns) '  bottom x/y cords of buttons
  20.  
  21. 'set screen based on grid & size
  22. SCREEN _NEWIMAGE(size * grid, size * grid, 32)
  23.  
  24. 'set button values
  25. bc = 1 'counter
  26. FOR row = 1 TO grid
  27.     FOR col = 1 TO grid
  28.         x = (row * size): y = (col * size)
  29.         bx(bc) = x - size: bx2(bc) = x ' generate x/y values
  30.         by(bc) = y - size: by2(bc) = y
  31.         bv(bc) = 0 'set to 0, button off
  32.         bc = bc + 1
  33.     NEXT
  34.  
  35. bv(INT(btns / 2)) = 1 'set one random button ON
  36.  
  37. updateboard
  38.  
  39. '==== MAIN GAME LOOP
  40.  
  41.  
  42.     _LIMIT 60 'to save CPU cycles - the program even starts faster on my XP virtual machine
  43.     WHILE _MOUSEINPUT: WEND 'reads mouse input in a loop to prevent lagging
  44.  
  45.         mx = _MOUSEX: my = _MOUSEY
  46.  
  47.         'handle button pressed
  48.         FOR t = 1 TO btns
  49.             IF mx >= bx(t) AND mx <= bx2(t) AND my >= by(t) AND my <= by2(t) THEN
  50.                 'change the clicked button
  51.                 IF bv(t) = 0 THEN bv(t) = 1 ELSE bv(t) = 0
  52.                 setbv "U", t 'now change 1 up above t
  53.                 setbv "D", t 'now change 1 down t
  54.                 setbv "L", t 'now change the left
  55.                 setbv "R", t 'now change the right
  56.                 updateboard
  57.             END IF
  58.         NEXT
  59.  
  60.         'wait until mouse button up to continue
  61.         WHILE _MOUSEBUTTON(1) <> 0: n = _MOUSEINPUT: WEND
  62.  
  63.     END IF
  64.  
  65.  
  66.  
  67. '==============================================
  68.  
  69. SUB updateboard ()
  70.     _DISPLAY: CLS
  71.     FOR b = 1 TO btns
  72.         IF bv(b) = 0 THEN
  73.             LINE (bx(b), by(b))-(bx2(b), by2(b)), _RGB(55, 55, 55), BF
  74.             LINE (bx(b), by(b))-(bx2(b), by2(b)), _RGB(88, 88, 88), B
  75.         ELSE
  76.             LINE (bx(b), by(b))-(bx2(b), by2(b)), _RGB(88, 88, 88), BF
  77.             LINE (bx(b), by(b))-(bx2(b), by2(b)), _RGB(55, 55, 55), B
  78.         END IF
  79.     NEXT
  80.  
  81. '==============================================
  82.  
  83. SUB setbv (way$, b)
  84.     'sets new button values.
  85.  
  86.     IF way$ = "U" THEN
  87.         FOR j = 1 TO btns STEP grid
  88.             IF b = j THEN EXIT SUB
  89.         NEXT
  90.         IF bv(b - 1) = 0 THEN bv(b - 1) = 1 ELSE bv(b - 1) = 0
  91.     END IF
  92.  
  93.     IF way$ = "D" THEN
  94.         FOR j = grid TO btns STEP grid
  95.             IF b = j THEN EXIT SUB
  96.         NEXT
  97.         IF bv(b + 1) = 0 THEN bv(b + 1) = 1 ELSE bv(b + 1) = 0
  98.     END IF
  99.  
  100.     IF way$ = "L" THEN
  101.         FOR j = 1 TO grid
  102.             IF b = j THEN EXIT SUB
  103.         NEXT j
  104.         IF bv(b - grid) = 0 THEN bv(b - grid) = 1 ELSE bv(b - grid) = 0
  105.     END IF
  106.  
  107.     IF way$ = "R" THEN
  108.         FOR j = btns - grid + 1 TO btns
  109.             IF b = j THEN EXIT SUB
  110.         NEXT j
  111.         IF bv(b + grid) = 0 THEN bv(b + grid) = 1 ELSE bv(b + grid) = 0
  112.     END IF
  113.  
  114.  
« Last Edit: September 10, 2018, 08:06:10 am by Dav »

FellippeHeitor

  • Guest
Re: FlipIt v1.0 - Light Out puzzle game
« Reply #1 on: September 09, 2018, 10:55:02 pm »
Nice sample, Dav. Two suggestions:

Code: QB64: [Select]
  1. '==== MAIN GAME LOOP
  2.  
  3.     _LIMIT 60 'to save CPU cycles - the program even starts faster on my XP virtual machine
  4.     WHILE _MOUSEINPUT: WEND 'reads mouse input in a loop to prevent lagging

The two new lines above made a world of a difference in my experience.

I did write one similar to this, but I ended up giving it a slightly different spin. Here's the code if you want to check it out: https://github.com/FellippeHeitor/LightsOn (Download here)
« Last Edit: September 09, 2018, 11:05:28 pm by FellippeHeitor »

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: FlipIt v1.0 - Light Out puzzle game
« Reply #2 on: September 10, 2018, 03:23:34 am »
This seems like a port of a MacOS terminal game '5x5'.
If you want to try that:
1. Get a Mac.
2. Open terminal
3. Type in 'emacs'
4. Press escape
5. Press x
6. Type 5x5 into the input field at the bottom.

Is this you game you based this off of Dav?

Zeppelin
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: FlipIt v1.0 - Light Out puzzle game
« Reply #3 on: September 10, 2018, 08:00:48 am »
Thanks, Fillippe! That works well.  I had tried _LIMIT 60 before, but it lagged horribly the way I was getting the mouse input so I removed it.  The way you put them together works very well and I will adopt that method.  Your game looks great!

Zeppelin: I think it is the same type game, originally called 5x5 according to the wikipedia. I first ran across it as called lights out, on a pc.

I have edited the code with Fillippes suggestions, and also added _DISPLAY/_AUTODISPLAY in the updateboard SUB to reduce any screen flickering.

- Dav

« Last Edit: September 10, 2018, 08:07:32 am by Dav »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FlipIt v1.0 - Light Out puzzle game
« Reply #4 on: September 10, 2018, 10:17:49 am »
Thanks Dav, I've not seen that game before. Really like these puzzle games!

Fellippe, wow! nice attention to details, sound, light bulb images, help at start...

FellippeHeitor

  • Guest
Re: FlipIt v1.0 - Light Out puzzle game
« Reply #5 on: September 10, 2018, 10:32:53 am »
@Dav: glad to hear it works well with the changes there too.

@bplus: thanks a lot!