r/Python • u/[deleted] • 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.
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
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
7
u/hobbicon May 16 '21 edited May 16 '21
Looks like your are better at programming it than playing it.
2
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
0
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
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
33
40
26
10
9
7
4
4
4
1
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
May 23 '21
Here's the github for it, glad you like it!
https://github.com/Boss68/Flappy-Bird-written-entirely-in-one-line-of-python
1
u/JonasMArnold May 23 '21
Thanks a lot! As mentioned, especially the hardcoded images are cleverly embedded.
3
3
2
2
2
u/SnooAvocados9605 May 16 '21
imagine someone else debugging your code. whole career fucked by a single line!
3
2
1
1
1
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
1
1
1
1
-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
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
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
-3
u/funkmaster322 May 16 '21
Seriously? What's the point of this?
6
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
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
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
331
u/iLoveStarsInTheSky May 16 '21
Syntax Error on line 1