Python Pygame Implementations of Classic Games: 2048, Snake, Tetris, and LianLianKan
This article provides step-by-step tutorials and complete Python Pygame source code for implementing four classic games—2048, Snake, Tetris, and LianLianKan—explaining game mechanics, design principles, and code structure to help Python developers build and understand these interactive applications.
This tutorial introduces four classic games implemented with Python's Pygame (and Tkinter) library, offering conceptual explanations and full source code for each.
1. 2048 – A popular sliding‑tile puzzle where numbers combine when moved left, right, up or down. The article describes the 4×4 grid representation, the logic for merging tiles, and provides a complete Python script that defines a Map class, movement methods, and the game loop.
<pre>
import random
import sys
import pygame
from pygame.locals import *
PIXEL = 150
SCORE_PIXEL = 100
SIZE = 4
class Map:
def __init__(self, size):
self.size = size
self.score = 0
self.map = [[0 for i in range(size)] for i in range(size)]
self.add()
self.add()
# 新增2或4,有1/4概率产生4
def add(self):
while True:
p = random.randint(0, self.size * self.size - 1)
if self.map[int(p / self.size)][int(p % self.size)] == 0:
x = random.randint(0, 3) > 0 and 2 or 4
self.map[int(p / self.size)][int(p % self.size)] = x
self.score += x
break
# 地图向左靠拢,其他方向的靠拢可以通过适当旋转实现,返回地图是否更新
def adjust(self):
changed = False
for a in self.map:
b = []
last = 0
for v in a:
if v != 0:
if v == last:
b.append(b.pop() << 1)
last = 0
else:
b.append(v)
last = v
b += [0] * (self.size - len(b))
for i in range(self.size):
if a[i] != b[i]:
changed = True
a[:] = b
return changed
# 逆时针旋转地图90度
def rotate90(self):
self.map = [[self.map[c][r] for c in range(self.size)] for r in reversed(range(self.size))]
# 判断游戏结束
def over(self):
for r in range(self.size):
for c in range(self.size):
if self.map[r][c] == 0:
return False
for r in range(self.size):
for c in range(self.size - 1):
if self.map[r][c] == self.map[r][c + 1]:
return False
for r in range(self.size - 1):
for c in range(self.size):
if self.map[r][c] == self.map[r + 1][c]:
return False
return True
def moveUp(self):
self.rotate90()
if self.adjust():
self.add()
self.rotate90()
self.rotate90()
self.rotate90()
def moveRight(self):
self.rotate90()
self.rotate90()
if self.adjust():
self.add()
self.rotate90()
self.rotate90()
def moveDown(self):
self.rotate90()
self.rotate90()
self.rotate90()
if self.adjust():
self.add()
self.rotate90()
def moveLeft(self):
if self.adjust():
self.add()
def show(map):
for i in range(SIZE):
for j in range(SIZE):
screen.blit(map.map[i][j] == 0 and block[(i + j) % 2] or block[2 + (i + j) % 2], (PIXEL * j, PIXEL * i))
if map.map[i][j] != 0:
map_text = map_font.render(str(map.map[i][j]), True, (106, 90, 205))
text_rect = map_text.get_rect()
text_rect.center = (PIXEL * j + PIXEL / 2, PIXEL * i + PIXEL / 2)
screen.blit(map_text, text_rect)
screen.blit(score_block, (0, PIXEL * SIZE))
score_text = score_font.render((map.over() and "Game over with score " or "Score: ") + str(map.score), True, (106, 90, 205))
score_rect = score_text.get_rect()
score_rect.center = (PIXEL * SIZE / 2, PIXEL * SIZE + SCORE_PIXEL / 2)
screen.blit(score_text, score_rect)
pygame.display.update()
map = Map(SIZE)
pygame.init()
screen = pygame.display.set_mode((PIXEL * SIZE, PIXEL * SIZE + SCORE_PIXEL))
pygame.display.set_caption("2048")
block = [pygame.Surface((PIXEL, PIXEL)) for i in range(4)]
block[0].fill((152, 251, 152))
block[1].fill((240, 255, 255))
block[2].fill((0, 255, 127))
block[3].fill((225, 255, 255))
score_block = pygame.Surface((PIXEL * SIZE, SCORE_PIXEL))
score_block.fill((245, 245, 245))
map_font = pygame.font.Font(None, int(PIXEL * 2 / 3))
score_font = pygame.font.Font(None, int(SCORE_PIXEL * 2 / 3))
clock = pygame.time.Clock()
show(map)
while not map.over():
clock.tick(12)
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
pressed_keys = pygame.key.get_pressed()
if pressed_keys[K_w] or pressed_keys[K_UP]:
map.moveUp()
elif pressed_keys[K_s] or pressed_keys[K_DOWN]:
map.moveDown()
elif pressed_keys[K_a] or pressed_keys[K_LEFT]:
map.moveLeft()
elif pressed_keys[K_d] or pressed_keys[K_RIGHT]:
map.moveRight()
show(map)
pygame.time.delay(3000)
</pre>2. Snake – The classic snake game where the player controls a growing line that eats food. The design points cover screen resolution, block size, score handling, and collision detection. The full source code includes the Snake class, direction handling, food generation, and the main loop.
<pre>
import random
import sys
import pygame
from pygame.locals import *
PIXEL = 150
SCORE_PIXEL = 100
SIZE = 4
class Map:
def __init__(self, size):
self.size = size
self.score = 0
self.map = [[0 for i in range(size)] for i in range(size)]
self.add()
self.add()
def add(self):
while True:
p = random.randint(0, self.size * self.size - 1)
if self.map[int(p / self.size)][int(p % self.size)] == 0:
x = random.randint(0, 3) > 0 and 2 or 4
self.map[int(p / self.size)][int(p % self.size)] = x
self.score += x
break
def adjust(self):
changed = False
for a in self.map:
b = []
last = 0
for v in a:
if v != 0:
if v == last:
b.append(b.pop() << 1)
last = 0
else:
b.append(v)
last = v
b += [0] * (self.size - len(b))
for i in range(self.size):
if a[i] != b[i]:
changed = True
a[:] = b
return changed
def rotate90(self):
self.map = [[self.map[c][r] for c in range(self.size)] for r in reversed(range(self.size))]
def over(self):
for r in range(self.size):
for c in range(self.size):
if self.map[r][c] == 0:
return False
for r in range(self.size):
for c in range(self.size - 1):
if self.map[r][c] == self.map[r][c + 1]:
return False
for r in range(self.size - 1):
for c in range(self.size):
if self.map[r][c] == self.map[r + 1][c]:
return False
return True
def moveUp(self):
self.rotate90()
if self.adjust():
self.add()
self.rotate90()
self.rotate90()
self.rotate90()
def moveRight(self):
self.rotate90()
self.rotate90()
if self.adjust():
self.add()
self.rotate90()
self.rotate90()
def moveDown(self):
self.rotate90()
self.rotate90()
self.rotate90()
if self.adjust():
self.add()
self.rotate90()
def moveLeft(self):
if self.adjust():
self.add()
def show(map):
for i in range(SIZE):
for j in range(SIZE):
screen.blit(map.map[i][j] == 0 and block[(i + j) % 2] or block[2 + (i + j) % 2], (PIXEL * j, PIXEL * i))
if map.map[i][j] != 0:
map_text = map_font.render(str(map.map[i][j]), True, (106, 90, 205))
text_rect = map_text.get_rect()
text_rect.center = (PIXEL * j + PIXEL / 2, PIXEL * i + PIXEL / 2)
screen.blit(map_text, text_rect)
screen.blit(score_block, (0, PIXEL * SIZE))
score_text = score_font.render((map.over() and "Game over with score " or "Score: ") + str(map.score), True, (106, 90, 205))
score_rect = score_text.get_rect()
score_rect.center = (PIXEL * SIZE / 2, PIXEL * SIZE + SCORE_PIXEL / 2)
screen.blit(score_text, score_rect)
pygame.display.update()
map = Map(SIZE)
pygame.init()
screen = pygame.display.set_mode((PIXEL * SIZE, PIXEL * SIZE + SCORE_PIXEL))
pygame.display.set_caption("2048")
block = [pygame.Surface((PIXEL, PIXEL)) for i in range(4)]
block[0].fill((152, 251, 152))
block[1].fill((240, 255, 255))
block[2].fill((0, 255, 127))
block[3].fill((225, 255, 255))
score_block = pygame.Surface((PIXEL * SIZE, SCORE_PIXEL))
score_block.fill((245, 245, 245))
map_font = pygame.font.Font(None, int(PIXEL * 2 / 3))
score_font = pygame.font.Font(None, int(SCORE_PIXEL * 2 / 3))
clock = pygame.time.Clock()
show(map)
while not map.over():
clock.tick(12)
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
pressed_keys = pygame.key.get_pressed()
if pressed_keys[K_w] or pressed_keys[K_UP]:
map.moveUp()
elif pressed_keys[K_s] or pressed_keys[K_DOWN]:
map.moveDown()
elif pressed_keys[K_a] or pressed_keys[K_LEFT]:
map.moveLeft()
elif pressed_keys[K_d] or pressed_keys[K_RIGHT]:
map.moveRight()
show(map)
pygame.time.delay(3000)
</pre>3. Tetris – The falling‑block puzzle with seven tetromino shapes. The article explains the grid layout, shape definitions, rotation logic, line clearing, and scoring. The provided script implements the CubeShape class, drawing functions, and the main game loop.
<pre>
import pygame
import random
import os
pygame.init()
GRID_WIDTH = 20
GRID_NUM_WIDTH = 15
GRID_NUM_HEIGHT = 25
WIDTH, HEIGHT = GRID_WIDTH * GRID_NUM_WIDTH, GRID_WIDTH * GRID_NUM_HEIGHT
SIDE_WIDTH = 200
SCREEN_WIDTH = WIDTH + SIDE_WIDTH
WHITE = (0xff, 0xff, 0xff)
BLACK = (0, 0, 0)
LINE_COLOR = (0x33, 0x33, 0x33)
CUBE_COLORS = [
(0xcc, 0x99, 0x99), (0xff, 0xff, 0x99), (0x66, 0x66, 0x99),
(0x99, 0x00, 0x66), (0xff, 0xcc, 0x00), (0xcc, 0x00, 0x33),
(0xff, 0x00, 0x33), (0x00, 0x66, 0x99), (0xff, 0xff, 0x33),
(0x99, 0x00, 0x33), (0xcc, 0xff, 0x66), (0xff, 0x99, 0x00)
]
screen = pygame.display.set_mode((SCREEN_WIDTH, HEIGHT))
pygame.display.set_caption("俄罗斯方块")
clock = pygame.time.Clock()
FPS = 30
score = 0
level = 1
screen_color_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]
base_folder = os.path.dirname(__file__)
def show_text(surf, text, size, x, y, color=WHITE):
font_name = os.path.join(base_folder, 'font/font.ttc')
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
class CubeShape(object):
SHAPES = ['I', 'J', 'L', 'O', 'S', 'T', 'Z']
I = [[(0, -1), (0, 0), (0, 1), (0, 2)],
[(-1, 0), (0, 0), (1, 0), (2, 0)]]
J = [[(-2, 0), (-1, 0), (0, 0), (0, -1)],
[(-1, 0), (0, 0), (0, 1), (0, 2)],
[(0, 1), (0, 0), (1, 0), (2, 0)],
[(0, -2), (0, -1), (0, 0), (1, 0)]]
L = [[(-2, 0), (-1, 0), (0, 0), (0, 1)],
[(1, 0), (0, 0), (0, 1), (0, 2)],
[(0, -1), (0, 0), (1, 0), (2, 0)],
[(0, -2), (0, -1), (0, 0), (-1, 0)]]
O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]
S = [[(-1, 0), (0, 0), (0, 1), (1, 1)],
[(1, -1), (1, 0), (0, 0), (0, 1)]]
T = [[(0, -1), (0, 0), (0, 1), (-1, 0)],
[(-1, 0), (0, 0), (1, 0), (0, 1)],
[(0, -1), (0, 0), (0, 1), (1, 0)],
[(-1, 0), (0, 0), (1, 0), (0, -1)]]
Z = [[(0, -1), (0, 0), (1, 0), (1, 1)],
[(-1, 0), (0, 0), (0, -1), (1, -1)]]
SHAPES_WITH_DIR = {'I': I, 'J': J, 'L': L, 'O': O, 'S': S, 'T': T, 'Z': Z}
def __init__(self):
self.shape = self.SHAPES[random.randint(0, len(self.SHAPES) - 1)]
self.center = (2, GRID_NUM_WIDTH // 2)
self.dir = random.randint(0, len(self.SHAPES_WITH_DIR[self.shape]) - 1)
self.color = CUBE_COLORS[random.randint(0, len(CUBE_COLORS) - 1)]
def get_all_gridpos(self, center=None):
curr_shape = self.SHAPES_WITH_DIR[self.shape][self.dir]
if center is None:
center = [self.center[0], self.center[1]]
return [(cube[0] + center[0], cube[1] + center[1]) for cube in curr_shape]
def conflict(self, center):
for cube in self.get_all_gridpos(center):
if cube[0] < 0 or cube[1] < 0 or cube[0] >= GRID_NUM_HEIGHT or cube[1] >= GRID_NUM_WIDTH:
return True
if screen_color_matrix[cube[0]][cube[1]] is not None:
return True
return False
def rotate(self):
new_dir = (self.dir + 1) % len(self.SHAPES_WITH_DIR[self.shape])
old_dir = self.dir
self.dir = new_dir
if self.conflict(self.center):
self.dir = old_dir
return False
return True
def down(self):
center = (self.center[0] + 1, self.center[1])
if self.conflict(center):
return False
self.center = center
return True
def left(self):
center = (self.center[0], self.center[1] - 1)
if self.conflict(center):
return False
self.center = center
return True
def right(self):
center = (self.center[0], self.center[1] + 1)
if self.conflict(center):
return False
self.center = center
return True
def draw(self):
for cube in self.get_all_gridpos():
pygame.draw.rect(screen, self.color, (cube[1] * GRID_WIDTH, cube[0] * GRID_WIDTH, GRID_WIDTH, GRID_WIDTH))
pygame.draw.rect(screen, WHITE, (cube[1] * GRID_WIDTH, cube[0] * GRID_WIDTH, GRID_WIDTH, GRID_WIDTH), 1)
def draw_grids():
for i in range(GRID_NUM_WIDTH):
pygame.draw.line(screen, LINE_COLOR, (i * GRID_WIDTH, 0), (i * GRID_WIDTH, HEIGHT))
for i in range(GRID_NUM_HEIGHT):
pygame.draw.line(screen, LINE_COLOR, (0, i * GRID_WIDTH), (WIDTH, i * GRID_WIDTH))
pygame.draw.line(screen, WHITE, (GRID_WIDTH * GRID_NUM_WIDTH, 0), (GRID_WIDTH * GRID_NUM_WIDTH, GRID_WIDTH * GRID_NUM_HEIGHT))
def draw_matrix():
for i, row in enumerate(screen_color_matrix):
for j, color in enumerate(row):
if color is not None:
pygame.draw.rect(screen, color, (j * GRID_WIDTH, i * GRID_WIDTH, GRID_WIDTH, GRID_WIDTH))
pygame.draw.rect(screen, WHITE, (j * GRID_WIDTH, i * GRID_WIDTH, GRID_WIDTH, GRID_WIDTH), 2)
def draw_score():
show_text(screen, u'得分:{}'.format(score), 20, WIDTH + SIDE_WIDTH // 2, 100)
def remove_full_line():
global screen_color_matrix, score, level
new_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]
index = GRID_NUM_HEIGHT - 1
n_full_line = 0
for i in range(GRID_NUM_HEIGHT - 1, -1, -1):
is_full = True
for j in range(GRID_NUM_WIDTH):
if screen_color_matrix[i][j] is None:
is_full = False
break
if not is_full:
new_matrix[index] = screen_color_matrix[i]
index -= 1
else:
n_full_line += 1
score += n_full_line
level = score // 20 + 1
screen_color_matrix = new_matrix
def show_welcome(screen):
show_text(screen, u'俄罗斯方块', 30, WIDTH / 2, HEIGHT / 2)
show_text(screen, u'按任意键开始游戏', 20, WIDTH / 2, HEIGHT / 2 + 50)
running = True
gameover = True
counter = 0
live_cube = None
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if gameover:
gameover = False
live_cube = CubeShape()
break
if event.key == pygame.K_LEFT:
live_cube.left()
elif event.key == pygame.K_RIGHT:
live_cube.right()
elif event.key == pygame.K_DOWN:
live_cube.down()
elif event.key == pygame.K_UP:
live_cube.rotate()
elif event.key == pygame.K_SPACE:
while live_cube.down() == True:
pass
remove_full_line()
if not gameover and counter % (FPS // level) == 0:
if not live_cube.down():
for cube in live_cube.get_all_gridpos():
screen_color_matrix[cube[0]][cube[1]] = live_cube.color
live_cube = CubeShape()
if live_cube.conflict(live_cube.center):
gameover = True
score = 0
live_cube = None
screen_color_matrix = [[None] * GRID_NUM_WIDTH for i in range(GRID_NUM_HEIGHT)]
remove_full_line()
counter += 1
screen.fill(BLACK)
draw_grids()
draw_matrix()
draw_score()
if live_cube is not None:
live_cube.draw()
if gameover:
show_welcome(screen)
pygame.display.update()
</pre>4. LianLianKan – A tile‑matching puzzle where two identical tiles can be removed if they can be connected with at most two bends. The design covers map generation, link‑checking algorithms (direct, one‑corner, two‑corner), and user interaction using Tkinter. Complete source code is provided.
<pre>
from tkinter import *
from tkinter.messagebox import *
from threading import Timer
import time
import random
class Point:
# 点类
def __init__(self, x, y):
self.x = x
self.y = y
'''
判断选中的两个方块是否可以消除
'''
def IsLink(p1, p2):
if lineCheck(p1, p2):
return True
if OneCornerLink(p1, p2): # 一个转弯(折点)的联通方式
return True
if TwoCornerLink(p1, p2): # 两个转弯(折点)的联通方式
return True
return False
# ---------------------------
def IsSame(p1, p2):
if map[p1.x][p1.y] == map[p2.x][p2.y]:
print("clicked at IsSame")
return True
return False
def callback(event): # 鼠标左键事件代码
global Select_first, p1, p2
global firstSelectRectId, SecondSelectRectId
x = (event.x) // 40 # 换算棋盘坐标
y = (event.y) // 40
print("clicked at", x, y)
if map[x][y] == " ":
showinfo(title="提示", message="此处无方块")
else:
if Select_first == False:
p1 = Point(x, y)
firstSelectRectId = cv.create_rectangle(x * 40, y * 40, x * 40 + 40, y * 40 + 40, width=2, outline="blue")
Select_first = True
else:
p2 = Point(x, y)
if (p1.x == p2.x) and (p1.y == p2.y):
return
SecondSelectRectId = cv.create_rectangle(x * 40, y * 40, x * 40 + 40, y * 40 + 40, width=2, outline="yellow")
cv.pack()
if IsSame(p1, p2) and IsLink(p1, p2):
print('连通', x, y)
Select_first = False
drawLinkLine(p1, p2)
t = Timer(timer_interval, delayrun) # 定时函数
t.start()
else:
cv.delete(firstSelectRectId)
cv.delete(SecondSelectRectId)
Select_first = False
timer_interval = 0.3 # 0.3秒
# --------------------------------------
def delayrun():
clearTwoBlock() # 清除连线及方块
def clearTwoBlock(): # 清除连线及方块
cv.delete(firstSelectRectId)
cv.delete(SecondSelectRectId)
map[p1.x][p1.y] = " "
cv.delete(image_map[p1.x][p1.y])
map[p2.x][p2.y] = " "
cv.delete(image_map[p2.x][p2.y])
Select_first = False
undrawConnectLine() # 清除选中方块之间连接线
def drawQiPan(): # 画棋盘
for i in range(0, 15):
cv.create_line(20, 20 + 40 * i, 580, 20 + 40 * i, width=2)
for i in range(0, 15):
cv.create_line(20 + 40 * i, 20, 20 + 40 * i, 580, width=2)
cv.pack()
def print_map(): # 输出map地图
global image_map
for x in range(0, Width): # 0--14
for y in range(0, Height): # 0--14
if (map[x][y] != ' '):
img1 = imgs[int(map[x][y])]
id = cv.create_image((x * 40 + 20, y * 40 + 20), image=img1)
image_map[x][y] = id
cv.pack()
for y in range(0, Height): # 0--14
for x in range(0, Width): # 0--14
print(map[x][y], end=' ')
print(",", y)
'''直角连接,即X,Y坐标都不同的,可以用这个方法尝试连接'''
def OneCornerLink(p1, p2):
checkP = Point(p1.x, p2.y)
checkP2 = Point(p2.x, p1.y)
if (map[checkP.x][checkP.y] == " "):
if (lineCheck(p1, checkP) and lineCheck(checkP, p2)):
linePointStack.append(checkP)
print("直角消除ok", checkP.x, checkP.y)
return True
if (map[checkP2.x][checkP2.y] == " "):
if (lineCheck(p1, checkP2) and lineCheck(checkP2, p2)):
linePointStack.append(checkP2)
print("直角消除ok", checkP2.x, checkP2.y)
return True
print("不能直角消除")
return False
'''双直角连接方法'''
def TwoCornerLink(p1, p2):
checkP = Point(p1.x, p1.y)
for i in range(0, 4):
checkP.x = p1.x
checkP.y = p1.y
if (i == 3): # 向下
checkP.y += 1
while ((checkP.y < Height) and map[checkP.x][checkP.y] == " "):
linePointStack.append(checkP)
if (OneCornerLink(checkP, p2)):
print("下探测OK")
return True
else:
linePointStack.pop()
checkP.y += 1
if checkP.y == Height:
z = Point(p2.x, Height - 1) # 底部边界点
if lineCheck(z, p2):
linePointStack.append(Point(p1.x, Height))
linePointStack.append(Point(p2.x, Height))
print("下探测到游戏区域外部OK")
return True
elif (i == 2): # 向右
checkP.x += 1
while ((checkP.x < Width) and map[checkP.x][checkP.y] == " "):
linePointStack.append(checkP)
if (OneCornerLink(checkP, p2)):
print("右探测OK")
return True
else:
linePointStack.pop()
checkP.x += 1
if checkP.x == Width:
z = Point(Width - 1, p2.y) # 右部边界点
if lineCheck(z, p2):
linePointStack.append(Point(Width, p1.y))
linePointStack.append(Point(Width, p2.y))
print("右探测到游戏区域外部OK")
return True
elif (i == 1): # 向左
checkP.x -= 1
while ((checkP.x >= 0) and map[checkP.x][checkP.y] == " "):
linePointStack.append(checkP)
if (OneCornerLink(checkP, p2)):
print("左探测OK")
return True
else:
linePointStack.pop()
checkP.x -= 1
elif (i == 0): # 向上
checkP.y -= 1
while ((checkP.y >= 0) and map[checkP.x][checkP.y] == " "):
linePointStack.append(checkP)
if (OneCornerLink(checkP, p2)):
print("上探测OK")
return True
else:
linePointStack.pop()
checkP.y -= 1
print("两直角连接没找到适合的checkP点")
return False
# 画连接线
def drawLinkLine(p1, p2):
if (len(linePointStack) == 0):
Line_id.append(drawLine(p1, p2))
else:
print(linePointStack, len(linePointStack))
if (len(linePointStack) == 1):
z = linePointStack.pop()
print("一折连通点z", z.x, z.y)
Line_id.append(drawLine(p1, z))
Line_id.append(drawLine(p2, z))
if (len(linePointStack) == 2):
z1 = linePointStack.pop()
print("2折连通点z1", z1.x, z1.y)
Line_id.append(drawLine(p2, z1))
z2 = linePointStack.pop()
print("2折连通点z2", z2.x, z2.y)
Line_id.append(drawLine(z1, z2))
Line_id.append(drawLine(p1, z2))
# 删除连接线
def undrawConnectLine():
while len(Line_id) > 0:
idpop = Line_id.pop()
cv.delete(idpop)
def drawLine(p1, p2):
print("drawLine p1,p2", p1.x, p1.y, p2.x, p2.y)
id = cv.create_line(p1.x * 40 + 20, p1.y * 40 + 20, p2.x * 40 + 20, p2.y * 40 + 20, width=5, fill='red')
return id
# 产生map地图
def create_map():
global map
tmpMap = []
m = (Width) * (Height) // 10
print('m=', m)
for x in range(0, m):
for i in range(0, 10): # 每种方块有10个
tmpMap.append(x)
random.shuffle(tmpMap)
for x in range(0, Width): # 0--14
for y in range(0, Height): # 0--14
map[x][y] = tmpMap[x * Height + y]
# 自动查找
def find2Block(event):
global firstSelectRectId, SecondSelectRectId
m_nRoW = Height
m_nCol = Width
bFound = False
for i in range(0, m_nRoW * m_nCol):
if bFound:
break
x1 = i % m_nCol
y1 = i // m_nCol
p1 = Point(x1, y1)
if (map[x1][y1] == ' '):
continue
for j in range(i + 1, m_nRoW * m_nCol):
x2 = j % m_nCol
y2 = j // m_nCol
p2 = Point(x2, y2)
if (map[x2][y2] != ' ' and IsSame(p1, p2)):
if (IsLink(p1, p2)):
bFound = True
break
if (bFound):
print('找到后', p1.x, p1.y, p2.x, p2.y)
firstSelectRectId = cv.create_rectangle(x1 * 40, y1 * 40, x1 * 40 + 40, y1 * 40 + 40, width=2, outline="red")
secondSelectRectId = cv.create_rectangle(x2 * 40, y2 * 40, x2 * 40 + 40, y2 * 40 + 40, width=2, outline="red")
return bFound
root = Tk()
root.title("Python连连看 ")
imgs = [PhotoImage(file='images\\bar_0' + str(i) + '.gif') for i in range(0, 10)]
Select_first = False
firstSelectRectId = -1
SecondSelectRectId = -1
clearFlag = False
linePointStack = []
Line_id = []
Height = 10
Width = 10
map = [[" " for y in range(Height)] for x in range(Width)]
image_map = [[" " for y in range(Height)] for x in range(Width)]
cv = Canvas(root, bg='green', width=440, height=440)
cv.bind("<Button-1>", callback) # 鼠标左键事件
cv.bind("<Button-3>", find2Block) # 鼠标右键事件
cv.pack()
create_map() # 产生map地图
print_map() # 打印map地图
root.mainloop()
</pre>Each section includes example screenshots (omitted here) and can be run directly after installing the required libraries, making the guide suitable for beginners with basic Python knowledge who want to practice game development.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.