鱼C论坛

 找回密码
 立即注册
查看: 443|回复: 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
回复

使用道具 举报

发表于 2024-8-25 20:15:27 | 显示全部楼层
看起来您发了一个未完成的贪吃蛇游戏的源码片段。但代码有点不完整,后面的部分似乎没有粘贴完。下面给您一份完整的示例,您可以参考这个完整的游戏代码:

  1. import random
  2. import turtle as t

  3. t.bgcolor('yellow')
  4. # Caterpillar setup
  5. caterpillar = t.Turtle()
  6. caterpillar.shape('square')
  7. caterpillar.color('red')
  8. caterpillar.speed(0)
  9. caterpillar.penup()
  10. caterpillar.hideturtle()

  11. # Leaf setup
  12. leaf = t.Turtle()
  13. leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
  14. t.register_shape('leaf', leaf_shape)
  15. leaf.shape('leaf')
  16. leaf.color('green')
  17. leaf.penup()
  18. leaf.hideturtle()
  19. leaf.speed(0)

  20. game_started = False
  21. text_turtle = t.Turtle()
  22. text_turtle.write('Press SPACE to start', align='center', font=('Arial', 16, 'bold'))
  23. text_turtle.hideturtle()

  24. score_turtle = t.Turtle()
  25. score_turtle.hideturtle()
  26. score_turtle.speed(0)

  27. # Check if the caterpillar is outside the window
  28. def outside_window(caterpillar):
  29.     left_wall = -t.window_width() / 2
  30.     right_wall = t.window_width() / 2
  31.     top_wall = t.window_height() / 2
  32.     bottom_wall = -t.window_height() / 2
  33.     (x, y) = caterpillar.pos()
  34.     outside = x < left_wall or x > right_wall or y < bottom_wall or y > top_wall
  35.     return outside

  36. # Display game over message
  37. def game_over():
  38.     caterpillar.color('yellow')
  39.     leaf.color('yellow')
  40.     t.penup()
  41.     t.hideturtle()
  42.     t.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))

  43. # Display the current score
  44. def display_score(current_score):
  45.     score_turtle.clear()
  46.     score_turtle.penup()
  47.     x = (t.window_width() / 2) - 50
  48.     y = (t.window_height() / 2) - 50
  49.     score_turtle.setpos(x, y)
  50.     score_turtle.write(str(current_score), align='right', font=('Arial', 40, 'bold'))

  51. # Place the leaf at a random location
  52. def place_leaf():
  53.     leaf.ht()
  54.     leaf.setx(random.randint(-200, 200))
  55.     leaf.sety(random.randint(-200, 200))
  56.     leaf.st()

  57. # Start the game
  58. def start_game():
  59.     global game_started
  60.     if game_started:
  61.         return
  62.     game_started = True

  63.     score = 0
  64.     text_turtle.clear()

  65.     caterpillar_speed = 2
  66.     caterpillar_length = 3
  67.     caterpillar.shapesize(1, caterpillar_length, 1)
  68.     caterpillar.showturtle()
  69.     display_score(score)
  70.     place_leaf()

  71.     while True:
  72.         caterpillar.forward(caterpillar_speed)
  73.         if caterpillar.distance(leaf) < 20:
  74.             place_leaf()
  75.             caterpillar_length = caterpillar_length + 1
  76.             caterpillar.shapesize(1, caterpillar_length, 1)
  77.             caterpillar_speed = caterpillar_speed + 1
  78.             score = score + 10
  79.             display_score(score)
  80.         if outside_window(caterpillar):
  81.             game_over()
  82.             break

  83. # Turn the caterpillar upwards
  84. def move_up():
  85.     if caterpillar.heading() != 270:
  86.         caterpillar.setheading(90)

  87. # Turn the caterpillar downwards
  88. def move_down():
  89.     if caterpillar.heading() != 90:
  90.         caterpillar.setheading(270)

  91. # Turn the caterpillar left
  92. def move_left():
  93.     if caterpillar.heading() != 0:
  94.         caterpillar.setheading(180)

  95. # Turn the caterpillar right
  96. def move_right():
  97.     if caterpillar.heading() != 180:
  98.         caterpillar.setheading(0)

  99. # Bind keyboard inputs to functions
  100. t.onkey(start_game, 'space')
  101. t.onkey(move_up, 'Up')
  102. t.onkey(move_down, 'Down')
  103. t.onkey(move_left, 'Left')
  104. t.onkey(move_right, 'Right')
  105. t.listen()
  106. t.mainloop()
复制代码


这个代码展示了如何用Python的Turtle库制作一个简单的贪吃蛇游戏。按下空格键(SPACE)开始游戏,并用方向键(上、下、左、右)控制贪吃蛇的移动方向。游戏过程中得吃绿叶来增长长度并增加分数。如果贪吃蛇撞到窗口边界,游戏结束,显示"GAME OVER"。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 01:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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