50 lines of code to implement a snake (specific ideas and code)

Source: Internet
Author: User
Recently I have been preparing a few demos for the interview, in order to show themselves, so they are designed and implemented, one is to implement a snake in 50 lines of code, in order to illustrate my own practice of programming one way-write short, in order to understand the language details.
Copy CodeThe code is as follows:


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


Game:

Description
1. In fact, there is no need to pygame, in some conditions to change the judgment, estimated can be another short half. After your own Python level is high and then come back to try it.
2. But 50 lines of snake code, still readable, write too short to really no.
3. The key is to rotate, move, and so on. These algorithms are implemented with LAMDA expressions, as well as function objects.
4. Which "Walker" can write shorter, younger brother is willing to enlighten ....
Author: aiqier
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.