, ,

A Universe in One Line of Code with 10 PRINT

files image

10 PRINT is a brand unusual e book which explores the magical energy of a single line of code.

That single line of code ran on the Commodore sixty four, in 1982. It gave us one of the earliest examples of the facility of computer methods to generate complete worlds.

(Gif from Thomas Winningham)

With appropriate a single incantation:

10 PRINT CHR$(205.5+RND(1)); : GOTO 10 

Our fearless particular person got to gaze a complete universe unfold in front of them.

This magic of most recent chances is a big instance of what makes programming so sharp. With appropriate one line of code, we are able to scrutinize a complete world, randomly constructing itself. We scrutinize paths and picture unusual areas with our eyes.

Figuring out How 10 PRINT Builds a Universe

10 PRINT used to be written in the Traditional programming language. This implies it has about a quirks, having a scrutinize motivate at it from our contemporary languages.

For one, Traditional used to be appropriate a raw terminal you typed your applications into. With every entered, you were writing a particular residing of commands to be followed linearly.

First we’ve the line quantity, which is outmoded at the tip to repeat the line of code infinitely. Most contemporary programming languages don’t accept this concept of GOTO in-constructed, because we’ve tried to summary away from line numbers, and into more sure modules and capabilities with names.

Next, we spend the CHR$ objective, and pass it the quantity 205.5 + a random ticket between zero and 1. All numbers were floating level values on the platform, and rounded down. So this intended our code would generate either the persona 205 (), or 206 (/).

With this, we loop infinitely, generating either a forward reduce, or a motivate reduce.

We are able to achieve this in Python, for sure, in honest a single line from the terminal.

As suggested by reddit particular person WK02, here’s the code to generate 10 PRINT in a single line of Python from the shell:

from random import resolution; print(''.join(resolution(('\', '/')) for i in differ 10000))) 

Rewriting 10 PRINT in Python three and Pygame

Unfortunately, writing 10 PRINT in Python and Pygame isn’t in point of fact doable in a single line.

The code to generate the traces is unassuming ample, nonetheless setting up our window for drawing takes up the massive majority of our code.

import pygame

import random

# residing our show conceal's width and height
screenWidth, screenHeight = (800, 800)


pygame.suppose.set_caption("10 PRINT in Pygame")
show conceal = pygame.suppose.set_mode((screenWidth, screenHeight))

operating = Proper

white = (255, 255, 255)
sunless = (zero, zero, zero)

# size of square, in pixels
square = 20

def drawScreen():
    show conceal.accept(sunless)
    # Python version of 10 PRINT happens here
    for x in differ(zero, screenWidth, square):
        for y in differ(zero, screenHeight, square):
            if random.random() > zero.5:
                pygame.plan.line(show conceal, white, (x, y), (x + square, y + square))
                pygame.plan.line(show conceal, white, (x, y + square), (x + square, y))

while operating:
    key = pygame.key.get_pressed()
    # handiest plan if particular person presses spacebar
    if key[pygame.K_SPACE]:
    # quit if particular person presses q
    if key[pygame.K_q]:
    for match in pygame.match.catch():
        if match.variety == pygame.QUIT:
            operating = Fraudulent
    # this updates the suppose

The real work of drawing 10 PRINT in our program comes from the next traces:

    for x in differ(zero, screenWidth, square):
        for y in differ(zero, screenHeight, square):
            if random.random() > zero.5:
                pygame.plan.line(show conceal, white, (x, y), (x + square, y + square))
                pygame.plan.line(show conceal, white, (x, y + square), (x + square, y))

We spend the scale of the square in an x and y coordinate across the show conceal using differ. If the square size is determined to twenty, x and y shall be zero,20,Forty,60,Eighty... the total manner by to our screenHeight and screenWidth.

Next, we spend the random.random() objective to generate a quantity between zero and 1. If that quantity is increased than zero.5, we plan a diagonal line of our square size, from the underside to the tip trusty (/).

If the random.random() quantity is decrease than zero.5, we assassinate a diagonal line going from the tip left of our square to the underside trusty ().

With this, we’ve got our comprise implementation of the maze carry out in Python three, nonetheless not one of the scrolling forever.

Coding Grief: Animating 10 PRINT

If we’re looking out to animate our 10 PRINT, we’ll have to open with a static residing of line capabilities to manage, in set of setting up unusual ones whenever the spacebar is pressed.

Something take care of this can work:

for x in differ(zero, screenWidth, square):
    for y in differ(zero, screenHeight, square):
        if random.random() > zero.5:           
            traces.append([x, y, x + square, y + square])  
            traces.append([x, y + square, x + square, y])

With this, we’ve got a residing of line capabilities we are able to catch admission to and manipulate using our loop.

Try making your comprise modifications to 10 PRINT to assassinate a brand unusual build of animation. In case you catch stuck, you may maybe well presumably also check out the code outmoded to assassinate the above gif on Github.

Where to Straggle From Here

The above code provides you a starting level for manipulating your comprise interpretation of 10 PRINT. In case you catch stuck, be at liberty to scrutinize at the Github repository to resolve out animate it yourself.

Be warned that the code at the moment writes an image to disk for every step, so that you’ll are looking out to comment out that line whilst you don’t are looking out to set aside your photography.

Truly be at liberty to portion your photography on Twitter or Instagram with the hashtag 10PRINT. I’d like to scrutinize what else you may maybe well presumably also assassinate.

For assorted references to 10 PRINT, check out the YouTube channel The Coding Put collectively’s interpretation of 10 PRINT completed in Processing.js.

Reddit particular person baltoxydase posted a version of 10 PRINT in a single line for Pygame. Test up on whilst you may maybe well presumably also attain better:

while"p"in globals()or(plan(globals().__setitem__,"baltox",plan(__import__,["pygame",
,[(l("v",a.random()>zero.5),b.plan.line(w,(255,)*three,(f,y+v*z),(f+z,y+z*(not v))))for f
in j(zero,_[zero],z)for y in j(zero,_[1],z)],d.flip())or u[113]and x(),[e.variety==12 and x()
for e in b.match.catch()]),exit]),t()):o()

Finally, whilst you enjoyed this put up and would comprise to scrutinize more take care of it, join for my e-newsletter, or assassinate an account here on Invent Art work with Python to catch catch admission to to the first three chapters of my e book free.

Be taught Extra

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%