# -*- coding: utf-8 -*-
"""
Created on Wed May 26 15:42:02 2021
@author: Administrator
"""
import pygame,sys
import random
import time
from pygame.locals import *
fps=5
wall_length=500
wall_width=400
screen=pygame.display.set_mode((wall_length,wall_width))
pygame.display.set_caption("贪吃蛇")
#蛇
snake_size=10
snake=[[100,100],[90,100],[80,100]]
head=[70,100]
#颜色各RGB值
white=[255,255,255]
black=(0,0,0)
color0=(200,200,0)
color1=(0,250,255)
color2=(250,100,100)
#初始化蛇移动的方向
direction='right'
#控制移动方向
direction2='0'
#食物是否被吃
ate=0
def barrier(screen):
b=list()
for i in range(40):
x=[random.randint(0,(wall_length-snake_size)/snake_size),random.randint(0,(wall_width-snake_size)/snake_size)]
if [x[0]*snake_size,x[1]*snake_size] in snake:
x=[random.randint(0,(wall_length-snake_size)/snake_size),random.randint(0,(wall_width-snake_size)/snake_size)]
print(1)
b.append(x)
return b
barrier=barrier(screen)
def randFoodPos(snake):
flag=0
while True:
a=[random.randint(0,(wall_length-snake_size)/snake_size),random.randint(0,(wall_width-snake_size)/snake_size)]
if [a[0]*snake_size,a[1]*snake_size] not in snake and a not in barrier:
flag=1
return a
else:
print("default")
if flag==1:
break
rf=[12,12]
def draw_flip(screen,snake,head,rf):
for i in snake:
if i==head:
pygame.draw.rect(screen,color0,[i[0],i[1],snake_size,snake_size])
else:
pygame.draw.rect(screen,color1,[i[0],i[1],snake_size,snake_size])
pygame.draw.line(screen,black,[i[0],i[1]],[i[0],i[1]+10],1)
pygame.draw.line(screen,black,[i[0],i[1]],[i[0]+10,i[1]],1)
pygame.draw.line(screen,black,[i[0]+10,i[1]],[i[0]+10,i[1]+10],1)
pygame.draw.line(screen,black,[i[0],i[1]+10],[i[0]+10,i[1]+10],1)
pygame.draw.rect(screen,color2,[rf[0]*snake_size,rf[1]*snake_size,snake_size,snake_size])
for i in barrier:
pygame.draw.rect(screen,white,[i[0]*snake_size,i[1]*snake_size,snake_size,snake_size])
pygame.draw.line(screen,black,[i[0]*snake_size,i[1]*snake_size],[i[0]*snake_size,i[1]*snake_size+10],1)
pygame.draw.line(screen,black,[i[0]*snake_size,i[1]*snake_size],[i[0]*snake_size+10,i[1]*snake_size],1)
pygame.draw.line(screen,black,[i[0]*snake_size+10,i[1]*snake_size],[i[0]*snake_size+10,i[1]*snake_size+10],1)
pygame.draw.line(screen,black,[i[0]*snake_size,i[1]*snake_size+10],[i[0]*snake_size+10,i[1]*snake_size+10],1)
def gameover(screen,snake,head):
if head[0]+10==0 or head[0]==wall_length or head[1]+10==0 or head[1]==wall_width:
return 1
for i in range(1,len(snake)):
if head==snake[i]:
return 1
for i in barrier:
if head==[i[0]*snake_size,i[1]*snake_size]:
return 1
return 0
def change_direction(direction,direction2):
if direction=='right' or direction=='left':
if direction2=='up':
direction='up'
elif direction2=='down':
direction='down'
elif direction=='up' or direction=='down':
if direction2=='right':
direction='right'
elif direction2=='left':
direction='left'
return direction
def begin(screen):
screen.fill(black)
pygame.font.init()
# 通过字体文件获得字体对象
fontObj = pygame.font.SysFont('SimHei', 50)
# 配置要显示的文字
textSurfaceObj = fontObj.render('开始', True,white,black)
# 获得要显示的对象的rect
textRectObj = textSurfaceObj.get_rect()
# 设置显示对象的坐标
textRectObj.center = (250,200)
# 绘制字体
screen.blit(textSurfaceObj, textRectObj)
pygame.display.update()
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
if 200<=pos[0]<=300 and 175<=pos[1]<=225:
return 1
elif event.type==QUIT:
pygame.quit()
sys.exit()
def level_choose(screen):
pygame.font.init()
fontObj=pygame.font.SysFont('SimHei', 30)
textSurfaceObj=fontObj.render('难度选择:', True, white,black)
textRectObj=textSurfaceObj.get_rect()
textRectObj.center=(250,100)
screen.blit(textSurfaceObj, textRectObj)
textSurfaceObj=fontObj.render('简单', True, white,black)
textRectObj=textSurfaceObj.get_rect()
textRectObj.center=(250,200)
screen.blit(textSurfaceObj, textRectObj)
textSurfaceObj=fontObj.render('普通', True, white,black)
textRectObj=textSurfaceObj.get_rect()
textRectObj.center=(250,250)
screen.blit(textSurfaceObj, textRectObj)
textSurfaceObj=fontObj.render('困难', True, white,black)
textRectObj=textSurfaceObj.get_rect()
textRectObj.center=(250,300)
screen.blit(textSurfaceObj, textRectObj)
pygame.display.update()
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
if 200<=pos[0]<=300:
if 175<pos[1]<225:
return 1
elif 225<pos[1]<275:
return 2
elif 275<pos[1]<325:
return 3
elif event.type==QUIT:
pygame.quit()
sys.exit()
def restart(screen,snake,head):
pygame.font.init()
fontObj=pygame.font.SysFont('SimHei', 15)
textSurfaceObj=fontObj.render('重新开始', True, white,black)
textRectObj=textSurfaceObj.get_rect()
textRectObj.center=(450,350)
screen.blit(textSurfaceObj, textRectObj)
pygame.display.update()
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
if 425<=pos[0]<=475 and 325<=pos[1]<375:
return 1
return 0
def initial_snake(sreen):
snake=[[100,100],[90,100],[80,100]]
head=[70,100]
direction='right'
return [snake,head,direction]
def move(head,direction):
if direction=='right':
head[0]+=snake_size
elif direction=='left':
head[0]-=snake_size
elif direction=='up':
head[1]-=snake_size
elif direction=='down':
head[1]+=snake_size
def main(screen,direction,direction2,snake,head,rf,ate):
while True:
if level_choose(screen)==1:
fps=5
break
elif level_choose(screen)==2:
fps=10
break
elif level_choose(screen)==3:
fps=15
break
while True:
if begin(screen)==1:
break
#获得pygame的时钟
fpsClock = pygame.time.Clock()
pygame.mixer.init(frequency=15500,size=-16,channels=4)
#加载背景音乐文件
pygame.mixer.music.load('F:\\music\\bgm\\贪吃蛇bgm.wav')
#播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第二个参数是设置播放的起点(单位为秒)
pygame.mixer.music.play(-1, 0.0)
while True:
screen.fill(black)
move(head,direction)
for event in pygame.event.get():
if event.type == QUIT:
#停止播放背景音乐
pygame.mixer.music.stop()
pygame.quit()
sys.exit()
elif event.type==KEYDOWN:
if event.key==K_a:
direction2='left'
elif event.key==K_s:
direction2='down'
elif event.key==K_w:
direction2='up'
elif event.key==K_d:
direction2='right'
elif event.key==K_SPACE:
time.sleep(8)
direction=change_direction(direction,direction2)
snake.insert(0,list(head))
if head[0]==rf[0]*snake_size and head[1]==rf[1]*snake_size:
sound = pygame.mixer.Sound('F:\\music\\bgm\\吃食音效.mp3')
sound.play()
ate=1
else:
snake.pop()
if ate==1:
rf=randFoodPos(snake)
ate=0
if gameover(screen,snake,head)==1:
#加载并播放一个特效音频文件
sound = pygame.mixer.Sound('F:\\music\\bgm\\死亡音效.wav')
sound.play()
# 停止播放背景音乐
pygame.mixer.music.stop()
time.sleep(3)
while True:
pygame.font.init()
fontObj=pygame.font.SysFont('SimHei', 50)
textSurfaceObj=fontObj.render('游戏结束', True, white,black)
textRectObj=textSurfaceObj.get_rect()
textRectObj.center=(250,200)
screen.blit(textSurfaceObj, textRectObj)
pygame.display.update()
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
if 200<=pos[0]<=300 and 150<=pos[1]<250:
pygame.quit()
sys.exit()
elif event.type==QUIT:
pygame.quit()
sys.exit()
if restart(screen, snake, head)==1:
snake=initial_snake(screen)[0]
head=initial_snake(screen)[1]
direction=initial_snake(screen)[2]
pygame.mixer.init(frequency=15500,size=-16,channels=4)
#加载背景音乐文件
pygame.mixer.music.load('F:\\music\\bgm\\贪吃蛇bgm.wav')
#播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第二个参数是设置播放的起点(单位为秒)
pygame.mixer.music.play(-1, 0.0)
break
draw_flip(screen,snake,head,rf)
#刷新屏幕
pygame.display.update()
# 设置pygame时钟的间隔时间
fpsClock.tick(fps)
if __name__=='__main__':
main(screen,direction,direction2,snake,head,rf,ate)