50行代码实现贪吃蛇(具体思路及代码)
(编辑:jimmy 日期: 2024/11/27 浏览:3 次 )
最近一直在准备用来面试的几个小demo,为了能展现自己,所以都是亲自设计并实现的,其中一个就是在50行代码内来实现一个贪吃蛇,为了说明鄙人自己练习编程的一种方式--把代码写短,为了理解语言细节。
复制代码 代码如下:
<SPAN style="FONT-SIZE: 14px">import sys, pygame
from pygame.locals import *
from random import randrange
up =lambda x:(x[0]-1,x[1])
down = lambda x :(x[0]+1,x[1])
left = lambda x : (x[0],x[1]-1)
right = lambda x : (x[0],x[1]+1)
tl = lambda x :x<3 and x+1 or 0
tr = lambda x :x==0 and 3 or x-1
dire = [up,left,down,right]
move = lambda x,y:[y(x[0])]+x[:-1]
grow = lambda x,y:[y(x[0])]+x
s = [(5,5),(5,6),(5,7)]
d = up
food = randrange(0,30),randrange(0,40)
FPSCLOCK=pygame.time.Clock()
pygame.init()
pygame.display.set_mode((800,600))
pygame.mouse.set_visible(0)
screen = pygame.display.get_surface()
screen.fill((0,0,0))
times=0.0
while True:
time_passed = FPSCLOCK.tick(30)
if times>=150:
times =0.0
s = move(s,d)
else:
times +=time_passed
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN and event.key == K_UP:
s = move(s,d)
if event.type == KEYDOWN and event.key == K_LEFT:
d=dire[tl(dire.index(d))]
if event.type == KEYDOWN and event.key == K_RIGHT:
d=dire[tr(dire.index(d))]
if s[0]==food:
s = grow(s,d)
food =randrange(0,30),randrange(0,40)
if s[0] in s[1:] or s[0][0]<0 or s[0][0] >= 30 or s[0][1]<0 or s[0][1]>=40:
break
screen.fill((0,0,0))
for r,c in s:
pygame.draw.rect(screen,(255,0,0),(c*20,r*20,20,20))
pygame.draw.rect(screen,(0,255,0),(food[1]*20,food[0]*20,20,20))
pygame.display.update()</SPAN>
游戏截图:
说明:
1.其实不用pygame,在把一些条件判断改改,估计可以再短一半。。等以后自己python水平高了再回来试试。。
2.但是50行的贪吃蛇代码,还是有可读性的,写的太短就真没有了。。
3.关键是把旋转,移动,等等这些算法用lamda表达式实现,还有函数对象。。
4.哪位“行者”能写的更短,小弟愿意赐教....
作者:aiqier
复制代码 代码如下:
<SPAN style="FONT-SIZE: 14px">import sys, pygame
from pygame.locals import *
from random import randrange
up =lambda x:(x[0]-1,x[1])
down = lambda x :(x[0]+1,x[1])
left = lambda x : (x[0],x[1]-1)
right = lambda x : (x[0],x[1]+1)
tl = lambda x :x<3 and x+1 or 0
tr = lambda x :x==0 and 3 or x-1
dire = [up,left,down,right]
move = lambda x,y:[y(x[0])]+x[:-1]
grow = lambda x,y:[y(x[0])]+x
s = [(5,5),(5,6),(5,7)]
d = up
food = randrange(0,30),randrange(0,40)
FPSCLOCK=pygame.time.Clock()
pygame.init()
pygame.display.set_mode((800,600))
pygame.mouse.set_visible(0)
screen = pygame.display.get_surface()
screen.fill((0,0,0))
times=0.0
while True:
time_passed = FPSCLOCK.tick(30)
if times>=150:
times =0.0
s = move(s,d)
else:
times +=time_passed
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN and event.key == K_UP:
s = move(s,d)
if event.type == KEYDOWN and event.key == K_LEFT:
d=dire[tl(dire.index(d))]
if event.type == KEYDOWN and event.key == K_RIGHT:
d=dire[tr(dire.index(d))]
if s[0]==food:
s = grow(s,d)
food =randrange(0,30),randrange(0,40)
if s[0] in s[1:] or s[0][0]<0 or s[0][0] >= 30 or s[0][1]<0 or s[0][1]>=40:
break
screen.fill((0,0,0))
for r,c in s:
pygame.draw.rect(screen,(255,0,0),(c*20,r*20,20,20))
pygame.draw.rect(screen,(0,255,0),(food[1]*20,food[0]*20,20,20))
pygame.display.update()</SPAN>
游戏截图:
说明:
1.其实不用pygame,在把一些条件判断改改,估计可以再短一半。。等以后自己python水平高了再回来试试。。
2.但是50行的贪吃蛇代码,还是有可读性的,写的太短就真没有了。。
3.关键是把旋转,移动,等等这些算法用lamda表达式实现,还有函数对象。。
4.哪位“行者”能写的更短,小弟愿意赐教....
作者:aiqier
下一篇:利用python获得时间的实例说明