Skip to main content

NanoSnake - 371 B

A snake game written to be as small as possible.


ffao
(ffao)
The original NanoSnake file had 1147 B. In the process of shortening it, I've had to make several bytes/usability tradeoffs. Also, I've incorporated suggestions from several people (Vitor Bosshard, Jordan Trudgett, Uli Eggermann, roebros, Jackson Gatenby). The updated code is below, enjoy!
import pygame as p,random
q=p.display
T=16
b=q.set_mode([256]*2).fill
l=[]
d=a=x=1
c=p.event.get
while not(x&528or x in l):
 l=l[a!=x:]+[x]
 while a&528or a in l:a=random.randrange(512)
 b(0);[b(99,(o%T*T,o/32*T,T,T))for o in l+[a]];q.flip();p.time.wait(99);D=d
 for e in c(2):
  v=e.key-272;n=((v&2)-1)*[1,32][v<3]
  if-n-D and 0<v<5:d=n
 c();x+=d

Changes

- Made changes suggested by Uli Eggermann:
  - Now that the 'competition' is over, the line breaks are all LF;
  - Changed indents of two spaces by tabs.
    - Change reverted due to uncompatibility with all platforms.
- Stored pygame.event.get() in a variable, so that I can use its "type" argument and delete one if.
- Made two changes suggested by roebros:
  - Changed "while x&528or x in l:" to "if x&528or x in l:". It was originally an if, but it probably got changed along the way.
    - This change was superseded when I moved the exit condition to the while loop. It's here for historical reasons.
  - Changed "if -n-d:" to "if-n-d:"
- Removed useless variable "s".
- Condensed list update code in one line.
- Replaced map() with a list comprehension.
- Improved input handling (the two ifs are now one)
- Moved exit condition (x&528 or x in l) into the while loop; no more crash exits!
- Changed the pellet spawn location from 2 to 1, mainly so it has the same value as d and x.
- Removed the player's 'running start', the snake is only one square long at the start.
- Fixed bug noted by Jordan (the snake can no longer reverse its direction in one frame only).
We're under 400 bytes now, and I'm beginning to get impressed by the compactness of my own code!

Links

Home Page
http://www.pygame.org
Source
http://stuffr.net/d/tz3d5kc

Releases

NanoSnake 522 B — 30 Jul, 2008

NanoSnake 429 B — 31 Jul, 2008

NanoSnake 502 B -- The Show Must Go On — 31 Jul, 2008

NanoSnake 364 B — 12 Aug, 2008

NanoSnake 554 B — 30 Jul, 2008

NanoSnake 478 B -- The Harder They Fall — 31 Jul, 2008

NanoSnake 371 B — 2 Aug, 2008

NanoSnake 348 B — 16 Aug, 2008

Pygame.org account Comments

  • Guest 2014-02-28 02:15

    308 bytes:
    from pygame import*;q=display;T=16;f=q.set_mode([256]*2).fill;l=[];d=a=x=n=1;E=event.get
    while not(x&528 or x in l):
    while a&528or a in l:a=a*9%512
    l=l[a!=x:]+[x];f(0);[f(-1,(o%T*T,o/32*T,T,T))for o in l+[a]];q.flip();time.wait(99);x+=d
    for e in E(2):
    v=e.key&7;n=((v&2)-1)*[1,32][v<3]
    if d+n:d=n
    E()

  • Guest 2014-02-28 02:16

    308 bytes (replace underscore "_" with space " "):
    from pygame import*;q=display;T=16;f=q.set_mode([256]*2).fill;l=[];d=a=x=n=1;E=event.get
    while not(x&528 or x in l):
    _while a&528or a in l:a=a*9%512
    _l=l[a!=x:]+[x];f(0);[f(-1,(o%T*T,o/32*T,T,T))for o in l+[a]];q.flip();time.wait(99);x+=d
    _for e in E(2):
    __v=e.key&7;n=((v&2)-1)*[1,32][v<3]
    __if d+n:d=n
    _E()