from machine import Pin, SPI, SoftI2C
import ili9342c
import random
import focaltouch
class Button:
def __init__(self, x, y, breite, hoehe, color, display):
self.x = x
self.y = y
self.breite = breite
self.hoehe = hoehe
self.color = color
self.display = display
def setColor(self, color):
self.color = color
def getColor(self):
return self.color
def draw(self):
self.display.fill_rect(self.x, self.y, self.breite, self.hoehe, ili9342c.BLACK)
self.display.fill_rect(self.x+3, self.y+3, self.breite-6, self.hoehe-6, self.color)
def test(self, tx, ty):
if (tx >= self.x and tx <= self.x + self.breite) and (ty >= self.y and ty <= self.y + self.hoehe):
return True
return False
def checkWinner(btn_liste):
board = []
for btn in btn_liste:
board.append(btn.getColor())
kombinationen = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
]
for k in kombinationen:
print(k[0])
if ili9342c.WHITE not in board:
return "TIE"
return None
def tftinit():
spi = SPI(2,baudrate=60000000,sck=Pin(18),mosi=Pin(23)) # SPI init
tft = ili9342c.ILI9342C(spi, cs=Pin(5, Pin.OUT), dc=Pin(15, Pin.OUT), rst=Pin(33, Pin.OUT),width=320, height=240,rotation=270)
return tft
def main():
tft = tftinit()
tft.clear(ili9342c.WHITE)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
touch = focaltouch.FocalTouch(i2c)
liste = []
x_counter = 3
y_counter = 3
abstand = 10
hoehe = int((240-((y_counter-1)*abstand)) / y_counter)
breite = int((320-((x_counter-1)*abstand)) / x_counter)
x = 0
y = 0
for i in range(y_counter):
for j in range(x_counter):
x = j * (breite + abstand)
y = i * (hoehe + abstand)
liste.append(Button(x, y, breite, hoehe, ili9342c.WHITE, tft))
for btn in liste:
btn.draw()
player = 0
while 1:
if touch.touched == 1 and not pressed:
raw_x = touch.touches[0]['x']
raw_y = touch.touches[0]['y']
x = raw_y
y = 240 - raw_x
for btn in liste:
if btn.test(x, y):
if btn.getColor() == ili9342c.WHITE:
if player == 0:
btn.setColor(ili9342c.RED)
player = 1
else:
btn.setColor(ili9342c.BLUE)
player = 0
btn.draw()
checkWinner()
pressed = True
if touch.touched == 0:
pressed = False
if __name__ == "__main__":
main()
"""spi = SPI(2, baudrate=60000000, sck=Pin(18), mosi=Pin(23))
tft = ili9342c.ILI9342C(spi, cs=Pin(5, Pin.OUT), dc=Pin(15, Pin.OUT), rst=Pin(33, Pin.OUT),width=320, height=240,rotation=270)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
# Touch initialisieren
touch = focaltouch.FocalTouch(i2c)
n,m = 3,3
padding = 10
width = (320 - (n+1)*padding) // m
height = (240 - (m+1) * padding)//n
class Button:
def __init__(self, x, y, width, height, color, display):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.display = display
def setColor(self, c):
self.color = c
def getColor(self):
return self.color
def draw(self):
dsp = self.display
dsp.fill_rect(
self.x,
self.y,
self.width,
self.height,
ili9342c.BLACK
)
dsp.fill_rect(
self.x + 3,
self.y + 3,
self.width - 6,
self.height - 6,
self.color
)
def test(self, px, py):
if (px >= self.x and px <= self.x + self.width
and py >= self.y and py <= self.y + self.height):
return True
return False
def checkWinner():
board = []
for btn in b_list:
board.append(btn.getColor())
kombinationen = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
]
for k in kombinationen:
a = board[k[0]]
b = board[k[1]]
c = board[k[2]]
if a != ili9342c.WHITE and a == b and a == c:
if a == ili9342c.RED:
return "RED"
else:
return "BLUE"
full = True
for c in board:
if c == ili9342c.WHITE:
full = False
if full:
return "TIE"
return None
b_list = []
for row in range(n):
for col in range(m):
x = padding + col * (width + padding)
y = padding + row * (height + padding)
btn = Button(x, y, width, height, ili9342c.WHITE, tft)
b_list.append(btn)
tft.clear(ili9342c.WHITE)
for btn in b_list:
btn.draw()
player = 0
while 1:
if touch.touched == 1 and not pressed:
#x = touch.touches[0]['x']
#y = touch.touches[0]['y']
raw_x = touch.touches[0]['x']
raw_y = touch.touches[0]['y']
x = raw_y
y = 240 - raw_x
for btn in b_list:
if btn.test(x, y):
if btn.getColor() == ili9342c.WHITE:
if player == 0:
btn.setColor(ili9342c.BLUE)
player = 1
else:
btn.setColor(ili9342c.RED)
player = 0
btn.draw()
ergebnis = checkWinner()
if ergebnis:
tft.fill_rect(0, 100, 320, 40, ili9342c.WHITE)
balken_hoehe = 20
y = 100 + (40 - balken_hoehe) // 2
if ergebnis == "RED":
farbe = ili9342c.RED
elif ergebnis == "BLUE":
farbe = ili9342c.BLUE
else:
farbe = ili9342c.GREEN
# von links bis rechts über die komplette Breite
tft.fill_rect(
0, # x
y, # mittig im weißen Balken
320, # gesamte Breite
balken_hoehe, # halbe Höhe
farbe
)
pressed = True
if touch.touched == 0:
pressed = False
"""
"""from machine import Pin, SPI
import ili9342c
import random
class Rechteck:
def __init__(self, x, y, breite, hoehe, display):
self.x = x
self.y = y
self.breite = breite
self.hoehe = hoehe
self.display = display
def draw(self):
self.display.fill_rect(self.x, self.y, self.breite, self.hoehe, color=random.randint(0,65535))
self.display.rect(self.x, self.y, self.breite, self.hoehe, ili9342c.BLACK)
self.display.rect(self.x+1, self.y+1, self.breite-2, self.hoehe-2, ili9342c.BLACK)
def tftinit():
spi = SPI(2,baudrate=60000000,sck=Pin(18),mosi=Pin(23)) # SPI init
tft = ili9342c.ILI9342C(spi, cs=Pin(5, Pin.OUT), dc=Pin(15, Pin.OUT), rst=Pin(33, Pin.OUT),width=320, height=240,rotation=270)
return tft
def main():
tft = tftinit()
tft.clear(ili9342c.WHITE)
liste = []
x_counter = 3
y_counter = 3
abstand = 10
hoehe = int((240-((y_counter-1)*abstand)) / y_counter)
breite = int((320-((x_counter-1)*abstand)) / x_counter)
x = 0
y = 0
for i in range(y_counter):
for j in range(x_counter):
x = j * (breite + abstand)
y = i * (hoehe + abstand)
liste.append(Rechteck(x, y, breite, hoehe, tft))
for r in liste:
r.draw()
if __name__ == "__main__":
main()"""