鱼C论坛

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

[作品展示] 看完小甲鱼书后,自己动手编写的贪吃蛇游戏

[复制链接]
发表于 2020-11-24 18:45:26 | 显示全部楼层 |阅读模式

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

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

x
看完小甲鱼书后,自己动手编写的贪吃蛇游戏

  1. import sys
  2. from pygame.locals import *
  3. import pygame
  4. from random import *

  5. WHITE = (255,255,255)
  6. BLACK = (0,0,0)
  7. GREEN = (0,255,0)
  8. REN = (255,0,0)
  9. BLUE = (0,0,255)

  10. pygame.init()
  11. pygame.mixer.init()
  12. pygame.font.init()

  13. # 导入文字
  14. words = pygame.font.Font("font/font.ttf", 45)

  15. # 暂停文字
  16. words_text = words.render("Suspended",True,BLUE)

  17. # 结束对话框
  18. words_text_GAMEOVER = words.render("GAMEOVER",True,BLUE)

  19. # 重来按键字
  20. wods_text_Reopen = words.render("Press F1 to restart",True,BLUE)

  21. # 导入背景音音效
  22. pygame.mixer.music.load("bgm/我不信这么高雅的作品破不了100播放量1.mp3")
  23. pygame.mixer.music.set_volume(0.2)
  24. pygame.mixer.music.play()

  25. # 导入音效
  26. chi = pygame.mixer.Sound("bgm/录音1.wav")
  27. chi.set_volume(0.5)
  28. stop_sound = pygame.mixer.Sound("bgm/暂停.wav")
  29. chi.set_volume(0.2)

  30. size = width, height = 400, 400
  31. screen = pygame.display.set_mode(size)
  32. pygame.display.set_caption("欧阳麟君")
  33. clock = pygame.time.Clock()


  34. def main():


  35.     # 用来倒计时后关闭游戏
  36.     count_GAMEOVER = 0

  37.     # 蛇的像素大小
  38.     she_size = 40
  39.    
  40.     # 2个 for 循环 遍历坐标位置,默认是用1234这样的,具体像素可以自己乘以具体像素的大小就好了
  41.     shetou_overlap = []
  42.     for i in range(0,10):
  43.         for b in range(0,10):
  44.             shetou_overlap.insert(0,list((i*she_size,b*she_size)))
  45.             
  46.     # 蛇头
  47.     shetou = [[4*she_size,3*she_size]]

  48.     # 给个对象指定蛇头第一个位置,用来刷新蛇吃到的食物变成蛇头
  49.     shetou_1 = shetou[0]

  50.     # 先生成一个随机对象,用来生成食物的初始随机位置,判断是否随机食物初始化在蛇头上,是的话就重新生成
  51.     shiwu = choice(shetou_overlap)
  52.     while shiwu == shetou[0]:
  53.         choice(shetou_overlap)
  54.     # 生成一个副蛇身用来判断是否被撞
  55.     shetou_to = []
  56.    
  57.     # 判断食物是否被吃
  58.     food = False
  59.     # 开局随机上下左右移动
  60.     move = choice(("upper","lower","Left","right"))
  61.     # 给个加速器
  62.     conut = 0
  63.     # 修改这里提高速度
  64.     speed = 20

  65.     # 给个判断,用来暂停游戏
  66.     stop = True

  67.     # 用来切换暂停图片
  68.     suspend_picture = False

  69.     # 按回车开始
  70.     kaishi = True
  71.    
  72.     # 用来控制流程
  73.     running = True
  74.    
  75.     # 3条命
  76.     she_life = 3
  77.     while running:
  78.         
  79.         # 随着吃到越多的食物就开始越来越快
  80.         if len(shetou) > 10:
  81.             speed = 15
  82.         elif len(shetou) >20:
  83.             speed = 13
  84.         elif len(shetou) > 30:
  85.             speed = 7
  86.         
  87.         # 给一个对象,用来后边生成食物来随机
  88.         shiwu_random = []
  89.         shiwu_random += shetou_overlap
  90.         
  91.         # 给一个对象,用来后边生成食物来随机
  92.         shiwu_random
  93.         # 绘制背景
  94.         screen.fill(WHITE)
  95.         ##########################下面宫格线条绘制 ##########################
  96.         # 横
  97.         pygame.draw.aaline(screen,BLACK,(0,0),(400,0),1)
  98.         pygame.draw.aaline(screen,BLACK,(0,40),(400,40),1)
  99.         pygame.draw.aaline(screen,BLACK,(0,80),(400,80),1)
  100.         pygame.draw.aaline(screen,BLACK,(0,120),(400,120),1)
  101.         pygame.draw.aaline(screen,BLACK,(0,160),(400,160),1)
  102.         pygame.draw.aaline(screen,BLACK,(0,200),(400,200),1)
  103.         pygame.draw.aaline(screen,BLACK,(0,240),(400,240),1)
  104.         pygame.draw.aaline(screen,BLACK,(0,280),(400,280),1)
  105.         pygame.draw.aaline(screen,BLACK,(0,320),(400,320),1)
  106.         pygame.draw.aaline(screen,BLACK,(0,360),(400,360),1)

  107.         # 竖
  108.         pygame.draw.aaline(screen,BLACK,(40,0),(40,400),1)
  109.         pygame.draw.aaline(screen,BLACK,(80,0),(80,400),1)
  110.         pygame.draw.aaline(screen,BLACK,(120,0),(120,400),1)
  111.         pygame.draw.aaline(screen,BLACK,(160,0),(160,400),1)
  112.         pygame.draw.aaline(screen,BLACK,(200,0),(200,400),1)
  113.         pygame.draw.aaline(screen,BLACK,(240,0),(240,400),1)
  114.         pygame.draw.aaline(screen,BLACK,(280,0),(280,400),1)
  115.         pygame.draw.aaline(screen,BLACK,(320,0),(320,400),1)
  116.         pygame.draw.aaline(screen,BLACK,(360,0),(360,400),1)
  117.         ########################## 上面宫格线条绘制 ##########################
  118.         
  119.         for event in pygame.event.get():
  120.             if event.type == QUIT:
  121.                 pygame.quit()
  122.                 sys.exit()
  123.                      
  124.             # 当用户按下空格之后
  125.             elif event.type == KEYDOWN:
  126.                 if event.key == K_SPACE:
  127.                     stop_sound.play(-1)
  128.                     pygame.mixer.music.stop()
  129.                     stop = not stop
  130.                     suspend_picture = not suspend_picture
  131.                     if stop:
  132.                         pygame.mixer.music.play()

  133.         # 控制游戏流程
  134.         if stop and she_life > 0:
  135.             stop_sound.stop()
  136.             # 获取用户按键
  137.             key_pressed = pygame.key.get_pressed()
  138.             
  139.             if key_pressed[K_a] or key_pressed[K_LEFT]:# 左
  140.                 move = "Left"

  141.             if key_pressed[K_d] or key_pressed[K_RIGHT]:# 右
  142.                 move = "right"
  143.                
  144.             if key_pressed[K_w] or key_pressed[K_UP]: # 上
  145.                 move = "upper"

  146.             if key_pressed[K_s] or key_pressed[K_DOWN]: #下
  147.                 move = "lower"

  148.                
  149.             """
  150.                 因为移动蛇头的话,肉眼看到的正常情况是往前进一步,
  151.                 蛇尾少一步所以移动先添加一个,给头减少,
  152.                 另一个作为尾部,用来判断是吃了食物,
  153.                 如果吃了食物直接添加到头部,尾部不动。
  154.                 如果没吃食物则移动时减少尾部,

  155.                 """        
  156.             # 自动移动,当给定的计时器达到判断的数后
  157.             if conut > speed:
  158.                 conut = 0
  159.                 if move == "upper": #上
  160.                     shetou.insert(0,list(shetou[0]))
  161.                     shetou[0][1] -= she_size
  162.                     
  163.                 elif move == "lower":#下
  164.                     shetou.insert(0,list(shetou[0]))
  165.                     shetou[0][1] += she_size
  166.                     
  167.                 elif move == "Left":#左
  168.                     shetou.insert(0,list(shetou[0]))
  169.                     shetou[0][0] -= she_size
  170.                     
  171.                 elif move == "right":#右
  172.                     shetou.insert(0,list(shetou[0]))
  173.                     shetou[0][0] += she_size

  174.                 #  判断是否吃了食物        
  175.                 if shetou[0] == shiwu:
  176.                     food = not food
  177.                     chi.play()
  178.                 else:
  179.                     # 没吃的话移动会减少一个
  180.                     shetou.pop(-1)
  181.                     
  182.                 # 防止新的生成在蛇身上,所有来2个for 循环遍历完全部,有就删除在重新生成
  183.                 if food:
  184.                     for i in shetou_overlap:
  185.                         for b in shetou:
  186.                             if i == b:
  187.                                 #print(b)
  188.                                 shiwu_random.remove(b)
  189.                     #print(shiwu_random)
  190.                     shiwu = choice(shiwu_random)
  191.                     #print(shiwu,"后面")
  192.                     food = not food
  193.                     
  194.                 # 撞墙后
  195.                
  196.                 if shetou[0][0] < 0 or shetou[0][0] > width-she_size\
  197.                 or shetou[0][1] < 0 or shetou[0][1] > height - she_size:
  198.                     she_life -= 1
  199.                     conut = 0
  200.                     shiwu = choice(shiwu_random)
  201.                     shetou = [[4*she_size,3*she_size]]
  202.                     
  203.                 # 判断是否撞到了蛇身
  204.                
  205.                 if len(shetou) > 2:
  206.                     # 副蛇身+= 蛇身,然后判断是否撞蛇身
  207.                     shetou_to += shetou
  208.                     for each in range(len(shetou_to)-1):
  209.                         if shetou_to[each+1] == shetou_to[0]:
  210.                             she_life -= 1
  211.                             conut = 0
  212.                             shiwu = choice(shiwu_random)
  213.                             shetou = [[4*she_size,3*she_size]]
  214.                            
  215.             # 重新赋值。不然一直加值               
  216.             shetou_to = []
  217.             
  218.             #移动蛇身
  219.             for each in shetou:
  220.                     pygame.draw.rect(screen,REN,(each[0],each[1],she_size,she_size),0)
  221.                     pygame.draw.rect(screen,GREEN,(shetou[0][0],shetou[0][1],she_size,she_size),0)
  222.                     pygame.draw.rect(screen,BLACK,(shiwu[0],shiwu[1],she_size,she_size),0)

  223.             conut += 1

  224.         # 暂停时切换的图片
  225.         elif suspend_picture:
  226.             screen.blit(words_text,(width//2 -100, height // 2-50))
  227.             conut = 0
  228.             
  229.         elif she_life <= 0:
  230.             screen.blit(words_text_GAMEOVER,(width//2 -100, height // 2-50))
  231.             screen.blit(wods_text_Reopen,(width // 2-180,height// 2 -150))
  232.             # 停止背景音乐
  233.             pygame.mixer.music.stop()
  234.             
  235.             # 停止全部音效
  236.             pygame.mixer.stop()
  237.             count_GAMEOVER += 1

  238.             # 获取用户按键
  239.             key_pressed = pygame.key.get_pressed()
  240.             
  241.             # 倒计时后退出游戏
  242.             if count_GAMEOVER >= 1000:
  243.                 pygame.quit()
  244.                 sys.exit()
  245.                
  246.             # 倒计时前按下F1则重开
  247.             elif key_pressed[K_F1]:
  248.                 main()
  249.                
  250.         pygame.display.flip()
  251.         # 修改帧数的话会影响速度,默认60就好了
  252.         clock.tick(60)
  253.       
  254. if __name__ == "__main__":
  255.     main()
  256.    
  257.    
复制代码


利用绘图模块整的,算是很完善了,
没有弄游戏开始前的界面,有大佬在的话可以帮改改
没有弄蛇头指定一个方向之后就不能回头,2个蛇身可以回头,3个蛇身就会判断为自杀撞蛇身死完
没有弄计分板一类的,
用现成的素材文字,中文会乱码,就英文代替
大小跟蛇身大小可改,不过结束界面跟暂停界面的文字尺寸应该还是居中的吧,没弄过大一点的游戏尺寸


游戏规则
空格暂停,
生命3条
撞墙死完,
撞自己死亡
吃到10蛇身加速
吃到20蛇身加速
吃到30蛇身加速【可以自己改。代码哪里注释了】
3条命死亡后可以按F1重来,
若是死亡后没按F1,等待一段时间则自动关闭游戏




害。折腾了好几天。掉头发,刚学完基础,日子还长着,
若是代码还有可以完善或者好的逻辑流程,欢迎大佬指出
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-24 18:53:07 | 显示全部楼层
忘了上素材了

bgm.zip

132.04 KB, 下载次数: 7

BGM

bgm.zip

1.48 MB, 下载次数: 4

BGM

font.zip

38.21 KB, 下载次数: 4

bgm.zip

1.48 MB, 下载次数: 2

BGM

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 01:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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