Представляем версию игры Flip-Flop 2x4, написанную на Python с использованием библиотеки Tkinter. За чёрных играет королевский пингвин, за красных - перец с трезубцем.
В этой игре ведут борьбу кнопки с логотипом пингвин, как известно, олицетворяющим OS Linux и кнопки с логотипом OS FreeBSD (красный перец с рожками и с трезубцем). На рис.1 представлены наши герои.
Рис. 1.
#! /usr/bin/env python3
from tkinter import *
Tk()
mainloop()
Лист. 1. Программа.
Рис. 2. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
Tk()
Button().pack()
mainloop()
Лист. 2. Программа.
Рис. 3. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
Tk()
img = PhotoImage(file='PenguinLinux.png')
Button(image=img).pack()
mainloop()
Лист. 3. Программа.
Рис. 4. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
Tk()
img = PhotoImage(file='PenguinLinux.png')
for i in range(0, 8):
Button(image=img).pack()
mainloop()
Лист. 4. Программа.
Рис. 5. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
Tk()
img = PhotoImage(file='PenguinLinux.png')
for i in range(0, 8):
Button(image=img).pack(side=LEFT)
mainloop()
Лист. 5. Программа.
Рис. 6. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
Tk()
img = PhotoImage(file='PenguinLinux.png')
frm = [Frame(), Frame()]
for i in frm: i.pack()
for i in range(0, 8):
Button(frm[i//4], image=img).pack(side=LEFT)
mainloop()
Лист. 6. Программа.
Рис. 7. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
Tk()
img = [PhotoImage(file='PenguinLinux.png'), PhotoImage(file='FreeBSD.png')]
frm = [Frame(), Frame()]
for i in frm: i.pack()
for i in range(0, 8):
Button(frm[i//4], image=img[i//4]).pack(side=LEFT)
mainloop()
Лист. 7. Программа.
Рис. 8. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
playArea = [1,1,0,0,1,0,0,1]
Tk()
img = [PhotoImage(file='PenguinLinux.png'), PhotoImage(file='FreeBSD.png')]
frm = [Frame(), Frame()]
for i in frm: i.pack()
for i in range(0, 8):
Button(frm[i//4], image=img[playArea[i]]).pack(side=LEFT)
mainloop()
Лист. 8. Программа.
Рис. 9. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
playArea = [1,1,0,0,1,0,0,1]
Tk()
img = [PhotoImage(file='PenguinLinux.png'), PhotoImage(file='FreeBSD.png')]
frm = [Frame(), Frame()]
for i in frm: i.pack()
btn = [Button(frm[i//4], image=img[playArea[i]]) for i in range(8)]
for i in btn: i.pack(side=LEFT)
mainloop()
Лист. 9. Программа.
#! /usr/bin/env python3
from tkinter import *
playArea = [1,1,0,0,1,0,0,1]
def play(i):
global playArea
playArea[i]=not(playArea[i])
btn[i].config(image=img[playArea[i]])
Tk()
img = [PhotoImage(file='PenguinLinux.png'), PhotoImage(file='FreeBSD.png')]
frm = [Frame(), Frame()]
for i in frm: i.pack()
btn = [Button(frm[i//4], image=img[playArea[i]], command=lambda n=i: play(n)) for i in range(8)]
for i in btn: i.pack(side=LEFT)
mainloop()
Лист. 10. Программа.
Рис. 10. Игра Flip-Flop
#! /usr/bin/env python3
from tkinter import *
playArea = [1,1,0,0,1,0,0,1]
def play(i):
global playArea
playArea[i]=not(playArea[i])
for j in range((not(i//4))*4, (not(i//4))*4+4):
playArea[j]=not(playArea[j])
for i in range(8): btn[i].config(image=img[playArea[i]])
Tk()
Button(text='New Game').pack()
img = [PhotoImage(file='PenguinLinux.png'), PhotoImage(file='FreeBSD.png')]
frm = [Frame(), Frame()]
for i in frm: i.pack()
btn = [Button(frm[i//4], image=img[playArea[i]], command=lambda n=i: play(n)) for i in range(8)]
for i in btn: i.pack(side=LEFT)
mainloop()
Лист. 11. Программа.
Рис. 11. Игра Flip-Flop
#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
#
# LinuxORfreeBSD.py
# Copyright (C) 2021 Aleksandr Diorditsa, see <https://adior.ru>
# I want to thank all my students for the inspiration they give me.
#
# LinuxORfreeBSD.py is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LinuxORfreeBSD.py is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
from tkinter import *
from random import shuffle
def play(i):
global playArea
playArea[i]=not(playArea[i])
for j in range((not(i//4))*4, (not(i//4))*4+4):
playArea[j]=not(playArea[j])
for i in range(8): btn[i].config(image=img[playArea[i]])
def newGame():
global playArea
playArea = [1,1,0,0,1,0,0,1]
shuffle(playArea)
for i in range(8): btn[i].config(image=img[playArea[i]])
Tk()
Button(text='New Game', command=newGame).pack()
img = [PhotoImage(file='PenguinLinux.png'), PhotoImage(file='FreeBSD.png')]
frm = [Frame(), Frame()]
for i in frm: i.pack()
btn = [Button(frm[i//4], command=lambda n=i: play(n)) for i in range(8)]
for i in btn: i.pack(side=LEFT)
newGame()
mainloop()
Лист. 12. Программа.
Рис. 12. Игра Flip-Flop