ad208三国

 找回密码
 加入
搜索
热搜: 活动 交友 discuz
查看: 1353|回复: 0

python贪吃蛇源码

[复制链接]
发表于 2019-5-11 12:02:55 | 显示全部楼层 |阅读模式

pc2g,电脑好游戏


代码比较完善 不过还是有bug
代码需要pygame库 请自行下载
  1. #!/usr/bin/env python
  2. import pygame,sys,time,random
  3. from pygame.locals import *
  4. # 定义颜色变量
  5. redColour = pygame.Color(255,46,10)
  6. blackColour = pygame.Color(0,0,16)
  7. whiteColour = pygame.Color(255,55,255)
  8. greyColour = pygame.Color(150,10,150)

  9. # 定义gameOver函数
  10. def gameOver(playSurface):
  11.     gameOverFont = pygame.font.Font('arial.ttf',72)
  12.     gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
  13.     gameOverRect = gameOverSurf.get_rect()
  14.     gameOverRect.midtop = (320, 10)
  15.     playSurface.blit(gameOverSurf, gameOverRect)
  16.     pygame.display.flip()
  17.     time.sleep(5)
  18.     pygame.quit()
  19.     sys.exit()

  20. # 定义main函数
  21. def main():
  22.     # 初始化pygame
  23.     pygame.init()
  24.     fpsClock = pygame.time.Clock()
  25.     # 创建pygame显示层
  26.     playSurface = pygame.display.set_mode((640,480))
  27.     pygame.display.set_caption('Raspberry Snake')

  28.     # 初始化变量
  29.     snakePosition = [100,100]
  30.     snakeSegments = [[100,100],[80,100],[60,100]]
  31.     raspberryPosition = [300,300]
  32.     raspberrySpawned = 1
  33.     direction = 'right'
  34.     changeDirection = direction
  35.     while True:
  36.         # 检测例如按键等pygame事件
  37.         for event in pygame.event.get():
  38.             if event.type == QUIT:
  39.                 pygame.quit()
  40.                 sys.exit()
  41.             elif event.type == KEYDOWN:
  42.                 # 判断键盘事件
  43.                 if event.key == K_RIGHT or event.key == ord('d'):
  44.                     changeDirection = 'right'
  45.                 if event.key == K_LEFT or event.key == ord('a'):
  46.                     changeDirection = 'left'
  47.                 if event.key == K_UP or event.key == ord('w'):
  48.                     changeDirection = 'up'
  49.                 if event.key == K_DOWN or event.key == ord('s'):
  50.                     changeDirection = 'down'
  51.                 if event.key == K_ESCAPE:
  52.                     pygame.event.post(pygame.event.Event(QUIT))
  53.         # 判断是否输入了反方向
  54.         if changeDirection == 'right' and not direction == 'left':
  55.             direction = changeDirection
  56.         if changeDirection == 'left' and not direction == 'right':
  57.             direction = changeDirection
  58.         if changeDirection == 'up' and not direction == 'down':
  59.             direction = changeDirection
  60.         if changeDirection == 'down' and not direction == 'up':
  61.             direction = changeDirection
  62.         # 根据方向移动蛇头的坐标
  63.         if direction == 'right':
  64.             snakePosition[0] += 20
  65.         if direction == 'left':
  66.             snakePosition[0] -= 20
  67.         if direction == 'up':
  68.             snakePosition[1] -= 20
  69.         if direction == 'down':
  70.             snakePosition[1] += 20
  71.         # 增加蛇的长度
  72.         snakeSegments.insert(0,list(snakePosition))
  73.         # 判断是否吃掉了树莓
  74.         if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
  75.             raspberrySpawned = 0
  76.         else:
  77.             snakeSegments.pop()
  78.         # 如果吃掉树莓,则重新生成树莓
  79.         if raspberrySpawned == 0:
  80.             x = random.randrange(1,32)
  81.             y = random.randrange(1,24)
  82.             raspberryPosition = [int(x*20),int(y*20)]
  83.             raspberrySpawned = 1
  84.         # 绘制pygame显示层
  85.         playSurface.fill(blackColour)
  86.         for position in snakeSegments:
  87.             pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],20,20))
  88.             pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1],20,20))

  89.         # 刷新pygame显示层
  90.         pygame.display.flip()
  91.         # 判断是否死亡
  92.         if snakePosition[0] > 620 or snakePosition[0] < 0:
  93.             gameOver(playSurface)
  94.         if snakePosition[1] > 460 or snakePosition[1] < 0:
  95.             for snakeBody in snakeSegments[1:]:
  96.                 if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
  97.                     gameOver(playSurface)
  98.         # 控制游戏速度
  99.         fpsClock.tick(5)

  100. if __name__ == "__main__":
  101.     main()
复制代码



Archiver|手机版|小黑屋|ad208三国

GMT+8, 2024-4-25 21:04

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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