用python的turtle制作的基本的一个飞行棋游戏,这是一个原形。可以轻易地把它扩展成一个好玩的飞行棋游戏。
以下是部分代码预览:
""" 飞行棋小游戏.py 按空格键会以抛物线扔出色子。QQ会根据色子的点数在棋内移动。 """ import colorsys from turtle import * from random import * from time import sleep def loadmap(xs,ys): """ xs,ys是以换行分隔的数据列表。 """ f1 = open(xs) xcors = f1.read() f1.close() xcors = xcors.split("\n") xcors.pop() xcors = [int(x) for x in xcors] f2 = open(ys) ycors = f2.read() f2.close() ycors = ycors.split("\n") ycors.pop() ycors = [int(y) for y in ycors] cors = [(x,y) for x,y in zip(xcors,ycors)] return cors class Sprite(Turtle): """继承自Turtle的精灵类""" def __init__(self,image,cors): """image:造型,cors:坐标表""" Turtle.__init__(self,visible=False) self.end = False # 描述是否到终点 self.penup() self.shape(image) self.index = 0 # 坐标表从0开始 self.cors = cors pass def move(self,steps): """从cors取坐标,移动""" pass class Square(Turtle): """继承自Turtle的方块类,做色子的""" def __init__(self,images,cat=None): """初始化方法 images:造型列表 cat:海龟对象 """ Turtle.__init__(self,visible=False) self.penup() self.cat = cat self.images = images self.amounts = len(images) self.sw = self.screen.window_width() self.sh = self.screen.window_height() self.da = -1 # 加速度 self.screen.onkeypress(self.init,"space") def init(self): """设定初始速度准备做抛物线移动""" self.screen.onkeypress(None,"space") self.dx = randint(15,25) # 水平速度 self.dy = randint(15,25) # 垂直速度 self.goto(-self.sw//2,-self.sh//2) pass def move(self): """做抛物线移动""" pass if __name__ == '__main__': cors = loadmap("x表.txt","y表.txt") width,height = 480,360 screen = Screen() screen.tracer(0,0) screen.colormode(255) screen.setup(width,height) screen.title("飞行棋小游戏by李兴球") # 注册色子的造型到形状字典中 sezis = [f'im/{i}.png' for i in range(6)] pass # 产生颜色表 colorlist = [] for y in range(100): x = random() r,g,b = colorsys.hsv_to_rgb(x,1,1) r,g,b = int(r*255),int(g*255),int(b*255) colorlist.append((r,g,b)) # t根据cors铺地图 t = Turtle(visible=False,shape='square') t.penup() t.shapesize(2.5,2.5,2.5) for cor in cors: t.color('gray',choice(colorlist)) t.goto(cor) t.stamp() screen.update() sleep(0.01) sprite = Sprite(qq,cors) # 新建一个精灵 sezi = Square(sezis,sprite) # 新建一个色子 screen.listen() # 监听键盘按键 screen.mainloop()
如需要下载完整源代码及素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)