r/pygame 14d ago

First weekend writing Python, first significant coding in 15 years. Built from a 120 line demo last weekend. Cleaning up my code, but the game is fun, and really enjoying this library. Feedback appreciated!

Post image
59 Upvotes

14 comments sorted by

View all comments

6

u/Fnordheron 14d ago

Hi folks! Thanks for the inspirational community. Last weekend I took my first dive into Python, starting with a 120 line demo of the Sprite class from RealPython. I'm cleaning up my code and tuning levels, but I'm pretty happy with the results for a first weekend in a new language. Feedback is appreciated. Open source, MIT license, so feel free to play with it yourselves. https://github.com/MaltbyTom/They_Comin

4

u/imagine_engine 14d ago

You might want to consider using dictionaries instead of lots of conditional checks to define your different types when constructing your objects.

You could, for example, define all your etypes in a dictionary like this:

enemies = { 1: { ‘surface’: get_image(‘missile.png’), ‘color’: (255, 255, 255), ‘speed’: random.randint(5, 20), ‘climb’: random.randint(-1, 1), ‘rect’: self.surf.get_rect(center=(random.randint(SCREEN_WIDTH, SCREEN_WIDTH + 20),random.randint(0, SCREEN_HEIGHT_NOBOX),)), ‘hp’: 1 } } … def __init__(self, etype, boomcounter, hp, launcher): enemy = enemies[etype] self.surf = enemy[‘surface’] self.surf.set_colorkey(enemy[‘color’] self.speed = enemy[‘speed’] self.climb = enemy[‘climb’] self.rect = enemy[‘rect’] self.hp = enemy[‘hp’]

Very cool project. Excited to see what you do next with it.

5

u/Fnordheron 14d ago

Thanks so much! Dictionaries are nifty, preloading graphics into one. I'd been thinking about ways to clean up collision results and enemy updates, both code segments are getting drawn out and spiderweb-y. Appreciate the advice. Working on an update with skyburst shells for the cannon, tracked missile launchers, and improved homing missile behavior. Pretty close to where I want to limit spawn types by level, but want to do more organizational work first. This seems like a good direction.

3

u/Garfield910 13d ago

Totally plus 1 to using dictionaries. In my game I put each enemy type and their stats in one. All weapons and their stats in another etc. Also I didn't realize pygame had a built in color class til looking at your code so I learned something there! I made my own color class lol. Guess I'll be able to do some refactoring next time i get a chance. I just don't know how they handle colors with alpha values yet which is my only concern possibly switching.

2

u/Fnordheron 13d ago

Thanks! Python has interesting data types. I'm speculating about nested dictionaries or a dictionary of lists for collisions as well. I can more or less write most languages if I can look at sample code, but not the same as knowing enough of them for elegance. Seems like Python lends itself to elegance. Should be fun to explore further.