Toggle Navigation
Hatchery
Eggs
Score counter
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import display, mpr121, machine, keyboard, system, easydraw, time player1Name = machine.nvs_getstr("score_counter", "player1Name") player2Name = machine.nvs_getstr("score_counter", "player2Name") if not player1Name: player1Name = "Player 1" if not player2Name: player2Name = "Player 2" score = [] score.append(machine.nvs_getint("score_counter", "player1InitialScore")) score.append(machine.nvs_getint("score_counter", "player2InitialScore")) if not score[0]: score[0] = 0 if not score[1]: score[1] = 0 print(player1Name, player2Name, score) scoreHeight = display.getTextHeight("0","7x5")*5 scoreY = (display.height()-scoreHeight)//2 playerHeight = display.getTextHeight("0","7x5")*3 currHeight = display.getTextHeight("0","7x5")*3 def calcX(text, position, scale=1): playerWidth = display.getTextWidth(text,"7x5")*scale x = (display.width()//4)-(playerWidth//2) if position>0: x += display.width()//2 return x def drawPlayer(player, score, position, selected): color = 0x000000 if selected: color = 0xFFFFFF rectX = 0 if position > 0: rectX = display.width()//2 display.drawRect(rectX, 0, display.width()//2, display.height(), True, 0x000000) display.drawText(calcX(str(score), position, 5),scoreY,str(score),color,"7x5", 5, 5) display.drawText( calcX(player, position, 3), 0, player, color, "7x5", 3, 3) display.drawLine((display.width()//2)*position, playerHeight+4, (display.width()//2)*(position+1), playerHeight+4, color) def drawMenu(amount, position, selected, currentChange): color = 0x000000 if selected: color = 0xFFFFFF if amount < 0: amount = str(amount) elif amount == 0: amount = "-" else: amount = "+"+str(amount) display.drawLine((display.width()//2)*position, display.height()-currHeight-8, (display.width()//2)*(position+1), display.height()-currHeight-8, color) display.drawText( calcX(amount, position, 3), display.height()-currHeight-4, amount, color, "7x5", 3, 3) def draw(player1, player2, score1, score2, currentPlayer, currentChange): display.drawFill() drawPlayer(player1, score1, 0, currentPlayer == 0) drawPlayer(player2, score2, 1, currentPlayer == 1) drawMenu(currentChange, currentPlayer, True, currentChange) display.flush(display.FLAG_LUT_FASTEST) currentPlayer = 0 currentChange = 0 def updateScreen(): global score, player1Name, player2Name, currentPlayer, currentChange draw(player1Name, player2Name, score[0], score[1], currentPlayer, currentChange) def onA(pressed): global score, currentChange, currentPlayer if pressed: score[currentPlayer] += currentChange currentPlayer += 1 if currentPlayer > 1: currentPlayer = 0 currentChange = 0 updateScreen() def onDown(pressed): global currentChange if pressed: currentChange -= 1 updateScreen() def onUp(pressed): global currentChange if pressed: currentChange += 1 updateScreen() def onLeft(pressed): global currentChange if pressed: currentChange -= 10 updateScreen() def onRight(pressed): global currentChange if pressed: currentChange += 10 updateScreen() def onB(pressed): pass def onStart(pressed): system.launcher() def onSave2(newName): global player1Name, player2Name if newName: player2Name = newName machine.nvs_setstr("score_counter", "player1Name", player1Name) machine.nvs_setstr("score_counter", "player2Name", player2Name) print(player1Name, player2Name) easydraw.messageCentered("Player names saved!") time.sleep(2) attachKeys() updateScreen() def onSave1(newName): global player1Name, player2Name if newName: player1Name = newName keyboard.show("Player 2", player2Name, onSave2) def onSelect(pressed): global player1Name keyboard.show("Player 1", player1Name, onSave1) def attachKeys(): mpr121.attach(0, onA) mpr121.attach(1, onB) mpr121.attach(2, onStart) mpr121.attach(3, onSelect) mpr121.attach(4, onDown) mpr121.attach(5, onRight) mpr121.attach(6, onUp) mpr121.attach(7, onLeft) # Initial refresh: attachKeys() updateScreen()