欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python贪吃蛇游戏代码

程序员文章站 2023-11-12 22:45:52
本文实例为大家分享了python贪吃蛇游戏的具体代码,供大家参考,具体内容如下 贪吃蛇游戏截图: 首先安装pygame,可以使用pip安装pygame: pi...

本文实例为大家分享了python贪吃蛇游戏的具体代码,供大家参考,具体内容如下

贪吃蛇游戏截图:

python贪吃蛇游戏代码

首先安装pygame,可以使用pip安装pygame:

pip install pygame

运行以下代码即可:

#!/usr/bin/env python
import pygame,sys,time,random
from pygame.locals import *
# 定义颜色变量
redcolour = pygame.color(255,0,0)
blackcolour = pygame.color(0,0,0)
whitecolour = pygame.color(255,255,255)
greycolour = pygame.color(150,150,150)

# 定义gameover函数
def gameover(playsurface):
 gameoverfont = pygame.font.font('arial.ttf',72)
 gameoversurf = gameoverfont.render('game over', true, greycolour)
 gameoverrect = gameoversurf.get_rect()
 gameoverrect.midtop = (320, 10)
 playsurface.blit(gameoversurf, gameoverrect)
 pygame.display.flip()
 time.sleep(5)
 pygame.quit()
 sys.exit()

# 定义main函数
def main():
 # 初始化pygame
 pygame.init()
 fpsclock = pygame.time.clock()
 # 创建pygame显示层
 playsurface = pygame.display.set_mode((640,480))
 pygame.display.set_caption('raspberry snake')

 # 初始化变量
 snakeposition = [100,100]
 snakesegments = [[100,100],[80,100],[60,100]]
 raspberryposition = [300,300]
 raspberryspawned = 1
 direction = 'right'
 changedirection = direction
 while true:
 # 检测例如按键等pygame事件
 for event in pygame.event.get():
  if event.type == quit:
  pygame.quit()
  sys.exit()
  elif event.type == keydown:
  # 判断键盘事件
  if event.key == k_right or event.key == ord('d'):
   changedirection = 'right'
  if event.key == k_left or event.key == ord('a'):
   changedirection = 'left'
  if event.key == k_up or event.key == ord('w'):
   changedirection = 'up'
  if event.key == k_down or event.key == ord('s'):
   changedirection = 'down'
  if event.key == k_escape:
   pygame.event.post(pygame.event.event(quit))
 # 判断是否输入了反方向
 if changedirection == 'right' and not direction == 'left':
  direction = changedirection
 if changedirection == 'left' and not direction == 'right':
  direction = changedirection
 if changedirection == 'up' and not direction == 'down':
  direction = changedirection
 if changedirection == 'down' and not direction == 'up':
  direction = changedirection
 # 根据方向移动蛇头的坐标
 if direction == 'right':
  snakeposition[0] += 20
 if direction == 'left':
  snakeposition[0] -= 20
 if direction == 'up':
  snakeposition[1] -= 20
 if direction == 'down':
  snakeposition[1] += 20
 # 增加蛇的长度
 snakesegments.insert(0,list(snakeposition))
 # 判断是否吃掉了树莓
 if snakeposition[0] == raspberryposition[0] and snakeposition[1] == raspberryposition[1]:
  raspberryspawned = 0
 else:
  snakesegments.pop()
 # 如果吃掉树莓,则重新生成树莓
 if raspberryspawned == 0:
  x = random.randrange(1,32)
  y = random.randrange(1,24)
  raspberryposition = [int(x*20),int(y*20)]
  raspberryspawned = 1
 # 绘制pygame显示层
 playsurface.fill(blackcolour)
 for position in snakesegments:
  pygame.draw.rect(playsurface,whitecolour,rect(position[0],position[1],20,20))
  pygame.draw.rect(playsurface,redcolour,rect(raspberryposition[0], raspberryposition[1],20,20))

 # 刷新pygame显示层
 pygame.display.flip()
 # 判断是否死亡
 if snakeposition[0] > 620 or snakeposition[0] < 0:
  gameover(playsurface)
 if snakeposition[1] > 460 or snakeposition[1] < 0:
  for snakebody in snakesegments[1:]:
  if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
   gameover(playsurface)
 # 控制游戏速度
 fpsclock.tick(5)

if __name__ == "__main__":
 main()

操作方法:

上下左右键或wsad键控制

esc键退出游戏

下载代码:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。