之前我做了个贪吃蛇小游戏,这回增加了一个电脑控制的蛇
代码:
import pygame
import random
import time
import os
from pygame.locals import *
from sys import exit
grid_size=25
die1=True
die2=True
die1st=0
die2st=0
die3st=0
die3=True
bug1=0
bug2=0
def init(d):
r1=random.randint(3,33)*grid_size
r2=random.randint(3,23)*grid_size
if abs(d)>=2:
s1b=[[r1,r2],[r1+25,r2],[r1+50*(-d),r2]]
else:
s1b=[[r1,r2],[r1+25*(-d),r2],[r1+50,r2]]
return s1b
pygame.init()
screen = pygame.display.set_mode((900,650))
pygame.display.set_caption("PVP")
yellow=(255,255,0)
state='START'
blue=(0,0,255)
green=(0,255,0)
cyan=(0,255,255)
red=(255,0,0)
orange=(225,175,0)
vel=0.25
white=(255,255,255)
grey=(180,180,180)
def event():
global state,bug1,bug2
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type==KEYDOWN and event.key==K_SPACE:
if state=='START':
state='RUNNING'
if event.type==KEYDOWN and event.key==K_w:
if snake1.direction!=2:
snake1.direction=-2
if event.type==KEYDOWN and event.key==K_s:
if snake1.direction!=-2:
snake1.direction=2
if event.type==KEYDOWN and event.key==K_a:
if snake1.direction!=1:
snake1.direction=-1
if event.type==KEYDOWN and event.key==K_d:
if snake1.direction!=-1:
snake1.direction=1
if event.type==KEYDOWN and event.key==K_UP:
if snake2.direction!=2:
snake2.direction=-2
if event.type==KEYDOWN and event.key==K_DOWN:
if snake2.direction!=-2:
snake2.direction=2
if event.type==KEYDOWN and event.key==K_LEFT:
if snake2.direction!=1:
snake2.direction=-1
if event.type==KEYDOWN and event.key==K_RIGHT:
if snake2.direction!=-1:
snake2.direction=1
if event.type==KEYDOWN and event.key==K_e:
if len(snake1.body)>1:
snake1.acc_st=time.time()
if event.type==KEYDOWN and event.key==K_PAGEDOWN:
if len(snake2.body)>1:
snake2.acc_st=time.time()
if bug1==2 or bug1==3:
if event.type==KEYDOWN and event.key==K_s:
bug1+=1
if bug1==0 or bug1==1:
if event.type==KEYDOWN and event.key==K_w:
bug1+=1
if bug1==4 or bug1==5:
if event.type==KEYDOWN and event.key==K_a:
bug1+=1
if bug1==6 or bug1==7:
if event.type==KEYDOWN and event.key==K_d:
bug1+=1
if bug1==8:
if event.type==KEYDOWN and event.key==K_e:
bug1+=1
if bug1==2 or bug1==3:
if event.type==KEYDOWN and event.key==K_DOWN:
bug2+=1
if bug1==0 or bug1==1:
if event.type==KEYDOWN and event.key==K_UP:
bug2+=1
if bug1==4 or bug1==5:
if event.type==KEYDOWN and event.key==K_LEFT:
bug2+=1
if bug1==6 or bug1==7:
if event.type==KEYDOWN and event.key==K_RIGHT:
bug2+=1
if bug1==8:
if event.type==KEYDOWN and event.key==K_PAGEDOWN:
bug2+=1
def showscore():
fillText('snake1:%s'%str(snake1.score),(0,0))
fillText('snake2:%s'%str(snake2.score),(333,0))
fillText('snake3:%s'%str(snake3.score),(666,0))
class Bean():
def __init__(self,x,y):
self.x=x
self.y=y
def draw(self):
pygame.draw.rect(screen,yellow,(self.x,self.y,grid_size,grid_size),0)
beanlist=[Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size)]
class Magicbean():
def __init__(self,x,y):
self.x=x
self.y=y
def draw(self):
pygame.draw.rect(screen,blue,(self.x,self.y,grid_size,grid_size),0)
magicbeanlist=[Magicbean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size),Magicbean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size)]
a=0
class Snake():
def __init__(self,body,firstcolor,color):
self.body=body
self.fc=firstcolor
self.color=color
self.direction=-1
self.score=len(self.body)
self.acc_st=0
self.acc_et=time.time()
self.vel=0.25
def draw(self):
for i in range(len(self.body)):
if i==0:
pygame.draw.rect(screen,self.fc,(self.body[i][0],self.body[i][1],grid_size,grid_size),0)
else:
pygame.draw.rect(screen,self.color,(self.body[i][0],self.body[i][1],grid_size,grid_size),0)
pygame.draw.rect(screen,self.fc,(self.body[0][0],self.body[0][1],grid_size,grid_size),0)
def run(self):
if abs(self.direction)<2:
self.body.insert(0,[self.body[0][0]+self.direction*grid_size,self.body[0][1]])
else:
self.body.insert(0,[self.body[0][0],self.body[0][1]+self.direction/2*grid_size])
self.body.pop(-1)
def die(self,s,s2):
self.score=len(self.body)
if self.body[0][0]>900 or self.body[0][0]<0 or self.body[0][1]>650 or self.body[0][1]<0:
self.snakebean()
self.body=init(self.direction)
if self.body[0]==s.body[0]:
self.snakebean()
self.body=init(self.direction)
s.snakebean()
s.body=init(s.direction)
for i in range(1,len(self.body)):
if self.body[i]==self.body[0]:
self.snakebean()
self.body=init(self.direction)
break
for i in range(len(s.body)):
if s.body[i]==self.body[0]:
self.snakebean()
self.body=init(self.direction)
break
for i in range(len(s2.body)):
if s2.body[i]==self.body[0]:
self.snakebean()
self.body=init(self.direction)
break
def snakebean(self):
for i in self.body:
beanlist.append(Bean(i[0],i[1]))
def accelerate(self):
global vel
self.acc_et=time.time()
if self.acc_et-self.acc_st<5:
if self.acc_et-self.acc_st>=3:
self.vel=0.25
self.body.pop()
self.acc_st=0
else:
self.vel=0.15
snake1=Snake(init(1),cyan,green)
snake2=Snake(init(1),red,orange)
snake3=Snake(init(1),grey,white)
st1=time.time()
st2=time.time()
st3=time.time()
def EatAndDraw():
global beanlist,a,magicbeanlist,die1,die2,die3
#bean
while True:
for i in range(len(beanlist)):
if [beanlist[i].x,beanlist[i].y]==snake1.body[0]:
snake1.body.append(snake1.body[-1])
beanlist.pop(i)
a=1
break
if [beanlist[i].x,beanlist[i].y]==snake2.body[0]:
snake2.body.append(snake2.body[-1])
beanlist.pop(i)
a=1
break
if [beanlist[i].x,beanlist[i].y]==snake3.body[0]:
snake3.body.append(snake3.body[-1])
beanlist.pop(i)
a=1
break
beanlist[i].draw()
if a==0:
break
a=0
while len(beanlist)<10:
beanlist.append(Bean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size))
while len(beanlist)>100:
beanlist.pop(0)
#magicbean
while True:
for i in range(len(magicbeanlist)):
if [magicbeanlist[i].x,magicbeanlist[i].y]==snake1.body[0]:
b=random.randint(0,10)
if b==1:
b=random.randint(3,10)
elif b>=8:
die1=False
else:
b=3
for j in range(b):
snake1.body.append(snake1.body[-1])
magicbeanlist.pop(i)
a=1
break
if [magicbeanlist[i].x,magicbeanlist[i].y]==snake2.body[0]:
b=random.randint(0,10)
if b==1:
b=random.randint(3,10)
elif b>=8:
die2=False
else:
b=3
for j in range(b):
snake2.body.append(snake2.body[-1])
magicbeanlist.pop(i)
a=1
break
if [magicbeanlist[i].x,magicbeanlist[i].y]==snake3.body[0]:
b=random.randint(0,10)
if b==1:
b=random.randint(3,10)
elif b>=8:
die3=False
else:
b=3
for j in range(b):
snake3.body.append(snake3.body[-1])
magicbeanlist.pop(i)
a=1
break
magicbeanlist[i].draw()
if a==0:
break
a=0
while len(magicbeanlist)<2:
magicbeanlist.append(Magicbean(random.randint(1,35)*grid_size,random.randint(1,25)*grid_size))
computerbeanlist=[]
npc_dis1=[]
npc_dis2=[]
npc_min_dis1=0
npc_min_dis2=0
npc_min=0
computermagicbeanlist=[]
def computer(s):
global npc_dis1,npc_dis2,npc_min_dis,beanlist,magicbeanlist,computerbeanlist,computermagicbeanlist
# if len(computerbeanlist)!=len(beanlist)+2:
# computerbeanlist=beanlist
# computerbeanlist.append(magicbeanlist[0])
# computerbeanlist.append(magicbeanlist[1])
computerbeanlist=[]
npc_dis1=[]
npc_dis2=[]
npc_min_dis1=0
npc_min_dis2=0
npc_min=0
computermagicbeanlist=[]
for i in beanlist:
computerbeanlist.append(abs(i.x-snake3.body[0][0])+abs(i.y-snake3.body[0][1]))
for i in magicbeanlist:
computermagicbeanlist.append(abs(i.x-snake3.body[0][0])+abs(i.y-snake3.body[0][1]))
for i in beanlist:
npc_dis1.append(abs(i.x-snake3.body[0][0])+abs(i.y-snake3.body[0][1]))
npc_min_dis=npc_dis1.index(min(npc_dis1))
for i in magicbeanlist:
npc_dis2.append(abs(i.x-snake3.body[0][0])+abs(i.y-snake3.body[0][1]))
npc_min_dis1=min(npc_dis1)
npc_min_dis2=min(npc_dis2)
if npc_min_dis1<npc_min_dis2:
npc_min_dis=computerbeanlist.index(min([npc_min_dis1,npc_min_dis2]))
npc_min=beanlist[npc_min_dis]
else:
npc_min_dis=computermagicbeanlist.index(min([npc_min_dis1,npc_min_dis2]))
npc_min=magicbeanlist[npc_min_dis]
if abs(npc_min.x-s.body[0][0])>(npc_min.y-s.body[0][1]):
if npc_min.x-s.body[0][0]<0:
if s.direction!=1:
s.direction=-1
else:
while True:
s.direction=random.randint(-2,2)
if s.direction==1:
continue
else:
break
else:
if s.direction!=-1:
s.direction=1
else:
while True:
s.direction=random.randint(-2,2)
if s.direction==-1:
continue
else:
break
else:
if npc_min.y-s.body[0][1]<0:
if s.direction!=2:
s.direction=-2
else:
while True:
s.direction=irandom.randint(-2,2)
if s.directon==2:
continue
else:
break
else:
if s.direction!=-2:
s.direction=2
else:
while True:
s.direction=random.randint(-2,2)
if s.direction==-2:
continue
else:
break
if abs(snake3.direction)==2:
for i in snake2.body:
if snake3.body[0][0]==i[0]:
if snake3.body[0][1]+snake3.direction/2*grid_size==i[1]:
if snake3.direction<0:
snake3.direction+=1
else:
snake3.direction-=1
for i in snake1.body:
if snake3.body[0][0]==i[0]:
if snake3.body[0][1]+snake3.direction/2*grid_size==i[1]:
if snake3.direction<0:
snake3.direction+=1
else:
snake3.direction-=1
for i in snake3.body:
if snake3.body[0][0]==i[0]:
if snake3.body[0][1]+snake3.direction/2*grid_size==i[1]:
if snake3.direction<0:
snake3.direction+=1
else:
snake3.direction-=1
else:
for i in snake2.body:
if snake3.body[0][1]==i[1]:
if snake3.body[0][0]+snake3.direction*grid_size==i[0]:
if snake3.direction<0:
snake3.direction-=1
else:
snake3.direction+=1
for i in snake1.body:
if snake3.body[0][1]==i[1]:
if snake3.body[0][0]+snake3.direction*grid_size==i[0]:
if snake3.direction<0:
snake3.direction-=1
else:
snake3.direction+=1
for i in snake3.body:
if snake3.body[0][1]==i[1]:
if snake3.body[0][0]+snake3.direction*grid_size==i[0]:
if snake3.direction<0:
snake3.direction-=1
else:
snake3.direction+=1
if s.body[0][0]>=900:
if s.direction==1:
s.direction=2
else:
s.direction=-1
if s.body[0][0]<0:
if s.direction==-1:
s.direction=-2
else:
s.direction=1
if s.body[0][1]>=650:
if s.direction==2:
s.direction=1
else:
s.direction=-2
if s.body[0][1]<=0:
if s.direction==-2:
s.direction=-1
else:
s.direction=2
for i in s.body:
if i[0]%25!=0:
i[0]=int(i[0]/25)*25
if i[1]%25!=0:
i[1]=int(i[1]/25)*25
computerbeanlist1=[]
npc_dis11=[]
npc_dis21=[]
npc_min_dis11=0
npc_min_dis21=0
npc_min1=0
computermagicbeanlist1=[]
def bug(s):
global npc_dis11,npc_dis21,npc_min_dis1,beanlist,magicbeanlist,computerbeanlist1,computermagicbeanlist
computerbeanlist1=[]
npc_dis11=[]
npc_dis21=[]
npc_min_dis11=0
npc_min_dis21=0
npc_min1=0
computermagicbeanlist1=[]
for i in beanlist:
computerbeanlist1.append(abs(i.x-s.body[0][0])+abs(i.y-s.body[0][1]))
for i in magicbeanlist:
computermagicbeanlist1.append(abs(i.x-s.body[0][0])+abs(i.y-s.body[0][1]))
for i in beanlist:
npc_dis11.append(abs(i.x-s.body[0][0])+abs(i.y-s.body[0][1]))
npc_min_dis11=npc_dis11.index(min(npc_dis11))
for i in magicbeanlist:
npc_dis21.append(abs(i.x-s.body[0][0])+abs(i.y-s.body[0][1]))
npc_min_dis11=min(npc_dis11)
npc_min_dis21=min(npc_dis21)
if npc_min_dis11<npc_min_dis21:
npc_min_dis11=computerbeanlist1.index(min([npc_min_dis11,npc_min_dis21]))
#print(len(beanlist),' ',npc_min_dis11,' ',computerbeanlist1)
npc_min1=beanlist[npc_min_dis11]
else:
npc_min_dis1=computermagicbeanlist1.index(min([npc_min_dis21,npc_min_dis11]))
npc_min1=magicbeanlist[npc_min_dis1]
if abs(npc_min1.x-s.body[0][0])>(npc_min1.y-s.body[0][1]):
if npc_min1.x-s.body[0][0]<0:
if s.direction!=1:
s.direction=-1
else:
while True:
s.direction=random.randint(-2,2)
if s.direction==1:
continue
else:
break
else:
if s.direction!=-1:
s.direction=1
else:
while True:
s.direction=random.randint(-2,2)
if s.direction==-1:
continue
else:
break
else:
if npc_min1.y-s.body[0][1]<0:
if s.direction!=2:
s.direction=-2
else:
while True:
s.direction=irandom.randint(-2,2)
if s.directon==2:
continue
else:
break
else:
if s.direction!=-2:
s.direction=2
else:
while True:
s.direction=random.randint(-2,2)
if s.direction==-2:
continue
else:
break
if s.body[0][0]>=900:
s.body[0][0]=875
if s.body[0][0]<0:
s.body[0][0]=0
if s.body[0][1]>=650:
s.body[0][1]=625
if s.body[0][1]<=0:
s.body[0][1]=0
def fillText(text, position):
# 设置字体
TextFont = pygame.font.SysFont('微软雅黑', 40)
# 设置字体其他样式
newText = TextFont.render(text, True, (255, 0, 0))
screen.blit(newText, position)
die1et=time.time()
die2et=time.time()
while True:
if state=='RUNNING':
et=time.time()
if et-st1>=snake1.vel:
pygame.draw.rect(screen,(0,0,0),(0,0,1000,750),0)
snake1.run()
snake1.draw()
snake2.draw()
snake3.draw()
st1=time.time()
if et-st2>=snake2.vel:
pygame.draw.rect(screen,(0,0,0),(0,0,1000,750),0)
snake2.run()
snake1.draw()
snake2.draw()
snake3.draw()
st2=time.time()
if et-st3>=snake3.vel:
computer(snake3)
pygame.draw.rect(screen,(0,0,0),(0,0,1000,750),0)
snake3.run()
snake1.draw()
snake2.draw()
snake3.draw()
st3=time.time()
snake1.accelerate()
snake2.accelerate()
snake3.accelerate()
if die1:
snake1.die(snake2,snake3)
die1st=time.time()
else:
die1et=time.time()
if die1et-die1st>=5:
die1=True
fillText(('snake1'+str(6-int(die1et-die1st))),(0,50))
if die2:
snake2.die(snake1,snake3)
die2st=time.time()
else:
die2et=time.time()
if die2et-die2st>=5:
die2=True
fillText(('snake2'+str(6-int(die2et-die2st))),(333,50))
if die3:
snake3.die(snake1,snake2)
die3st=time.time()
else:
die3et=time.time()
if die3et-die3st>=5:
die3=True
fillText(('snake3'+str(6-int(die2et-die2st))),(333,50))
if bug1==9:
die1=False
snake1.vel=0
bug(snake1)
if snake1.score>=120:
bug1=10
showscore()
if bug2==9:
die2=False
snake2.vel=0
bug(snake2)
if snake2.score>=120:
bug2=10
showscore()
EatAndDraw()
showscore()
event()
pygame.display.update()
|