Python贪吃蛇代码

今天我分享一个代码,贪吃蛇的代码,废话不多说,直接上原码

  1. import pygame, sys

  2. from pygame.locals import *

  3.  
  4. pygame.init()

  5. fpsClock = pygame.time.Clock()

  6.  
  7. WINDOW = pygame.display.set_mode((400, 300))

  8. pygame.display.set_caption('贪吃蛇')

  9.  
  10. BLACK = pygame.Color(0, 0, 0)

  11. WHITE = pygame.Color(255, 255, 255)

  12. RED = pygame.Color(255, 0, 0)

  13.  
  14. snake_position = [100, 50]

  15. snake_body = [[100, 50], [90, 50], [80, 50]]

  16.  
  17. food_position = [300, 150]

  18. food_spawn = True

  19.  
  20. direction = 'RIGHT'

  21. change_to = direction

  22.  
  23. def game_over():

  24. pygame.quit()

  25. sys.exit()

  26.  
  27. while True:

  28. for event in pygame.event.get():

  29. if event.type == QUIT:

  30. game_over()

  31. elif event.type == KEYDOWN:

  32. if event.key == K_RIGHT or event.key == ord('d'):

  33. change_to = 'RIGHT'

  34. if event.key == K_LEFT or event.key == ord('a'):

  35. change_to = 'LEFT'

  36. if event.key == K_UP or event.key == ord('w'):

  37. change_to = 'UP'

  38. if event.key == K_DOWN or event.key == ord('s'):

  39. change_to = 'DOWN'

  40. if event.key == K_ESCAPE:

  41. pygame.event.post(pygame.event.Event(QUIT))

  42.  
  43. if change_to == 'RIGHT' and not direction == 'LEFT':

  44. direction = 'RIGHT'

  45. if change_to == 'LEFT' and not direction == 'RIGHT':

  46. direction = 'LEFT'

  47. if change_to == 'UP' and not direction == 'DOWN':

  48. direction = 'UP'

  49. if change_to == 'DOWN' and not direction == 'UP':

  50. direction = 'DOWN'

  51.  
  52. if direction == 'RIGHT':

  53. snake_position[0] += 10

  54. if direction == 'LEFT':

  55. snake_position[0] -= 10

  56. if direction == 'UP':

  57. snake_position[1] -= 10

  58. if direction == 'DOWN':

  59. snake_position[1] += 10

  60.  
  61. snake_body.insert(0, list(snake_position))

  62. if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:

  63. food_spawn = False

  64. else:

  65. snake_body.pop()

  66.  
  67. if not food_spawn:

  68. food_position = [pygame.randint(0, 39) * 10, pygame.randint(0, 29) * 10]

  69. food_spawn = True

  70. WINDOW.fill(BLACK)

  71. for pos in snake_body:

  72. pygame.draw.rect(WINDOW, RED, pygame.Rect(

  73. pos[0], pos[1], 10, 10))

  74. pygame.draw.rect(WINDOW, WHITE, pygame.Rect(

  75. food_position[0], food_position[1], 10, 10))

  76. if snake_position[0] >= 400 or snake_position[0] < 0:

  77. game_over()

  78. if snake_position[1] >= 300 or snake_position[1] < 0:

  79. game_over()

  80. for block in snake_body[1:]:

  81. if snake_position[0] == block[0] and snake_position[1] == block[1]:

  82. game_over()

  83. pygame.display.update()

  84. fpsClock.tick(12)

  85.  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值