一個好玩的迷宮小游戲

迷宮小游戲

前言


迷宮游戲是非常經(jīng)典的游戲,在該項目要求隨機生成一個迷宮,并求解迷宮


一、python是什么?

Python是一種跨平臺的計算機程序設計語言。是一個高層次的結(jié)合了解釋性、編譯性、互動性和面向?qū)ο蟮哪_本語言。

二、使用步驟

1.引入庫

代碼如下:

import pygame

import sys

import random

import time

from pygame.localsimport *

from randomimport randint, choice

import maze

import color

2.讀入數(shù)據(jù)

代碼如下:

# 設置屏幕寬度和高度為全局變量

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))

2.1.游戲開始

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 iin range(maze.room_m):

for jin 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))

2.2.控制角色移動

while True:

for eventin 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

? ? ? ? # 鍵盤響應,只取按“→”鍵作為例子,“↑”、“↓”、“←”類似,只要改改參數(shù)即可

? ? ? ? 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 # 計房間數(shù)

? ? ? ? ? ? ? ? ? ? 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))

2.3.鼠標響應

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 iin 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):# 在迷宮地圖范圍內(nèi)且上邊無墻

? ? ? ? ? ? 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)# 步數(shù)統(tǒng)計

? ? ? ? ? ? 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))


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容