• Python——贪吃蛇


    以下是一个简单的贪吃蛇游戏的Python代码示例:

    1. import pygame
    2. import time
    3. import random
    4. # 初始化 Pygame
    5. pygame.init()
    6. # 定义颜色
    7. BLACK = (0, 0, 0)
    8. WHITE = (255, 255, 255)
    9. RED = (255, 0, 0)
    10. GREEN = (0, 255, 0)
    11. BLUE = (0, 0, 255)
    12. # 设置屏幕尺寸
    13. screen_width = 800
    14. screen_height = 600
    15. screen = pygame.display.set_mode((screen_width, screen_height))
    16. pygame.display.set_caption("贪吃蛇游戏")
    17. # 游戏帧率
    18. clock = pygame.time.Clock()
    19. snake_block_size = 20
    20. # 字体设置
    21. font_style = pygame.font.SysFont(None, 50)
    22. score_font = pygame.font.SysFont(None, 35)
    23. def your_score(score):
    24. value = score_font.render("Your Score: " + str(score), True, WHITE)
    25. screen.blit(value, [0, 0])
    26. def our_snake(snake_block_size, snake_list):
    27. for x in snake_list:
    28. pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])
    29. # 游戏主循环
    30. def game_loop():
    31. game_over = False
    32. game_quit = False
    33. x1 = screen_width / 2
    34. y1 = screen_height / 2
    35. x1_change = 0
    36. y1_change = 0
    37. snake_list = []
    38. snake_length = 1
    39. foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
    40. foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
    41. while not game_quit:
    42. while game_over == True:
    43. screen.fill(BLACK)
    44. message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, WHITE)
    45. screen.blit(message, [screen_width / 6, screen_height / 3])
    46. your_score(snake_length - 1)
    47. pygame.display.update()
    48. # 游戏结束等待用户输入
    49. for event in pygame.event.get():
    50. if event.type == pygame.QUIT:
    51. game_quit = True
    52. game_over = False
    53. if event.type == pygame.KEYDOWN:
    54. if event.key == pygame.K_q:
    55. game_quit = True
    56. game_over = False
    57. if event.key == pygame.K_c:
    58. game_loop()
    59. for event in pygame.event.get():
    60. if event.type == pygame.QUIT:
    61. game_quit = True
    62. if event.type == pygame.KEYDOWN:
    63. if event.key == pygame.K_LEFT:
    64. x1_change = -snake_block_size
    65. y1_change = 0
    66. elif event.key == pygame.K_RIGHT:
    67. x1_change = snake_block_size
    68. y1_change = 0
    69. elif event.key == pygame.K_UP:
    70. y1_change = -snake_block_size
    71. x1_change = 0
    72. elif event.key == pygame.K_DOWN:
    73. y1_change = snake_block_size
    74. x1_change = 0
    75. if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
    76. game_over = True
    77. x1 += x1_change
    78. y1 += y1_change
    79. screen.fill(BLACK)
    80. pygame.draw.rect(screen, RED, [foodx, foody, snake_block_size, snake_block_size])
    81. snake_head = []
    82. snake_head.append(x1)
    83. snake_head.append(y1)
    84. snake_list.append(snake_head)
    85. if len(snake_list) > snake_length:
    86. del snake_list[0]
    87. for x in snake_list[:-1]:
    88. if x == snake_head:
    89. game_over = True
    90. our_snake(snake_block_size, snake_list)
    91. your_score(snake_length - 1)
    92. pygame.display.update()
    93. if x1 == foodx and y1 == foody:
    94. foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
    95. foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
    96. snake_length += 1
    97. clock.tick(10)
    98. pygame.quit()
    99. quit()
    100. game_loop()

    这个代码使用Pygame库来创建一个简单的贪吃蛇游戏。在游戏中,玩家通过键盘控制贪吃蛇的移动方向,目标是吃到食物并尽可能多地增长长度。如果贪吃蛇碰到边界或自己的身体,游戏结束。游戏过程中会显示当前的分数。

  • 相关阅读:
    编程内功心法「底层原理系列」 回归与本质,让本文带你认识什么是计算机软件系统
    Failed to prepare the device for development
    【游戏建模全流程】在Maya中制作赛博朋克风格场景
    RSA密码系统的特定密钥泄露攻击与Coppersmith方法的应用
    [附源码]java毕业设计病历管理系统
    leetcode4. 寻找两个正序数组的中位数python_二分查找和递归(困难)
    蓝桥杯每日一题2023.11.21
    经验熵和经验条件熵
    英语口语学习(07-09)
    python中的装饰器
  • 原文地址:https://blog.csdn.net/2302_81225694/article/details/142150282