python基于pygame的贪吃蛇大作战

游戏截图

在这里插入图片描述
在这里插入图片描述在这里插入图片描述## 源代码

import pygame,sys,random,os
from pygame.locals import*
pygame.init()
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption('snake')
def print_text(font,x,y,text,color=(255,0,0)):
    imgtext=font.render(text,True,color)
    screen.blit(imgtext,(x,y))
font1=pygame.font.SysFont('arial',24)
font2=pygame.font.SysFont('arial',40)
clock=pygame.time.Clock()
hard=15
flag_play=0
#在桌面新建一个txt文本
file=open('C:\\Users\\admin\\Desktop\\123.text','a+')
size=os.path.getsize('C:\\Users\\admin\\Desktop\\123.text')
if size==0:
    file.write('0\n0\n0\n')
file.close()
#最大值判断
with open(r'C:\\Users\\admin\\Desktop\\123.text','r') as t:
    str1=t.read()
lst=str1.split('\n')
max=0
lst.pop()
for i in lst:
    if max<=int(i):
        max=int(i)

#列表最后元素
near_score=lst.copy().pop()

flag_1=0
flag_2=0
flag_3=0

def rect(point,color):
    left=point.col*20
    top=point.row*20
    pygame.draw.rect(screen,color,(left,top,20,20))

def givefood():
    global pos
    while True:
        pos=point(random.randint(0,29),random.randint(0,39))
        iscoll=False
        if head.row==pos.row and head.col==pos.col:
            iscoll=True
        for i in l:
            if i.row==pos.row and i.col==pos.col:
                iscoll=True
                break
        if not iscoll:
            break
        return pos

row,col=30,40
a,b=20,20
class point():
    def __init__(self,row=0,col=0):
        self.row=row
        self.col=col
    def copy(self):
        return point(row=self.row,col=self.col)

head=point(a,b)
l=[]
givefood()
direct='left'
gameover=False
score=0
black,black1,black2=0,0,0
while True:
    while True:#开始游戏,记分榜,退出游戏
        clock.tick(hard)
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
            if event.type==MOUSEMOTION:
                mouse_x,mouse_y=event.pos
                move_x,move_y=event.rel
            elif event.type==MOUSEBUTTONUP and mouse_x>300 and mouse_x<450 and mouse_y>200 and mouse_y<250:
                flag_play=1#退出该循环,进入游戏开始的循环
            elif event.type==MOUSEBUTTONUP and mouse_x>300 and mouse_x<450 and mouse_y>300 and mouse_y<350:
                pygame.quit()
                sys.exit()
            elif event.type==MOUSEBUTTONUP and mouse_x>300 and mouse_x<450 and mouse_y>400 and mouse_y<450:
                flag_3=1
            '''elif event.type==MOUSEMOTION and mouse_x>300 and mouse_x<450 and mouse_y>200 and mouse_y<250:
                black=1
            elif event.type==MOUSEMOTION and mouse_x>300 and mouse_x<450 and mouse_y>300 and mouse_y<350:
                black1=1
            elif event.type==MOUSEMOTION and mouse_x>300 and mouse_x<450 and mouse_y>400 and mouse_y<450:
                black2=1'''

        if flag_3==1:
            while True:
                for event in pygame.event.get():
                    if event.type==QUIT:
                        pygame.quit()
                        sys.exit()
                
                    elif event.type==MOUSEBUTTONUP and mouse_x>300 and mouse_x<450 and mouse_y>400 and mouse_y<450:
                        flag_1=1
                screen.fill((230,230,230))
                print_text(font1,280,100,'histury highest score:'+str(max),)
                print_text(font1,280,200,'the nearly score:'+str(near_score),)
                pygame.draw.rect(screen,(255,55,55),(300,400,150,50),1)
                print_text(font2,330,400,'back',)


                pygame.display.update()
                if flag_1==1:
                    flag_1=0
                    flag_2=1
                    flag_3=0
                    break
        
        if flag_2==1:
            flag_2=0
            continue
        screen.fill((230,230,230))
        #开始游戏
        pygame.draw.rect(screen,(255,55,55),(300,200,150,50),1)
        
        pygame.draw.rect(screen,(255,55,55),(300,300,150,50),1)
       
        pygame.draw.rect(screen,(255,55,55),(300,400,240,50),1)
        
        print_text(font2,340,200,'play',)
        print_text(font2,340,300,'quit',)
        print_text(font2,300,400,' histury score',)
        if flag_play==1:
            flag_play=0
            break
        pygame.display.update()

    #游戏中
    while True:
        clock.tick(hard)
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
        keys=pygame.key.get_pressed()
        if keys[K_w] and direct!='down':
            direct='up'
            pass
           
        elif keys[K_s] and direct!='up':
            direct='down'
            pass
           
        elif keys[K_a] and direct!='right':
            direct='left'
            pass
           
        elif keys[K_d] and direct!='left':
            direct='right'
            pass
           
        elif keys[K_ESCAPE]:
            pygame.quit()
            sys.exit()

        if direct=='left' and gameover==False:
            head.col-=1
        elif direct=='right' and gameover==False:
            head.col+=1
        elif direct=='up' and gameover==False:
            head.row-=1
        elif direct=='down' and gameover==False:
            head.row+=1  
            
        screen.fill((230,230,230))

        #判断吃身体
        for k in l:
            if k.row==head.row and k.col==head.col:
                gameover=True
                with open('C:\\Users\\admin\\Desktop\\123.text','a+') as t:
                    t.write(str(score)+'\n')
                    break
                

        l.insert(0,head.copy())

        #蛇头
        rect(head,(255,0,0))
        rect(pos,(0,255,0))
        for i in l:
            rect(i,(130,130,130))
        eat=(head.row==pos.row and head.col==pos.col) 
        if not eat:
            l.pop()
        if eat:
            score+=10
            pos=point(random.randint(0,29),random.randint(0,39))

        #边框限制
        
        if head.row<0 or head.row>29 or head.col<0 or head.col>39:
            gameover=True
            with open('C:\\Users\\admin\\Desktop\\123.text','a+') as t:
                t.write(str(score)+'\n')
                
                break
        if gameover:
            break 
        print_text(font1,0,0,'score:'+str(score))
        #判断分数,分数越高,速度越快
        if score>=200:
            hard=17
        elif score>=300:
            hard=19
        elif score>=400:
            hard=21
        elif score>=500:
            hard=23
        elif score>=600:
            hard=25
        elif score>=700:
            hard=27
        
        pygame.display.update()

    #(游戏结束,本轮成绩),再来一局,退出游戏
    while True:
        clock.tick(hard)
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
            if event.type==MOUSEMOTION:
                mouse_x,mouse_y=event.pos
                move_x,move_y=event.rel
            elif event.type==MOUSEBUTTONUP and mouse_x>300 and mouse_x<450 and mouse_y>200 and mouse_y<250:
                flag_play=1#退出该循环,再来一局
                score=0
                for i in range(len(l)):
                    l.pop()
                gameover=False
                head=point(a,b)
                hard=15
            elif event.type==MOUSEBUTTONUP and mouse_x>300 and mouse_x<450 and mouse_y>300 and mouse_y<350:
                pygame.quit()
                sys.exit()
        screen.fill((230,230,230))
        print_text(font1,300,100,'gameover',)
        print_text(font1,300,150,'your score:'+str(score),)
        pygame.draw.rect(screen,(255,55,55),(300,200,180,50),1)
        print_text(font2,300,200,'play again',)
        pygame.draw.rect(screen,(255,55,55),(300,300,150,50),1)
        print_text(font2,340,300,'quit',)
        if flag_play==1:
            flag_play=0
            break
        pygame.display.update()

python写的僵尸世界大战文字游戏

发布了29 篇原创文章 · 获赞 129 · 访问量 8718

猜你喜欢

转载自blog.csdn.net/weixin_44072077/article/details/101228671