❶ python入门可以做的小游戏
1、Python入门拼图小游戏
简单介绍:
将图像分为m×n个矩形块,并将图像右下角的矩形块替换为空白块后,将这些矩形块随机摆放成原图像的形状。
2、Python入门推箱子小游戏
简单介绍:
这是来自日本的一个经典游戏,在狭小的仓库中,要求把木箱放到指定的位置,如果不小心就可能出现箱子无法移动或者通道被堵的情况,所以,如何巧妙利用有限的空间和通道,合理安排移动顺序,就成了这个游戏能否通关的关键。
3、Python入门小游戏之外星人入侵
简单介绍:
玩家可以通过鼠标控制飞船的移动和射击,如果能在敌人达到游戏界面低端之前消灭所有敌人,则游戏胜利,否则游戏失败。
4、Python入门小游戏之吃豆子
简单介绍:
通过键盘方向键,控制游戏的人物吃豆人,吃掉藏在迷宫内的所有豆子,并且不能被敌人抓到。
5、Python入门小游戏之宝石消消乐
简单介绍:
玩家通过鼠标交换相邻的拼图,若交换后,在水平/竖直方向存在连续三个相同的拼图,则这些拼图消失,玩家得分。
6、Python入门小游戏之乒乓球对战
简单介绍:
中间是球网,玩家通过上下键移动球拍,并且这个游戏是可以两个人玩的哦。
7、还有其他四个游戏
它们是:炸弹人小游戏、逃出迷宫、飞扬的小鸟、五子棋
❷ Python基于递归算法实现的走迷宫问题
Python基于递归算法实现的走迷宫问题
本文实例讲述了Python基于递归算法实现的走迷宫问题。分享给大家供大家参考,具体如下:
什么是递归?
简单地理解就是函数调用自身的过程就称之为递归。
什么时候用到递归?
如果一个问题可以表示为更小规模的迭代运算,就可以使用递归算法。
迷宫问题:一个由0或1构成的二维数组中,假设1是可以移动到的点,0是不能移动到的点,如何从数组中间一个值为1的点出发,每一只能朝上下左右四个方向移动一个单位,当移动到二维数组的边缘,即可得到问题的解,类似的问题都可以称为迷宫问题。
在python中可以使用list嵌套表示二维数组。假设一个6*6的迷宫,问题时从该数组坐标[3][3]出发,判断能不能成功的走出迷宫。
maze=[[1,0,0,1,0,1],
[1,1,1,0,1,0],
[0,0,1,0,1,0],
[0,1,1,1,0,0],
[0,0,0,1,0,0],
[1,0,0,0,0,0]]
针对这个迷宫问题,我们可以使用递归的思想很好的解决。对于数组中的一个点,该点的四个方向可以通过横纵坐标的加减轻松的表示,每当移动的一个可移动的点时候,整个问题又变为和初始状态一样的问题,继续搜索四个方向找可以移动的点,知道移动到数组的边缘。
所以我们可以这样编码:
# 判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。
def valid(maze,x,y):
if (x>=0 and x<len(maze) and y>=0 and y<len(maze[0]) and maze[x][y]==1):
return True
else:
return False
# 移步函数实现
def walk(maze,x,y):
# 如果位置是迷宫的出口,说明成功走出迷宫
if(x==0 and y==0):
print("successful!")
return True
# 递归主体实现
if valid(maze,x,y):
# print(x,y)
maze[x][y]=2 # 做标记,防止折回
# 针对四个方向依次试探,如果失败,撤销一步
if not walk(maze,x-1,y):
maze[x][y]=1
elif not walk(maze,x,y-1):
maze[x][y]=1
elif not walk(maze,x+1,y):
maze[x][y]=1
elif not walk(maze,x,y+1):
maze[x][y]=1
else:
return False # 无路可走说明,没有解
return True
walk(maze,3,3)
递归是个好东西呀!
❸ python走迷宫算法题怎么解
示例:
#coding:UTF-8
globalm,n,path,minpath,pathnum
m=7
n=7
k=[0,1,2,3,4,5,6,7]#循环变量取值范围向量
a=[[0,0,1,0,0,0,0,0],
[1,0,1,0,1,1,1,0],
[0,0,0,0,1,0,0,0],
[1,1,1,1,1,0,0,0],
[0,0,0,0,0,1,1,0],
[0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0]]#迷宫矩阵
b=[[1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]] #状态矩阵
path=[]
minpath=[]
min=10000
step=0
pathnum=0
#print(a)
#print(b)
defnextone(x,y):
globalpath,minpath,m,n,min,step,pathnum
if(x==0)and(y==0):
path=[]
step=0
if(x==m)and(y==n):
pathnum+=1
print("step=",step)
print("path=",path)
ifstep<min:
min=step
minpath=path[:]
else:
if(x+1ink)and(yink):
if(b[x+1][y]==0)and(a[x+1][y]==0):
b[x+1][y]=1
path.append([x+1,y])
step+=1
nextone(x+1,y)
step-=1
path.remove([x+1,y])
b[x+1][y]=0#回溯
if(xink)and(y+1ink):
if(b[x][y+1]==0)and(a[x][y+1]==0):
b[x][y+1]=1
path.append([x,y+1])
step+=1
nextone(x,y+1)
step-=1
path.remove([x,y+1])
b[x][y+1]=0#回溯
if(x-1ink)and(yink):
if(b[x-1][y]==0)and(a[x-1][y]==0):
b[x-1][y]=1
path.append([x-1,y])
step+=1
nextone(x-1,y)
step-=1
path.remove([x-1,y])
b[x-1][y]=0#回溯
if(xink)and(y-1ink):
if(b[x][y-1]==0)and(a[x][y-1]==0):
b[x][y-1]=1
path.append([x,y-1])
step+=1
nextone(x,y-1)
step-=1
path.remove([x,y-1])
b[x][y-1]=0#回溯
nextone(0,0)
print()
print("min=",min)
print("minpath=",minpath)
print("pathnum=",pathnum)
❹ Python 解决一个简单的迷宫问题 在线等
具体是什么情况呢
# -*- coding: utf-8 -*-
matrix = []
row, col = 0, 0
def LoadData():
global matrix
file = open("maze.txt")
lines = file.readlines()
for line in lines:
matrix.append(line.strip())
def Init():
r = len(matrix)
c = len(matrix[0])
global row, col
for cc in xrange(c):
for rr in xrange(r):
if matrix[rr][cc] in ('O', 'F'):
row, col = rr, cc
OutputPos()
return
def OutputPos():
print "You are at position (%d, %d)" % (row, col)
def Move(d):
global row, col
m = (0, 0)
if d == 'N':
m = (-1, 0)
elif d == 'E':
m = (0, 1)
elif d == 'S':
m = (1, 0)
elif d == 'W':
m = (0, -1)
if row + m[0] >= 0 and row + m[0] < len(matrix) and col + m[1] >= 0 and col + m[1] < len(matrix[0]) and matrix[row + m[0]][col + m[1]] in ('O', 'F'):
row = row + m[0]
col = col + m[1]
OutputPos()
else:
print "You can't go in that direction"
def Input():
cmd = raw_input("Command: ")
cmd = cmd.strip()
if cmd == '?':
print '''\
? - Help.
N - move North one square.
S - move South one square.
E - move East one square.
W - move West one square.
R - Reset to the beginning.
B - Back up a move.
L - List all possible legal directions from the current position.
Q - Quit.'''
OutputPos()
elif cmd in ('N', 'E', 'S', 'W'):
Move(cmd)
if matrix[row][col] == 'F':
print "Congratulations - you made it"
elif cmd == 'L':
l = []
if row - 1 >= 0 and matrix[row - 1][col] in ('O', 'F'):
l.append('N')
if col + 1 < len(matrix[0]) and matrix[row][col + 1] in ('O', 'F'):
l.append('E')
if row + 1 < len(matrix) and matrix[row + 1][col] in ('O', 'F'):
l.append('S')
if col - 1 >= 0 and matrix[row][col - 1] in ('O', 'F'):
l.append('W')
for i, x in enumerate(l):
if i > 0:
print ",",
print x,
print ""
elif cmd == 'R':
Init()
elif cmd == 'Q':
if raw_input("Are you sure you want to quit? [y] or n:").strip() == 'y':
return False
else:
print "Invalid Command:", cmd
OutputPos()
return True
if __name__ == "__main__":
LoadData()
Init()
while Input():
pass
❺ 关于python 设计一个小游戏
应该可以的。设计一个阵列,描述墙壁和空间,通过算法使阵列可以旋转。
小球从入口进入以后,在阵列里滚动,通过计算重力和在斜面上的分力,算出小球运动的方向和速度。
到达阵列墙壁时,根据速度和方向以及墙壁的角度,计算反弹的方向和速度。直到小球滚出阵列。
我有一个Python3写的匀速运动弹球的代码,可以参考下
importturtle
defstop():
globalrunning
running=False
defmain():
globalrunning
screenx,screeny=turtle.Screen().screensize()
x,y=turtle.pos()
stepx=10
stepy=10
print(x,y,screenx,screeny)
turtle.clear()
turtle.speed(0)
#turtle.Screen().bgcolor("gray10")
#turtle.Screen().tracer(False)
turtle.up()
turtle.shape("circle")
turtle.shapesize(5,5)
turtle.left(45)
whileTrue:
ifx+5>screenx:
stepx=-stepx
turtle.left(90)
ify+5>screeny:
stepy=-stepy
turtle.left(90)
ifx+5<-screenx:
stepx=-stepx
turtle.left(90)
ify+5<-screeny:
stepy=-stepy
turtle.left(90)
turtle.fd(10)
x+=stepx
y+=stepy
if__name__=='__main__':
print(main())
turtle.done()
❻ Python 里itchat 模块能实现什么有趣的东西
只用标准库是吧,不用任何第三方模块或者软件是吧,没问题,我来展示一个用Python写的GIF动态图,演示的是概率论中的WilsonUniformSpanningTree算法(也是一个迷宫生成算法),连tkinter,turtle之类的发行版内置的图形库都统统不需要。
❼ 使用左手法则/右手法则摸墙法(左手或右手在迷宫中始终不离开墙)写一个python程序解迷宫
下面的代码假定你的迷宫数据一定是合法的 (单一的入口和出口,不会打环,不会无解),如果数据不合法,可能导致死循环,数据合法性的检查你自己实现。
另外,我用 东南西北四个方向来代替所谓的上下左右,因为左右概念是相对的。 用python 2。
puzzle_walk 函数返回一个list, 每个元素是每次移动的路径坐标, 其第一个参数为迷宫的list数据, 第二个参数默认为 True 表示左手抹墙, False 则是右手抹墙。
简单说一下算法:首先找到入口格,设定初始面向 East ( 如果是右手抹墙则是 West),然后重复执行以下操作:
1. 如果当前格为最后一排且向南可以移动,则说明当前格为终点,结束。
2. 根据当前格的数据,找到下一步需要面向的方向, 方法是,如果当前方向上有墙,则顺时针(右手抹墙则逆时针)转身,重复这一步骤直到面向的方向上可以行走. 这里可以参考 turn 函数
3. 沿当前方向走一步, 参考 move 函数
4. 逆时针(右手抹墙则顺时针)转身一次,使当前面对方向为第3步之后的左手(或右手)方向, 然后回到步骤1
最后, 我的代码假定迷宫入口一定是从第1行的North 方向进入,出口一定是从最后一行的 South 方向出去,如果要支持从第一行的其他方向进(比如从 (0, 0) 的West进) ,最后一行的其他方向出,你需修改查找入口的代码和判断出口的代码,这个很简单, 自己去搞定吧。
N=0
E=1
S=2
W=3
classWalker(object):
def__init__(self,x,y,direction):
#coordinates
self.x=x
self.y=y
self.direction=direction
defturn(self,clockwise=True):
ifclockwise:
self.direction=(self.direction+1)%4
else:
self.direction=(self.direction+4-1)%4
defmove(self):
ifself.direction==N:
self.x-=1
elifself.direction==E:
self.y+=1
elifself.direction==S:
self.x+=1
elifself.direction==W:
self.y-=1
defget_coords(self):
return(self.x,self.y)
defget_direction(self):
returnself.direction
defpuzzle_walk(puzzle,left_touching=True):
route=[]
rows=len(puzzle)
columns=len(puzzle[0])
#locatetheentrance
coords=(-1,-1)
foryinrange(columns):
cell=puzzle[0][y]
ifcell[N]:
coords=(0,y)
break
assertcoords[0]>=0andcoords[1]>=0
walker=Walker(coords[0],coords[1],Eifleft_touchingelseW)
whileTrue:
x,y=walker.get_coords()
cell=puzzle[x][y]
route.append(tuple([x,y]))
ifx==rows-1andcell[S]:
#foundtheexit
break
#
whilenotcell[walker.get_direction()]:
walker.turn(left_touching)
#move
walker.move()
#facetothedirectionofthehand
walker.turn(notleft_touching)
returnroute
#运行结果
>>>p=[[(False,True,True,False),(True,True,False,True),(False,False,True,True)],[(True,False,True,False),(False,True,False,False),(True,False,False,True)]]
#左手抹墙
>>>puzzle_walk(p)
[(0,1),(0,2),(1,2),(1,1),(1,2),(0,2),(0,1),(0,0),(1,0)]
#右手抹墙
>>>puzzle_walk(p,False)
[(0,1),(0,0),(1,0)]
❽ python写了一个迷宫的小游戏
map_list=后面直接接数组,不要隔行写,隔行写会认为你语法错误,python对格式是很严格的。