r/Python • • Jan 20 '19

Asteroids pyxel game! timbledum/asteroids on github

481 Upvotes

30 comments sorted by

28

u/timbledum Jan 20 '19 edited Jan 20 '19

Hey all! Game here.

Just wanted to share my game but also wanted to highlight pyxel as a python game library. If you haven't heard of it, it's a really cool, easy to use retro game library.

If you have heard of it but haven't checked it out for a while, have another look. Kitao has been absolutely slaying it, and pyxel now has a really nice editing tool that covers images, tiles, sounds, and music. It also has some stubs and type annotations so everything works smoothly with your IDE.

There are a bunch of user made games listed here.

5

u/Please_Not__Again Jan 20 '19

Just curious on how long it would take the average person to make a simple game like snake or the dinasour running e.t.c. how long did it take you to make this?

11

u/timbledum Jan 20 '19

This asteroids game probably took me 5-7 hours? Hard to track as I chip at it half an hour at a time. There’s a bit more time lying in bed thinking of how I’m going to implement the next bit.

6

u/Please_Not__Again Jan 20 '19

Oh I see thanks for the answer. Don't plan on making games but was always curious on how hard it is to make one.

6

u/TheIncorrigible1 `__import__('rich').get_console().log(':100:')` Jan 20 '19

It's mostly collision detection and rendering that are the hard bits

4

u/Please_Not__Again Jan 20 '19

If you don't mind answering would you do an if else statement for it? Like if it comes too close then game over?

3

u/TheIncorrigible1 `__import__('rich').get_console().log(':100:')` Jan 20 '19

I'm not sure if there's an event loop or something like that with the pyxel library they're using, but the approach I would take: check the bounding boxes between the game area, asteroids, and the player with each frame; you'd be checking for overlapping coordinates.

4

u/timbledum Jan 20 '19

For this game I used a super simple method of collision detection assuming each object is a circle. Then you just compare the distance between the two objects (using the hypotenuse) to the combination of the two radiuses.

Not perfect but works well enough. Sometimes the tip of the ship can pass through an asteroid without dying.

1

u/timbledum Jan 20 '19

Oh and pyxel totally has an event loop with two end points accessible to the user: an update function for game state and a display function for rendering - this allows it to skip frames if necessary.

4

u/[deleted] Jan 20 '19

Yes you would! There are if else statements all over the place.

3

u/Please_Not__Again Jan 20 '19

Yeah will probably look at some videos of someone making a simple game and seeing how it is done.

5

u/timbledum Jan 20 '19

u/DaFluffyPotato, creator of Super Potato Bruh made a cool video explaining some collision detection stuff. Worth checking out. https://www.youtube.com/watch?v=a_YTklVVNoQ

2

u/Please_Not__Again Jan 22 '19

Damn thanks for the video. That guy is really good and never knew you could make games with python untill recently. It really is a jack of all trades.

Thanks for the video and will check it out for sure.

3

u/timbledum Jan 20 '19

I didn’t make the dinasour one, but snake was fairly quick and has relatively little code. It took me a Sunday afternoon, then about the same time again for the music / sound effects.

2

u/Please_Not__Again Jan 20 '19

Really cool, have you always wanted to make games? Or is this just a little side project?

2

u/timbledum Jan 20 '19

Nah - not really. I’m an accountant by trade and I only use python from time for automating data transformations or producing documents or reports and the like. Quite different from this stuff.

I just saw this library and thought I’ld have a go at a simple game to test my python chops and it was funner than I expected!

2

u/Please_Not__Again Jan 20 '19

Thanks for the answer and good luck.

2

u/bakurtz Jan 20 '19

I was thinking about making a Astroids-like game with pixel art a lot recently. This is awesome! Thank you for sharing.

2

u/timbledum Jan 20 '19

No problem – hope you learn some stuff! Hopefully it's relatively easy to follow the code – I just added some docstrings to help.

2

u/RandomJacobP Jan 20 '19 edited Jan 20 '19

Wow, I’m really impressed by your code. It’s super easy to read and clean. I have just learned something about python, didn’t know how to use class_init properly before.

I must find another cool retro game and try to code it by myself, asteroid game looks awesome but now I would think too much about your code to not just copy your idea(I mean the structure of the game and the asteroid class).

1

u/timbledum Jan 20 '19

That's really good to hear – I try to make things as clear as possible as I often pick up and drop projects over time.

Although I'm using a class method for init_class (which I do have to manually call, it's not a magic dunder method), I am using the name of the classes directly a lot to access the class variables (e.g., Asteroid.asteroids) This isn't great practice in general I guess as it means everything breaks if I sub-class the class, but that isn't going to happen in this case.

I guess what I should be doing is creating an Asteroid factory class which creates the individual asteroids, but abusing the class variables seems to work for this.

u/yawpitch had some ideas over on the post over on the pyxel subreddit, like lunar lander.

2

u/RandomJacobP Jan 21 '19

Lunar lander seems awesome! Thanks for idea! I have read about different OOP frameworks/patterns like factory but haven’t understand its potential to be honest, I must try to understand it and write one by myself in python.

34

u/[deleted] Jan 20 '19

You aren’t very good at your game

24

u/timbledum Jan 20 '19

I didn’t want to discourage people playing it with my awesomeness ;)

2

u/Niohzxs Jan 20 '19

Well made Op

6

u/CeeMX Jan 20 '19

I first read "on Grub" and thought why the hell one would code a game to play in a bootloader :D

3

u/ThatsJustUn-American Jan 20 '19

To be fair, full fledged python from the grub shell would be a pretty notable accomplishment.

1

u/auntedith Jan 20 '19

Great game but only I can see is schore.

3

u/timbledum Jan 20 '19

Yeah noticed as soon as I posted. Should be fixed for the actual version. 😔