日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

編程怎么做游戲 怎么編程游戲( 二 )


return blank_cell_idx + num_cols
復(fù)制代碼Step3:游戲主界面
OK,有了前面的鋪墊,我們可以開始實現(xiàn)我們的游戲主界面了 。
首先,我們需要打亂拼圖,但是隨機打亂很可能導(dǎo)致拼圖無解,因此我們通過隨機移動拼圖來實現(xiàn)打亂拼圖的效果,這也是我們先定義拼圖的移動操作的主要原因:
'''獲得打亂的拼圖'''
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
復(fù)制代碼
游戲主界面初始化:
最后實現(xiàn)主界面的顯示刷新以及事件響應(yīng)等功能:
while True:
game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells)
if not isGameOver(game_board, size):
break
# 游戲主循環(huán)
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)
# —-鼠標(biāo)操作
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)
# –判斷游戲是否結(jié)束
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)

推薦閱讀