鱼C论坛

 找回密码
 立即注册
查看: 953|回复: 5

[已解决]制作贪吃蛇遇到了问题

[复制链接]
发表于 2019-3-27 09:42:32 | 显示全部楼层 |阅读模式

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

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

x
学习完‘摩擦摩擦’后自己想做个贪吃蛇,但是不知道怎么让蛇每次吃完后变长,请高手提供思路,我的代码如下:
  1. import pygame
  2. import sys
  3. import os
  4. from pygame.locals import *
  5. from random import *

  6. class Snake(pygame.sprite.Sprite):
  7.     def __init__(self, image, position, speed, size):
  8.         pygame.sprite.Sprite.__init__(self)
  9.         self.image = pygame.image.load(image)
  10.         self.rect = self.image.get_rect()
  11.         self.rect.left, self.rect.top = position
  12.         self.speed = speed
  13.         # 设置长宽用于检测小球是否到达边界
  14.         self.width = size[0]
  15.         self.height = size[1]

  16.     def move(self):
  17.         self.rect = self.rect.move(self.speed)

  18. class Food(pygame.sprite.Sprite):
  19.     def __init__(self, image, position):
  20.         pygame.sprite.Sprite.__init__(self)
  21.         self.image = pygame.image.load(image)
  22.         self.rect = self.image.get_rect()
  23.         self.rect.left, self.rect.top = position

  24. def wellcome_page():
  25.     pygame.init()
  26.     # 设置屏幕
  27.     size = width, height = 400, 400
  28.     screen = pygame.display.set_mode(size)
  29.     pygame.display.set_caption("贪吃蛇")
  30.     while True:
  31.         for event in pygame.event.get():
  32.             if event.type == pygame.QUIT:
  33.                 pygame.quit()
  34.                 sys.exit()
  35.             if event.type == KEYDOWN:
  36.                 if event.key == K_SPACE:
  37.                     main()
  38.         screen.fill((255, 255, 255))
  39.         font = pygame.font.Font('C:\Windows\Fonts\simhei.ttf', 50)  # 黑体
  40.         screen.blit(font.render('按空格键继续', True, (0, 0, 0)), (50, 50))
  41.         pygame.display.flip()
  42.    
  43.    

  44. def main():
  45.     pygame.init()
  46.     # 获得当前路径
  47.     current_path = os.path.dirname(__file__)
  48.     # 设置颜色
  49.     blue = (0, 255, 255)
  50.     black = (0, 0, 0)
  51.     white = (255, 255, 255)
  52.     # 设置屏幕
  53.     size = width, height = 400, 400
  54.     screen = pygame.display.set_mode(size)
  55.     pygame.display.set_caption("贪吃蛇")
  56.     # 设置帧率
  57.     clock = pygame.time.Clock()
  58.     # 导入图片
  59.     snake_img = 'snake.png'
  60.     food_img = 'food.png'
  61.     # 创建蛇对象
  62.     position = width // 2, height // 2
  63.     speed = [0, 0]
  64.     snake = Snake(snake_img, position, speed, size)
  65.     # 创建食物对象
  66.     position_food = randint(0, width - 20), randint(0, height - 20)
  67.     food = Food(food_img, position_food)

  68.     running = True
  69.     while running:
  70.         for event in pygame.event.get():
  71.             if event.type == pygame.QUIT:
  72.                 pygame.quit()
  73.                 sys.exit()
  74.             elif event.type == KEYDOWN:
  75.                 if event.key == K_UP and snake.speed[1] != 15:
  76.                     snake.speed = [0, -15]
  77.                 if event.key == K_DOWN and snake.speed[1] != -15:
  78.                     snake.speed = [0, 15]
  79.                 if event.key == K_LEFT and snake.speed[0] != 15:
  80.                     snake.speed = [-15, 0]
  81.                 if event.key == K_RIGHT and snake.speed[0] != -15:
  82.                     snake.speed = [15, 0]
  83.         # 绘制背景
  84.         screen.fill(blue)
  85.         # 绘制蛇
  86.         screen.blit(snake.image, snake.rect)
  87.         # 解决提前撞墙问题
  88.         if snake.width - 1 > snake.rect.right > snake.width - 15:
  89.             snake.speed = [snake.width - snake.rect.right, 0]
  90.         elif 1 < snake.rect.top < 15:
  91.             snake.speed = [0, -snake.rect.top]
  92.         elif 1 < snake.rect.left < 15:
  93.             snake.speed = [-snake.rect.left, 0]
  94.         elif snake.height - 1 > snake.rect.bottom > snake.height - 15:
  95.             snake.speed = [0, snake.rect.bottom - snake.width]
  96.         # 移动蛇
  97.         snake.move()
  98.         if snake.rect.left < 0:
  99.             running = False
  100.         elif snake.rect.right > snake.width:
  101.             running = False
  102.         elif snake.rect.top < 0:
  103.             running = False
  104.         elif snake.rect.bottom > snake.height:
  105.             running = False

  106.         # 绘制食物
  107.         screen.blit(food.image,food.rect)
  108.         # 碰撞检测
  109.         group = pygame.sprite.Group()
  110.         group.add(food)
  111.         if pygame.sprite.spritecollide(snake, group, False):
  112.             food.rect.left, food.rect.top = randint(0, width - 20), randint(0, height - 20)

  113.         pygame.display.flip()
  114.         clock.tick(5)

  115.     wellcome_page()

  116. if __name__ == '__main__':
  117.     wellcome_page()

复制代码
最佳答案
2019-3-28 17:57:09
xyzhu 发表于 2019-3-28 16:45
请问谁有飞机大战的图片和声音素材?我想自己动手练一练

自己用画图软件先画一个不就行了

这些都是做UI的需要解决的事情。

大圆圈代表飞机,中圆圈代表敌方飞机,小圆圈代表子弹
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-27 09:55:25 | 显示全部楼层
一般按思维来说是你把食物方块吃了,方块就变成了蛇头,蛇头的速度、方向和原蛇头方向一致
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-28 16:43:58 | 显示全部楼层
终于解决了,感觉提升很多,棒棒的
  1. import pygame
  2. import sys
  3. import os
  4. from pygame.locals import *
  5. from random import *

  6. class Snake(pygame.sprite.Sprite):
  7.     def __init__(self, image, position, speed, size):
  8.         pygame.sprite.Sprite.__init__(self)
  9.         self.image = pygame.image.load(image)
  10.         self.rect = self.image.get_rect()
  11.         self.rect.left, self.rect.top = position
  12.         self.speed = speed
  13.         # 设置长宽用于检测小球是否到达边界
  14.         self.width = size[0]
  15.         self.height = size[1]

  16.     def move(self):
  17.         self.rect = self.rect.move(self.speed)

  18. class Food(pygame.sprite.Sprite):
  19.     def __init__(self, image, position):
  20.         pygame.sprite.Sprite.__init__(self)
  21.         self.image = pygame.image.load(image)
  22.         self.rect = self.image.get_rect()
  23.         self.rect.left, self.rect.top = position

  24. class Tail(pygame.sprite.Sprite):
  25.     def __init__(self, image, position):
  26.         pygame.sprite.Sprite.__init__(self)
  27.         self.image = pygame.image.load(image)
  28.         self.rect = self.image.get_rect()
  29.         self.rect.left = position[0]
  30.         self.rect.top = position[1]

  31. def wellcome_page():
  32.     pygame.init()
  33.     # 设置屏幕
  34.     size = width, height = 400, 400
  35.     screen = pygame.display.set_mode(size)
  36.     pygame.display.set_caption("贪吃蛇")
  37.     while True:
  38.         for event in pygame.event.get():
  39.             if event.type == pygame.QUIT:
  40.                 pygame.quit()
  41.                 sys.exit()
  42.             if event.type == KEYDOWN:
  43.                 if event.key == K_SPACE:
  44.                     main()
  45.         screen.fill((255,255,255))
  46.         font = pygame.font.Font('C:\Windows\Fonts\simhei.ttf', 50)  # 黑体
  47.         screen.blit(font.render('按空格键继续', True, (0, 0, 0)), (50, 50))
  48.         pygame.display.flip()
  49.    
  50.    

  51. def main():
  52.     pygame.init()
  53.     # 获得当前路径
  54.     current_path = os.path.dirname(__file__)
  55.     # 设置颜色
  56.     blue = (0, 255, 255)
  57.     black = (0, 0, 0)
  58.     white = (255, 255, 255)
  59.     # 设置屏幕
  60.     size = width, height = 400, 400
  61.     screen = pygame.display.set_mode(size)
  62.     pygame.display.set_caption("贪吃蛇")
  63.     # 设置帧率
  64.     clock = pygame.time.Clock()
  65.     # 导入图片
  66.     snake_img = 'snake.png'
  67.     food_img = 'food.png'
  68.     tail_img = 'snake.png'
  69.     # 创建蛇对象
  70.     position = width // 2, height // 2
  71.     speed = [0, 0]
  72.     snake = Snake(snake_img, position, speed, size)
  73.     # 创建食物对象
  74.     position_food = randint(0, width - 20), randint(0, height - 20)
  75.     food = Food(food_img, position_food)

  76.     tail_list = []
  77.     tails_pos = [(0, 0)]
  78.     tails_num = 0

  79.     running = True
  80.     while running:
  81.         for event in pygame.event.get():
  82.             if event.type == pygame.QUIT:
  83.                 pygame.quit()
  84.                 sys.exit()
  85.             elif event.type == KEYDOWN:
  86.                 if event.key == K_UP and snake.speed[1] != 15:
  87.                     snake.speed = [0, -15]
  88.                 if event.key == K_DOWN and snake.speed[1] != -15:
  89.                     snake.speed = [0, 15]
  90.                 if event.key == K_LEFT and snake.speed[0] != 15:
  91.                     snake.speed = [-15, 0]
  92.                 if event.key == K_RIGHT and snake.speed[0] != -15:
  93.                     snake.speed = [15, 0]
  94.         # 绘制背景
  95.         screen.fill(blue)
  96.         # 绘制蛇
  97.         screen.blit(snake.image, snake.rect)
  98.         # 解决提前撞墙问题
  99.         if snake.width - 1 > snake.rect.right > snake.width - 15:
  100.             snake.speed = [snake.width - snake.rect.right, 0]
  101.         elif 1 < snake.rect.top < 15:
  102.             snake.speed = [0, -snake.rect.top]
  103.         elif 1 < snake.rect.left < 15:
  104.             snake.speed = [-snake.rect.left, 0]
  105.         elif snake.height - 1 > snake.rect.bottom > snake.height - 15:
  106.             snake.speed = [0, snake.rect.bottom - snake.width]
  107.         # 每个尾巴位置更新
  108.         del tails_pos[-1]
  109.         tails_pos.insert(0, (snake.rect.left, snake.rect.top))

  110.         if tail_list:
  111.             i = 0
  112.             for each in tail_list:
  113.                 screen.blit(each.image, each.rect)
  114.                 each.rect.left = tails_pos[i][0]
  115.                 each.rect.top = tails_pos[i][1]
  116.                 i += 1

  117.         # 移动蛇
  118.         snake.move()
  119.         if snake.rect.left < 0:
  120.             running = False
  121.         elif snake.rect.right > snake.width:
  122.             running = False
  123.         elif snake.rect.top < 0:
  124.             running = False
  125.         elif snake.rect.bottom > snake.height:
  126.             running = False

  127.         # 绘制食物
  128.         screen.blit(food.image,food.rect)
  129.         # 碰撞检测
  130.         group = pygame.sprite.Group()
  131.         group.add(food)
  132.         if pygame.sprite.spritecollide(snake,group,False):
  133.             food.rect.left, food.rect.top = randint(0, width - 20), randint(0, height - 20)
  134.             position_tail = tails_pos[tails_num][0], tails_pos[tails_num][1]
  135.             tail = Tail(tail_img, position_tail)
  136.             tail_list.append(tail)
  137.             tails_pos.append((0, 0))
  138.             tails_num += 1
  139.             #print(tails_num)
  140.             #print(tail_list)

  141.         pygame.display.flip()
  142.         clock.tick(5)

  143.     wellcome_page()


  144. if __name__ == '__main__':
  145.     wellcome_page()

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

使用道具 举报

 楼主| 发表于 2019-3-28 16:45:56 | 显示全部楼层
请问谁有飞机大战的图片和声音素材?我想自己动手练一练
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-28 17:57:09 | 显示全部楼层    本楼为最佳答案   
xyzhu 发表于 2019-3-28 16:45
请问谁有飞机大战的图片和声音素材?我想自己动手练一练

自己用画图软件先画一个不就行了

这些都是做UI的需要解决的事情。

大圆圈代表飞机,中圆圈代表敌方飞机,小圆圈代表子弹
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-29 10:32:48 | 显示全部楼层
cwhsmile 发表于 2019-3-28 17:57
自己用画图软件先画一个不就行了

这些都是做UI的需要解决的事情。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-11 02:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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