r/Python • • May 15 '21

Intermediate Showcase Using a combination of lambda functions, list comprehension, and ternary operators, (and Pygame), I was able to make Flappy Bird in one line of Python!

I also hardcoded the raw bytes of all of the images so that the program can run completely independent of any other file, in one line.

1.2k Upvotes

70 comments sorted by

331

u/iLoveStarsInTheSky May 16 '21

Syntax Error on line 1

25

u/[deleted] May 16 '21

pain.

43

u/Hellohihi0123 May 16 '21

Dead 💀💀

116

u/julsmanbr May 16 '21

Client: "thanks for the app, but can you just change a small thing? Nothing major, I'm sure it takes just one extra line of code lol"

The extra line of code:

3

u/[deleted] May 16 '21

lmao

12

u/R3D3-1 May 16 '21

I remember a story of an open source maintainer of how a one-line pull request of a user turned into a month long project, because though the line solved the users problem, it caused all sorts of problems in other use cases.

6

u/miciomacho May 16 '21

I can read the frustration in your soul my pal

4

u/hidegitsu May 16 '21

I think we've all been there.

7

u/hobbicon May 16 '21 edited May 16 '21

Looks like your are better at programming it than playing it.

2

u/[deleted] May 16 '21

that is true 😔

384

u/redditor_286 May 15 '21

This is the single most disturbing thing I've seen this month

Please have my upvote Your Highness!

119

u/Eye_Of_Forrest May 15 '21

you can technically make any program into one line using exec()

1

u/[deleted] May 17 '21

Reminded me of execline.

0

u/[deleted] May 16 '21

[deleted]

29

u/TSM- 🐱‍💻📚 May 16 '21 edited May 16 '21

str( your code here ) then exec it later. It's now one line because the newlines are represented as "\n" instead of being new lines.

11

u/Eye_Of_Forrest May 16 '21

you can do it like that, there are other ways tho

code = """for i in range(99):\n\tprint(f"we are on the cycle #{i}")\nelse:\n\tprint("We are done!")"""; exec(code)

on my end it softwrapped but you can certantly put this in one line and it will work
(python 3.x)

0

u/boomminecraft8 May 16 '21

Try it out by yourself and tell us the results! :)

252

u/imma_go_take_a_nap May 15 '21

You were so preoccupied with whether or not you could, that you didn't stop to ask if you should.

23

u/Tranqui11ion May 16 '21

Spot on Dr Malcolm!

33

u/[deleted] May 16 '21

cries in PEP 8

26

u/TheBlackCat13 May 16 '21

"This is a one line proof...if we start sufficiently far to the left."

10

u/ButtFuckingEbonies May 16 '21

You're a sociopath

9

u/justalurker19 May 16 '21

I wouldn't even try to understand the code.

7

u/Break_my_soul May 16 '21

That's the case when one line means pure obfuscation

4

u/CreatureWarrior May 16 '21

Ewww, what the hell is wrong with you?? Have my upvote

4

u/Rasekuro May 16 '21

But... Why?

PS: Amazing. You have my upvote, but... Just why?

1

u/Goel40 May 16 '21

Can somebody paste the github link? I'm on mobile

1

u/JonasMArnold May 22 '21

One of the most impressive things I've seen in a long time! To code a game within one line is a known "party trick", though hard coding the bytes of the images is a nice feature I haven't yet seen.

Anyhow, could you upload the one-liner to GitHub? Would like to read through :)

2

u/[deleted] May 23 '21

1

u/JonasMArnold May 23 '21

Thanks a lot! As mentioned, especially the hardcoded images are cleverly embedded.

3

u/O_X_E_Y May 16 '21

r/badcode? Hell nah, this is r/goodcode :p

3

u/[deleted] May 16 '21

3

u/R3D3-1 May 16 '21

r/badcode and r/programminghorror want a word with you.

2

u/detroitmatt May 16 '21

average javascript

2

u/agentgreen420 May 16 '21

Talk about a hole in one!

2

u/SnooAvocados9605 May 16 '21

imagine someone else debugging your code. whole career fucked by a single line!

3

u/Paddy_does_stuff May 15 '21

When pythonic coding goes too far...

4

u/king_julian_the_5th May 16 '21

Satanic....I think you mean satanic!

2

u/420drillas May 16 '21

That’s amazing

1

u/name99 May 16 '21

Now what happens when you run prettify? I'd still count it as a one-liner

1

u/crossedline0x01 May 16 '21

Me: new to python wishing python had ternary operators. Some rando on reddit: Using a combination of lambda functions, list comprehension, and TERNARY operators, (and Pygame), I was able to make Flappy Bird in one line of Python!

You get an upvote just for that.

1

u/[deleted] May 16 '21

Wow that's really impressive!

1

u/louismge May 16 '21

This is not APL man… 😂

1

u/lemurrhino May 17 '21

I wonder if that breaks PEP8 E501...

1

u/agb64 May 17 '21

Problem: you forgot the score, medals and sound. DO IT AGAIN.

1

u/axlfol333 Jun 17 '21

Hilarious!!!

-24

u/Allanon001 May 15 '21

Not that impressed, you can write almost any program on one line just using a tuples and/or separating lines with semicolons.

5

u/[deleted] May 15 '21

Python doesn't use semicolons, so that's not an option, but I'm actually curious how you would do it with tuples?

31

u/bulsoni May 15 '21 edited May 15 '21

It does not require semicolons since it usually uses line breaks, but you can totally use it instead of a line break for this specific case (trying to fit everything into a single line). Example:

print(“test”); print(“test”)

Outputs exactly the same as

print(“test”)
print(“test”)

That would likely break compliance with PEP8, but again, fitting everything into a single line also would.

7

u/[deleted] May 15 '21

Woah that's actually really cool!

9

u/WhyDoIHaveAnAccount9 May 15 '21

learn something new everyday

15

u/Allanon001 May 15 '21

This works:

import time; print(time.time()); x = 2; print('x is less than 5') if x < 5 else print('x is greater than 5')

Using tuples you can call multiple statements on one line:

(print('hello'), locals().update({'x':2}), print('x is less than 5') if x < 5 else print('x is 5 or greater'))

6

u/[deleted] May 15 '21

Oh wow, cool!

-3

u/funkmaster322 May 16 '21

Seriously? What's the point of this?

6

u/[deleted] May 16 '21

it was just supposed to be a fun challenge for me

2

u/funkmaster322 May 16 '21

Did you gain anything useful from it? That would be more interesting to share imo.

3

u/[deleted] May 16 '21

I learned about using lambda functions for recursion, I was gonna do that for a main application loop until I realized that there was a recursion limit, so I had to come up with another way to iterate indefinitely until the close button was reached. So I just made the program iterate through a list that was constantly increasing in size, and once the close button is clicked, the list stops increasing so the app will stop iterating and the expression will end. So that's the main thing I learned, but I also just enjoyed the challenge!

4

u/Lambda-guy May 16 '21

Playing Flappy bird

-2

u/funkmaster322 May 16 '21

You could just download it from an appstore then

4

u/Goel40 May 16 '21

But it won't be written in one line?

-2

u/funkmaster322 May 16 '21

Writing it in one line doesnt make it faster or better. Quite the contrary I would think.

0

u/[deleted] May 15 '21

[deleted]

20

u/okiejosh May 16 '21

It’s cool! No one is advocating for one line projects, it’s just a fun challenge in and of itself