Update the Pairs Game - use Timers, AddAttribute() & More functions instead of using Sleep() and QApp.ProcessEvents() ---> The code is more complex but it's now more portable and ready for RingQt WebAssembly (Under development - Will be ready for use in Ring 1.13)
@@ -203,6 +203,24 @@ app = new qApp {
start()
}
+ TimerMan2 = new qTimer(win) {
+ AddAttribute(self,:row)
+ AddAttribute(self,:col)
+ setInterval(500)
+ setTimeoutEvent("timerMan2()") ### ==>> Func
+ stop()
+ }
+
+ TimerMan3 = new qTimer(win) {
+ AddAttribute(self,:row)
+ AddAttribute(self,:col)
+ setInterval(500)
+ setTimeoutEvent("timerMan3()") ### ==>> Func
+ stop()
+ }
+
+
+
randomPairs()
show()
@@ -277,6 +295,7 @@ func randomPairs
func pairsClick Row,Col
+ if TimerMan2.isActive() or TimerMan3.isActive() return ok
click++
if click = 1
Pair1 = Pairs[Row][Col]
@@ -292,17 +311,48 @@ func pairsClick Row,Col
row2 = Row
col2 = Col
Button[Row][Col] { seticon(new qicon(new qpixmap(Pair2))) }
- app.processevents()
- sleep(0.5)
- Button[Row][Col] { seticon(new qicon(new qpixmap(C_COVER))) }
+
+ TimerMan2.row = row
+ TimerMan2.col = col
+ TimerMan2.start()
ok
+
+func TimerMan2
+
+ row = TimerMan2.row
+ col = TimerMan2.col
+ TimerMan2.stop()
+ Button[Row][Col] { seticon(new qicon(new qpixmap(C_COVER))) }
if (Pair1 = Pair2) and (click = 2) and not(row1=row2 and col1=col2)
PairOld = Pair1
Button[row1][col1] { seticon(new qicon(new qpixmap(Pair1))) }
Button[row2][col2] { seticon(new qicon(new qpixmap(Pair2))) }
- app.processevents()
- sleep(0.5)
+ TimerMan3.row = row
+ TimerMan3.col = col
+ TimerMan3.start()
+ return
+ else
+ Button[row1][col1] { seticon(new qicon(new qpixmap(C_COVER))) }
+ ok
+
+ if (row1=row2) and (col1=col2)
+ Button[row1][col1] { seticon(new qicon(new qpixmap(Pair1))) }
+ ok
+
+ if click = 2
+ click = 0
+ move++
+ labelShowMove.settext(string(move))
+ ok
+
+
+func TimerMan3
+
+ row = TimerMan3.row
+ col = TimerMan3.col
+ TimerMan3.stop()
+
Button[row1][col1] { seticon(new qicon(new qpixmap(C_EMPTY))) }
Button[row2][col2] { seticon(new qicon(new qpixmap(C_EMPTY))) }
Button[row1][col1].setenabled(false)
@@ -310,9 +360,7 @@ func pairsClick Row,Col
Pairs[row1][col1] = C_EMPTY
Pairs[row2][col2] = C_EMPTY
gameOver()
- else
- Button[row1][col1] { seticon(new qicon(new qpixmap(C_COVER))) }
- ok
+
if (row1=row2) and (col1=col2)
Button[row1][col1] { seticon(new qicon(new qpixmap(Pair1))) }
