1 подписчик
Хочешь сделать интересную игру на питоне?
Тебе сюда!
Код ниже запускает раскраску которую очень легко пройти, но ею можно удивить друзей!
from tkinter import *
from random import choice
SIDE = 17; SIZE = 47 # элементов, пикселей
COLORSCHEMES = ('#f00','#0f0','#00f','#ff0','#d7f','#988') # RGB
PLAYGROUND = [] # Цветное поле int
nextColor = oldColor = 0 # Новый цвет и цвет в левом верхнем углу
def play(self):
global nextColor, oldColor
n = self.x//SIZE + self.y//SIZE*SIDE
nextColor = PLAYGROUND[n]
oldColor = PLAYGROUND[0]
if nextColor != oldColor: # не запускать бесконечную рекурсию (масло маслянное)
zone(0, 0)
elif n == 0:
newGame()
doDraw()
def zone(x, y): # Author of this algorithm : Diorditsa A.
if x in range(0, SIDE) and y in range(0, SIDE):
global nextColor, oldColor
if PLAYGROUND[x+y*SIDE] == oldColor:
PLAYGROUND[x+y*SIDE] = nextColor
zone(x+1, y)
zone(x, y+1)
zone(x-1, y)
zone(x, y-1)
def doDraw():
cnv.delete('rctng')
for j in range(SIDE):
for i in range(SIDE):
cnv.create_rectangle(i*SIZE, j*SIZE, (i+1)*SIZE, (j+1)*SIZE,
fill=COLORSCHEMES[PLAYGROUND[(i)+(j*SIDE)]],
width=0, tag='rctng')
cnv.tkraise('txtStart')
def newGame():
PLAYGROUND.clear()
for i in range(SIDE*SIDE):
PLAYGROUND.append(choice(range(6)))
tk = Tk()
tk.title('Раскраска')
cnv = Canvas(width=SIDE*SIZE, height=SIDE*SIZE, bg='white')
cnv.pack(expand=YES, fill=BOTH)
cnv.bind('<Button-1>', play)
cnv.create_text(SIZE//2, SIZE//2, text='Start', anchor=CENTER, tag='txtStart')
newGame()
doDraw()
mainloop()
Ну вот и все! Прошу вас поставить лайк и подписаться!
1 минута
26 февраля 2024