下面是部分代码预览:
""" 贪吃蛇列表版.py 贪吃蛇游戏真正的原理是维护一个先进先出的列表。不断地在列表末尾删除项目,然后又不断地在列表前面加入新的项目。 按左右上下方向箭头操作蛇移动,按空格键增加长度。 """ from turtle import * from time import sleep class Block(Turtle): xspeed = 4 yspeed = 0 def __init__(self,position): Turtle.__init__(self,shape='square',visible=False) screen = Screen() screen.delay(2) all_sprites = [] for i in range(5): # 贪吃蛇初始为5段 position = (-160 - i*24,0) all_sprites.append(Block(position)) sleep(0.001) screen.onkeypress(move_left,"Left") screen.onkeypress(move_right,"Right") screen.onkeypress(move_up,"Up") screen.onkeypress(move_down,"Down") screen.onkeypress(spawn,"space") screen.listen() while True: x = all_sprites[0].xcor() + Block.xspeed * 6 y = all_sprites[0].ycor() + Block.yspeed * 6
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)