Tower Defence Tutorial in Pygame | Part 9 - Upgrading Turrets
HTML-код
- Опубликовано: 25 июн 2023
- In this Python tutorial I code tower defence game using the PyGame module. I'm going to add a file containing data for different levels of turret and use this to upgrade the placed turrets. Each upgrade makes the turret stronger by reducing cooldown between shots and increasing the turret's range.
Code & assets on github: github.com/russs123/tower_def...
Credits for assets used:
Tileset: www.kenney.nl/assets/tower-de...
Turrets: zintoki.itch.io/ground-shaker
Heart: nicolemariet.itch.io/pixel-he...
Coin: totuslotus.itch.io/pixel-coins
I'm learning a lot from your videos Russ. Thank you for these amazing videos. I've started doing a new game project. I would love to share it with you after my project is finished. I hope you get what you deserve. Good Luck! :)
That's great to hear, thanks! Will be interesting to see your project once it's finished
love your vids !
Cheers!
thank you so much.
Hey Russ. I love these videos and the way you build things up step by step. When you get to the end, you should mention something about code optimisation. Rather than repeating the same code in different places, you should aim to write it once and call it multiple times. That way, your code is easier to maintain. Speaking from experience where I had to debug a code base where the same mistake had been copied into about 10 different places 😱
Thanks! It's a good point about optimisation, keeps the code tidy and like you say, it avoids mistakes being spread as easily. I try to refactor my code as I work on it, to see if sections can be simplified or turned into a function that can be called instead of pasting code, although even after that there's probably always room for improvement :)
experiencing this error and couldn't locate where its emerging from ;
File "E:\programs\pygame apps\tower\tower.py", line 170, in
create_turret(mouse_pos)
File "E:\programs\pygame apps\tower\tower.py", line 62, in create_turret
new_turret = Turret(turret_sheet,mouse_tile_x,mouse_tile_y)
File "E:\programs\pygame apps\tower\turret.py", line 24, in __init__
self.animation_list = self.load_images(self.sprite_sheets[self.upgrade_level - 1])
TypeError: 'pygame.surface.Surface' object is not subscriptable
It means that the self.sprite_sheets variable is an image rather than a list. Double check that you have updated the argument names and are passing the list of sprite sheets rather than an individual image and that should fix it.
First