return blank_cell_idx + num_cols
复制代码Step3:游戏主界面
OK,有了前面的铺垫,我们可以开始实现我们的游戏主界面了 。
首先,我们需要打乱拼图,但是随机打乱很可能导致拼图无解,因此我们通过随机移动拼图来实现打乱拼图的效果,这也是我们先定义拼图的移动操作的主要原因:
'''获得打乱的拼图'''
def CreateBoard(num_rows, num_cols, num_cells):
board = []
for i in range(num_cells): board.append(i)
# 去掉右下角那块
blank_cell_idx = num_cells – 1
board[blank_cell_idx] = -1
for i in range(cfg.NUMRANDOM):
# 0: left, 1: right, 2: up, 3: down
direction = random.randint(0, 3)
if direction == 0: blank_cell_idx = moveL(board, blank_cell_idx, num_cols)
elif direction == 1: blank_cell_idx = moveR(board, blank_cell_idx, num_cols)
elif direction == 2: blank_cell_idx = moveU(board, blank_cell_idx, num_rows, num_cols)
elif direction == 3: blank_cell_idx = moveD(board, blank_cell_idx, num_cols)
return board, blank_cell_idx
复制代码
游戏主界面初始化:
最后实现主界面的显示刷新以及事件响应等功能:
while True:
game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells)
if not isGameOver(game_board, size):
break
# 游戏主循环
is_running = True
while is_running:
# –事件捕获
for event in pygame.event.get():
# —-退出游戏
if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
# —-键盘操作
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)
elif event.key == pygame.K_RIGHT or event.key == ord('d'):
blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)
elif event.key == pygame.K_UP or event.key == ord('w'):
blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)
elif event.key == pygame.K_DOWN or event.key == ord('s'):
blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)
# —-鼠标操作
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = pygame.mouse.get_pos()
x_pos = x // cell_width
y_pos = y // cell_height
idx = x_pos + y_pos * num_cols
if idx == blank_cell_idx-1:
blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)
elif idx == blank_cell_idx+1:
blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)
elif idx == blank_cell_idx+num_cols:
blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)
elif idx == blank_cell_idx-num_cols:
blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)
# –判断游戏是否结束
if isGameOver(game_board, size):
game_board[blank_cell_idx] = num_cells – 1
is_running = False
# –更新屏幕
screen.fill(cfg.BACKGROUNDCOLOR)
for i in range(num_cells):
if game_board[i] == -1:
continue
x_pos = i // num_cols
y_pos = i % num_cols
rect = pygame.Rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height)
img_area = pygame.Rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height)
screen.blit(game_img_used, rect, img_area)
for i in range(num_cols+1):
pygame.draw.line(screen, cfg.BLACK, (i*cell_width, 0), (i*cell_width, game_img_used_rect.height))
for i in range(num_rows+1):
pygame.draw.line(screen, cfg.BLACK, (0, i*cell_height), (game_img_used_rect.width, i*cell_height))
pygame.display.update()
clock.tick(cfg.FPS)
推荐阅读
- 野生动物与自然保护区管理专业就业方向与就业前景怎么样
- 自然保护区建设与管理专业就业方向与就业前景怎么样
- 元气骑士怎么换人物,元气骑士怎么更换人物
- 元气骑士修改教程 怎么修改元气骑士文件
- 元气骑士皮肤全解锁 元气骑士皮肤怎么解锁
- 元气骑士武器怎么合成,元气骑士武器怎么合成?
- 元气骑士怎么修改文件 元气骑士文件修改教程
- 元气骑士怎么兑换礼包 元气骑士怎么兑换礼包码?
- 元气骑士找回云存档 元气骑士存档丢了该怎么找回
- 万国觉醒怎么解除未成年,万国觉醒已经绑定了未成年怎么改