首页 > 编程语言 >python pygame 迷宫生成

python pygame 迷宫生成

时间:2022-10-03 13:36:42浏览次数:49  
标签:box python 迷宫 window pygame ay ax line

import random
import sys
import pygame

# 使用pygame之前必须初始化
pygame.init()


# 参数设置
box_w, box_h = 5, 5 # 盒子宽高
window_w, window_h = 400, 400
x, y = 0, 0 #盒子起始点

# 设置主屏窗口
screen = pygame.display.set_mode((window_w, window_h))

# 设置窗口标题
pygame.display.set_caption('迷宫生成')


def line_left(ax, ay):
left1 = ax + box_w, ay
left2 = ax + box_w, ay - box_h
return left1, left2


def line_right(ax, ay):
right1 = ax, ay
right2 = ax, ay - box_h
return right1, right2


def line_up(ax, ay):
up1 = ax, ay - box_h
up2 = ax + box_w, ay - box_h
return up1, up2


def line_down(ax, ay):
down1 = ax, ay
down2 = ax + box_w, ay
return down1, down2


line_list = []
for i in range(1, int(window_w/box_h)):
for j in range(1, int(window_w/box_h)):
ax, ay = (j - 1) * box_w, i * box_h
l1, l2 = random.choice([line_down(ax, ay),line_left(ax, ay)])
line_list.append([l1, l2])



# 如果没有下列主循环代码,运行结果会一闪而过
while True:
# 更新屏幕内容
pygame.display.flip()
# #填充主窗口的背景颜色,参数值RGB(颜色元组)
screen.fill((255, 255, 255))
# 循环获取事件,监听事件
for event in pygame.event.get():
# 判断用户是否点了关闭按钮
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序
sys.exit()
if event.type == pygame.KEYDOWN:
# 右移
if event.key == pygame.K_RIGHT:
t = True
if [(x + box_w, y + box_h), (x + box_w, y)] in line_list:
t = False
if (0 <= (x + box_w) < window_w) and t:
x += box_w
# 左移
elif event.key == pygame.K_LEFT:
t = True
if [(x, y + box_h), (x, y)] in line_list:
t = False
if 0 < x < window_w and t:
x -= box_w
# 上移
elif event.key == pygame.K_UP:
t = True
if [(x, y), (x + box_w, y)] in line_list:
t = False
if 0 < y < window_h and t:
y -= box_h
# 下移
elif event.key == pygame.K_DOWN:
t = True
if [(x, y + box_h), (x + box_w, y + box_h)] in line_list:
if 0 < (y + box_h) < window_h and t:
y += box_h

pygame.draw.rect(screen, (255, 0, 255), (x, y, box_w, box_h),width=0)



for l1,l2 in line_list:
pygame.draw.line(screen, (255, 0, 255), l1, l2, 2)

screen.blit(screen, (0, 0))
# 定义频率
clock = pygame.time.Clock()
# 设定刷新帧率
clock.tick(60) # 越大刷新的越快
pygame.display.update()

标签:box,python,迷宫,window,pygame,ay,ax,line
From: https://www.cnblogs.com/lld76/p/16750385.html

相关文章

  • python pygame 生命的游戏
    importsysimportpygameimportrandom#参数设置box_w,box_h=10,10#盒子宽高window_w,window_h=400,400x,y=0,0#使用pygame之前必须初始化pygame.init()#设......
  • 学习python遇到的问题
    python重定向输入:io.UnsupportedOperation:notreadable两处错误一、用open打开一个文件,此时调用的是w写入模式,下面使用read是没有权限的,得使用w+读写模式二、使用wri......
  • python学习:multiprocessing多进程-Pool进程池模块
    Multiprocessing.Pool可以提供指定数量的进程供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;但如果池中的进程数已经达到规定......
  • 【推荐收藏】时间序列分析全面指南(附Python代码)
    大家好,时间序列是在规律性时间间隔上记录的观测值序列。本文我将带你了解在Python中分析给定时间序列的特征的21个全过程。内容较长,建议收藏、点赞、关注。内容​​1.什......
  • TransBigData:一款基于 Python 的超酷炫交通时空大数据工具包
    今天分享一次Python交通数据分析与可视化的实战!其中主要是使用TransBigData库快速高效地处理、分析、挖掘出租车GPS数据。所介绍的相关技术开发了Python开源库TransBigData,......
  • 解决conda安装虚拟Python环境联网失败问题
    使用condacreate-ncarlapython=3.7命令安装虚拟环境出错,说网络连接异常,经久查询未果:在终端输入下面这些:condaconfig--addchannelshttps://mirrors.tuna......
  • python 如何快速配置pip加速
    相关地址PyPI镜像使用帮助https://mirrors.tuna.tsinghua.edu.cn/help/pypi/使用清华镜像源加速0.打开cmd1.输入pipconfigsetglobal.index-urlhttps://pypi.tun......
  • 使用bt面板中Python项目管理部署Django项目找不到static采坑记
    工作需要,准备在用django做一个小项目,本地测试没有问题,但是使用bt的工具“python项目管理器”部署到服务器上,找不到static文件(python项目管理器应用可以参考https://www.bt.......
  • pushgateway python脚本
    vimget_network.pyimportprometheus_clientfromprometheus_clientimportCounterfromprometheus_clientimportGaugefromprometheus_client.coreimportCollec......
  • Python+Django+Nginx的从0到1的个人网站搭建
    前言本文以实现最终结果为导向,对知识点不做过多阐述。流程中有任何问题请及时面向搜索程序或他人解决问题。一、准备工作Python的下载Python下载地址不会安装的......