💦前言

大家好,是我:
一大早高清無碼好東西剛到手
就給大家安排推文的栗子同學上線·放牛小編——一切隨緣更新+慢更新的我!
決心痛改前非,每周爭取能多寫幾篇,(這個flag立在這里,完不成......那就算了!)
大家有時候會和朋友聊聊孩子,說起能讓孩子心無旁騖的制勝法寶,我想大家應該一致認為“迷宮”
必須當選其一👏,迷宮類游戲也是大家閑暇實踐之中消磨時間的法寶,
嘿!boys and girs——今天栗子給大家帶來一波有趣好玩的走迷宮小程式,

💦 正文

游戲規則:顧名思義,玩家出現在迷宮的外面,進入之后要在很多不同的路中間找到一條可以抵達
終點的路,可以發反復的嘗試,能走到終點就是勝利,當人你如果在迷宮中迷路了,就走不出來會
失敗,迷宮游戲則傾向于吸引那些更有興趣解決謎題和迎接挑戰的人群,
1)需要做哪些準備呢?
本文的游戲迷宮是2個單獨的迷宮組合而成的,一個簡單的迷宮,一個難的迷宮,
環境安裝的話:Python3、Pycharm、Pygame模塊,
模塊安裝:pip install pygame
2)代碼(區域分)
簡易版主程式:
import pygame
import sys
import random
import time
from pygame.locals import *
from random import randint, choice
import mapp
import color
# 設定螢屏寬度和高度為全域變數
global screen_width
screen_width = 800
global screen_height
screen_height = 800
room_size = 15 # 每個房間的大小
steps = 0
# 輸出文本資訊
def print_text(font, x, y, text, color, shadow=True):
if shadow:
imgText = font.render(text, True, (0, 0, 0))
screen.blit(imgText, (x-2,y-2))
imgText = font.render(text, True, color)
screen.blit(imgText, (x,y))
# 存盤迷宮
r_list = mapp.map_list
# 游戲開始
if __name__ == '__main__':
# 初始化 Pygame
pygame.init()
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption('Maze_AI——by Wonz')
global font1, font2, font3
clock = pygame.time.Clock()
fps = 20
screen.fill(color.White)
# 加載角色照片
user = pygame.image.load("user.png").convert_alpha()
width, height = user.get_size()
user = pygame.transform.smoothscale(user, (8, 8))
# draw the user
width, height = user.get_size()
x = 25 + 42 * room_size
y = 25 + 9 * room_size
roomx = 42
roomy = 9
screen.blit(user, (x, y))
# 畫迷宮
for i in range(43):
for j in range(42):
if (r_list[j][i] == 3):
pygame.draw.circle(screen, color.Red, [30 + i * room_size, 30 + j * room_size], 5, 0)
pygame.display.flip()
r_list[j][i] = 0
elif (r_list[j][i] == 1):
# 畫10*10的矩形,線寬為1,這里不能是0,因為10*10無空白區域
pygame.draw.rect(screen, color.Black, [25 + i * room_size, 25 + j * room_size, 10, 10], 0)
pygame.display.flip()
elif (r_list[j][i] == 0):
pygame.draw.rect(screen, color.White, [25 + i * room_size, 25 + j * room_size, 10, 10], 1)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 走到終點
elif(roomx == 0 and roomy == 9):
font3 = pygame.font.Font(None, 32)
print_text(font3, 350, 350, "Win", color.Red)
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# 右邊無墻
if(0 <= roomx < 42 and 0 <= roomy <= 41 and r_list[roomy][roomx+1] == 0):
x += room_size
roomx += 1 # 計房間數
steps += 1 # 計步
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25)) # x:25 y:0 width:200 height:25
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x-room_size, y, 10, 10))
screen.blit(user, (x, y))
# 右邊有墻
elif (0 <= roomx < 42 and 0 <= roomy <= 41 and r_list[roomy][roomx+1] == 1):
steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
elif event.key == pygame.K_LEFT:
# 左邊無墻
if (0 < roomx <= 42 and 0 <= roomy <= 41 and r_list[roomy][roomx-1] == 0):
x -= room_size
roomx -= 1
steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x+room_size, y, 10, 10))
screen.blit(user, (x, y))
# 左邊有墻
elif (0 < roomx <= 42 and 0 <= roomy <= 41 and r_list[roomy][roomx-1] == 1):
steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
elif event.key == pygame.K_UP:
# 上邊無墻
if (0 <= roomx <= 42 and 0 < roomy <= 41 and r_list[roomy-1][roomx] == 0):
y -= room_size
roomy -= 1
steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x, y+room_size, 10, 10))
screen.blit(user, (x, y))
# 上邊有墻
elif (0 <= roomx <= 42 and 0 < roomy <= 41 and r_list[roomy-1][roomx] == 1):
steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
elif event.key == pygame.K_DOWN:
# 下邊無墻
if (0 <= roomx <= 42 and 0 <= roomy < 41 and r_list[roomy+1][roomx] == 0):
y += room_size
roomy += 1
steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x, y-room_size, 10, 10))
screen.blit(user, (x, y))
# 下邊無墻
elif (0 <= roomx <= 42 and 0 <= roomy < 41 and r_list[roomy+1][roomx] == 1):
steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
# 更新螢屏
pygame.display.update()
難版迷宮主程式:
import pygame
import sys
import random
import time
from pygame.locals import *
from random import randint, choice
import maze
import color
# 設定螢屏寬度和高度為全域變數
global screen_width
screen_width = 800
global screen_height
screen_height = 600
# 輸出文本資訊
def print_text(font, x, y, text, color, shadow=True):
if shadow:
imgText = font.render(text, True, (0, 0, 0))
screen.blit(imgText, (x-2,y-2))
imgText = font.render(text, True, color)
screen.blit(imgText, (x,y))
# 游戲開始
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption('Maze_AI——by Wonz') # 游戲標題
global font1, font2, font3, font4 # 文字
clock = pygame.time.Clock()
fps = 20
screen.fill(color.White)
r_list = maze.room.creat_map(maze.room_m, maze.room_n)
begin_point = [0, 0]
begin_room = r_list[0][0]
maze.room.creat_migong(r_list, begin_room)
# 畫出去起點和終點的其他點
for i in range(maze.room_m):
for j in range(maze.room_n):
begin_point[0] = 25 + i * maze.room_size
begin_point[1] = 25 + j * maze.room_size
r_color = color.Black
maze.room.draw_room(screen, begin_point, r_list[i][j].walls, maze.room_size, r_color)
# 畫起點
maze.room.draw_room(screen, [25, 25], [0, 0, 0, 1], maze.room_size, color.White)
# 畫終點
maze.room.draw_room(screen, [25 + (maze.room_m - 1) * maze.room_size, 25 + (maze.room_n - 1) * maze.room_size],
[0, 1, 0, 0], maze.room_size, color.White)
# 加載角色照片
user = pygame.image.load("user.png").convert_alpha()
width,height = user.get_size()
user = pygame.transform.smoothscale(user, (8,8))
# 畫角色
width, height = user.get_size()
x = 27
y = 30
roomx = 0
roomy = 0
screen.blit(user, (x, y))
# 鍵盤控制角色移動
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 走到終點
elif(roomx == maze.room_m-1 and roomy == maze.room_n-1):
font4 = pygame.font.Font(None, 32)
print_text(font4, 350, 350, "Win" , color.Red)
break
# 鍵盤回應,只取按“→”鍵作為例子,“↑”、“↓”、“←”類似,只要改改引數即可
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# 右邊無墻
if(r_list[roomx][roomy].walls[1] == False):
x += maze.room_size
roomx += 1 # 計房間數
maze.steps += 1 # 計步
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25)) # x:25 y:0 width:200 height:25
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x-15,y,10,10))
screen.blit(user, (x, y))
# 右邊有墻
elif (r_list[roomx][roomy].walls[1] == True):
maze.steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25)) # x:25 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
elif event.key == pygame.K_LEFT:
# 左邊無墻
if (r_list[roomx][roomy].walls[3] == False):
x -= maze.room_size
roomx -= 1
maze.steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x+15,y,10,10))
screen.blit(user, (x, y))
# 左邊有墻
elif (r_list[roomx][roomy].walls[3] == True):
maze.steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
elif event.key == pygame.K_UP:
# 上邊無墻
if (r_list[roomx][roomy].walls[0] == False):
y -= maze.room_size
roomy -= 1
maze.steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x,y+15,10,10))
screen.blit(user, (x, y))
# 上邊有墻
elif (r_list[roomx][roomy].walls[0] == True):
maze.steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
elif event.key == pygame.K_DOWN:
# 下邊無墻
if (r_list[roomx][roomy].walls[2] == False):
y += maze.room_size
roomy += 1
maze.steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
pygame.display.flip()
screen.fill(color.White, (x,y-15,10,10))
screen.blit(user, (x, y))
# 下邊無墻
elif (r_list[roomx][roomy].walls[2] == True):
maze.steps += 1
font1 = pygame.font.Font(None, 32)
font2 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)
print_text(font2, 350, 0, "This is a wall!", color.Black)
pygame.display.flip()
screen.blit(user, (x, y))
# 滑鼠回應,執行 AI 程式
elif event.type == pygame.MOUSEBUTTONUP:
font3 = pygame.font.Font(None, 32)
print_text(font3, 750, 0, "AI", color.Red)
pygame.display.flip()
start_x = 0
start_y = 0
steps = 0
for i in range(1000000000):
if (start_x == maze.room_m-1 and start_y == maze.room_n-1):
font4 = pygame.font.Font(None, 32)
print_text(font4, 350, 350, "Win", color.Red)
break
d = random.randint(1,4)
# 上邊無墻
if (d == 1 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and
r_list[start_x][start_y].walls[0] == False): # 在迷宮地圖范圍內且上邊無墻
start_y -= 1
steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25)) # x:25 y:0 width:200 height:25
screen.fill(color.White, (300, 0, 200, 25)) # x:300 y:0 width:200 height:25
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black) # 步數統計
pygame.display.flip()
screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))
# 右邊無墻
if (d == 2 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and
r_list[start_x][start_y].walls[1] == False):
start_x += 1
steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
pygame.display.flip()
screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))
# 下邊無墻
if (d == 3 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and
r_list[start_x][start_y].walls[2] == False):
start_y += 1
steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
pygame.display.flip()
screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))
# 左邊無墻
if (d == 4 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and
r_list[start_x][start_y].walls[3] == False):
start_x -= 1
steps += 1
font1 = pygame.font.Font(None, 32)
screen.fill(color.White, (25, 0, 200, 25))
screen.fill(color.White, (300, 0, 200, 25))
print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)
pygame.display.flip()
screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))
# 更新螢屏
pygame.display.update()
3)效果展示
簡易版——

難版——

哈哈哈~栗子很好滴 2個版本的路線都給大家標出來啦,不會的跟著走不會錯哦!

💦結尾
悄悄告訴你,這條迷宮我是一眼就看出來了173步就可以完美走出來哦,你來試試嘛?
怎么樣,挑戰成功了嗎?
完整的專案原始碼:關注小編私信01即可,
![]()
如果你喜歡,請“點贊” “關注” “評論”給我小心心吧
![]()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/343098.html
標籤:其他
上一篇:設計模式 簡單工廠,策略模式,幾種基本原則,Unity基礎
下一篇:夢開始的地方
