当前位置:   article > 正文

pygame 实现贪吃蛇_pygame贪吃蛇代码

pygame贪吃蛇代码

面是一份基础版的贪吃蛇代码,主要展示了游戏的基本逻辑,如界面绘制、蛇的移动、食物的产生等。

  1. import pygame
  2. import random
  3. # 初始化游戏
  4. pygame.init()
  5. width, height = 500500
  6. screen = pygame.display.set_mode((width, height))
  7. pygame.display.set_caption('贪吃蛇')
  8. clock = pygame.time.Clock()
  9. # 定义颜色
  10. white = (255255255)
  11. black = (000)
  12. green = (02550)
  13. red = (25500)
  14. # 定义蛇和食物
  15. block_size = 10
  16. snake_speed = 15
  17. font_style = pygame.font.SysFont(None, 30)
  18. def draw_snake(snake_list):
  19.     for x,y in snake_list:
  20.         pygame.draw.rect(screen, green, [x, y, block_sizeblock_size])
  21. def message(msg, color):
  22.     message = font_style.render(msg, True, color)
  23.     screen.blit(message, [width/6, height/3])
  24. def gameLoop():
  25.     game_over = False
  26.     game_close = False
  27.     x1 = width / 2
  28.     y1 = height / 2
  29.     x1_change = 0       
  30.     y1_change = 0
  31.     snake_List = []
  32.     Length_of_snake = 1
  33.     foodx = round(random.randrange(0, width - block_size/ 10.0* 10.0
  34.     foody = round(random.randrange(0, height - block_size/ 10.0* 10.0
  35.     # 游戏主循环
  36.     while not game_over:
  37.         while game_close == True:
  38.             screen.fill(white)
  39.             message("你输了!再玩一次按Q,退出按C", red)
  40.             pygame.display.update()
  41.             for event in pygame.event.get():
  42.                 if event.type == pygame.KEYDOWN:
  43.                     if event.key == pygame.K_q:
  44.                         gameLoop()
  45.                     elif event.key == pygame.K_c:
  46.                         game_over = True
  47.                         game_close = False
  48.         # 监听事件
  49.         for event in pygame.event.get():
  50.             if event.type == pygame.QUIT:
  51.                 game_over = True
  52.             if event.type == pygame.KEYDOWN:
  53.                 if event.key == pygame.K_LEFT:
  54.                     x1_change = -block_size
  55.                     y1_change = 0
  56.                 elif event.key == pygame.K_RIGHT:
  57.                     x1_change = block_size
  58.                     y1_change = 0
  59.                 elif event.key == pygame.K_UP:
  60.                     y1_change = -block_size
  61.                     x1_change = 0
  62.                 elif event.key == pygame.K_DOWN:
  63.                     y1_change = block_size
  64.                     x1_change = 0
  65.         # 判断是否出界
  66.         if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
  67.             game_close = True
  68.         # 更新蛇的坐标
  69.         x1 += x1_change
  70.         y1 += y1_change
  71.         # 清空屏幕
  72.         screen.fill(white)
  73.         # 绘制食物和蛇
  74.         pygame.draw.rect(screen, red, [foodx, foody, block_sizeblock_size])
  75.         snake_Head = []
  76.         snake_Head.append(x1)
  77.         snake_Head.append(y1)
  78.         snake_List.append(snake_Head)
  79.         if len(snake_List) > Length_of_snake:
  80.             del snake_List[0]
  81.         for x in snake_List[:-1]:
  82.             if x == snake_Head:
  83.                 game_close = True
  84.         draw_snake(snake_List)
  85.         pygame.display.update()
  86.         # 判断蛇是否吃到食物
  87.         if x1 == foodx and y1 == foody:
  88.             foodx = round(random.randrange(0, width - block_size/ 10.0* 10.0
  89.             foody = round(random.randrange(0, height - block_size/ 10.0* 10.0
  90.             Length_of_snake += 1
  91.         clock.tick(snake_speed)
  92.     pygame.quit()
  93.     quit()
  94. gameLoop()

代码主要分为三部分:初

始化游戏、游戏主循环和游戏结束处理。在游戏主循环中,首先检测用户的输入以改变蛇的运动方向,并根据蛇的坐标更新游戏界面。然后判断蛇是否撞墙或咬到自己以及是否吃到食物,以决定游戏是否结束或者蛇是否增长。最后,游戏结束时提供继续游戏和退出游戏的选项。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/937511
推荐阅读
相关标签
  

闽ICP备14008679号