鱼C论坛

 找回密码
 立即注册
查看: 968|回复: 1

[作品展示] 用Python-Turtle做贪吃蛇

[复制链接]
发表于 2024-8-25 20:14:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x


闲话少叙,上源码!!!

  1. import random
  2. import turtle as t

  3. t.bgcolor('yellow')
  4. caterpillar = t.Turtle()
  5. caterpillar.shape('square')
  6. caterpillar.color('red')
  7. caterpillar.speed(0)
  8. caterpillar.penup()
  9. caterpillar2 = t.Turtle()
  10. caterpillar2.shape('square')
  11. caterpillar2.color('blue')
  12. caterpillar2.speed(0)
  13. caterpillar2.penup()
  14. caterpillar2.hideturtle()

  15. leaf = t.Turtle()
  16. leaf_shape = ((0, 0),(14, 2), (18, 6), (20, 20), \
  17.               (6, 18), (2, 14))
  18. t.register_shape('leaf', leaf_shape)
  19. leaf.shape('leaf')
  20. leaf.color('green')
  21. leaf.penup()
  22. leaf.hideturtle()
  23. leaf.speed(0)

  24. game_started = False
  25. text_turtle = t.Turtle()
  26. text_turtle.write('Press SPACE To Start', align='center', \
  27.                   font=('Arial', 16, 'bold'))
  28. text_turtle.hideturtle()

  29. score_turtle = t.Turtle()
  30. score_turtle.hideturtle()
  31. score_turtle.speed(0)

  32. def outside_window(caterpillar):
  33.     left_wall = -t.window_width() / 2
  34.     right_wall = t.window_width() / 2
  35.     top_wall = t.window_height() / 2
  36.     bottom_wall = -t.window_height() / 2
  37.     (x, y) = caterpillar.pos()
  38.     outside = \
  39.             x< left_wall or \
  40.             x> right_wall or \
  41.             y< bottom_wall or \
  42.             y> top_wall
  43.     return outside
  44.    
  45. def game_over():
  46.     caterpillar.color('yellow')
  47.     caterpillar2.color('yellow')
  48.     leaf.color('yellow')
  49.     t.penup()
  50.     t.hideturtle()
  51.     t.write('GAME OVER !', align='center', font=('Arial', 30, 'normal'))

  52. def display_score(current_score):
  53.     score_turtle.clear()
  54.     score_turtle.penup()
  55.     x = (t.window_width() / 2) - 50
  56.     y = (t.window_height() / 2) - 50
  57.     score_turtle.setpos(x, y)
  58.     score_turtle.write(str(current_score), align='right', \
  59.                        font=('Arial', 40, 'bold'))
  60.    
  61. def place_leaf():
  62.     leaf.ht()
  63.     leaf.setx(random.randint(-200, 200))
  64.     leaf.sety(random.randint(-200, 200))
  65.     leaf.st()

  66. def start_game():
  67.     global game_started
  68.     if game_started:
  69.         return
  70.     game_started = True

  71.     score = 0
  72.     text_turtle.clear()

  73.     caterpillar_speed = 2
  74.     caterpillar_length = 3
  75.     caterpillar.shapesize(1, caterpillar_length, 1)
  76.     caterpillar.showturtle()
  77.     caterpillar2.shapesize(1, caterpillar_length, 1)
  78.     caterpillar2.setheading(180)
  79.     caterpillar2.showturtle()
  80.     display_score(score)
  81.     place_leaf()

  82.     while True:
  83.         caterpillar.forward(caterpillar_speed)
  84.         caterpillar2.forward(caterpillar_speed)
  85.         if caterpillar.distance(leaf) < 20 or leaf.distance(caterpillar2) < 20:
  86.             place_leaf()
  87.             caterpillar_length = caterpillar_length + 1
  88.             caterpillar.shapesize(1, caterpillar_length, 1)
  89.             caterpillar2.shapesize(1, caterpillar_length, 1)
  90.             caterpillar_speed = caterpillar_speed+1
  91.             score = score + 10
  92.             display_score(score)
  93.         if outside_window(caterpillar) or outside_window(caterpillar2):
  94.             game_over()
  95.             break

  96. def move_up():
  97.     if caterpillar.heading() == 0 or caterpillar.heading() == 180:
  98.         caterpillar.setheading(90)

  99. def move_down():
  100.     if caterpillar.heading() == 0 or caterpillar.heading() == 180:
  101.         caterpillar.setheading(270)

  102. def move_left():
  103.     if caterpillar.heading() == 90 or caterpillar.heading() == 270:
  104.         caterpillar.setheading(180)

  105. def move_right():
  106.     if caterpillar.heading() == 90 or caterpillar.heading() == 270:
  107.         caterpillar.setheading(0)
  108.         
  109. def caterpillar2_move_up():
  110.     if caterpillar2.heading() == 0 or caterpillar2.heading() == 180:
  111.         caterpillar2.setheading(90)

  112. def caterpillar2_move_down():
  113.     if caterpillar2.heading() == 0 or caterpillar2.heading() == 180:
  114.         caterpillar2.setheading(270)

  115. def caterpillar2_move_left():
  116.     if caterpillar2.heading() == 90 or caterpillar2.heading() == 270:
  117.         caterpillar2.setheading(180)

  118. def caterpillar2_move_right():
  119.     if caterpillar2.heading() == 90 or caterpillar2.heading() == 270:
  120.         caterpillar2.setheading(0)

  121. t.onkey(start_game, 'space')
  122. t.onkey(move_up, 'Up')
  123. t.onkey(move_right, 'Right')
  124. t.onkey(move_down, 'Down')
  125. t.onkey(move_left, 'Left')
  126. t.onkey(caterpillar2_move_up, 'w')
  127. t.onkey(caterpillar2_move_right, 'd')
  128. t.onkey(caterpillar2_move_down, 's')
  129. t.onkey(caterpillar2_move_left, 'a')
  130. t.onkey(start_game, 'space')
  131. t.listen()
  132. t.mainloop()
复制代码


事实上,这只是一个比较简陋的模型……
但是,我一定会努力的!!!

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-6 03:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表