欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

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

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()

雖然可能還有不足之處,但玩起來(lái)感覺(jué)還不錯(cuò)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 離線安裝Pyecharts的步驟以及依賴包流程

    離線安裝Pyecharts的步驟以及依賴包流程

    這篇文章主要介紹了離線安裝Pyecharts的步驟以及依賴包流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • mac徹底卸載Anaconda簡(jiǎn)單步驟

    mac徹底卸載Anaconda簡(jiǎn)單步驟

    這篇文章主要給大家介紹了關(guān)于mac徹底卸載Anaconda的相關(guān)資料,Anaconda指的是一個(gè)開(kāi)源的Python發(fā)行版本,其包含了conda、Python等180多個(gè)科學(xué)包及其依賴項(xiàng),需要的朋友可以參考下
    2023-10-10
  • Python連接Mysql進(jìn)行增刪改查的示例代碼

    Python連接Mysql進(jìn)行增刪改查的示例代碼

    這篇文章主要介紹了Python連接Mysql進(jìn)行增刪改查的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Pytest如何使用mark的方法

    Pytest如何使用mark的方法

    本文主要介紹了Pytest如何使用mark的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • python 獲取et和excel的版本號(hào)

    python 獲取et和excel的版本號(hào)

    在進(jìn)行OA開(kāi)發(fā)過(guò)程中,經(jīng)常會(huì)用到當(dāng)前辦公軟件的版本號(hào),在python可以通過(guò)如下的方法獲取。
    2009-04-04
  • Python-Flask:動(dòng)態(tài)創(chuàng)建表的示例詳解

    Python-Flask:動(dòng)態(tài)創(chuàng)建表的示例詳解

    今天小編就為大家分享一篇Python-Flask:動(dòng)態(tài)創(chuàng)建表的示例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Python調(diào)用ffmpeg開(kāi)源視頻處理庫(kù),批量處理視頻

    Python調(diào)用ffmpeg開(kāi)源視頻處理庫(kù),批量處理視頻

    本文主要介紹了如何用Python調(diào)用ffmpeg開(kāi)源視頻處理庫(kù),來(lái)實(shí)現(xiàn)視頻批量的處理:水印、背景音樂(lè)、剪輯、合并、幀率、速率、分辨率等操作
    2020-11-11
  • Django商城項(xiàng)目注冊(cè)功能的實(shí)現(xiàn)

    Django商城項(xiàng)目注冊(cè)功能的實(shí)現(xiàn)

    注冊(cè)功能是網(wǎng)頁(yè)項(xiàng)目必備的需求,本文提供了一個(gè)Django項(xiàng)目實(shí)現(xiàn)注冊(cè)功能的示例,有此需求的同學(xué)可以參考下
    2021-06-06
  • 讓文件路徑提取變得更簡(jiǎn)單的Python Path庫(kù)

    讓文件路徑提取變得更簡(jiǎn)單的Python Path庫(kù)

    這里我們介紹 Python3 自帶的庫(kù) Path,可以讓我們使用更少的代碼但是與之而來(lái)的是更高的效率,文中有非常詳細(xì)的介紹及代碼示例 ,需要的朋友可以參考下
    2021-05-05
  • Python機(jī)器學(xué)習(xí)pytorch交叉熵?fù)p失函數(shù)的深刻理解

    Python機(jī)器學(xué)習(xí)pytorch交叉熵?fù)p失函數(shù)的深刻理解

    這篇文章主要為大家介紹了Python機(jī)器學(xué)習(xí)中對(duì)交叉熵?fù)p失函數(shù)的深刻理解,文中作出了詳細(xì)易懂的講解,有需要的朋友可以借鑒參考下希望能夠有所幫助
    2021-10-10

最新評(píng)論