最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pygame實現(xiàn)貪吃蛇小游戲

 更新時間:2022年01月12日 11:05:35   作者:xhh22900  
這篇文章主要為大家詳細介紹了pygame實現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了pygame實現(xiàn)貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下

由于這段時間實在是太聊了,沒什么事做,游戲也玩膩了,所以玩起來pygame。pygame是真的容易上手,但用來做游戲也有很大的局限性,做一些小游戲還是沒什么問題的。

首先,制作游戲最大的問題我認為是確定要制作的游戲的玩法,具體的細節(jié)都確定清楚之后再去實現(xiàn)還是很容易的。所以開發(fā)游戲最大的問題可能是需要一個好的創(chuàng)意,沒有好的創(chuàng)意都沒法開始。貪吃蛇相信沒有人沒玩過了,玩法也很簡單,沒吃一個方塊可以使蛇的身體變長,頭部碰到墻壁或者頭部碰到自己身體就game over了。然后也需要一個計分板記錄分數(shù)。由于只是一個簡單的小游戲,不用一個具體的步驟了,就自己碼代碼的時候不斷完善代碼還是很容易完成的。

初步的版本,實現(xiàn)了蛇的移動和吃方塊身體變長,但還沒有檢測導致game over 的時間。自己想到了不少事情,如可以記錄游戲分數(shù)并保存到本地文件,記錄最高分,但畢竟只是個小游戲,只要保證游戲的完整性就算過關了,再怎么追求完美也不過是用pygame開發(fā)的,其價值始終只在于自娛自樂。之后將這個版本完善一下就算完成了。

import pygame,sys,numpy as nu,random as ra
from pygame.sprite import Sprite,Group
class setting():
? ? def __init__(self,w,h):
? ? ? ? self.w1=0.1*w
? ? ? ? self.w2=0.7*w
? ? ? ? self.w3=0.75*w
? ? ? ? self.w4=0.95*w
? ? ? ? self.h1=0.1*h
? ? ? ? self.h2=0.7*h
? ? ? ? self.length=0.05*self.w1
? ? ? ? self.speed=self.length
? ? ? ? self.wall_length=0.1*self.length
? ? ? ? self.wall_color=(0,0,0)
? ? ? ? self.snake_color=(0,0,230)
? ? ? ? self.head_color=(230,0,0)
class block(Sprite):
? ? def __init__(self,screen,rect,test,color):
? ? ? ? super().__init__()
? ? ? ? self.screen=screen
? ? ? ? self.rect=rect ? ? ?#pygame.Rect(0,0,10,10)
? ? ? ? self.test=test
? ? ? ? self.color=color
? ? def update(self):
? ? ? ? self.rect=self.test(self.rect)
? ? def draw_bullet(self):
? ? ? ? pygame.draw.rect(self.screen,self.color,self.rect)
class textBlock(Sprite):
? ? def __init__(self,screen,rect,color,text,text_color):
? ? ? ? super().__init__()
? ? ? ? self.screen=screen
? ? ? ? self.rect=rect ? ? ?#pygame.Rect(0,0,10,10)
? ? ? ? self.color=color
? ? ? ? self.text_color=text_color
? ? ? ? self.font=pygame.font.SysFont(None,68)
? ? ? ? self.image=self.font.render(text,True,self.text_color,self.color)
? ? ? ? self.image_rect=self.image.get_rect()
? ? ? ? self.image_rect.left=rect.left-200
? ? ? ? self.image_rect.top=rect.top-200
? ? def update(self):
? ? ? ? pass
? ? def changeText(self,text):
? ? ? ? self.image=self.font.render(text,True,self.text_color,self.color)
? ? def draw_bullet(self):
? ? ? ? self.screen.blit(self.image,self.image_rect)
? ? ? ? #pygame.draw.rect(self.screen, self.color, self.rect)
def run_game():
? ? pygame.init()
? ? w,h=1200,800
? ? screen=pygame.display.set_mode((w,h))
? ? pygame.display.set_caption("pygame1")
? ? bg_color=(230,230,230)
? ? settings=setting(w,h)

? ? g,gg=Group(),Group()

? ? a = block(screen, pygame.Rect((settings.w1+settings.w2)/2, (settings.h1+settings.h2)/2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.length, settings.length),lambda r:r,settings.head_color)
? ? aa = block(screen, pygame.Rect((settings.w1 + settings.w2) / 2, (settings.h1 + settings.h2) / 2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.length, settings.length), lambda r: r, settings.snake_color)
? ? g.add(a)
? ? #墻壁
? ? wall=Group()
? ? w1=block(screen, pygame.Rect(settings.w1, settings.h1, settings.wall_length, settings.h2-settings.h1),
? ? ? ? ? ? ? lambda r:r,settings.wall_color)
? ? w2 = block(screen, pygame.Rect(settings.w1, settings.h1, settings.w2-settings.w1, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r,settings.wall_color)
? ? w3 = block(screen, pygame.Rect(settings.w2, settings.h1, settings.wall_length, settings.h2-settings.h1),
? ? ? ? ? ? ? ?lambda r: r,settings.wall_color)
? ? w4 = block(screen, pygame.Rect(settings.w1, settings.h2, settings.w2-settings.w1, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r,settings.wall_color)
? ? wall.add(w1);wall.add(w2);wall.add(w3);wall.add(w4)

? ? w5 = block(screen, pygame.Rect(settings.w3, settings.h1, settings.wall_length, settings.h2 - settings.h1),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? w6 = block(screen, pygame.Rect(settings.w3, settings.h1, settings.w4 - settings.w3, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? w7 = block(screen, pygame.Rect(settings.w4, settings.h1, settings.wall_length, settings.h2 - settings.h1),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? w8 = block(screen, pygame.Rect(settings.w3, settings.h2, settings.w4 - settings.w3, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? wall.add(w5)
? ? wall.add(w6)
? ? wall.add(w7)
? ? wall.add(w8)
? ? timesec = textBlock(screen, pygame.Rect(settings.w3+200, settings.h1+200, 50,50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "time:"+str(pygame.time.get_ticks()), (200, 0, 20))
? ? leng=1
? ? snakeLength = textBlock(screen, pygame.Rect(settings.w3 + 200, settings.h1 + 400, 50, 50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "length:" + str(leng), (200, 0, 20))
? ? start=textBlock(screen,pygame.Rect(w/2,h/2,50,50),(230,230,230),"Gluttonous Snake",(0,200,200))
? ? start_1 = textBlock(screen, pygame.Rect(w / 2+50, h / 2+100, 50, 50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "Start game",(200,20,20))
? ? start_2 = textBlock(screen, pygame.Rect(w / 2 + 50, h / 2 + 200, 50, 50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "exit", (200, 20, 20))
? ? block_=block(screen, pygame.Rect(w / 2-170, h / 2-85, 10, 10),
? ? ? ? ? ? ? lambda r:r,settings.wall_color)
? ? kw,ks,ka,kd=False,False,False,False
? ? start_game,exit_game,generator=False,False,False
? ? timeStart=0
? ? ls=[start_game,kw,ks,ka,kd,exit_game,timeStart,generator,leng]
? ? lx,ly,la=[],[],[]
? ? def check_event(ls,lx,ly,la):
? ? ? ? for event in pygame.event.get():
? ? ? ? ? ? if event.type==pygame.QUIT:
? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? elif event.type==pygame.KEYDOWN and ls[0]:
? ? ? ? ? ? ? ? if event.key==pygame.K_w:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[True,False,False,False]
? ? ? ? ? ? ? ? elif event.key==pygame.K_a:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[False,False,True,False]
? ? ? ? ? ? ? ? elif event.key==pygame.K_s:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[False,True,False,False]
? ? ? ? ? ? ? ? elif event.key==pygame.K_d:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[False,False,False,True]
? ? ? ? ? ? elif event.type==pygame.KEYDOWN:
? ? ? ? ? ? ? ? if event.key==pygame.K_SPACE :
? ? ? ? ? ? ? ? ? ? if(ls[5]):
? ? ? ? ? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ls[0],ls[2],ls[7]=True,True,True
? ? ? ? ? ? ? ? ? ? ? ? ls[6]=pygame.time.get_ticks()
? ? ? ? ? ? ? ? elif event.key==pygame.K_w or event.key==pygame.K_s:
? ? ? ? ? ? ? ? ? ? if(ls[5]):
? ? ? ? ? ? ? ? ? ? ? ? block_.rect.y-=100
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? block_.rect.y+=100
? ? ? ? ? ? ? ? ? ? ls[5]=not ls[5]
? ? ? ? if(ls[0]):#start game
? ? ? ? ? ? if(ls[7]):
? ? ? ? ? ? ? ? aa.rect.x,aa.rect.y=ra.randint(settings.w1+20,settings.w2-20),\
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ra.randint(settings.h1+20,settings.h2-20)
? ? ? ? ? ? ? ? ls[7]=False
? ? ? ? ? ? if(pygame.time.get_ticks()%30==1):
? ? ? ? ? ? ? ? if(ls[1] and a.rect.y>settings.h1):
? ? ? ? ? ? ? ? ? ? a.rect.y-=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(0)
? ? ? ? ? ? ? ? ? ? ly.append(-settings.speed)
? ? ? ? ? ? ? ? elif(ls[2] and a.rect.y<settings.h2-settings.length):
? ? ? ? ? ? ? ? ? ? a.rect.y+=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(0)
? ? ? ? ? ? ? ? ? ? ly.append(settings.speed)
? ? ? ? ? ? ? ? elif(ls[3] and a.rect.x>settings.w1):
? ? ? ? ? ? ? ? ? ? a.rect.x-=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(-settings.speed)
? ? ? ? ? ? ? ? ? ? ly.append(0)
? ? ? ? ? ? ? ? elif(ls[4] and a.rect.x<settings.w2-settings.length):
? ? ? ? ? ? ? ? ? ? a.rect.x+=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(settings.speed)
? ? ? ? ? ? ? ? ? ? ly.append(0)
? ? ? ? ? ? ? ? dir=-2
? ? ? ? ? ? ? ? for als in la:
? ? ? ? ? ? ? ? ? ? als.rect.x+=lx[dir]
? ? ? ? ? ? ? ? ? ? als.rect.y+=ly[dir]
? ? ? ? ? ? ? ? ? ? dir-=1
? ? ? ? ? ? a.draw_bullet()
? ? ? ? ? ? g.update()
? ? ? ? ? ? aa.draw_bullet()
? ? ? ? ? ? for all in gg:
? ? ? ? ? ? ? ? all.draw_bullet()
? ? ? ? ? ? for walls in wall:
? ? ? ? ? ? ? ? walls.draw_bullet()
? ? ? ? ? ? timesec.changeText("time:"+str((pygame.time.get_ticks()-ls[6])//1000))
? ? ? ? ? ? timesec.draw_bullet()
? ? ? ? ? ? snakeLength.draw_bullet()
? ? ? ? ? ? if (pygame.sprite.spritecollideany(aa, g)):
? ? ? ? ? ? ? ? print("collide")
? ? ? ? ? ? ? ? ls[7]=True
? ? ? ? ? ? ? ? ls[8]+=1
? ? ? ? ? ? ? ? snakeLength.changeText("length:" + str(ls[8]))
? ? ? ? ? ? ? ? if(len(la)==0):
? ? ? ? ? ? ? ? ? ? aaa = block(screen, pygame.Rect(a.rect.x,a.rect.y,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.length, settings.length), lambda r: r, settings.snake_color)
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? aaa = block(screen, pygame.Rect(a.rect.x, a.rect.y,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.length, settings.length), lambda r: r,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.snake_color)
? ? ? ? ? ? ? ? for x in range(len(la)+1):
? ? ? ? ? ? ? ? ? ? aaa.rect.x-=lx[-x-1]
? ? ? ? ? ? ? ? ? ? aaa.rect.y-=ly[-x-1]
? ? ? ? ? ? ? ? gg.add(aaa)
? ? ? ? ? ? ? ? la.append(aaa)
? ? ? ? else:
? ? ? ? ? ? start.draw_bullet()
? ? ? ? ? ? start_1.draw_bullet()
? ? ? ? ? ? start_2.draw_bullet()
? ? ? ? ? ? block_.draw_bullet()
? ? while True:
? ? ? ? screen.fill(bg_color)
? ? ? ? check_event(ls,lx,ly,la)
? ? ? ? pygame.display.flip()
run_game()

最終完成版本:

import pygame,sys,numpy as nu,random as ra
from pygame.sprite import Sprite,Group
class setting():
? ? def __init__(self,w,h):
? ? ? ? self.w1=0.1*w
? ? ? ? self.w2=0.7*w
? ? ? ? self.w3=0.75*w
? ? ? ? self.w4=0.95*w
? ? ? ? self.h1=0.1*h
? ? ? ? self.h2=0.7*h
? ? ? ? self.length=0.05*self.w1
? ? ? ? self.speed=self.length
? ? ? ? self.wall_length=0.1*self.length
? ? ? ? self.wall_color=(0,0,0)
? ? ? ? self.snake_color=(0,0,230)
? ? ? ? self.head_color=(230,0,0)
class block(Sprite):
? ? def __init__(self,screen,rect,test,color):
? ? ? ? super().__init__()
? ? ? ? self.screen=screen
? ? ? ? self.rect=rect ? ? ?#pygame.Rect(0,0,10,10)
? ? ? ? self.test=test
? ? ? ? self.color=color
? ? def update(self):
? ? ? ? self.rect=self.test(self.rect)
? ? def draw_bullet(self):
? ? ? ? pygame.draw.rect(self.screen,self.color,self.rect)
class textBlock(Sprite):
? ? def __init__(self,screen,rect,color,text,text_color):
? ? ? ? super().__init__()
? ? ? ? self.screen=screen
? ? ? ? self.rect=rect ? ? ?#pygame.Rect(0,0,10,10)
? ? ? ? self.color=color
? ? ? ? self.text_color=text_color
? ? ? ? self.font=pygame.font.SysFont(None,68)
? ? ? ? self.image=self.font.render(text,True,self.text_color,self.color)
? ? ? ? self.image_rect=self.image.get_rect()
? ? ? ? self.image_rect.left=rect.left-200
? ? ? ? self.image_rect.top=rect.top-200
? ? def update(self):
? ? ? ? pass
? ? def changeText(self,text):
? ? ? ? self.image=self.font.render(text,True,self.text_color,self.color)
? ? def draw_bullet(self):
? ? ? ? self.screen.blit(self.image,self.image_rect)
? ? ? ? #pygame.draw.rect(self.screen, self.color, self.rect)
def run_game():
? ? pygame.init()
? ? w,h=1200,800
? ? screen=pygame.display.set_mode((w,h))
? ? pygame.display.set_caption("pygame1")
? ? bg_color=(230,230,230)
? ? settings=setting(w,h)

? ? g,gg=Group(),Group()

? ? a = block(screen, pygame.Rect((settings.w1+settings.w2)/2, (settings.h1+settings.h2)/2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.length, settings.length),lambda r:r,settings.head_color)
? ? aa = block(screen, pygame.Rect((settings.w1 + settings.w2) / 2, (settings.h1 + settings.h2) / 2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.length, settings.length), lambda r: r, settings.snake_color)
? ? g.add(a)
? ? #墻壁
? ? wall=Group()
? ? w1=block(screen, pygame.Rect(settings.w1, settings.h1, settings.wall_length, settings.h2-settings.h1),
? ? ? ? ? ? ? lambda r:r,settings.wall_color)
? ? w2 = block(screen, pygame.Rect(settings.w1, settings.h1, settings.w2-settings.w1, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r,settings.wall_color)
? ? w3 = block(screen, pygame.Rect(settings.w2, settings.h1, settings.wall_length, settings.h2-settings.h1),
? ? ? ? ? ? ? ?lambda r: r,settings.wall_color)
? ? w4 = block(screen, pygame.Rect(settings.w1, settings.h2, settings.w2-settings.w1, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r,settings.wall_color)
? ? wall.add(w1);wall.add(w2);wall.add(w3);wall.add(w4)

? ? w5 = block(screen, pygame.Rect(settings.w3, settings.h1, settings.wall_length, settings.h2 - settings.h1),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? w6 = block(screen, pygame.Rect(settings.w3, settings.h1, settings.w4 - settings.w3, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? w7 = block(screen, pygame.Rect(settings.w4, settings.h1, settings.wall_length, settings.h2 - settings.h1),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? w8 = block(screen, pygame.Rect(settings.w3, settings.h2, settings.w4 - settings.w3, settings.wall_length),
? ? ? ? ? ? ? ?lambda r: r, settings.wall_color)
? ? wall.add(w5)
? ? wall.add(w6)
? ? wall.add(w7)
? ? wall.add(w8)
? ? timesec = textBlock(screen, pygame.Rect(settings.w3+200, settings.h1+200, 50,50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "time:"+str(pygame.time.get_ticks()), (200, 0, 20))
? ? leng=1
? ? snakeLength = textBlock(screen, pygame.Rect(settings.w3 + 200, settings.h1 + 400, 50, 50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "length:" + str(leng), (200, 0, 20))
? ? start=textBlock(screen,pygame.Rect(w/2,h/2,50,50),(230,230,230),"Gluttonous Snake",(0,200,200))
? ? start_1 = textBlock(screen, pygame.Rect(w / 2+50, h / 2+100, 50, 50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "Start game",(200,20,20))
? ? start_2 = textBlock(screen, pygame.Rect(w / 2 + 50, h / 2 + 200, 50, 50), (230, 230, 230),
? ? ? ? ? ? ? ? ? ? ? ? "exit", (200, 20, 20))
? ? block_=block(screen, pygame.Rect(w / 2-170, h / 2-85, 10, 10),
? ? ? ? ? ? ? lambda r:r,settings.wall_color)
? ? kw,ks,ka,kd=False,False,False,False
? ? start_game,exit_game,generator,gameOver=False,False,False,False
? ? timeStart=0
? ? ls=[start_game,kw,ks,ka,kd,exit_game,timeStart,generator,leng,gameOver]
? ? lx,ly=[],[]
? ? def check_event(ls,lx,ly,gg):
? ? ? ? for event in pygame.event.get():
? ? ? ? ? ? if event.type==pygame.QUIT:
? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? elif event.type==pygame.KEYDOWN and ls[0] and not ls[9]:
? ? ? ? ? ? ? ? if event.key==pygame.K_w:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[True,False,False,False]
? ? ? ? ? ? ? ? elif event.key==pygame.K_a:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[False,False,True,False]
? ? ? ? ? ? ? ? elif event.key==pygame.K_s:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[False,True,False,False]
? ? ? ? ? ? ? ? elif event.key==pygame.K_d:
? ? ? ? ? ? ? ? ? ? ls[1:5]=[False,False,False,True]
? ? ? ? ? ? elif event.type==pygame.KEYDOWN and not ls[9]:
? ? ? ? ? ? ? ? if event.key==pygame.K_SPACE :
? ? ? ? ? ? ? ? ? ? if(ls[5]):
? ? ? ? ? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ls[0],ls[2],ls[7]=True,True,True
? ? ? ? ? ? ? ? ? ? ? ? ls[6]=pygame.time.get_ticks()
? ? ? ? ? ? ? ? elif event.key==pygame.K_w or event.key==pygame.K_s:
? ? ? ? ? ? ? ? ? ? if(ls[5]):
? ? ? ? ? ? ? ? ? ? ? ? block_.rect.y-=100
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? block_.rect.y+=100
? ? ? ? ? ? ? ? ? ? ls[5]=not ls[5]
? ? ? ? ? ? elif event.type==pygame.KEYDOWN:
? ? ? ? ? ? ? ? if event.key==pygame.K_SPACE :
? ? ? ? ? ? ? ? ? ? if(ls[5]):
? ? ? ? ? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ls[0],ls[2],ls[7],ls[9],ls[8]=True,True,True,False,1
? ? ? ? ? ? ? ? ? ? ? ? la ,lx,ly= [],[],[]
? ? ? ? ? ? ? ? ? ? ? ? snakeLength.changeText("length:" + str(ls[8]))
? ? ? ? ? ? ? ? ? ? ? ? gg.empty()
? ? ? ? ? ? ? ? ? ? ? ? ls[6]=pygame.time.get_ticks()
? ? ? ? ? ? ? ? ? ? ? ? a.rect.x,a.rect.y=(settings.w1+settings.w2)/2, (settings.h1+settings.h2)/2
? ? ? ? ? ? ? ? elif event.key==pygame.K_w or event.key==pygame.K_s:
? ? ? ? ? ? ? ? ? ? if(ls[5]):
? ? ? ? ? ? ? ? ? ? ? ? block_.rect.y-=100
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? block_.rect.y+=100
? ? ? ? ? ? ? ? ? ? ls[5]=not ls[5]
? ? ? ? if(ls[0] and not ls[9]):#start game
? ? ? ? ? ? if(ls[7]):
? ? ? ? ? ? ? ? aa.rect.x,aa.rect.y=ra.randint(settings.w1+20,settings.w2-20),\
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ra.randint(settings.h1+20,settings.h2-20)
? ? ? ? ? ? ? ? ls[7]=False
? ? ? ? ? ? if(pygame.time.get_ticks()%30==1):
? ? ? ? ? ? ? ? if(ls[1] and a.rect.y>=settings.h1):
? ? ? ? ? ? ? ? ? ? a.rect.y-=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(0)
? ? ? ? ? ? ? ? ? ? ly.append(-settings.speed)
? ? ? ? ? ? ? ? elif(ls[2] and a.rect.y<=settings.h2-settings.length):
? ? ? ? ? ? ? ? ? ? a.rect.y+=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(0)
? ? ? ? ? ? ? ? ? ? ly.append(settings.speed)
? ? ? ? ? ? ? ? elif(ls[3] and a.rect.x>=settings.w1):
? ? ? ? ? ? ? ? ? ? a.rect.x-=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(-settings.speed)
? ? ? ? ? ? ? ? ? ? ly.append(0)
? ? ? ? ? ? ? ? elif(ls[4] and a.rect.x<=settings.w2-settings.length):
? ? ? ? ? ? ? ? ? ? a.rect.x+=settings.speed
? ? ? ? ? ? ? ? ? ? lx.append(settings.speed)
? ? ? ? ? ? ? ? ? ? ly.append(0)
? ? ? ? ? ? ? ? dir=-2
? ? ? ? ? ? ? ? for als in gg:
? ? ? ? ? ? ? ? ? ? als.rect.x+=lx[dir]
? ? ? ? ? ? ? ? ? ? als.rect.y+=ly[dir]
? ? ? ? ? ? ? ? ? ? dir-=1
? ? ? ? ? ? a.draw_bullet()
? ? ? ? ? ? g.update()
? ? ? ? ? ? aa.draw_bullet()
? ? ? ? ? ? for all in gg:
? ? ? ? ? ? ? ? all.draw_bullet()
? ? ? ? ? ? for walls in wall:
? ? ? ? ? ? ? ? walls.draw_bullet()
? ? ? ? ? ? timesec.changeText("time:"+str((pygame.time.get_ticks()-ls[6])//1000))
? ? ? ? ? ? timesec.draw_bullet()
? ? ? ? ? ? snakeLength.draw_bullet()
? ? ? ? ? ? if (pygame.sprite.spritecollideany(aa, g)):
? ? ? ? ? ? ? ? print("collide")
? ? ? ? ? ? ? ? ls[7]=True
? ? ? ? ? ? ? ? ls[8]+=1
? ? ? ? ? ? ? ? snakeLength.changeText("length:" + str(ls[8]))
? ? ? ? ? ? ? ? aaa = block(screen, pygame.Rect(a.rect.x, a.rect.y,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.length, settings.length), lambda r: r,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settings.snake_color)
? ? ? ? ? ? ? ? for x in range(len(gg)+1):
? ? ? ? ? ? ? ? ? ? aaa.rect.x-=lx[-x-1]
? ? ? ? ? ? ? ? ? ? aaa.rect.y-=ly[-x-1]
? ? ? ? ? ? ? ? gg.add(aaa)
? ? ? ? ? ? if pygame.sprite.spritecollideany(a,wall) or pygame.sprite.spritecollideany(a,gg):
? ? ? ? ? ? ? ? ls[9],ls[1],ls[2],ls[3],ls[4]=True,False,False,False,False
? ? ? ? ? ? ? ? print("game over")
? ? ? ? ? ? if a.rect.x==settings.w1-settings.length or a.rect.x==settings.w2 \
? ? ? ? ? ? ? ? or a.rect.y==settings.h1-settings.length or a.rect.y==settings.h2:
? ? ? ? ? ? ? ? ls[9], ls[1], ls[2], ls[3], ls[4] = True, False, False, False, False
? ? ? ? ? ? ? ? print("game over")
? ? ? ? else:
? ? ? ? ? ? if ls[9]:
? ? ? ? ? ? ? ? start.changeText("again or exit")
? ? ? ? ? ? ? ? start_1.changeText("again")
? ? ? ? ? ? start.draw_bullet()
? ? ? ? ? ? start_1.draw_bullet()
? ? ? ? ? ? start_2.draw_bullet()
? ? ? ? ? ? block_.draw_bullet()
? ? while True:
? ? ? ? screen.fill(bg_color)
? ? ? ? check_event(ls,lx,ly,gg)
? ? ? ? pygame.display.flip()
run_game()

雖然可能還有不足之處,但玩起來感覺還不錯。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • python 實現(xiàn)全球IP歸屬地查詢工具

    python 實現(xiàn)全球IP歸屬地查詢工具

    這篇文章主要介紹了python 實現(xiàn)全球IP歸屬地查詢工具的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python threading.local代碼實例及原理解析

    Python threading.local代碼實例及原理解析

    這篇文章主要介紹了Python threading.local代碼實例及原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • opencv-python圖像處理安裝與基本操作方法

    opencv-python圖像處理安裝與基本操作方法

    OpenCV是一個C++庫,目前流行的計算機視覺編程庫,用于實時處理計算機視覺方面的問題,它涵蓋了很多計算機視覺領域的模塊。在Python中常使用OpenCV庫實現(xiàn)圖像處理。本文介紹opencv-python圖像處理安裝與基本操作,感興趣的朋友一起看看吧
    2022-01-01
  • Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的方法實例

    Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的方法實例

    這篇文章主要給大家介紹了關于Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的相關資料,好的圖表能幫助我們深化數(shù)據(jù)的記憶點,文中通過圖文以及代碼示例將實現(xiàn)的方法介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • 淺談Python中的全局鎖(GIL)問題

    淺談Python中的全局鎖(GIL)問題

    今天小編就為大家分享一篇淺談Python中的全局鎖(GIL)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python中 pickle 模塊的 dump() 和 load() 方法詳解

    Python中 pickle 模塊的 dump() 和 load()&

    Python 的 pickle 模塊用于實現(xiàn)二進制序列化和反序列化,一個對象可以被序列化到文件中,然后可以從文件中恢復,這篇文章主要介紹了Python中 pickle 模塊的 dump() 和 load() 方法詳解,需要的朋友可以參考下
    2022-12-12
  • Python實現(xiàn)釘釘發(fā)送報警消息的方法

    Python實現(xiàn)釘釘發(fā)送報警消息的方法

    今天小編就為大家分享一篇Python實現(xiàn)釘釘發(fā)送報警消息的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python 應用之Pycharm 新建模板默認添加編碼格式-作者-時間等信息【推薦】

    python 應用之Pycharm 新建模板默認添加編碼格式-作者-時間等信息【推薦】

    這篇文章主要介紹了Pycharm 新建模板默認添加編碼格式-作者-時間等信息 ,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • python 計算概率密度、累計分布、逆函數(shù)的例子

    python 計算概率密度、累計分布、逆函數(shù)的例子

    這篇文章主要介紹了python 計算概率密度、累計分布、逆函數(shù)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python如何更新包

    python如何更新包

    在本篇文章里小編給大家整理的是關于python更新包的相關知識點內(nèi)容,有興趣的朋友們可以參考下。
    2020-06-06

最新評論

宁远县| 邛崃市| 虎林市| 淮滨县| 高尔夫| 肇庆市| 贡嘎县| 客服| 富川| 新绛县| 洪泽县| 东辽县| 额济纳旗| 长春市| 洮南市| 宁国市| 西峡县| 昌乐县| 樟树市| 蒙阴县| 阿拉善盟| 张家口市| 凤山市| 米林县| 海晏县| 故城县| 双流县| 盘山县| 泸定县| 滁州市| 石林| 台南县| 通江县| 寻乌县| 保德县| 常宁市| 承德县| 巴东县| 北川| 明水县| 周至县|