Lv.1
在 【Python作品分享】运行试试!【求助帖】 中回复
我的程序还没保存!!!刚编的 ,300多行,2个小时!!!!!!!!!!!!!!!!!!!!!!
2021-02-10T10:17:28 点赞:0
在 谁会做Python贪吃蛇游戏。 中回复
很短,只有40多行,但很好用 from turtle import * from random import randrange from freegames import square, vector food = vector(0, 0) snake = [vector(10, 0)] aim = vector(0, 10) def change(x, y): "Change snake direction." aim.x = x aim.y = y def inside(head): "Return True if head inside boundaries." return-200 < head.x < 190and-200 < head.y < 200 def move(): "Move snake forward one segment." head = snake[-1].copy() head.move(aim) ifnot inside(head)or head in snake: square(head.x, head.y, 9, "red") update() return snake.append(head) if head == food: print("Snake:",len(snake)) food.x = randrange(-15, 15)*10 food.y = randrange(-15, 15)*10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, "black") square(food.x, food.y, 9, "green") update() ontimer(move, 300) def main(): setup(420, 420, 370, 0) hideturtle() tracer(False) listen() onkey(lambda: change(10, 0), "Right") onkey(lambda: change(-10, 0), "Left") onkey(lambda: change(0, 10), "Up") onkey(lambda: change(0, -10), "Down") move() done() main()
2021-02-10T19:22:27 点赞:0
在 用一句话证明你是外星人(滑滑稽稽) 中回复
||||||| ||| || || | ||| ||| || | | || | | | || | || || ||| | | ||| | |||| | ||||| ||| | |
2021-06-14T18:25:20 点赞:0