NanoSnake - 371 B
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(*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
- 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!
Pygame.org account Comments
If you wish to leave a comment with your pygame.org account, please sign in first.