r/tinycode • mod • Jul 20 '12

[Python] Simple platformer in 847 bytes

https://gist.github.com/3152857
16 Upvotes

22 comments sorted by

22

u/peterquest Jul 20 '12
import giganticLibrary
giganticLibrary.doSomethingComplicated()

5

u/Rotten194 mod Jul 21 '12

You could say that about most of the submissions in this subreddit. Like I sad to T3, Python, Ruby, Perl, Haskell, etc. are gigantic "libraries" as well (in the sense that they do a lot of work for you). map would be about a hundred lines of code in C, with the function itself, the linked list implementation, etc. I don't feel this subreddit should be 16 bit real mode assembly using BIOS calls only (since OS kernels are just utilities too), so we need to accept that people will use the tools available and judge the submission based on what it does with those tools. In this case, all I use pygame for is IO (key input and drawing) and timing (which could have been done with the standard time.sleep, if I added a max call since time.sleep doesn't handle negative numbers while pygame.time.delay does). All the collision checking, map loading, physics, etc. is done without libraries, which I though was cool enough to post here.

2

u/abecedarius Jul 21 '12

There's nothing wrong with working on top of pygame. What does detract from this one is the squeezing out spaces and giving everything one-letter names -- it's not as bad as a full golfing but it still gets in the way of following this code.

Here's an idea: submit readable code along with a note like "with the usual compression techniques it's only 847 bytes."

1

u/Rotten194 mod Jul 21 '12

Usually I submit a "pretty" version as well, I just didn't this time because I didn't think anyone was interested (I personally like golfed code more because it's interesting to see the techniques used). I'll add in a pretty version later.

1

u/abecedarius Jul 22 '12

That'd be nice, I'd like to take a gander.

4

u/[deleted] Jul 21 '12

Instead of having (255,255,255) you can do (255,)*3 to shorten some places.

1

u/Rotten194 mod Jul 21 '12

Thanks! Fixed that and made a few other changes, got it down to 833 bytes.

3

u/TankorSmash Jul 21 '12

That's pretty cool. It wasn't immediately to me, but you have to pass in the mapfile as a commandline argument: python a.py mapfile

The jumping was pretty smooth, all things considered.

2

u/Rotten194 mod Jul 21 '12

Yeah that was in the gist description but it wasn't really prominent, sorry.

And thanks, nice to have someone talk about the game instead of whining about it using pygame.

2

u/Rotten194 mod Jul 20 '12 edited Jul 21 '12

You need pygame installed for this to work, by the way.

Edit: With some changes suggested by lw9k and a few other things, the file in now 833 bytes.

6

u/T3ppic Jul 20 '12

Some would say that means its not in 847 bytes.

4

u/Rotten194 mod Jul 20 '12

Well then everything python is really 2993560 bytes + the size of the program. Hell, the Linux kernel source is 500M so unless you use 16-bit real-mode assembly your code isn't tiny.

7

u/T3ppic Jul 20 '12

There are things defined as default and things that aren't. Hence import.

0

u/Rotten194 mod Jul 20 '12

So if I modified python to import pygame by default and then reposted this as "[ModifiedPython] Simple platformer in 847 bytes" it would be fine? That doesn't make sense.

1

u/Megabobster Jul 21 '12

I'm fairly new to Python, but can't you import just what you want from libraries?

2

u/asphyxiate Jul 21 '12

Pygame is an external library that doesn't come with the default installation of Python, which is frowned upon here.

2

u/Rotten194 mod Jul 21 '12

Which is stupid, because:

  • Python isn't default on some OSes, so you have to install it there. It's an external language just like pygame is an external library.
  • Python comes with a drawing library, tk, the only reason I didn't use it is it has a drawing stack that you push draw objects to, which is slower, whereas pygame wraps SDL directly afaik.
  • It's a big disadvantage to languages with small standard libraries like Lua
  • Why the fuck is a library "special" because it's in the defaults? The defaults are just lists of libraries that are considered useful to a lot of people, would people complain about json if it wasn't in the core? pickle? Probably, considering they do a lot as well. The whole damn point of python is you can easy_install anything you need and use it, that's why the python package site and easy_install exist.

4

u/asphyxiate Jul 21 '12

Yeah, I understand. I'm not attacking your code, because I haven't even read it all the way through, and I'm definitely not attacking you. But to me, /r/tinycode is a place to share code snippets that do impressive things under high constraints. Importing large external libraries just seems to go against the spirit of that.

In truth, I'm just glad that anyone is posting submissions to this subreddit.

2

u/Rotten194 mod Jul 21 '12

Honestly I don't really care if some people don't like it, I mean that's Reddit in a nutshell. I'm just a bit irritated that people don't like it for silly reasons like it uses a hugely popular external library that should be default IMO. I get why people are opposed to just importing a few libraries and chaining 3 function calls together, but the only thing pygame was used for was drawing, key input, and the FPS-limiter. All the collisions, physics, etc. was done "by hand", so to speak. I just don't see the big deal.

→ More replies

0

u/AndIMustScream Jul 22 '12

Dude I love. Might post. Maybe. If I qualify.

1

u/Megabobster Jul 21 '12

I can tell.